
Security News
Knip Hits 500 Releases with v5.62.0, Improving TypeScript Config Detection and Plugin Integrations
Knip hits 500 releases with v5.62.0, refining TypeScript config detection and updating plugins as monthly npm downloads approach 12M.
github.com/openstreetmap/osm2pgsql
Converts OSM planet.osm data to a PostgreSQL / PostGIS database suitable for specific applications like rendering into map tiles by Mapnik or geocoding with Nominatim.
osm2pgsql currently supports two different database schemas
Both schemas were specifically optimized for the purpose they were intended for and they may therefore be less suitable for other general purpose processing. Nevertheless, the rendering schema might be useful for other purposes as well, and has been used for a variety of additionally purposes.
For a broader view of the whole map rendering tool chain see http://wiki.openstreetmap.org/index.php/Mapnik http://wiki.openstreetmap.org/index.php/Osm2pgsql http://wiki.openstreetmap.org/index.php/Slippy_Map
You may find that the wiki pages are more up to date than this readme and may include answers to issues not mentioned here.
Any questions should be directed at the osm dev list http://wiki.openstreetmap.org/index.php/Mailing_lists
The latest source code is available in the OSM git repository on github and can be downloaded as follows:
$ git clone git://github.com/openstreetmap/osm2pgsql.git
The code is written in C and C++ and relies on the libraries below:
To make use of the database generated by this tool you will probably also want to install:
Make sure you have installed the development packages for the libraries mentioned in the requirements section and a C and C++ compiler.
e.g. on Fedora:
on Debian:
On most Unix-like systems the program can be compiled by running './autogen.sh && ./configure && make'.
You must create a PostgreSQL user and a database with the PostGIS functions enabled. This requires access as the database administrator, normally the 'postgres' user.
The default name for this database is 'gis' but this may be changed by using the osm2pgsql --database option.
If the matches the unix user id running the import and rendering then this allows the PostgreSQL 'ident sameuser' authentication to be used which avoids the need to enter a password when accessing the database. This is setup by default on many Unix installs but does not work on Windows (due to the lack of unix sockets).
Some example commands are given below but you may find this wiki page has more up to data information: http://wiki.openstreetmap.org/wiki/Mapnik/PostGIS
$ sudo -u postgres createuser $ sudo -u postgres createdb -E UTF8 -O $ sudo -u postgres createlang plpgsql
Adding the PostGIS extensions. Note the location of the files may vary.
$ sudo -u postgres psql < /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql $ sudo -u postgres psql < /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
Next we need to give the access to update the postgis meta-data tables
$ sudo -u postgres psql -d -c "ALTER TABLE geometry_columns OWNER TO " $ sudo -u postgres psql -d -c "ALTER TABLE spatial_ref_sys OWNER TO "
The 900913 is not normally included with PostGIS. To add it you should run:
$ sudo psql -u postgres psql -d -f 900913.sql
If you want to use hstore support then you will also need to enable the PostgreSQL hstore-new extension.
$ sudo -u postgres psql < /usr/share/postgresql/8.4/contrib/hstore.sql
On PostgreSQL 9.1 and above, you can install it by running "CREATE EXTENSION hstore;" in your database.
Now you can run osm2pgsql to import the OSM data. This will perform the following actions:
If you are using --slim mode, it will create the following additional 3 tables:
Runs an XML parser on the input file (typically planet.osm) and processes the nodes, ways and relations.
If a node has a tag declared in the style file then it is added to planet_osm_point. If it has no such tag then the position is noted, but not added to the database.
Ways are read in converted into WKT geometries by using the positions of the nodes read in earlier. If the tags on the way are listed in the style file then the way will be written into the line or roads tables.
If the way has one or more tags marked as 'polygon' and forms a closed ring then it will be added to the planet_osm_polygon table.
The relations are parsed. Osm2pgsql has special handling for a limited number of types: multipolygon, route, boundary The code will build the appropriate geometries by referencing the members and outputting these into the database.
Indexes are added to speed up the queries by Mapnik.
For an efficient operation of PostgreSQL you will need to tune the config parameters of PostgreSQL from its default values. These are set in the config file at /etc/postgresql/8.4/main/postgresql.conf
The values you need to set will depend on the hardware you have available, but you will likely need to increase the values for the following parameters:
Depending on the command-line switches you can select which projection you want the database in. You have three choices:
4326: The standard lat/long coordinates 900913: The spherical Mercator projection, used by TileCache, Google Earth etc. 3395: The legacy (broken) WGS84 Mercator projection
Depending on what you're using one or the other is appropriate. The default Mapnik style (osm.xml) assumes that the data is stored in 900913 and this is the default for osm2pgsql.
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 initialize the projection as +init=epsg:. This allows you to use any projection recognized 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.
If you wish to access the data from the database then the queries below should give you some hints. Note that these examples all use the 'latlong' projection which is not the default.
$ 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:
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
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Knip hits 500 releases with v5.62.0, refining TypeScript config detection and updating plugins as monthly npm downloads approach 12M.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.