Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / geocoder_gen.sh
geocoder_gen.sh68 lines · 3.2 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # Build and run the offline geocoder database generator (scripts/geocoder_gen.cpp). |
| 3 | # |
| 4 | # Pipeline: |
| 5 | # planet-latest.osm.pbf --osmium--> addr.geojsonseq --geocoder_gen--> geocoder.geodb |
| 6 | # |
| 7 | # The generator self-verifies its output (reverse + forward round-trip) before finishing. |
| 8 | # Nothing here is committed except this script and geocoder_gen.cpp; simdjson and the build |
| 9 | # artifacts live in a scratch dir. Intended to run under WSL/Linux on the build machine. |
| 10 | # |
| 11 | # Usage: |
| 12 | # scripts/geocoder_gen.sh # full run (extract if needed, build, generate) |
| 13 | # PBF=... GEOJSON=... OUT=... scripts/geocoder_gen.sh |
| 14 | set -euo pipefail |
| 15 | |
| 16 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 17 | SCRATCH="${SCRATCH:-$HOME/geocoder-scratch}" |
| 18 | BUILD="$SCRATCH/build" |
| 19 | PBF="${PBF:-$SCRATCH/planet-latest.osm.pbf}" |
| 20 | GEOJSON="${GEOJSON:-$SCRATCH/addr.geojsonseq}" |
| 21 | OUT="${OUT:-$SCRATCH/geocoder.geodb}" |
| 22 | # Pinned to commit SHAs rather than tags: a tag can be repointed upstream, so it is not a content |
| 23 | # pin. To bump, resolve the new tag with `git ls-remote <repo> 'refs/tags/<tag>^{}'` and update the |
| 24 | # SHA and its version comment together. The `^{}` matters: it peels annotated tags (zstd's are) to |
| 25 | # the commit, whereas the bare ref returns the tag object, which is not a valid content ref here. |
| 26 | SIMDJSON_REV="${SIMDJSON_REV:-b4242d3b4ffb97854b035175be077aab712a2d46}" # v3.11.0 |
| 27 | ZSTD_REV="${ZSTD_REV:-63779c798237346c2b245c546c40b72a5a5913fe}" # v1.5.5 |
| 28 | |
| 29 | mkdir -p "$BUILD" |
| 30 | |
| 31 | echo "==> Toolchain check (no sudo; libzstd is linked from the existing runtime .so)" |
| 32 | command -v g++ >/dev/null || { echo "g++ not found. Install: sudo apt-get install -y g++" >&2; exit 1; } |
| 33 | ZSTD_SO="$(ldconfig -p | awk '/libzstd\.so/{print $NF; exit}')" |
| 34 | [ -n "$ZSTD_SO" ] || { echo "libzstd runtime not found (libzstd.so*). Install: sudo apt-get install -y libzstd1" >&2; exit 1; } |
| 35 | echo " using libzstd: $ZSTD_SO" |
| 36 | |
| 37 | echo "==> Headers: simdjson ($SIMDJSON_REV) + zstd.h ($ZSTD_REV)" |
| 38 | if [ ! -f "$BUILD/simdjson.cpp" ] || [ ! -f "$BUILD/simdjson.h" ]; then |
| 39 | base="https://raw.githubusercontent.com/simdjson/simdjson/$SIMDJSON_REV/singleheader" |
| 40 | curl -fL --ipv4 -o "$BUILD/simdjson.h" "$base/simdjson.h" |
| 41 | curl -fL --ipv4 -o "$BUILD/simdjson.cpp" "$base/simdjson.cpp" |
| 42 | fi |
| 43 | if [ ! -f "$BUILD/zstd.h" ]; then |
| 44 | curl -fL --ipv4 -o "$BUILD/zstd.h" \ |
| 45 | "https://raw.githubusercontent.com/facebook/zstd/$ZSTD_REV/lib/zstd.h" |
| 46 | fi |
| 47 | |
| 48 | echo "==> Compiling geocoder_gen" |
| 49 | g++ -O2 -fopenmp -std=c++17 -I"$BUILD" \ |
| 50 | "$HERE/geocoder_gen.cpp" "$BUILD/simdjson.cpp" \ |
| 51 | "$ZSTD_SO" -o "$BUILD/geocoder_gen" |
| 52 | |
| 53 | if [ ! -f "$GEOJSON" ]; then |
| 54 | echo "==> Extracting addresses from $PBF (osmium; multi-hour)" |
| 55 | command -v osmium >/dev/null || { echo "osmium not found. Install: sudo apt-get install -y osmium-tool" >&2; exit 1; } |
| 56 | [ -f "$PBF" ] || { echo "planet PBF not found: $PBF" >&2; exit 1; } |
| 57 | osmium tags-filter "$PBF" \ |
| 58 | n/addr:housenumber w/addr:housenumber r/addr:housenumber \ |
| 59 | -o "$SCRATCH/filtered.osm.pbf" --overwrite |
| 60 | osmium export "$SCRATCH/filtered.osm.pbf" -f geojsonseq -o "$GEOJSON" --overwrite |
| 61 | fi |
| 62 | |
| 63 | echo "==> Generating $OUT from $GEOJSON" |
| 64 | "$BUILD/geocoder_gen" generate "$GEOJSON" "$OUT" |
| 65 | |
| 66 | echo "==> Result" |
| 67 | ls -lh "$OUT" |
| 68 | sha256sum "$OUT" |