PostGIS OSM Import

Because of its capabilities in handling spatial data PostGIS is predestined for large vector files like Open Street Map data sets. That’s why a lot of popular OSM maps out there are powered by PostGIS and Mapnik.
Previously I showed you how to set up a PostGIS environment on Ubuntu Linux. Today I want to demonstrate how to import OSM pbf files into this database. If you have only small areas of interest you could download the data as shape files and import it via the database manager in QGIS. For larger data sets like whole country areas QGIS is not suited for that kind of task. We better use a command line tool: osm2pgsql.
So, let’s start!
First we are going to download a OSM raw file we are interested in. As a source you can use http://download.geofabrik.de/. As an example we are going to get the data set of Iceland because of its relatively small size (processing large files requires a bunch of RAM): iceland-latest.osm.pbf
Now let’s create a new spatial database. I will do this with some short commands in my Linux terminal. Alternatively, you can use pgAdmin.

Create database osm_data:

sudo -u postgres createdb -O  osm_data

Create the PostGIS extension for osm_data:

sudo -u postgres psql -c "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;" osm_data

Let’s install osm2pgsql:

sudo apt install osm2pgsql

How does osm2psql work? Have a look at the help:

osm2pgsql --help

osm2pgsql needs a custom style file for importing the raw data into the database. You can get the default style file here . You can modify the style file for your needs. A description is included.

Start import process:

osm2pgsql --create --database osm_data --username --host localhost --port 5432 --password --cache 20000 -S /default.style /iceland-latest.osm.pbf

Please modify the operators for your needs. For example, adjust the cache parameter to your RAM size. Default is 800 MB.

If everything went successful we can now integrate the data into QGIS and for example have a look at the road network of Reykjavik:

Reykjiavik road render example

Reykjavik road render example

About This Author

Simon is a research assistant in the field of remote sensing at Technical University of Munich (TUM). Currently he is working on methods to describe reed ecosystem parameters with the help of hyperpectral and LiDAR data. Besides geoinformatics he loves to do outdoor activities such as hiking and climbing.

Post A Reply

*