Import OSM data in Postgis – OSM2pgsql



This article lets you know how to import data in postgresql.  Open Street map (OSM) is open and free data. Anyone one can download this data. It mainly contains point data, line data, polygon data and geometry information. Data contains much information and becomes heavy i.e. upto 30GB or more. Many compression techniques are applied to make it available to user in minimum size.

Install osm2pgsql- Import OSM data in Postgis

This utility can be installed using command prompt. Follow the command,

--> apt-get install osm2pgsql

Now you can see the options available with the command. Type osm2pgsql in command prompt. You will get the following result.

Import OSM data in Postgis- OSM2pgsql

We know that you import data in database we must create a database for storing tables. You can download and install postgresql with our previous tutorial Install PostGIS and PostgreSQL in Linux Ubuntu 16.4 

Here we have database ‘potsgres’ in Postgresql with a user. In the command we specify option -s is slim mode,Store temporary tables in database. This allows incremental updates using diff files also available at OSM data servers, and reduces memory usage at a cost in disk space and import time. This mode of operation is recommended.  The option -G Generate multi-geometry features in PostgreSQL tables. Option -U is user available in Postgresql, -W  prompts for password and in the last give file name.

As files are heavy in size so this process takes time.

Import OSM data in Postgis- OSM2pgsql
Import OSM data in Postgis

This Open street map data is also available in pbf format (Protocolbuffer Binary Format). This format is specially designed to compress data. This is much faster than gzipped and bzipped planet while reading files.

The data available is in data.osm.pbf format so we need to extract this data first. This can be done with osmconvert command. Type osmconvert in command prompt, if not available install this with given command.

–>sudo apt-get install osmctools

Now use the following command to convret data in osm,

–>osmconvert Input.pbf > output.osm

After this check for the OSM file in folder. Now use the command given for importing data in postgresql.

Import OSM data in Postgis- OSM2pgsql

Output

This output can be seen in postgresql available in table format.

Import OSM data in Postgis- OSM2pgsql
Import OSM data in Postgis

In this way you can also import shapefile data in postgresql. You can also try out conversion using GDAL utilities as shapefile to sql, shapefile to mif etc.

Please let us know if you need any help by commenting in given comment box.

Author: Akshay Upadhyay

Owner and Director of a Private Limited company which serves individual to large scale industries in the field of Maps and GIS. He is a Gold Medalist in M.Tech(Spatial Information Technology) and owns some famous Technology blogs and website... Know more

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.