OSMtechnical
Download
Report
Transcript OSMtechnical
Using OSM data
The technical details...
Using OSM data
Extracting data from planet.osm
Setting up a PostGIS database
Importing data into a PostGIS database
Rendering map data with Mapnik
Using OpenLayers to create an OSM-based
website
Extracting data from planet.osm
Download a planet.osm extract from a server
e.g. geofabrik.de
Planet extracts available from
download.geofabrik.de/osm
Use Osmosis to extract the data
Available from wiki.openstreetmap.org/index.php/Osmosis
Using Osmosis
There is a good chance that we are only
interested in certain OSM data
For example, if we wish to create a map for
walkers, we might only want to use
countryside data
Osmosis allows you to extract the data you
want only
Osmosis
Osmosis allows you to:
Extract data by bounding box
Extract all nodes or ways containing a particular
key, or key/value pair
Write OSM data to a MySQL or PostGIS database
Read OSM data from a database and write to an
OSM data file
Osmosis
Example of Osmosis commands:
osmosis --rx file=uk.osm -–bounding-box top=52
bottom=50 left=-2 right=-0 –-wx
file=southern_england.osm
This extracts a bounding box from uk.osm and saves it as
southern_england.osm.
osmosis –-rx file=uk.osm -–tf reject-ways –-tf
accept-nodes place=* --wx file=ukplaces.osm
This filters out all ways, and accepts those nodes with a ‘place’ tag, and
writes to ukplaces.osm.
General principles of Osmosis
Has a set of commands specified as command-line options
e.g. --rx (read XML); --tf (tag filter)
Commands typically filter the input stream
So the following command:
osmosis –-rx file=uk.osm -–tf reject-ways -–tf
accept-nodes place=* --tf accept-nodes
amenity=* --wx file=output.osm
will only give nodes with both a place tag and an amenity tag
If we want nodes with either a place tag or an amenity tag
we must use named pipes and stream merging, see the
Osmosis page for details
Setting up an OSM database
You may wish to set up your own copy of the
OSM database
e.g. for rendering maps, or developing a web
service of some kind
Typically, a PostgreSQL / PostGIS database
is used for this
OSM data is imported into two tables
planet_osm_point and planet_osm_line
Setting up PostgreSQL/PostGIS
PostgreSQL and PostGIS normally available
as packages for Linux distributions
Installation fairly straightforward on current
Ubuntu or Debian distributions
Detailed installation instructions available at
wiki.openstreetmap.org/index.php/Mapnik/PostGIS
Importing OSM data
Having set up the PostGIS database we need
to import OSM data into it
A custom tool, osm2pgsql, is used for this
This can be downloaded from OSM's
subversion repository
osm2pgsql allows us to control which map
features are imported (by tag)
See the osm2pgsql wiki page:
http://wiki.openstreetmap.org/index.php/Osm2pgsql
Mapnik
To create our own maps we can use the
Mapnik library (www.mapnik.org)
C++ library with Python wrapper which allows
easy creation of high-quality maps
Source available from mapnik.org
Binary packages available for Debian,
Ubuntu, OS X, Windows
Main dependency: Boost C++ libraries
For installation details see:
trac.mapnik.org/wiki/MapnikInstallation
Using Mapnik
Mapnik is a library, not an application
So to create maps with Mapnik, we have to
write our own application (C++, Python) or
use a pre-built Mapnik-based application
Examples of pre-built applications:
mod_tile – Apache module to render tiles from
PostGIS using Mapnik
generate_tiles.py – Python script to generate tiles
at different zoom levels from a PostGIS database
Configuring Mapnik
Mapnik styles are configurable with an XML file
Basic units are styles, rules and layers
Each broad class of feature (e.g. road, path, natural
vegetation, landuse) is defined within a <Style> tag
Within that <Style>, we define a series of <Rule>s to
match various tagging patterns
We then set the colour, font, etc of each <Rule>
Finally we define <Layer>s, each of which can
contain several <Style>s
Example of Mapnik XML Style/Rule
<Style name='natural'>
<Rule>
<Filter>[natural] = 'wood' or [landuse] = 'wood'</Filter>
<PolygonSymbolizer>
<CssParameter name="fill">#aed1a0</CssParameter>
</PolygonSymbolizer>
</Rule>
<Rule>
<Filter>[natural] = 'heath'</Filter>
<PolygonSymbolizer>
<CssParameter name="fill">#ffffc0</CssParameter>
</PolygonSymbolizer>
</Rule>
</Style>
Example of Mapnik XML Layer
<Layer name="paths">
<StyleName>paths</StyleName>
<Datasource>
<Parameter name="table">
(select way,highway from planet_osm_line
where highway in ('path','footway','bridleway','cycleway')
order by z_order) as roads
</Parameter>
<Parameter name="type">postgis</Parameter>
<Parameter name="host">localhost</Parameter>
<Parameter name="port">5432</Parameter>
<Parameter name="user">gis</Parameter>
<Parameter name="dbname">gis</Parameter>
</Datasource>
</Layer>
Note how we link in a <Style> (paths) and specify the SQL to retrieve all objects in
that layer
A word on the OSM tiling system
OSM based sites normally use the “Google” tiling
system
Principle is:
- At the highest zoom level, the entire world is shown
on a 256x256 tile
- Each subsequent zoom level zooms in by a factor of
2
- So, for example, zoom level 1 requires 4 tiles for the
whole world (NW, NE, SW, SE quadrants), zoom
level 2 requires 16, etc.
See wiki.openstreetmap.org/wiki/
Slippy_map_tilenames
How to produce tiles
generate_tiles.py script available from OSM
Subversion repository
(http://svn.openstreetmap.org/applications/rendering/mapnik/)
This will pre-generate OSM map tiles from a postgis
database
Can configure bounding box and zoom levels
Alternatively use mod_tile
Apache module for live generation and caching of
tiles from an OSM PostGIS database
Producing your own “slippy map”
Having created your tiles, you need to use them in a
web application
Use the open-source OpenLayers JavaScript API
(www.openlayers.org)
An open source alternative to Google Maps, etc.
See www.freemap.org.uk/course/osm/openlayers.xhtml to get
started