This image comes from Dribbble.
In order to import OSM data into postgres, some softwares are required.
- PostgreSQL, the geo-spacial database, for Windows OS.
- PostGIS, a spatial database extender for PostgreSQL object-relational database.
When all the above mentioned softwares are installed, you can start to import OSM data following the details below.
- pbf file of targeted region. You can download from multiple websites. Commonly, we use GEOFABRIK.
- Osmconvert can be used to convert and process OpenStreetMap files. It can transfer .pbf files into .osm files.
- osm2pgsql. It is a tool used to transfer OSM data format to postGIS-enabled PostgreSQL databases.
- Osmfilter can extract OSM data with specified tags.
Then you need to open command line to do all the processing and tranformation.
- Convert pbf file to osm file:
osmconvert ./path/to/your/pbf/file/targeted region.pbf > ./path/to/your/expected/osm/file/targeted region.osm - For my application, I need all the road networks:
osmfilter ./path/to/your/expected/osm/file/targeted region.osm --keep="highway=" -o=./path/to/your/expected/osm/file/with/only/highway/targeted region.osm - Create a PostGIS database and remember the name, password and port number.
You need to open the PostGIS 2.0 shapefile and DBF Loader and Exporter by clicking the icon after installing PostGIS.
Then fill all the information into the blank box after clicking View connection detials... button.
The results can be found in the figure below.
- Enable PostGIS on the database:
Connect to database:
psql databasename
In the databased prompt:
create extension potgis - Import osm file into postgres database:
osm2pgsql.exe -s -U username -W -d database name -P port number ./path/to/the/osm/file/road.osm
An example is like:
osm2pgsql.exe -s -U postgres -W -d sgosm -P 5434 F:\road_network.osm - Input the password of your data base
- The success of importing shows "Osm2pgsql took 27s overall".
Then we need to open PostgreSQL to check the results. You need to open the pgAdmin 4 and check whether they are successfully imported. The items are not empty. You can find a successful screen shot below. The brackets have numbers inside, which means the OSM data are imported successfully.
success screenshotThis is a summary when I import OSM files into PostgreSQL database. This database can be easily conneted with Python.
网友评论