Categories
Web Map

Build your self-host world map – Download and import map data into PostGIS

This article logs the process of building my on-premise web map and talks about I use OpenStreetMap to download world map data and import it into PostGIS for late use.

What is OpenStreetMap(OSM)

OpenStreetMap(OSM) is a collaborative project to create a free editable map of the world. The geodata underlying the map is considered the primary output of the project. The creation and growth of OSM have been motivated by restrictions on the use or availability of map data across much of the world, and the advent of inexpensive portable satellite navigation devices.

Where to download map data

You can download OSM data from the official website https://planet.openstreetmap.org , or from a mirror website https://wiki.openstreetmap.org/wiki/Planet.osm#Downloading , and I suggest you download a small part such as a city for testing purpose. I downloaded the New Zealand data as below for testing:

From: http://download.geofabrik.de/

Pre-requisites

We need to install the importing tool “osm2pgsql” and “PostGIS” on the computer. For installing “osm2pgsql”, please refer: https://osm2pgsql.org/doc/install.html, for PostGIS, you can refer to my previous post “Install PostgreSQL 13 with PostGIS and PgAdmin 4 with Docker on Mac” if your laptop is MacBook.

Let’s start the process

  1. Create new user “osmuser” without any database creation privileges in PostgreSQL
    CREATE ROLE osmuser WITH LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT NOREPLICATION CONNECTION LIMIT -1 PASSWORD ‘123456’; COMMENT ON ROLE osmuser IS ‘User for OpenStreetMap(OSM)’;
  2. Create the new database “osm” in PostgreSQL
    CREATE DATABASE osm WITH OWNER = osmuser ENCODING = ‘UTF8’ CONNECTION LIMIT = -1;
  3. Add the extensions “postgis” and “hstore” in the database
    CREATE EXTENSION postgis; CREATE EXTENSION hstore;
  4. Import it into the database:
    osm2pgsql new-zealand-latest.osm.pbf -d osm -H localhost -U postgres -W

After executed successfully, you can see 4 tables have been created and OSM data have been inserted into the database.

Leave a Reply

Your email address will not be published.