Python + Mapnik: an example of how to display a map with a gps track on it

I am trying to display a map using mapnik and python from a recorded GPS track. I get gps data from the database, so this is just an array (lat, long).

Does anyone know an example for this? I know that I need to create a form file first, but I'm new to mapnik, and I don't understand it yet. maybe with a good example i will get it :-)

thanks

+4
source share
1 answer

The easiest way is to use a KML file. Install the simplekml module and then run it through an array to create a KML file.

import simplekml kml = simplekml.Kml() for i, coord in enumerate(coords): # assuming coord is a lat, lon tuple kml.newpoint(name="Point %s" % i, coords=[coord]) kml.save("GPS_tracking_data.kml") 

Now you can load this into mapnik as a data source and build it;

 import mapnik # Setup the map map_canvas = mapnik.Map(width_in_px, height_in_px) map_canvas.background = mapnik.Color('rgb(0,0,0,0)') # transparent # Create a symbolizer to draw the points style = mapnik.Style() rule = mapnik.Rule() point_symbolizer = mapnik.MarkersSymbolizer() point_symbolizer.allow_overlap = True point_symbolizer.opacity = 0.5 # semi-transparent rule.symbols.append(point_symbolizer) style.rules.append(rule) map_canvas.append_style('GPS_tracking_points', style) # Create a layer to hold the ponts layer = mapnik.Layer('GPS_tracking_points') layer.datasource = mapnik.Ogr(file="GPS_tracking_data.kml", layer_by_index=0) layer.styles.append('GPS_tracking_points') map_canvas.layers.append(layer) # Save the map map_canvas.zoom_all() mapnik.render_to_file(map_canvas, 'GPS_tracking_points.png', 'png') 

That should have done it. The docs for python + mapnik are a little weak, but you should be able to build it if you link;

+2
source

All Articles