6

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

otmezger
  • 10,410
  • 21
  • 64
  • 90

1 Answers1

4

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;

  1. The mapnik wiki
  2. The python mapnik package docs
Mark
  • 1,449
  • 1
  • 13
  • 13
  • where does the actual map data comes from? – otmezger May 12 '13 at 07:58
  • 1
    You have to add the map data as another layer (from any datasource). This would map the GPS coordinates spatially with nothing underneath (e.g., dots in space). You can add a map layer from OpenStreetMaps or another source (Google Maps, Bing Maps, etc.) depending on what's appropriate – Mark May 13 '13 at 00:46
  • thanks for your answer. Could you tell me how to use other sources? I know I can use my own postgis to use OSM data, but this is too much effort for me. I would need a server running it etc... I'm looking for an easier way, and if you say it is possible to add a layer of maps using google maps, i'll take it! or even an online version of OSM... do you know any examples for this? Thanks – otmezger May 13 '13 at 09:32
  • 1
    Check out this page for info on adding a background world map: https://github.com/mapnik/mapnik/wiki/GettingStartedInXML – S.. Sep 03 '15 at 18:44
  • For anyone looking for simplekml in 2020: It's on PyPI, and here are the [docs](https://simplekml.readthedocs.io/en/latest/) and its code on [GitHub](https://github.com/eisoldt/simplekml). – makeworld Aug 28 '20 at 00:23