Rendering OpenStreetMap Data using Mapnik
Upcoming SlideShare
Loading in...5
×
 

Rendering OpenStreetMap Data using Mapnik

on

  • 17,078 views

A high level overview of the concepts and tools required to produce maps using OpenStreetMap data using the Mapnik library.

A high level overview of the concepts and tools required to produce maps using OpenStreetMap data using the Mapnik library.

Statistics

Views

Total Views
17,078
Views on SlideShare
16,301
Embed Views
777

Actions

Likes
4
Downloads
202
Comments
1

27 Embeds 777

http://nerdytoad.blogspot.com 579
http://maps3.org.uk 53
http://nerdytoad.blogspot.co.uk 41
http://nerdytoad.blogspot.de 33
http://nerdytoad.blogspot.it 10
http://nerdytoad.blogspot.in 9
http://nerdytoad.blogspot.com.au 7
http://nerdytoad.blogspot.fr 7
http://nerdytoad.blogspot.nl 5
http://nerdytoad.blogspot.ca 4
http://nerdytoad.blogspot.ch 4
http://webcache.googleusercontent.com 3
http://nerdytoad.blogspot.jp 3
http://translate.googleusercontent.com 3
http://jones139.blogspot.com 2
http://nerdytoad.blogspot.hu 2
http://nerdytoad.blogspot.co.nz 2
http://nerdytoad.blogspot.com.ar 1
http://nerdytoad.blogspot.tw 1
http://nerdytoad.blogspot.fi 1
http://jones139.blogspot.com.au 1
http://nerdytoad.blogspot.com.br 1
http://nerdytoad.blogspot.mx 1
http://jones139.blogspot.co.uk 1
http://nerdytoad.blogspot.se 1
http://nerdytoad.blogspot.no 1
http://jones139.blogspot.com.es 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Rendering OpenStreetMap Data using Mapnik Rendering OpenStreetMap Data using Mapnik Presentation Transcript

  • Creating Maps from OpenStreetMap Data Using 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 Key OSM Data file (.osm) PostgreSQL / postGIS database mapnik Mapnik stylesheet (.xml) Extra Data (Coastline Shapefiles etc.) generate_image.py or generate_tiles.py osm2pgsql default.style Files to be Edited / Customised Fixed data / programs / libraries
  • 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 <list of database columns> FROM <database table> where <condition>.
      • 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'.
    • <map>
    • <!ENTITY symboldir “/home/OSM/symbols/”>
    • <style>
    • define a style
    • </style>
    • <layer>
    • define a layer
    • </layer>
    • </map>
  • Mapnik Styles and Layers
    • A mapnik stylesheet will contain one or more layers and one or more styles.
    • The initial <map> 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.
    • <Layer name=&quot;Historic&quot; >
    • <StyleName>Historic</StyleName>
    • <Datasource>
    • <Parameter name=&quot;type&quot;>postgis</Parameter>
    • <Parameter name=&quot;user&quot;>graham</Parameter>
    • <Parameter name=&quot;password&quot;>xxxx</Parameter>
    • <Parameter name=&quot;dbname&quot;>gis</Parameter>
    • <Parameter name=&quot;table&quot;>
    • (select way, name, historic from planet_osm_point where historic is not null) as points
    • </Parameter>
    • </Datasource>
    • </Layer>
    Style to use to display data Tell mapnik to use postgresql /postgis to get the data SQL 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).
    • <Style name=”Historic”>
    • <Rule>
    • <PointSymbolizer
    • file=&quot;&symbolDir;/tourist_ruin.n.16.png&quot;
    • />
    • </Rule>
    • </Style>
    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
    • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
    • <!DOCTYPE Map[
    • <!ENTITY symbolDir &quot;/home/disk2/OSM/Maps/historic/symbols&quot;>
    • ]>
    • <Map
    • bgcolor=&quot;white&quot;
    • >
    • <Style name=&quot;Historic&quot;>
    • <!-- ************** Ruins / Abbeys *************** -->
    • <Rule>
    • <PointSymbolizer
    • file=&quot;&symbolDir;/tourist_ruin.n.16.png&quot;
    • allow_overlap=&quot;false&quot;
    • />
    • </Rule>
    • </Style>
    • <Layer name=&quot;Historic&quot; >
    • <StyleName>Historic</StyleName>
    • <Datasource>
    • <Parameter name=&quot;type&quot;>postgis</Parameter>
    • <Parameter name=&quot;user&quot;>graham</Parameter>
    • <Parameter name=&quot;password&quot;>1234</Parameter>
    • <Parameter name=&quot;dbname&quot;>gis</Parameter>
    • <Parameter name=&quot;table&quot;>
    • (select way from planet_osm_point where historic='ruins') as points
    • </Parameter>
    • </Datasource>
    • </Layer>
    • </Map>
  • 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