Creating a OSM map tiles server in Django

5.4k views Asked by At

I followed the tutorial from switch2osm to create a tiles server, but this tutorial is only for an apache server.

I have a Django server and wanted to create a custom map for it. I already installed Mapnik, PostGIS and loaded OSM data into my PostGIS database.

I created a python script that, using Mapnik, creates a simple map with the following code:

#!/usr/bin/env python
import mapnik

stylesheet = 'database.xml'
image = 'database.png'
m = mapnik.Map(900, 450)
mapnik.load_map(m, stylesheet)
m.zoom_all() 
mapnik.render_to_file(m, image)
print "rendered image to '%s'" % image

And the xml file:

<Map background-color="steelblue" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">

  <Style name="My Style">
    <Rule>
      <PolygonSymbolizer fill="#f2eff9" />
      <LineSymbolizer stroke="rgb(50%,50%,50%)" stroke-width="0.1" />
    </Rule>
  </Style>

  <Layer name="countries" status="on" srs="+proj=latlong +datum=WGS84">
    <StyleName>My Style</StyleName>
    <Datasource>
      <Parameter name="type">postgis</Parameter>
      <Parameter name="host">localhost</Parameter>
      <Parameter name="dbname">db</Parameter>
      <Parameter name="user">user</Parameter>      
      <Parameter name="password"></Parameter>
      <Parameter name="table">(SELECT * from planet_osm_line where highway is not null) as foo</Parameter>
      <!-- <Parameter name="extent">-180,-90,180,89.99</Parameter> -->
      <!-- <Parameter name="extent">-20037508.34,-20037508.34,20037508.34,20037508.34</Parameter> -->
    </Datasource>
  </Layer>

</Map>

This generates the following image (I only loaded this OSM data and not the whole planet):

Map image

I was thinking of creating a something like this to create a Django view that would offer my map tiles, but it takes too long to process (3 minutes).

How can I use Mapnik to create, with Django, a tile server? Is there any Django library to make this easier?

1

There are 1 answers

4
MaM On BEST ANSWER

I guess you mix two complete independend things:

Django service

This is your part, where you create all kinds of logic, models, views etc. on your dedicated usecase. For example you code an plattform to mark interesting places on a map, where you can use djangos geoapp (fka. geodjango) to embedd the geospatial magic etc.

Tile map service

This is what (independly!) creates map tiles that can be consumed by any other application (desktop gis... JS webmap widgets). Usually people use existing tile-providers to get maps without any own work and for free. Only if you need to create a own custom mapstyle you need to host your own tile rendering stack, as explained at www.switch2osm.org . Anyway, it's highly recommend to follow this guide and rely on existing tools (mapnik, postgis, ...) to avoid a lot of troubles. Please keep in mind the hardware and service requirements (tile coverage, reliability, update-frequency, ...) before you start your own service!

This tiles how ever get just linked at your django frontend code as Leaflet/OpenLayers/... layer. Nothing more :)