Rendering OpenStreetMap Data using Mapnik
-
Upload
graham-jones -
Category
Technology
-
view
23.367 -
download
2
Transcript of Rendering OpenStreetMap Data using Mapnik
Creating Maps from OpenStreetMap DataUsing Mapnik
Graham Jones
23 February 2011
Purpose
The purpose of this presentation is to:Provide an overview of the concepts associated with creating a map from OpenStreetMap data using mapnik.
Describe the structure of an osm2pgsql and a mapnik stylesheet.
Provide references to more detailed instructions.
Scope
This presentation provides only a high level overview there are many more resources on the internet that go into more detail.
It is not about detailed installation instructions for the tools there are quite a few resources on the internet that do this.
References are provided at the end.
What do you need to make a map?
OpenStreetMap Data: a '.osm' file covering the region you are interested in. Possible sources of this data are:The OSM api (using say JOSM)
XAPI (if it is working)
A 'planet extract' from http://www.geofabrik.de.
Coastline & Population Centres data (for small sale maps).
Ideas about what information you would like to display, and how you would like to display it.
Why is it so complicated?
The mapnik library that is discussed in this presentation is a general purpose map drawing utilitiy.
It is very powerful and can do all sorts take data from different data sources, plot it in different ways depending on the map scale etc. With power comes complexity.
The main issue is that the usual way of using OSM data with mapnik is not to have mapnik read the data directly, but load the data into a database first, and mapnik gets the data from there. This means that you need a lot of tools to draw a map.
Is there an alternative?
Yes there are a few different renderers for OSM data try searching the OSM wiki for:Osmarender
Maperitive
But I understand mapnik better than these, so they are not considered further here.
The OSM Mapnik Rendering Tools
OSM Data file(.osm)PostgreSQL / postGIS databasemapnikMapnik stylesheet (.xml)Extra Data (Coastline Shapefiles etc.)
generate_image.py or generate_tiles.pyosm2pgsqldefault.style
Files to be Edited / CustomisedFixed data / programs / librariesKey
osm2pgsql
osm2pgsql is the tool that loads OSM data into the postgresql database.
It does three important things:Filters the data to limit the amount of data in the database.
Reprojects the data from lat,lon values to mercator projection x,y positions.
Creates points, lines and polygons from the OSM nodes, ways and relations, to be compatible with mapnik.
osm2pgsql style file
osm2pgsql takes a file (called default.style by default), which tells it which data to put into the database.
It is a list of keys (highway, natural, waterway etc.). Only OSM entities with keys listed in default.style will be put into the database.
This means you need to think about which osm keys you will need to use to create your map before you import the database!
The osm2pgsql database
The database contains a number of tables. planet_osm_point: OSM nodes.
planet_osm_line: OSM linear ways.
planet_osm_polygon: closed OSM ways (=areas).
Each table has a number of colums:One column for each tag defined in default.style.
A 'way' column which contains the geometry of the entity (e.g. position of a point).
SQL
SQL (Structured Query Language) is used to extract data from the postgresql database in the 'datasource' part of the mapnik layer definition (see later).
We need to use a select statement to get data out of the database.SELECT FROM where .
e.g. SELECT way, name from planet_osm_point where historic is not null
The 'way' column is important this tells mapnik where to plot the point!
What is a mapnik Stylesheet?
A mapnik stylesheet is a text file in a format called 'XML'.
define a style define a layer
Mapnik Styles and Layers
A mapnik stylesheet will contain one or more layers and one or more styles.
The initial element defines things like the map projection to be used, and the background colour.
Layers define what data should be displayed.
Styles define how to display it.
Mapnik Stylesheet Entities
It is possible to write a mapnik stylesheet as a very simple text file.
But unless your style is extremely simple, there will be a lot of repetition things like directory containing your map icons, your database username and password etc.
Entities are used to store this repeated data to make it easier to alter your stylesheet in the future.
Mapnik Layers
A layer specification has to tell mapnik:What data to include in the layer.
What style to use to display it.
Historic
postgis
graham
xxxx
gis
(select way, name, historic from planet_osm_point where historic is not null) as points
Style to use to display dataTell mapnik to use postgresql/postgis to get the dataSQL Statement to extract data from the database
Mapnik Styles
A style tells mapnik how to display data on the map.
A style can have one or more rules that can define different ways of displaying the data (e.g. at different zoom levels).
This example style is as simple as it gets just display an image, no matter what the zoom level.Reference to an Entity defined earlier in the file.
generate_image.py
generate_image.py is a simple python script to use mapnik to produce a map image using a specified stylesheet.
The only thing that needs altering in it is the bounding box that defines the are you want the map to cover.
generate_tiles.py is similar, but produces a set of tiles at various zoom levels for use on a web site.
generate_tiles.py
Generate_tiles.py is similar to generate_image.py, but it produces a set of map tiles to allow you to produce a 'slippy map' scrollable map on a web site.
There are more efficient tools for production services try searching for mod_tile or tirex on the OSM wiki.
A working Example
]>
Historic
postgis
graham
1234
gis
(select way from planet_osm_point where historic='ruins') as points
And the result:
So how do I get started?
Install the tools see the References on the next slide.
Test using the standard OSM style get that working before you try to create your own style sheets.
Start with something very simple and build up from it.
References
A good set of instructions for instaling the tools:http://weait.com/content/build-your-own-openstreetmap-server
A nice introduction to mapnik:http://www.slideshare.net/artemp/mapnik-sotm-2007
More information on mapnik:http://trac.mapnik.org/wiki/MapnikTutorials
What is this XML stuff?:
http://en.wikipedia.org/wiki/XML
What is SQL?:
http://www.w3schools.com/sql/default.asp
Click to edit the title text format
Click to edit the title text format