Files
watermaps/scripts/setup-local-routing.sh

96 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
GEOFABRIK_DIR="${WATERMAPS_GEOFABRIK_DIR:-${SEA_COMPASS_GEOFABRIK_DIR:-data/geofabrik}}"
PYTHON_TARGET="$ROOT_DIR/.tools/python"
OUTPUT_PATH="${WATERMAPS_LOCAL_FAIRWAYS_PATH:-$ROOT_DIR/data/local/germany-netherlands-fairways.json}"
if [[ "$GEOFABRIK_DIR" != /* ]]; then
GEOFABRIK_DIR="$ROOT_DIR/$GEOFABRIK_DIR"
fi
if [[ "$OUTPUT_PATH" != /* ]]; then
OUTPUT_PATH="$ROOT_DIR/$OUTPUT_PATH"
fi
PBF_PATHS=(
"$GEOFABRIK_DIR/germany-latest.osm.pbf"
"$GEOFABRIK_DIR/netherlands-latest.osm.pbf"
)
cd "$ROOT_DIR"
# This validates existing snapshots and only replaces them after a complete,
# checksum-verified download.
WATERMAPS_GEOFABRIK_DIR="$GEOFABRIK_DIR" \
./scripts/download-geofabrik.sh germany netherlands
# A successful previous build can be reused when both downloaded snapshots
# still match the checksums recorded in its per-source metadata.
if python3 - "$OUTPUT_PATH" "${PBF_PATHS[@]}" <<'PY'
import json
from pathlib import Path
import re
import sys
output = Path(sys.argv[1])
pbf_paths = [Path(value) for value in sys.argv[2:]]
try:
document = json.loads(output.read_text(encoding="utf-8"))
sources = {
source["file"]: source
for source in document["sources"]
if isinstance(source, dict) and isinstance(source.get("file"), str)
}
if (
document.get("version") != 1
or document.get("generatorVersion") != 2
or not isinstance(document.get("ways"), list)
):
raise ValueError("unsupported index format")
for pbf_path in pbf_paths:
checksum_text = Path(f"{pbf_path}.md5").read_text(encoding="utf-8")
checksum = checksum_text.split(maxsplit=1)[0].lower()
metadata = sources[pbf_path.name]
if not re.fullmatch(r"[0-9a-f]{32}", checksum):
raise ValueError("invalid checksum")
if metadata.get("checksumMd5") != checksum:
raise ValueError("snapshot changed")
if metadata.get("sizeBytes") != pbf_path.stat().st_size:
raise ValueError("snapshot size changed")
except (
FileNotFoundError,
IndexError,
KeyError,
TypeError,
ValueError,
json.JSONDecodeError,
):
raise SystemExit(1)
PY
then
echo "Lokaler Deutschland-/Niederlande-Routingindex ist bereits aktuell: $OUTPUT_PATH"
exit 0
fi
mkdir -p "$PYTHON_TARGET"
if ! PYTHONPATH="$PYTHON_TARGET" python3 - <<'PY'
from importlib.metadata import version
import osmium
raise SystemExit(0 if version("osmium") == "4.3.1" else 1)
PY
then
python3 -m pip install \
--disable-pip-version-check \
--target "$PYTHON_TARGET" \
--upgrade \
"osmium==4.3.1"
fi
PYTHONPATH="$PYTHON_TARGET" python3 scripts/build-local-fairways.py \
"${PBF_PATHS[@]}" \
--output "$OUTPUT_PATH"
echo "Lokale Deutschland-/Niederlande-Fahrrouten sind bereit: $OUTPUT_PATH"