Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / tiler_s1.rs
tiler_s1.rs156 lines · 7.5 KiBRaw
| 1 | pub struct Settings { |
| 2 | pub build_id: u64, |
| 3 | /// Where one zoom's chunks go while they wait for the merge. See [`crate::tilespill`]. |
| 4 | /// |
| 5 | /// Beside the output archive, as `<out>.tilechunks`, matching where the feature spill is placed. |
| 6 | /// Truncated at the start of every zoom and removed at the end of each, so it holds one zoom. |
| 7 | pub scratch: PathBuf, |
| 8 | /// Force the tile-chunk spill through the scratch *file* even when the budget gate would pick |
| 9 | /// anonymous pagefile-backed memory. Set from `MAPS_ANON_SPILL` (unset, or any value but |
| 10 | /// `1`/`true`/`yes`, forces files): on a box whose commit headroom is tens of GB against a |
| 11 | /// ~900 GB estimate, anon staging pages the merge to death -- files page too, but against |
| 12 | /// the scratch volume rather than the commit limit that aborts the build. Same chunks, same |
| 13 | /// offsets, same bytes; the merge reads through the same `ChunkReader` either way. |
| 14 | pub force_chunk_spill_file: bool, |
| 15 | /// The DEM heightmap dataset to sample one grid per output tile from. |
| 16 | /// See [`crate::dem::Dem`]. A tile with no DEM under it carries no heightmap |
| 17 | /// section (stays 16-byte). |
| 18 | pub dem: crate::dem::Dem, |
| 19 | /// Place-label id (tagged node) -> the tagged relation id of the admin boundary it names, from |
| 20 | /// stage A. The tiler stamps each `places` label whose id is a key here with the linked id in |
| 21 | /// the body's region-link table, so a tap on the label outlines that exact region. Empty when no |
| 22 | /// links were found (the section is then omitted from every tile). |
| 23 | pub region_links: std::collections::HashMap<u64, u64>, |
| 24 | /// Place-label id (tagged, `country` places only) -> the label's start zoom, from |
| 25 | /// footprint × headcount (see `extract_fallback::country_zoom`). The tiler drops the |
| 26 | /// label below its start zoom — same shape as `region_links`, probe-only so the |
| 27 | /// archive stays deterministic. Absent ids keep the schema floor (z0): a missing |
| 28 | /// score must never hide a country. |
| 29 | pub country_zooms: std::collections::HashMap<u64, u8>, |
| 30 | } |
| 31 | |
| 32 | /// One chunk's share of a zoom, keyed on `(tile id, layer id)`. |
| 33 | /// |
| 34 | /// Keyed on the **tile id**, not on `(x, y)`. Those are different orders: `tile_id` walks a Hilbert |
| 35 | /// curve, so row-major `(x, y)` ascends through it out of sequence and the writer rejects the |
| 36 | /// archive. Cheap to get wrong and caught only by an ordering check, which is why the writer has |
| 37 | /// one. |
| 38 | /// |
| 39 | /// Flat rather than `tile -> layer -> layer`. A nested map pays a whole inner `BTreeMap` node — |
| 40 | /// eleven `BodyLayer` slots, most of a kilobyte — for every tile, and nearly every tile carries one |
| 41 | /// or two layers; with a chunk map per chunk that overhead is multiplied by the chunk count. |
| 42 | /// Flattening puts eleven *entries* in a node instead, and changes no order: `(tile, layer)` |
| 43 | /// ascending is tile-major with layers in id order inside it, which is what the nested form yielded |
| 44 | /// and what the writer needs. |
| 45 | type Chunk = BTreeMap<(u64, u8), ChunkEntry>; |
| 46 | |
| 47 | /// One tile-layer in a chunk map: the geometry plus the label strings its features name. |
| 48 | /// |
| 49 | /// Names ride beside the layer (not inside `BodyLayer`, which is the codec's type) from `push` |
| 50 | /// through the spill and the merge to encode, where per-layer tables fuse into the body's one. |
| 51 | /// A chunk entry's `names` is deduplicated in first-use order, so `name_idx == i` names |
| 52 | /// `names[i - 1]` — the same convention as the body's table, which is what makes the merge's |
| 53 | /// remap a pure index translation. |
| 54 | /// |
| 55 | /// `ids` rides the same way and is simpler: it holds values rather than indices, so a merge |
| 56 | /// concatenates it instead of remapping it. It is empty for every layer but `places` and `poi`, |
| 57 | /// and dense-parallel to `layer.features` for those two — `ids[i]` is the OSM element that |
| 58 | /// produced `features[i]`. |
| 59 | #[derive(Debug, Clone, PartialEq)] |
| 60 | pub struct ChunkEntry { |
| 61 | pub layer: BodyLayer, |
| 62 | pub names: Vec<String>, |
| 63 | pub ids: Vec<u64>, |
| 64 | /// The per-feature turn-lane masks, dense-parallel to `layer.features` on the `roads` layer and |
| 65 | /// empty on every other. Rides the merge the way `ids` does — concatenated, not remapped — and |
| 66 | /// is remapped alongside `ids` when `coalesce` and stage C rebuild the feature vector. |
| 67 | pub turn_lanes: Vec<tilecodec::mamaps::body::LaneTurns>, |
| 68 | /// The per-feature S3DB attributes, dense-parallel to `layer.features` on the `buildings` layer |
| 69 | /// and empty on every other. Rides the merge like `ids` (concatenated, not remapped) and is |
| 70 | /// remapped alongside it when `coalesce` and stage C rebuild the feature vector, so the body's |
| 71 | /// building side table stays aligned to the features it describes. |
| 72 | pub buildings: Vec<BuildingAttrs>, |
| 73 | /// The per-feature directional carriageway split, dense-parallel to `layer.features` on the |
| 74 | /// `roads` layer and empty on every other. Rides the merge like `ids` (concatenated, not |
| 75 | /// remapped) and is remapped alongside it when `coalesce` rebuilds the feature vector. Stage C |
| 76 | /// is not given it: a road is a line, and `rings::normalise` never drops one. |
| 77 | pub carriageways: Vec<tilecodec::mamaps::body::Carriageway>, |
| 78 | } |
| 79 | |
| 80 | impl ChunkEntry { |
| 81 | pub(crate) fn new(layer_id: u8) -> ChunkEntry { |
| 82 | ChunkEntry { |
| 83 | layer: BodyLayer::new(layer_id), |
| 84 | names: Vec::new(), |
| 85 | ids: Vec::new(), |
| 86 | turn_lanes: Vec::new(), |
| 87 | buildings: Vec::new(), |
| 88 | carriageways: Vec::new(), |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// Intern `name` into a first-use-ordered table, returning its 1-based body index. |
| 94 | fn intern_name(names: &mut Vec<String>, name: Option<&str>) -> u16 { |
| 95 | match name.filter(|n| !n.is_empty()) { |
| 96 | None => tilecodec::mamaps::body::NAME_NONE, |
| 97 | Some(name) => match names.iter().position(|n| n == name) { |
| 98 | Some(at) => at as u16 + 1, |
| 99 | None => { |
| 100 | let idx = names.len() as u16 + 1; |
| 101 | names.push(name.to_string()); |
| 102 | idx |
| 103 | } |
| 104 | }, |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /// What one chunk contributed to a zoom's counters. Only ever summed, so the order they are summed |
| 109 | /// in cannot change the answer. |
| 110 | #[derive(Debug, Default, Clone, Copy)] |
| 111 | struct Tally { |
| 112 | features: u64, |
| 113 | points: u64, |
| 114 | dropped: u64, |
| 115 | // TEMP-PROFILE: per-stage map walls, summed across chunks (durations |
| 116 | // add; order irrelevant). Removed with the probes. NOT Copy (Duration |
| 117 | // is, but the explicit add below keeps the accounting visible). |
| 118 | profile: TallyProfile, |
| 119 | } |
| 120 | |
| 121 | /// TEMP-PROFILE: per-stage map-phase walls carried in the tally. Removed |
| 122 | /// with the probes. |
| 123 | #[derive(Debug, Default, Clone, Copy)] |
| 124 | struct TallyProfile { |
| 125 | project: std::time::Duration, |
| 126 | annotate: std::time::Duration, |
| 127 | filter: std::time::Duration, |
| 128 | subdiv: std::time::Duration, |
| 129 | } |
| 130 | |
| 131 | impl Tally { |
| 132 | fn add(&mut self, other: Tally) { |
| 133 | self.features += other.features; |
| 134 | self.points += other.points; |
| 135 | self.dropped += other.dropped; |
| 136 | self.profile.project += other.profile.project; |
| 137 | self.profile.annotate += other.profile.annotate; |
| 138 | self.profile.filter += other.profile.filter; |
| 139 | self.profile.subdiv += other.profile.subdiv; |
| 140 | } |
| 141 | |
| 142 | /// TEMP-PROFILE: attach this chunk's stage walls. Removed with the probes. |
| 143 | fn profile_add( |
| 144 | &mut self, |
| 145 | project: std::time::Duration, |
| 146 | annotate: std::time::Duration, |
| 147 | filter: std::time::Duration, |
| 148 | subdiv: std::time::Duration, |
| 149 | ) { |
| 150 | self.profile.project += project; |
| 151 | self.profile.annotate += annotate; |
| 152 | self.profile.filter += filter; |
| 153 | self.profile.subdiv += subdiv; |
| 154 | } |
| 155 | } |
| 156 |