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.
By the end, you’ll be able to:
- Download OSM data from Protomaps.
- Import the data into Geobase PostgreSQL.
- 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
- 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
. osm2pgsql
is installed:osm2pgsql
, a tool for importing OSM data into PostgreSQL/PostGIS databases. Installation instructions are available on the osm2pgsql website.
Step 1: Download OSM Data from Protomaps
- Access Protomaps: Visit Protomaps OpenStreetMap Extracts.
- Select Area: Use the Draw Rectangle or Draw Polygon tools to outline your area of interest on the map.
- Name Your Extract: Provide a meaningful name for your extract (e.g., “NYC_3D_Buildings”).
- Create Extract: Click on Create Extract. Once processed, download the
.osm.pbf
file.
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:
- Navigate to Project Settings: In your Geobase dashboard, go to
Project Settings > Connection Info
. - Note the Details: Record the following information:
- Host
- Database Name
- User
- Password
- Port
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:
- 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.
- 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 andYOUR_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:
- Access TileServer: In your Geobase dashboard, navigate to
TileServer
.
-
Enable Tables: Click on Tables and check the box next to the imported table(s) to enable them.
-
Integration Examples: Go to Integration Examples and select MapLibre.
- View the Map: Follow the provided instructions to visualize your data using MapLibre.
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.