#!/usr/bin/env bash # Import an OSM PBF extract into the local PostGIS stack via osm2pgsql. # # ./import.sh [file.osm.pbf] [extract-name] # # file.osm.pbf default: $OSM_DIR/colombia.osm.pbf (OSM_DIR defaults to # ~/trips/osm — the PBFs live outside the dev tree). # The merged New England file lives at # $HOME/osm-build/data/northeast.osm.pbf (setup-osrm.sh); # any absolute path works, e.g.: # ./import.sh ~/osm-build/data/northeast.osm.pbf northeast # extract-name default: basename without extension # # The osm2pgsql container reaches the DB through the host-published port # 5432 (see docker-compose.yml), so no shared compose network is needed. # After the load, the poi table is refreshed (refresh_poi()) and the # spatial_extract row updated. set -euo pipefail cd "$(dirname "$0")" # docker-compose v1 (python) or v2 (plugin) — whichever the host has DC=docker-compose command -v docker >/dev/null && docker compose version >/dev/null 2>&1 && DC="docker compose" # osm2pgsql image: iboates/osm2pgsql (the osm2pgsql/osm2pgsql repo image does # not exist on Docker Hub). Its /entrypoint.sh does its own DB probing, so we # bypass it and exec osm2pgsql directly. IMG=${OSM2PGSQL_IMAGE:-iboates/osm2pgsql:latest} OSM_DIR="${OSM_DIR:-$HOME/trips/osm}" export OSM_DIR PBF=${1:-$OSM_DIR/colombia.osm.pbf} NAME=${2:-$(basename "${PBF%.osm.pbf}")} ABS=$(realpath "$PBF") [[ -f $ABS && $(stat -c%s "$ABS") -gt 100000 ]] || { echo "no such PBF: $ABS" >&2; exit 1; } # 1. DB up (first boot runs schema.sql) $DC up -d postgis echo "waiting for postgis to be ready…" for i in $(seq 1 60); do if $DC exec -T postgis pg_isready -U trips -d trips >/dev/null 2>&1; then break; fi sleep 2 [[ $i -eq 60 ]] && { echo "postgis never became ready" >&2; exit 1; } done # 2. osm2pgsql load (one-shot container, PBF bind-mounted ro), on the # postgis compose network. Note: this osm2pgsql build's --password flag # FORCES an interactive prompt, so the password goes via PGPASSWORD. # --create is the 2.x default; the 1.x ops machinery is gone in 2.x. NET=$(docker inspect trips-postgis | python3 -c \ "import json,sys; print(next(iter(json.load(sys.stdin)[0]['NetworkSettings']['Networks'])))") docker run --rm \ --entrypoint osm2pgsql \ --network "$NET" \ -e PGPASSWORD=trips \ -v "$(dirname "$ABS"):/data:ro" \ "$IMG" "/data/$(basename "$ABS")" \ --host=postgis --port=5432 --database=trips --user=trips \ -k --slim --flat-nodes=/tmp/flat.nodes # -k: unmatched tags → hstore `tags` # 3. refresh the queryable poi table + extract bookkeeping $DC exec -T postgis psql -U trips -d trips -v ON_ERROR_STOP=1 <