Importing OpenStreetMap (OSM) Data into Geobase

In this guide, we’ll walk you through importing buildings data from OSM into your Geobase PostgreSQL database using osm2pgsql. The buildings will have heights and will be displayed as 3D buildings in Geobase.

3dbuildings

By the end, you’ll be able to:

  1. Download OSM data from Protomaps.
  2. Import the data into Geobase PostgreSQL.
  3. Visualize the imported data using MapLibre through the Geobase Tile Server.
ℹ️

PostGIS is automatically enabled in your Geobase instance. But if you need to check it, you can do so in your Geobase dashboard by navigating to Database > Extensions.

Prerequisites

  1. Geobase project is ready: Ensure you have created a project on Geobase and have your database connection details from your Geobase project dashboard by navigating to Project Settings > Connection Info.
  2. osm2pgsql is installed: osm2pgsql, a tool for importing OSM data into PostgreSQL/PostGIS databases. Installation instructions are available on the osm2pgsql website.

osm2pgsql

Step 1: Download OSM Data from Protomaps

  1. Access Protomaps: Visit Protomaps OpenStreetMap Extracts.

osm2pgsql

  1. Select Area: Use the Draw Rectangle or Draw Polygon tools to outline your area of interest on the map.
  2. Name Your Extract: Provide a meaningful name for your extract (e.g., “NYC_3D_Buildings”).
  3. Create Extract: Click on Create Extract. Once processed, download the .osm.pbf file.

osm2pgsql

ℹ️

Note: The .osm.pbf format is a compressed binary format commonly used for OSM data.

Step 2: Prepare Geobase Database Connection

Retrieve your Geobase database connection details:

  1. Navigate to Project Settings: In your Geobase dashboard, go to Project Settings > Connection Info.
  2. Note the Details: Record the following information:
    • Host
    • Database Name
    • User
    • Password
    • Port

connection-details

These details will be used to connect osm2pgsql to your Geobase Postgres database.

Step 3: Import OSM Data into Geobase

With your .osm.pbf file and database connection details ready, proceed to import the data:

  1. Download the Lua Configuration Script: For 3D building data, download the 3dbuildings.lua script from 3dbuildings.lua.

You can also use curl to download the script:

curl -O https://osm2pgsql.org/examples/3dbuildings/3dbuildings.lua
ℹ️

osm2pgsql uses the lua script as a recipe to process the osm data and import it into the database that you provide it a connection to. In our example we are using the 3dbuildings.lua script is for loading building footprints and their heights from osm into Geobase and displaying them as 3D buildings.

  1. Run osm2pgsql Command: Open your terminal and execute the following command, replacing placeholders with your actual details:
osm2pgsql -d postgres -U supabase_admin -H YOUR_PROJECT_REF.geobase.app \
  -P 6443 --password -O flex -S 3dbuildings.lua YOUR_OSM_FILE_PATH.osm.pbf

you will need to replace YOUR_PROJECT_REF with your actual project reference and you will be prompted for the database password and YOUR_OSM_FILE_PATH with the path to your downloaded .osm.pbf file.

  • -d: Database name.
  • -U: Database user.
  • -H: Database host.
  • -P: Database port.
  • --password: Prompts for the database password.
  • -O flex: Uses the flexible output mode.
  • -S 3dbuildings.lua: Specifies the Lua configuration script.
  • your_map.osm.pbf: Path to your downloaded .osm.pbf file.
ℹ️

Ensure the 3dbuildings.lua script is in your current directory or provide the full path to it.

Step 4: Visualize Data in Geobase

After importing the data:

  1. Access TileServer: In your Geobase dashboard, navigate to TileServer.

osm2pgsql

  1. Enable Tables: Click on Tables and check the box next to the imported table(s) to enable them.

  2. Integration Examples: Go to Integration Examples and select MapLibre.

osm2pgsql

  1. View the Map: Follow the provided instructions to visualize your data using MapLibre.

osm2pgsql

ℹ️

For detailed styling and customization, refer to the MapLibre documentation.

Additional Resources

  • maplibre: MapLibre is an open-source, interoperable fork of Mapbox GL JS.
  • osm2pgsql Documentation: Comprehensive guides and examples are available on the osm2pgsql website.
  • Protomaps Documentation: Learn more about data extraction and usage at Protomaps Docs.

Support

If you encounter any issues or have further questions, reach out on Discord.