Skip to content

Instantly share code, notes, and snippets.

@raphaelmerx
Last active August 31, 2021 04:37
Show Gist options
  • Save raphaelmerx/8dcf5e71216591fa9f47c4c2230c66ab to your computer and use it in GitHub Desktop.
Save raphaelmerx/8dcf5e71216591fa9f47c4c2230c66ab to your computer and use it in GitHub Desktop.

Extracting data from OpenStreetMap into a Postgres db

1. Download PNG data

wget https://download.geofabrik.de/australia-oceania/papua-new-guinea-latest.osm.pbf

2. Clone the openstreetmap-carto dir

git clone git://github.com/gravitystorm/openstreetmap-carto.git --depth 1

3. Install osm2pgsql

On MacOS brew install osm2pgsql

4. Import to a new postgres db

4.1. Set up the database

psql -c "create database png_osm"
psql -d png_osm -c "CREATE EXTENSION hstore;"
psql -d png_osm -c "CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;"

4.1. Import data from the PBF file to the db

osm2pgsql --host 127.0.0.1 -P 5432 -d png_osm --create --slim -G --hstore -C 2500 --number-processes 1 -S openstreetmap-carto/openstreetmap-carto.style papua-new-guinea-latest.osm.pbf

5. Start extracting data

Example: query all schools stored as a point:

png_osm=# select name from planet_osm_point where amenity = 'school' and name != '';
                         name
-------------------------------------------------------
 Daru Chalmers Primary School
 Tangi Primary School
 Towan Primary School
 Towan Primary School
 Pawari Holy Cross Primary School
 Margarima Secondary School
 Tukupawi Primary School
 Lake Haiabi Primary School

Note that some schools are stored as polygon:

png_osm=# select count(*) from planet_osm_polygon where amenity = 'school';
 count
-------
   854
(1 row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment