1
votes

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
Do you need django to generate the tiles (=> run mapnik from celery) or do you only need django to serve the files (=> put them into django's media directory and be done with it)? - dhke
I only need Django to serve the tiles, but I thought these tiles were generated dynamically, so that they wouldn't occupy as much disk space. - t.pimentel
Tiles are generated dynamically on the a tile server, but there are two bottlenecks: Tile generation time and disk space. Disk space is conserved by pruning less used tiles and generation time is handled by caching. That is also why you may get empty tiles in not-so-often-visited locations on the planet at tile generation is triggered asynchronously. Of course depending on implementation. - dhke
But is there any Django app/library to help with generating/caching/serving these tiles? If there is none, what would be the best approach for doing this? Or is the only simple approach to keep both my Django server and an apache one to serve these tiles? - t.pimentel

1 Answers

3
votes

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 :)