Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / geodb_build / README.md
| 1 | # geodb_build — offline geocoder database builder |
| 2 | |
| 3 | Builds `geocoder.db`, the offline reverse/forward geocoder database that |
| 4 | `:networklocation` serves the system `GeocodeProvider` from. |
| 5 | |
| 6 | Replaces the `osmium tags-filter | osmium export | scripts/geocoder_gen.cpp` chain, which |
| 7 | needed osmium, g++, OpenMP, simdjson and POSIX `mmap` — so WSL only — and wrote a **101 GB** |
| 8 | GeoJSONSeq intermediate on the way to a 1.4 GB database. This reads the `.osm.pbf` natively |
| 9 | through `scripts/maps/osm_ingest`, holds no intermediate, and runs on Windows. |
| 10 | |
| 11 | ## What v3 indexes that v2 did not |
| 12 | |
| 13 | v2 held **only** objects carrying both `addr:housenumber` and `addr:street`. Everything below |
| 14 | was simply absent, in both directions: |
| 15 | |
| 16 | | Added | Why it mattered | |
| 17 | | --- | --- | |
| 18 | | **Named streets**, sampled every ~250 m along their length (roads only, not footways, service aisles or bus stops) | Reverse geocoding could return a house number or nothing; it could never say "you are on Market Street". | |
| 19 | | **Points of interest** — `amenity`/`shop`/`tourism`/`leisure`/`office`/`healthcare` with a name | A named building or shop was invisible. | |
| 20 | | **Populated places** — `place=city\|town\|village\|hamlet\|suburb\|neighbourhood\|quarter\|borough\|island` | No way to say which neighbourhood or village a point is in. | |
| 21 | | **`addr:place` addresses** | Much of Germany and Austria, and most of Japan and Korea, address buildings against a place rather than a street. Requiring `addr:street` silently discarded all of them. | |
| 22 | |
| 23 | On California, v3 held 8.96 M records where the old rules would have given 3.66 M. |
| 24 | |
| 25 | ## v4: cut down for size |
| 26 | |
| 27 | v4 keeps everything v3 indexes but stores it more cheaply. The header version is 4; the |
| 28 | section layout is unchanged. |
| 29 | |
| 30 | - **Coordinates are e5** (about 1.1 m). At e7 the two coordinate columns were nearly half the |
| 31 | file, spent on centimetres no geocoder answer needs. |
| 32 | - **Fewer street samples**: ~250 m apart instead of ~100 m, roads only, and exact duplicate |
| 33 | rows (adjacent ways meeting at a shared node) written once. |
| 34 | - **The forward index holds only records with a street.** A forward query always names one; |
| 35 | keying on kind instead would lose POIs and places that carry a full address but no house |
| 36 | number. |
| 37 | - **The name index holds one entry per street name per grid cell**, not one per sample. |
| 38 | |
| 39 | ## v5: read from a memory map |
| 40 | |
| 41 | v5 changes no content, only the layout, so the app can serve the planet without holding it in |
| 42 | RAM. v4's reader inflated every dictionary on open: about 2 GB of native heap on the phone and |
| 43 | more than a minute before the first answer. |
| 44 | |
| 45 | - **Dictionaries are stored in blocks** of 1,024 strings, each its own zstd frame, behind an |
| 46 | uncompressed table of every block's first string. A lookup binary-searches that table in the |
| 47 | mapped file and decompresses one block. |
| 48 | - **Column directories store end offsets**, so any block is located straight from the mapping. |
| 49 | |
| 50 | On California this costs 1% of size (43.44 MiB vs 43.00 MiB). |
| 51 | |
| 52 | ## Building and running |
| 53 | |
| 54 | ```sh |
| 55 | cargo build --release -p geodb_build |
| 56 | |
| 57 | # The crate is a member of the repo-root workspace, so the binary lands in the root target/. |
| 58 | # rehearse on a state before the planet |
| 59 | target/release/geodb_build scripts/maps/inputs/california.osm.pbf --out analysis/geodb/ca.db |
| 60 | target/release/geodb_build scripts/maps/inputs/planet.osm.pbf --out analysis/geodb/geocoder.db |
| 61 | ``` |
| 62 | |
| 63 | Run from the repo root. |
| 64 | |
| 65 | Options: `--bbox W,S,E,N` to clip, `--threads N` (default: all cores, or `MAPS_THREADS`), |
| 66 | `--no-verify` to skip the read-back. |
| 67 | |
| 68 | Detached from the Android Rust workspace like `scripts/maps/osm_ingest`, so the build never |
| 69 | pulls the aarch64 toolchain. `zstd` is the one C dependency: the device reader decompresses |
| 70 | with the pure-Rust `ruzstd`, which has no encoder, and nothing here cross-compiles. |
| 71 | |
| 72 | ## Publishing |
| 73 | |
| 74 | ```sh |
| 75 | sha256sum geocoder.db |
| 76 | ../../maps/publish_r2.sh geocoder.db |
| 77 | ``` |
| 78 | |
| 79 | Then put the hash into `OfflineDatabases.sha256For` in the app. |
| 80 | |
| 81 | ## The three orderings that decide whether it works |
| 82 | |
| 83 | None of these fails loudly. A database with any of them wrong opens fine and returns |
| 84 | plausible nonsense, which is why `write::verify` re-reads the finished file through an |
| 85 | independent path and checks all three. |
| 86 | |
| 87 | 1. **Records are ordered by grid cell, then Z-order within the cell.** Reverse lookup walks |
| 88 | outward from the query's cell, so this ordering *is* the spatial index. |
| 89 | 2. **Searchable dictionaries are sorted by UTF-16 code unit**, because the reader binary |
| 90 | -searches them with a UTF-16 comparator. UTF-8 and UTF-16 disagree above the BMP: `U+10000` |
| 91 | sorts after `U+FFFF` in UTF-8 but before it in UTF-16, because surrogates start at `0xD800`. |
| 92 | Sorting the wrong way makes forward lookups start missing entries near the disagreement. |
| 93 | 3. **The forward and name indexes point into grid order**, each sorted by its own key. Out of |
| 94 | step with its dictionary, forward search returns the wrong rows. |
| 95 | |
| 96 | The Z-order interleave is the format's other landmine. v2 interleaved 16 bits per axis, which |
| 97 | worked only because an e6 cell is 50 000 units wide. `format::morton_in_cell` interleaves 20, |
| 98 | which covers an e5 cell (5 000 units, 13 bits) with room to spare; too few bits does not fail, |
| 99 | it silently produces a worse ordering, and on overflow a wrong one. |
| 100 | |
| 101 | ## Determinism |
| 102 | |
| 103 | Two runs over the same input produce byte-identical output, verified on full California. This |
| 104 | is not free: interned dictionary ids are assigned in first-seen order, which depends on how the |
| 105 | PBF's blobs were chunked, so `write` re-sorts each tied run once the final content-derived ids |
| 106 | are known (`sort_rows_deterministically`). A `HashMap` iteration or a non-total sort key |
| 107 | silently destroys the property, and only a byte comparison catches it. |
| 108 | |
| 109 | ## Memory |
| 110 | |
| 111 | The two things that decide whether a planet build fits: |
| 112 | |
| 113 | - **Rows are interned during extraction, not after.** Seven `String`s per row is about 250 GB |
| 114 | at planet scale; seven `u32` ids is 40 bytes a row. Every pass uses `run_pass_sink` rather |
| 115 | than `run_pass` so a chunk's owned strings are interned and dropped as it arrives, instead of |
| 116 | every chunk's strings being alive at once. |
| 117 | - **`osm_ingest::nodeloc` backs its coordinate array with a memory-mapped file**, so the node |
| 118 | table costs 8 bytes per needed node in RAM rather than 16. |
| 119 | |
| 120 | ## Compression |
| 121 | |
| 122 | Column blocks and dictionaries are zstd **level 3**. Measured on California's column blocks, |
| 123 | level 3 is 38.85 MiB in 0.8 s against 35.48 MiB in 30 s at level 19: 9.5% larger for a ~37x |
| 124 | faster write. Blocks are compressed in parallel (they are independent by construction), and the |
| 125 | output is byte-identical whatever the thread count. |