October 11, 2011
Maperitive: maperipy Progress

WARNING: this is one of those teaser-posts - you get to hear about the cool new features, but you won’t be able to try them out, at least not very soon.

Introduction

In the last few weeks since the beta release I’ve been working on a custom job involving Maperitive. I won’t go into details about the job itself, but the important thing is that this job was an opportunity for maperipy Python API to be extended with some new features. I’ll go through some of them, together with sample Python code.

Geometries

maperipy provides classes for basic geometries (compatible with OGC’s Simple Features Specification) like Point, LineString, LinearRing, Polygon, MultiPolygon etc. I’ve tried to stick to the syntax and semantics used by shapely library (unfortunately shapely itself cannot be used directly because it won’t run in IronPython - it depends on some C code in the backstage, but more about this some other time).

Custom Layers

maperipy now allows users to create their own custom map layers which can be programmatically filled with map symbols using geometries described previously:

# we first create a custom layer on a map
layer = map.add_custom_layer()

# create a simple geometry
line = LineString([(0, 0), (0, 10), (10, 10), (10, 0)])

# add a symbol for it...
symbol = LineSymbol("my_line", (line, ))
# ... and define the visual style
symbol.pen_width = 5
symbol.pen_color = Color("red")

# now we add the symbol to the layer
layer.add_symbol(layer)

Shapefiles

The custom job required the data to be loaded from shapefiles and then rendered on a map based on geometry attributes. This can now be achieved in maperipy:

# first we read the shapefile...
land_use = store.load_shapefile(r"LandUse.shp")

# ... and add polygons for a specific land use type
symbol = PolygonSymbol(
    "forest", 
    land_use.find_polygons(lambda p : p["land_use_type"] == 4000))
symbol.style.fill_color = symbol.style.pen_color = Color("green")
layer.add_symbol(symbol)

Rasters

maperipy supports several types of raster grid file formats: SRTM HGT, ESRI ASCII grid and XYZ. They can now all be used to generate hillshadings or relief contours.

Example code which reads a set of XYZ files, merges them together and then generates hillshading image from them:

parts = []

dem_dir = r"DEM"

for file_name in os.listdir(dem_dir):
    grid = store.load_xyz_grid (os.path.join (dem_dir, file_name))
    parts.append(grid)

whole_dem = Raster.merge(parts)

hillshader = IgorHillshader()

hillshadingBitmap = HillShadingProcessor.shade(whole_dem, hillshader, Color("black"), 1)
hillshadingBitmap.save("dem.png")

Spatial References

One of the biggest pieces of coding I had to do was to introduce SRIDs into the picture. This allows consuming of data sources which use spatial reference systems different from the WGS 84 lon/lat used by OSM. The next step will be to actually support different SRS-es, including different coordinate systems and different map projections.

When?

This is all very nice, but when will you get to use it? Well, maperipy needs a lot more work (what I’ve shown here is just a start!) and so does the main Maperitive code. I also need to write some documentation about the API. So my guesstimate would be a couple of months. Given that a lot of new functionality has been introduced since the last “official” release, I’m thinking of naming the next major release as “Maperitive 2.0” and this will include all the stuff introduced in the September beta release.

  1. braincrunch posted this