Rendering OpenStreetMap Data using Mapnik


Published on

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

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Rendering OpenStreetMap Data using Mapnik

  1. 1. Creating Maps from OpenStreetMap Data Using Mapnik Graham Jones 23 February 2011
  2. 2. Purpose <ul>The purpose of this presentation is to: <li>Provide an overview of the concepts associated with creating a map from OpenStreetMap data using mapnik.
  3. 3. Describe the structure of an osm2pgsql and a mapnik stylesheet.
  4. 4. Provide references to more detailed instructions. </li></ul>
  5. 5. Scope <ul><li>This presentation provides only a high level overview – there are many more resources on the internet that go into more detail.
  6. 6. It is not about detailed installation instructions for the tools – there are quite a few resources on the internet that do this.
  7. 7. References are provided at the end. </li></ul>
  8. 8. What do you need to make a map? <ul><li>OpenStreetMap Data: a '.osm' file covering the region you are interested in. Possible sources of this data are: </li><ul><li>The OSM api (using say JOSM)
  9. 9. XAPI (if it is working)
  10. 10. A 'planet extract' from . </li></ul><li>Coastline & Population Centres data (for small sale maps).
  11. 11. Ideas about what information you would like to display, and how you would like to display it. </li></ul>
  12. 12. Why is it so complicated? <ul><li>The mapnik library that is discussed in this presentation is a general purpose map drawing utilitiy.
  13. 13. 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.
  14. 14. 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. </li></ul>
  15. 15. Is there an alternative? <ul><li>Yes – there are a few different renderers for OSM data – try searching the OSM wiki for: </li><ul><li>Osmarender
  16. 16. Maperitive </li></ul><li>But I understand mapnik better than these, so they are not considered further here. </li></ul>
  17. 17. The OSM Mapnik Rendering Tools Key OSM Data file (.osm) PostgreSQL / postGIS database mapnik Mapnik stylesheet (.xml) Extra Data (Coastline Shapefiles etc.) or osm2pgsql Files to be Edited / Customised Fixed data / programs / libraries
  18. 18. osm2pgsql <ul><li>osm2pgsql is the tool that loads OSM data into the postgresql database.
  19. 19. It does three important things: </li><ul><li>Filters the data to limit the amount of data in the database.
  20. 20. Reprojects the data from lat,lon values to mercator projection x,y positions.
  21. 21. Creates points, lines and polygons from the OSM nodes, ways and relations, to be compatible with mapnik. </li></ul></ul>
  22. 22. osm2pgsql style file <ul><li>osm2pgsql takes a file (called by default), which tells it which data to put into the database.
  23. 23. It is a list of keys (highway, natural, waterway etc.). Only OSM entities with keys listed in will be put into the database.
  24. 24. This means you need to think about which osm keys you will need to use to create your map before you import the database! </li></ul>
  25. 25. The osm2pgsql database <ul><li>The database contains a number of tables. </li><ul><li>planet_osm_point : OSM nodes.
  26. 26. planet_osm_line : OSM linear ways.
  27. 27. planet_osm_polygon : closed OSM ways (=areas). </li></ul><li>Each table has a number of colums: </li><ul><li>One column for each tag defined in
  28. 28. A 'way' column which contains the geometry of the entity (e.g. position of a point). </li></ul></ul>
  29. 29. SQL <ul><li>SQL (Structured Query Language) is used to extract data from the postgresql database in the 'datasource' part of the mapnik layer definition (see later).
  30. 30. We need to use a select statement to get data out of the database. </li><ul><li>SELECT <list of database columns> FROM <database table> where <condition>.
  31. 31. e.g. SELECT way, name from planet_osm_point where historic is not null
  32. 32. The 'way' column is important – this tells mapnik where to plot the point! </li></ul></ul>
  33. 33. What is a mapnik Stylesheet? <ul><li>A mapnik stylesheet is a text file in a format called 'XML'.
  34. 34. <map>
  35. 35. <!ENTITY symboldir “/home/OSM/symbols/”>
  36. 36. <style>
  37. 37. define a style
  38. 38. </style>
  39. 39. <layer>
  40. 40. define a layer
  41. 41. </layer>
  42. 42. </map> </li></ul>
  43. 43. Mapnik Styles and Layers <ul><li>A mapnik stylesheet will contain one or more layers and one or more styles.
  44. 44. The initial <map> element defines things like the map projection to be used, and the background colour.
  45. 45. Layers define what data should be displayed .
  46. 46. Styles define how to display it . </li></ul>
  47. 47. Mapnik Stylesheet Entities <ul><li>It is possible to write a mapnik stylesheet as a very simple text file.
  48. 48. 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.
  49. 49. Entities are used to store this repeated data to make it easier to alter your stylesheet in the future. </li></ul>
  50. 50. Mapnik Layers <ul><li>A layer specification has to tell mapnik: </li><ul><li>What data to include in the layer.
  51. 51. What style to use to display it. </li></ul><li><Layer name=&quot;Historic&quot; >
  52. 52. <StyleName>Historic</StyleName>
  53. 53. <Datasource>
  54. 54. <Parameter name=&quot;type&quot;>postgis</Parameter>
  55. 55. <Parameter name=&quot;user&quot;>graham</Parameter>
  56. 56. <Parameter name=&quot;password&quot;>xxxx</Parameter>
  57. 57. <Parameter name=&quot;dbname&quot;>gis</Parameter>
  58. 58. <Parameter name=&quot;table&quot;>
  59. 59. (select way, name, historic from planet_osm_point where historic is not null) as points
  60. 60. </Parameter>
  61. 61. </Datasource>
  62. 62. </Layer> </li></ul>Style to use to display data Tell mapnik to use postgresql /postgis to get the data SQL Statement to extract data from the database
  63. 63. Mapnik Styles <ul><li>A style tells mapnik how to display data on the map.
  64. 64. A style can have one or more rules that can define different ways of displaying the data (e.g. at different zoom levels).
  65. 65. <Style name=”Historic”>
  66. 66. <Rule>
  67. 67. <PointSymbolizer
  68. 68. file=&quot;&symbolDir;/tourist_ruin.n.16.png&quot;
  69. 69. />
  70. 70. </Rule>
  71. 71. </Style> </li></ul>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.
  72. 72. <ul><li> is a simple python script to use mapnik to produce a map image using a specified stylesheet.
  73. 73. The only thing that needs altering in it is the bounding box that defines the are you want the map to cover.
  74. 74. is similar, but produces a set of tiles at various zoom levels for use on a web site. </li></ul>
  75. 75. <ul><li> is similar to, but it produces a set of map tiles to allow you to produce a 'slippy map' scrollable map on a web site.
  76. 76. There are more efficient tools for production services – try searching for mod_tile or tirex on the OSM wiki. </li></ul>
  77. 77. A working Example <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
  78. 78. <!DOCTYPE Map[
  79. 79. <!ENTITY symbolDir &quot;/home/disk2/OSM/Maps/historic/symbols&quot;>
  80. 80. ]>
  81. 81. <Map
  82. 82. bgcolor=&quot;white&quot;
  83. 83. >
  84. 84. <Style name=&quot;Historic&quot;>
  85. 85. <!-- ************** Ruins / Abbeys *************** -->
  86. 86. <Rule>
  87. 87. <PointSymbolizer
  88. 88. file=&quot;&symbolDir;/tourist_ruin.n.16.png&quot;
  89. 89. allow_overlap=&quot;false&quot;
  90. 90. />
  91. 91. </Rule>
  92. 92. </Style>
  93. 93. <Layer name=&quot;Historic&quot; >
  94. 94. <StyleName>Historic</StyleName>
  95. 95. <Datasource>
  96. 96. <Parameter name=&quot;type&quot;>postgis</Parameter>
  97. 97. <Parameter name=&quot;user&quot;>graham</Parameter>
  98. 98. <Parameter name=&quot;password&quot;>1234</Parameter>
  99. 99. <Parameter name=&quot;dbname&quot;>gis</Parameter>
  100. 100. <Parameter name=&quot;table&quot;>
  101. 101. (select way from planet_osm_point where historic='ruins') as points
  102. 102. </Parameter>
  103. 103. </Datasource>
  104. 104. </Layer>
  105. 105. </Map> </li></ul>
  106. 106. And the result:
  107. 107. So how do I get started? <ul><li>Install the tools – see the References on the next slide.
  108. 108. Test using the standard OSM style – get that working before you try to create your own style sheets.
  109. 109. Start with something very simple and build up from it. </li></ul>
  110. 110. References <ul><li>A good set of instructions for instaling the tools:
  111. 111. A nice introduction to mapnik:
  112. 112. More information on mapnik:
  113. 113. What is this XML stuff?:
  114. 114. What is SQL?: </li></ul>