1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
osm2pgsql
=========
Converts OSM planet.osm data to SQL suitable for loading into
a PostgreSQL database and then rendered into tiles by Mapnik.
The format of the database is optimised for ease of rendering
by mapnik. It may be less suitable for other general purpose
processing.
For a broader view of the whole map rendering tool chain see
http://wiki.openstreetmap.org/index.php/Mapnik and
http://wiki.openstreetmap.org/index.php/Slippy_Map
Any questions should be directed at the osm dev list
http://wiki.openstreetmap.org/index.php/Mailing_lists
Changes
=======
The current version of code features several enhancements
and changes over the previous code:
- A direct connection to the Postgres database is used
instead of outputting SQL
- Incorporation of setup_z_order.sql script so that
post-processing is no longer required
- Able to read .gz and .bz2 files directly
- Performs UTF8Sanitize while reading the planet.osm
- Simplified usage: "osm2pgsql planet-xxxxxx.osm.bz2"
- Geometries are now stored in mercator projection
- Introduction of databased backed middle-layer for
systems with low RAM (normally disabled).
- Support the choice of a few different projections
- Configurable table names
Requirements
============
The code is written in C and C++ and relies on the libraries
below:
- libxml2 http://xmlsoft.org/
- geos http://geos.refractions.net/
- proj http://www.remotesensing.org/proj/
- bzip2 http://www.bzip.org/
- zlib http://www.zlib.net/
- PostgreSQL http://www.postgresql.org/
- PostGIS http://postgis.refractions.net/
To make use of the database generated by this tool you will
probably also want to install:
- Mapnik from http://mapnik.org/
Building
========
On most Unix-like systems the program can be compiled by
running 'make'.
Note: the database connection parameters are hard coded to
connect to Postgres on localhost with the current user to
the database 'gis'.
Operation
=========
First you must have setup Postgres with a database named 'gis'
as per http://wiki.openstreetmap.org/index.php/Mapnik
1) Connects to database and creates the following 4 tables:
- planet_osm_point
- planet_osm_line
- planet_osm_roads
- planet_osm_polygon
The prefix "planet_osm" can be changed with the --prefix option,
the above is the default.
2) Runs an XML parser on the input file (typically planet.osm)
and processes the nodes, segments and ways.
3) If a node has a tag declaring one of the attributes below then
it is added to planet_osm_point. If it has no such tag then
the position is noted, but not added to the SQL.
name, place, landuse, waterway, highway,
railway, amenity, tourism, learning
4) Segments are not output in the XML, they are used purely to
locate the nodes during way processing.
5) Ways are read in and the segments are examined to determine
contiguous sequences by WKT(). Each sequence is added to
the tables. If way consists of several dis-joint sequences of
segments then multiple lines will be generated with the
osm_id of the original way.
6) Ways with the tags landuse or leisure are added to the
planet_osm_polygon table. Other ways are added to
planet_osm_line. Roads are also added to planet_osm_roads
7) Indexes are added to speed up the queries by Mapnik.
A quick note on projections
===========================
Depending on the command-line switches you can select which projection you
want the database in. You have three choices:
3395: The WGS84 mercator projection, used in the tile output
4326: The standard lat/long coordinates
900913: The spherical mercator projection, used by TileCache, Google Earth etc.
Depending on what you're using one or the other is appropriate. Most of the
current Mapnik tools and style sheets are configured for 3395 and reproject
to 900913 on the fly. But if you like you can project it correctly in one
step, but don't forget to change the Mapnik config to match.
Combining the -v and -h switches will tell about the exact definitions of
the projections.
In case you want to use some completely different projection there is the -E
option. It will initialise the projection as +init=epsg:<num>. This allows
you to use any projection recognised by proj4, which is useful if you want
to make a map in a different projection. These projections are usually
defined in /usr/share/proj/epsg.
Database Access Examples
========================
If you wish to access the data from the database then the
queries below should give you some hints:
$ psql gis
gis=> \d
List of relations
Schema | Name | Type | Owner
--------+--------------------+-------+----------
...
public | planet_osm_line | table | jburgess
public | planet_osm_point | table | jburgess
public | planet_osm_polygon | table | jburgess
public | planet_osm_roads | table | jburgess
...
gis=> \d planet_osm_line
Table "public.planet_osm_line"
Column | Type | Modifiers
-----------+----------+-----------
osm_id | integer |
name | text |
place | text |
landuse | text |
... [ lots of stuff deleted ] ...
way | geometry | not null
z_order | integer | default 0
Each of the tables contains a subset of the planet.osm file representing
a particular geometry type
- Point contains nodes which have interesting tags
e.g. place=city, name=London
- Line contains ways with interesting tags
e.g. highway=motorway, ref=M25
- Polygon contains ways which form an enclosed area
e.g. landuse=reservoir
The DB columns are used as follows:
- osm_id = the planet.osm ID of the node(point) or way(line,polygon)
- name, place, landuse, ... = the value of the given key, if present on
the node/way. If the tag is not present, the value is NULL. Only a
subset of all possible tags are stored in the DB. Only ones rendered in
the osm.xml are actually interesting to mapnik.
- way = PostGIS geometry describing the physical layout of the object.
Querying specific data requires knowlege of SQL and the OSM key/value
system, e.g.
gis=> select osm_id,astext(way),name from planet_osm_point where amenity='cinema' limit 5;
osm_id | astext | name
----------+-------------------------------------------+--------------------
26236284 | POINT(-79.7160836579093 43.6802306464618) |
26206699 | POINT(51.4051989797638 35.7066045032235) | Cinema Felestin
26206700 | POINT(51.3994885141459 35.7058460359352) | Cinema Asr-e Jadid
20979630 | POINT(151.225781789807 -33.8943079539886) | Paris Cinema
20979684 | POINT(151.226855394904 -33.8946830511095) | Hoyts
(5 rows)
Mapnik renders the data in each table by applying the rules in the
osm.xml file.
> How could I get e.g. all highways in a given bounding box?
The 'way' column contains the geo info and is the one which you need to
use in your WHERE clause. e.g.
gis=> select osm_id,highway,name from planet_osm_line where highway is not null and way && GeomFromText('POLYGON((0 52, 0.1 52, 0.1 52.1, 0 52.1, 0 52))',4326);
osm_id | highway | name
---------+--------------+------------------
4273848 | unclassified |
3977133 | trunk | to Royston (tbc)
4004841 | trunk |
4019198 | trunk |
4019199 | trunk |
4238966 | unclassified |
See the Postgis docs for details, e.g.
http://postgis.refractions.net/docs/ch04.html
|