Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify #22

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PGDATABASE=osm_feature_info
PGUSER=osm
PGPASSWORD=osm
PGHOST=postgis
6 changes: 0 additions & 6 deletions .env_template

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
*.zip
assets/
pg_featureserv
.env
41 changes: 5 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ Attempt to create an API to get information about OSM features around a location

## Setup

### Using Docker

Tested on Linux and WSL. Adapt for other operating systems.

```sh
cp .env_template .env
# Optionally change environment variables inside .env

# start services
docker compose up -d
docker compose up -d postgres pg_featureserv

# download sample data
wget -O data/sample.pbf https://download.geofabrik.de/europe/germany/bremen-latest.osm.pbf
Expand All @@ -27,41 +22,15 @@ docker compose run --rm osm2pgsql \
/data/sample.pbf

# post-process import
docker compose exec db psql -f data/post_process_tables.sql
docker compose exec postgres psql -f data/post_process_tables.sql

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

### Without Docker

1. create PostGIS database
2. load OSM data:

```sh
# load data into db
osm2pgsql \
--slim \
--output=flex \
--style=data/flex_style.lua \
--prefix=raw \
data/sample.pbf
```

3. Load these scripts to the database:
- `data/query_function.sql`
- `data/post_process_tables.sql`
4. Install `pg_featureserv` (Download link for Linux: <https://postgisftw.s3.amazonaws.com/pg_featureserv_latest_linux.zip>) and run it via:

```sh
export DATABASE_URL=postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}/${PGDATABASE}
export PGFS_WEBSITE_BASEMAPURL="https://tile.openstreetmap.de/{z}/{x}/{y}.png"
./pg_featureserv
```

## Request API

Request the API with your browser or any other tool:

- HTML: <http://localhost:9000/functions/postgisftw.osm_feature_info/items.html?latitude=53.112&longitude=8.755&distance=50&limit=10000>
- JSON: <http://localhost:9000/functions/postgisftw.osm_feature_info/items.json?latitude=53.112&longitude=8.755&distance=50&limit=10000>
- **HTML**: <http://localhost:9000/functions/postgisftw.osm_feature_info/items.html?latitude=53.112&longitude=8.755&distance=50&limit=10000>
- **JSON**: <http://localhost:9000/functions/postgisftw.osm_feature_info/items.json?latitude=53.112&longitude=8.755&distance=50&limit=10000>
51 changes: 24 additions & 27 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
services:
api:

pg_featureserv:
image: pramsey/pg_featureserv:20240305
ports:
- "0.0.0.0:${OSM_PG_FEATURESERV_API_SYSTEM_PORT}:9000"
- "9000:9000"
environment:
- DATABASE_URL=postgres://${OSM_PG_FEATURESERV_POSTGRES_USER}:${OSM_PG_FEATURESERV_POSTGRES_PASS}@db/${OSM_PG_FEATURESERV_POSTGRES_DBNAME}
- DATABASE_URL=postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}/${PGDATABASE}
- PGFS_WEBSITE_BASEMAPURL=https://tile.openstreetmap.de/{z}/{x}/{y}.png

depends_on:
db:
postgres:
condition: service_healthy
db:

postgres:
image: postgis/postgis:16-3.4
hostname: ${PGHOST}
ports:
- "0.0.0.0:${OSM_PG_FEATURESERV_DB_SYSTEM_PORT}:5432"
- "5432:5432"
volumes:
- ./postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- osm_pg_featureserv_db_data:/var/lib/postgresql/data
- ./data:/data
environment:
# required to initialize database
- POSTGRES_DB=${OSM_PG_FEATURESERV_POSTGRES_DBNAME}
- POSTGRES_USER=${OSM_PG_FEATURESERV_POSTGRES_USER}
- POSTGRES_PASSWORD=${OSM_PG_FEATURESERV_POSTGRES_PASS}
# required to be able to execute stuff in psql
- PGDATABASE=${OSM_PG_FEATURESERV_POSTGRES_DBNAME}
- PGUSER=${OSM_PG_FEATURESERV_POSTGRES_USER}
- PGPASSWORD=${OSM_PG_FEATURESERV_POSTGRES_PASS}
- POSTGRES_DB=${PGDATABASE}
- POSTGRES_USER=${PGUSER}
- POSTGRES_PASSWORD=${PGPASSWORD}
# required to be able to queries in psql
- PGDATABASE
- PGUSER
- PGPASSWORD
healthcheck:
test: [ "CMD-SHELL", "pg_isready",
"-d", "${OSM_PG_FEATURESERV_POSTGRES_DBNAME}",
"-U", "${OSM_PG_FEATURESERV_POSTGRES_USER}"]
test: [ "CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5

osm2pgsql:
image: iboates/osm2pgsql:1.11.0
environment:
- PGHOST=db
- PGPORT=5432
- PGDATABASE=${OSM_PG_FEATURESERV_POSTGRES_DBNAME}
- PGUSER=${OSM_PG_FEATURESERV_POSTGRES_USER}
- PGPASSWORD=${OSM_PG_FEATURESERV_POSTGRES_PASS}
- PGHOST
- PGDATABASE
- PGUSER
- PGPASSWORD
volumes:
- ./data:/data
command: osm2pgsql --slim --output=flex --style=/data/flex_style.lua --prefix=raw /data/sample.pbf
depends_on:
db:
postgres:
condition: service_healthy

volumes:
osm_pg_featureserv_db_data:
Loading