Modern Apps git

← Modern-Apps.git

networklocation: add geocoder db build scripts for reference

Commit
9c62db21ad11682568d3bdbe59442411f5d85dc9
Author
Vayun Mathur <[email protected]>
When
2026-08-11 03:51 UTC (3 months ago)
Parents
f7d2125

Diff

scripts/networklocation/README.md
diff --git a/scripts/networklocation/README.md b/scripts/networklocation/README.mdnew file mode 100644index 0000000000..9a8090958c--- /dev/null+++ b/scripts/networklocation/README.md@@ -0,0 +1,58 @@+# networklocation geocoder — build scripts (reference)++These are the **ad-hoc orchestration scripts** used to build the offline reverse/forward+geocoder database (`geocoder.geodb`) from a full OpenStreetMap planet dump. They are kept+here **for reference** — they document exactly how the shipped `.geodb` was produced. They+are **machine-specific** (hardcoded WSL paths, ~110 GB heap, ~190 GB disk) and are meant to+run on the Linux/WSL build box, not in CI.++The canonical, parameterized entry point already lives one level up:+`scripts/geocoder_gen.sh` (+ `scripts/geocoder_gen.cpp`). The scripts here are the raw+wrappers that were actually run for the planet-scale build, including the alternate+JVM (`GeocoderGenerator` unit test) generation path.++## Pipeline++```+planet-latest.osm.pbf            (~88 GB, OSM planet — the source)+    │  run-extract.sh → networklocation/tools/extract-osm-addresses.sh (osmium)+    ▼+addr.geojsonseq                  (~101 GB; every object with addr:housenumber,+    │                             one GeoJSON Feature per line — ~289.9 M lines)+    │  gen.sh   (JVM :networklocation GeocoderGenerator test, ~110 GB heap)+    ▼+geocoder.geodb                   (~2.3 GB, 260.8 M records; self-verified)+    │  reencode.sh  (scripts/geocoder_gen.cpp `reencode` → compacter format)+    ▼+geocoder2.geodb / geocoder-final.geodb   (~1.4 GB, the shipped product)+```++`addr.geojsonseq` is an **intermediate** — regenerable from the `.pbf` via `run-extract.sh`,+so it can be deleted to reclaim disk once the `.geodb` is built.++## Scripts++| Script | Role |+| --- | --- |+| `run-extract.sh` | planet `.pbf` → `addr.geojsonseq` via `networklocation/tools/extract-osm-addresses.sh` (osmium). Logs size + line count. |+| `gen.sh` | `addr.geojsonseq` → `geocoder.geodb` via the JVM `GeocoderGenerator` test (`GEOCODER_INPUT`/`GEOCODER_OUTPUT`/`GEOCODER_HEAP` env). Prints output size + sha256. |+| `reencode.sh` | Compiles `scripts/geocoder_gen.cpp` (+ simdjson, libzstd) and re-encodes `geocoder.geodb` → `geocoder2.geodb`. |+| `poll.sh` | Heartbeat monitor for the extraction (osmium PID, output growth); exits on `END` in `extract.log`. |+| `gpoll.sh` | Heartbeat monitor for generation (java RSS, free memory, geodb growth); exits on `GEN END` in `gen.log`. |+| `memest.py` | Estimates peak JVM heap for `GeocoderGenerator` over the whole planet from a sample of `addr.geojsonseq` (used to size `GEOCODER_HEAP`). |++## Dependencies++- **Extraction:** `osmium-tool` (invoked by `networklocation/tools/extract-osm-addresses.sh`).+- **JVM generation (`gen.sh`):** a JDK + the repo's Gradle wrapper; a machine with enough RAM+  for `GEOCODER_HEAP` (~110 GB for the full planet — see `memest.py`).+- **C++ reencode (`reencode.sh`):** `g++` (OpenMP, C++17), `libzstd`, and `simdjson`+  (`build/simdjson.cpp` in the scratch dir).++## Caveats++- Paths like `/home/vayun/geocoder-scratch` and `/mnt/c/Users/Vayun/Documents/code/Modern-Apps`+  are hardcoded — adjust before reuse.+- The full run needs ~190 GB of free disk (88 GB `.pbf` + 101 GB `.geojsonseq`) and a+  large-RAM box; on WSL, deleting the intermediates does not shrink the ext4 vhdx until you+  compact it.
scripts/networklocation/gen.sh
diff --git a/scripts/networklocation/gen.sh b/scripts/networklocation/gen.shnew file mode 100644index 0000000000..534826fc2d--- /dev/null+++ b/scripts/networklocation/gen.sh@@ -0,0 +1,19 @@+#!/usr/bin/env bash+set -euo pipefail+cd /mnt/c/Users/Vayun/Documents/code/Modern-Apps+export GEOCODER_HEAP="${GEOCODER_HEAP:-110g}"+export GEOCODER_INPUT="$HOME/geocoder-scratch/addr.geojsonseq"+export GEOCODER_OUTPUT="$HOME/geocoder-scratch/geocoder.geodb"+echo "GEN START $(date -u)"+echo "  heap=$GEOCODER_HEAP"+echo "  input=$GEOCODER_INPUT ($(stat -c%s "$GEOCODER_INPUT") bytes)"+echo "  output=$GEOCODER_OUTPUT"+rc=0+java -jar gradle/wrapper/gradle-wrapper.jar :networklocation:testDebugUnitTest \+  --tests '*GeocoderGenerator' \+  --rerun-tasks --no-configuration-cache --console=plain || rc=$?+echo "GEN END $(date -u) rc=$rc"+if [ -f "$GEOCODER_OUTPUT" ]; then+  echo "OUTPUT_BYTES $(stat -c%s "$GEOCODER_OUTPUT")"+  echo "OUTPUT_SHA $(sha256sum "$GEOCODER_OUTPUT" | cut -d" " -f1)"+fi
scripts/networklocation/gpoll.sh
diff --git a/scripts/networklocation/gpoll.sh b/scripts/networklocation/gpoll.shnew file mode 100644index 0000000000..54c50e8d0b--- /dev/null+++ b/scripts/networklocation/gpoll.sh@@ -0,0 +1,19 @@+#!/usr/bin/env bash+# Heartbeat poller for the generator. Emits status every 120s, exits when GEN END appears.+LOG="$HOME/geocoder-scratch/gen.log"+OUT="$HOME/geocoder-scratch/geocoder.geodb"+for i in $(seq 1 60); do+  sleep 120+  echo "=== gpoll $i $(date -u) ==="+  # biggest java RSS (the test worker with the big heap)+  ps -eo pid,rss,pcpu,etime,comm --sort=-rss | grep -m3 java || echo "no java"+  free -h | awk 'NR==1||/Mem|Swap/'+  [ -f "$OUT" ] && echo "geodb: $(stat -c%s "$OUT") bytes"+  echo "--- tail gen.log ---"+  tail -n 6 "$LOG"+  if grep -q 'GEN END' "$LOG"; then+    echo "*** GEN DONE ***"+    tail -n 20 "$LOG"+    break+  fi+done
scripts/networklocation/memest.py
diff --git a/scripts/networklocation/memest.py b/scripts/networklocation/memest.pynew file mode 100644index 0000000000..afd84359b4--- /dev/null+++ b/scripts/networklocation/memest.py@@ -0,0 +1,72 @@+#!/usr/bin/env python3+# Estimate JVM retained heap for GeocoderGenerator over the whole planet, from a sample.+# Models GeoAddress objects + their (non-deduped) Strings + the writer's transient arrays.+import json, sys++SAMPLE = int(sys.argv[2]) if len(sys.argv) > 2 else 5_000_000+TOTAL_LINES = 289_881_205  # wc -l addr.geojsonseq++def roundup8(x): return (x + 7) & ~7+# JDK17 compressed-oops sizes+def strbytes(s):+    if not s: return 0            # empty strings are shared -> ~0 amortized+    # String shell (~24B) + byte[] (header 16 + len, latin1 ~1B/char), 8-align+    return 24 + roundup8(16 + len(s))++path = sys.argv[1]+kept = 0+lines = 0+addr_obj = 0      # GeoAddress shells + arraylist backing ref+addr_str = 0      # retained (non-deduped) string bytes+uniq = {k:set() for k in ('house','street','city','state','country','postcode')}++FIELDS = ('house','street','city','state','country','postcode')+with open(path, 'r', encoding='utf-8', errors='replace') as f:+    for raw in f:+        if lines >= SAMPLE: break+        line = raw.strip().strip('\x1e')+        if not line or line[0] != '{':+            continue+        lines += 1+        try:+            o = json.loads(line)+        except Exception:+            continue+        p = o.get('properties') or {}+        street = p.get('addr:street','') or ''+        house  = p.get('addr:housenumber','') or ''+        if not street and not house:+            continue+        geom = o.get('geometry') or {}+        if 'coordinates' not in geom:+            continue+        city = p.get('addr:city','') or ''+        state = (p.get('addr:state','') or p.get('addr:province','')) or ''+        country = p.get('addr:country','') or ''+        postcode = p.get('addr:postcode','') or ''+        vals = dict(house=house,street=street,city=city,state=state,country=country,postcode=postcode)+        kept += 1+        addr_obj += 56 + 4       # GeoAddress shell (56) + one ArrayList ref (4)+        for k in FIELDS:+            v = vals[k]+            addr_str += strbytes(v)+            if v: uniq[k].add(v)++scale = TOTAL_LINES / lines if lines else 0+kept_total = kept * scale+retained_addr = (addr_obj + addr_str) * scale+# Writer transient peak: recs(56+4) + 8 int columns(32) + fwd boxed sort(20+4) per kept record+peak_extra = kept_total * (60 + 32 + 24)+peak = retained_addr + peak_extra++GB = 1024**3+print(f"sample_lines_parsed={lines}")+print(f"sample_kept={kept}  keep_ratio={kept/max(lines,1):.3f}")+print(f"scale_factor={scale:.2f}")+print(f"est_total_kept_records={kept_total/1e6:.1f} M")+print(f"est_retained_addresses={retained_addr/GB:.1f} GB")+print(f"est_writer_transient_extra={peak_extra/GB:.1f} GB")+print(f"est_PEAK_heap={peak/GB:.1f} GB")+print("--- sample unique counts (not scaled) ---")+for k in FIELDS:+    print(f"  uniq_{k}={len(uniq[k])}")
scripts/networklocation/poll.sh
diff --git a/scripts/networklocation/poll.sh b/scripts/networklocation/poll.shnew file mode 100644index 0000000000..2491f44941--- /dev/null+++ b/scripts/networklocation/poll.sh@@ -0,0 +1,24 @@+#!/usr/bin/env bash+# Heartbeat poller for the planet extraction. Emits status every 120s so the+# outer tool call does not hit its idle timeout, and exits when extraction ends.+LOG="$HOME/geocoder-scratch/extract.log"+OUT="$HOME/geocoder-scratch/addr.geojsonseq"+for i in $(seq 1 30); do+  sleep 120+  echo "=== poll $i $(date -u) ==="+  if pgrep osmium >/dev/null; then+    ps -o pid,etime,pcpu,cmd -p "$(pgrep osmium)" | tail -n +2+  else+    echo "osmium NOT running"+  fi+  for f in /tmp/tmp.*/addr.osm.pbf; do+    [ -f "$f" ] && echo "filtered_pbf: $(stat -c%s "$f") bytes"+  done+  [ -f "$OUT" ] && echo "geojsonseq: $(stat -c%s "$OUT") bytes"+  tail -2 "$LOG"+  if grep -q '^END' "$LOG"; then+    echo "*** EXTRACT DONE ***"+    tail -5 "$LOG"+    break+  fi+done
scripts/networklocation/reencode.sh
diff --git a/scripts/networklocation/reencode.sh b/scripts/networklocation/reencode.shnew file mode 100644index 0000000000..776ec2eb54--- /dev/null+++ b/scripts/networklocation/reencode.sh@@ -0,0 +1,10 @@+#!/usr/bin/env bash+set -euo pipefail+cd /home/vayun/geocoder-scratch+echo ==\> Compiling+g++ -O2 -fopenmp -std=c++17 -I build /mnt/c/Users/Vayun/Documents/code/Modern-Apps/scripts/geocoder_gen.cpp build/simdjson.cpp /lib/x86_64-linux-gnu/libzstd.so.1 -o build/geocoder_gen2+echo ==\> Reencoding+build/geocoder_gen2 reencode geocoder.geodb geocoder2.geodb+echo ==\> Result+ls -la geocoder2.geodb+sha256sum geocoder2.geodb
scripts/networklocation/run-extract.sh
diff --git a/scripts/networklocation/run-extract.sh b/scripts/networklocation/run-extract.shnew file mode 100644index 0000000000..588f8945ce--- /dev/null+++ b/scripts/networklocation/run-extract.sh@@ -0,0 +1,10 @@+#!/usr/bin/env bash+set -euo pipefail+cd /mnt/c/Users/Vayun/Documents/code/Modern-Apps+OUT="$HOME/geocoder-scratch/addr.geojsonseq"+PBF="$HOME/geocoder-scratch/planet-latest.osm.pbf"+echo "START $(date -u) -> $OUT"+bash networklocation/tools/extract-osm-addresses.sh "$PBF" "$OUT"+echo "END $(date -u)"+echo "SIZE $(stat -c%s "$OUT") bytes"+echo "LINES $(wc -l < "$OUT")"