Skip to content

Attempt to create an API to get information about OSM features around a location.

Notifications You must be signed in to change notification settings

JakobMiksch/osm-feature-info

Repository files navigation

Feature Info API for OpenStreetMap

Attempt to create an API to get information about OSM features around a location. The current goal is to create a function that mimics the same output as the existing feature info function on the official OSM website. Find the source code of the existing function here.

Setup

Tested on Debian/Ubuntu systems.

From scratch

  1. Ensure you have a Postgres/PostGIS database available

  2. Install osm2pgsql with minimal version 2.0.0

  3. Setup Themepark

    • clone the Themepark repo into a location of your choice
    • in your shell link the Themepark via this environment variable:
    export LUA_PATH="YOUR_PATH_TO/osm2pgsql-themepark/lua/?.lua;;"
  4. Import an OSM extract into the database:

    # set database connection
    export PGHOST=???
    export PGPORT=???
    export PGDATABASE=???
    export PGUSER=???
    
    # set password by environment variable or run "osm2pgsql" using "-W" to prompt for the password
    export PGPASSWORD=???
    
    osm2pgsql \
    --slim \
    --output=flex \
    --style=./data/flex_style.lua \
    --prefix=raw \
    ./PATH_TO_YOUR/OSM_EXTRACT.pbf
  5. Apply the SQL-function that queries the OSM features around a location:

    # use the same environment variables as above
    # you can also prompt for password using `-W`
    psql -f ./data/query_function.sql
  6. Install pg_featureserv and run:

    # use the environment variables from above
    export DATABASE_URL=postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}/${PGDATABASE}
    export PGFS_WEBSITE_BASEMAPURL=https://tile.openstreetmap.de/{z}/{x}/{y}.png
    export PGFS_PAGING_LIMITDEFAULT=100
    pg_featureserv
  7. make a query and get a GeoJSON as response to ensure it works as expected:

    export latitude=42.533888
    export longitude=1.5929665
    export radius=35
    export min_lon=1.590091
    export min_lat=42.526629
    export max_lon=1.607210
    export max_lat=42.53863
    
    curl "http://localhost:9000/functions/postgisftw.osm_website_combi/items.json?latitude=${latitude}&longitude=${longitude}&radius=${radius}&min_lon=${min_lon}&min_lat=${min_lat}&max_lon=${max_lon}&max_lat=${max_lat}"
  8. [optional] Use the demo web client for testing and debugging. See respective README. You might need to fix some CORS issues.

Using Docker

Ensure you have Docker installed. See official instructions.

Note: Depending on you installation the docker commands are differently:

  • it might be necessary that you have to add sudo before your docker commmands
  • Docker Compose can either be called by using docker-compose or docker compose (either with -(hyphen) or whitespace). It depends on your installation.
# download sample data (or use your own)
wget -O data/sample.pbf https://download.geofabrik.de/europe/andorra-latest.osm.pbf

# [optional] shut down existing compose stack
docker compose down --volumes --remove-orphans

# start services
docker compose up -d

# load data into db
docker compose run --rm osm2pgsql \
  --slim \
  --output=flex \
  --style=/data/flex_style.lua \
  --prefix=raw \
  /data/sample.pbf

# add function to DB
docker compose exec postgres psql -f /data/query_function.sql

Demo client can be accessed via http://localhost:4173/.

Replication can be executed like this:

# replication init
docker compose run --rm osm2pgsql replication init \
 --verbose \
 --prefix=raw \
 --osm-file=/data/sample.pbf

# replication update
docker compose run --rm osm2pgsql replication update \
  --prefix=raw \
  --verbose

Request API

Request the API with your browser or any other tool http://localhost:9000/functions/postgisftw.osm_website_combi/items.json?latitude=42.533888&longitude=1.5929665&radius=35&min_lon=1.590091&min_lat=42.526629&max_lon=1.607210&max_lat=42.53863