I'm trying to render a map using mapnik and python from a GPS track recorded. I get the gps data from a Database, so it is just an array (lat, long).
Does anyone knows an example for doing that? I know I need to create a shape file first, but I'm new to mapnik and I don't really understand it so far. maybe with a good example I will get it :-)
Thanks
The simplest method would actually be to use a KML file. Install the simplekml module and then run through your array to create the 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 that into mapnik as a datasource and plot 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 just about do it. The docs for python+mapnik are a little weak but you should be able to build on this if you reference;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With