Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / admin.rs
admin.rs232 lines · 8.4 KiBRaw
| 1 | //! The `admin_city` layer: OSM `admin_level=8` boundaries, for the app's border |
| 2 | //! masks and labels. |
| 3 | //! |
| 4 | //! A port of `normalize_admin.py --level city`. Only the city level comes from OSM; |
| 5 | //! `admin_country` and `admin_region` come from Natural Earth, which is not |
| 6 | //! derivable from OSM and is handled elsewhere. See the README's caveats. |
| 7 | //! |
| 8 | //! ## `name_en` has no fallback to plain `name` here |
| 9 | //! |
| 10 | //! The three levels disagree, and the disagreement is in the Python: |
| 11 | //! |
| 12 | //! | level | `name` from | `name_en` from | |
| 13 | //! |---|---|---| |
| 14 | //! | country | `NAME_EN, name:en, NAME, name` | `NAME_EN, name:en` | |
| 15 | //! | region | `name_en, NAME_EN, name, NAME` | `name_en, NAME_EN, name:en` | |
| 16 | //! | **city** | `name:en, name, NAME_EN, NAME` | **`name:en, NAME_EN`** | |
| 17 | //! |
| 18 | //! So a city tagged only `name=Oakland` gets `name` and **no** `name_en`, where a |
| 19 | //! region tagged only `name=California` would get both. Adding the fallback would |
| 20 | //! be a defensible schema decision and a silent change to what the layer contains, |
| 21 | //! so it is not made here. |
| 22 | //! |
| 23 | //! ## The level filter |
| 24 | //! |
| 25 | //! `boundary=administrative` (absent counts as administrative) **and** |
| 26 | //! `admin_level=8`. A relation with neither key is accepted -- that branch existed |
| 27 | //! for Natural Earth rows, which carry no OSM admin tagging -- but a PBF relation |
| 28 | //! always has both, so in practice both must match. |
| 29 | |
| 30 | use crate::geojson::{osm_ref, Feature, Geometry, Value}; |
| 31 | |
| 32 | /// The admin level this layer takes. |
| 33 | pub const CITY_LEVEL: i64 = 8; |
| 34 | |
| 35 | /// The `osmium tags-filter` expression for the layer. |
| 36 | pub const FILTERS: [&str; 1] = ["boundary=administrative"]; |
| 37 | |
| 38 | #[derive(Debug, Default, Clone, Copy)] |
| 39 | pub struct AdminTags<'a> { |
| 40 | pub boundary: Option<&'a str>, |
| 41 | pub admin_level: Option<&'a str>, |
| 42 | pub name_en_tag: Option<&'a str>, |
| 43 | pub name: Option<&'a str>, |
| 44 | /// The relation `type`, which must be `boundary` for a boundary relation. |
| 45 | pub type_: Option<&'a str>, |
| 46 | } |
| 47 | |
| 48 | /// `normalize_admin.py`'s `_get`: the first key whose value is neither absent nor |
| 49 | /// empty. |
| 50 | fn first<const N: usize>(values: [Option<&str>; N]) -> Option<&str> { |
| 51 | values.into_iter().flatten().find(|v| !v.is_empty()) |
| 52 | } |
| 53 | |
| 54 | /// `is_admin_level(tags, 8)`. |
| 55 | pub fn is_city(t: &AdminTags) -> bool { |
| 56 | if t.admin_level.is_none() && t.boundary.is_none() { |
| 57 | // Natural Earth rows have no OSM admin tagging. A PBF relation always has |
| 58 | // both, so this branch is unreachable from a PBF. |
| 59 | return true; |
| 60 | } |
| 61 | if t.boundary.unwrap_or("administrative") != "administrative" { |
| 62 | return false; |
| 63 | } |
| 64 | match t.admin_level { |
| 65 | None => true, |
| 66 | // A non-numeric admin_level is a tagging error, not level 8. |
| 67 | Some(v) => v.trim().parse::<i64>() == Ok(CITY_LEVEL), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// `build_city`, plus the `"name" not in props` drop the Python applied afterwards: |
| 72 | /// `None` when the boundary is unnamed, since an unnamed border is useless for both |
| 73 | /// the mask and the labels. |
| 74 | pub fn feature<'a>( |
| 75 | t: &AdminTags<'a>, |
| 76 | geometry: Geometry, |
| 77 | relation_id: i64, |
| 78 | ) -> Option<Feature<'a>> { |
| 79 | let name = first([t.name_en_tag, t.name])?; |
| 80 | // No fallback to plain `name` -- see the module docs. |
| 81 | let name_en = first([t.name_en_tag]); |
| 82 | |
| 83 | let mut props: Vec<(&'a str, Value)> = vec![ |
| 84 | ("admin_level", Value::num(CITY_LEVEL)), |
| 85 | ("name", Value::str(name)), |
| 86 | ]; |
| 87 | if let Some(v) = name_en { |
| 88 | props.push(("name_en", Value::str(v))); |
| 89 | } |
| 90 | props.push(("osm_id", osm_ref("relation", relation_id))); |
| 91 | Some(Feature { geometry, props }) |
| 92 | } |
| 93 | |
| 94 | #[cfg(test)] |
| 95 | mod tests { |
| 96 | use super::*; |
| 97 | |
| 98 | fn city<'a>(name: Option<&'a str>, name_en: Option<&'a str>) -> AdminTags<'a> { |
| 99 | AdminTags { |
| 100 | boundary: Some("administrative"), |
| 101 | admin_level: Some("8"), |
| 102 | name, |
| 103 | name_en_tag: name_en, |
| 104 | type_: Some("boundary"), |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | fn poly() -> Geometry { |
| 109 | Geometry::Polygon(vec![vec![ |
| 110 | (0.0, 0.0), |
| 111 | (1.0, 0.0), |
| 112 | (1.0, 1.0), |
| 113 | (0.0, 1.0), |
| 114 | (0.0, 0.0), |
| 115 | ]]) |
| 116 | } |
| 117 | |
| 118 | fn props_of(t: &AdminTags) -> Vec<(String, String)> { |
| 119 | feature(t, poly(), 1) |
| 120 | .expect("a named city") |
| 121 | .props |
| 122 | .iter() |
| 123 | .map(|(k, v)| { |
| 124 | let rendered = match v { |
| 125 | Value::Str(s) => String::from_utf8_lossy(s).to_string(), |
| 126 | Value::Raw(r) => r.clone(), |
| 127 | }; |
| 128 | (k.to_string(), rendered) |
| 129 | }) |
| 130 | .collect() |
| 131 | } |
| 132 | |
| 133 | /// `scripts/maps/test/fixtures/admin_city_sample.geojsonseq` holds San |
| 134 | /// Francisco and Oakland at `admin_level=8`, plus a county at `admin_level=6` |
| 135 | /// that must be dropped. |
| 136 | #[test] |
| 137 | fn only_admin_level_8_is_kept() { |
| 138 | assert!(is_city(&city(Some("San Francisco"), None))); |
| 139 | assert!(is_city(&city(Some("Oakland"), None))); |
| 140 | let county = AdminTags { |
| 141 | admin_level: Some("6"), |
| 142 | ..city(Some("Alameda County"), None) |
| 143 | }; |
| 144 | assert!(!is_city(&county), "a county is not a city"); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn admin_level_is_emitted_as_the_number_8() { |
| 149 | let p = props_of(&city(Some("Oakland"), None)); |
| 150 | assert_eq!(p[0], ("admin_level".to_string(), "8".to_string())); |
| 151 | // A number, not a string: the app's mask code reads it as one. |
| 152 | match &feature(&city(Some("Oakland"), None), poly(), 1).unwrap().props[0].1 { |
| 153 | Value::Raw(r) => assert_eq!(r, "8"), |
| 154 | _ => panic!("admin_level must be a JSON number"), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn name_prefers_the_english_tag_then_falls_back() { |
| 160 | // name:en wins. |
| 161 | let p = props_of(&city(Some("Ciudad de México"), Some("Mexico City"))); |
| 162 | assert_eq!(p[1].1, "Mexico City"); |
| 163 | // Only `name`: that is the name. |
| 164 | let p = props_of(&city(Some("Oakland"), None)); |
| 165 | assert_eq!(p[1].1, "Oakland"); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn name_en_has_no_fallback_to_plain_name() { |
| 170 | // The city level's one asymmetry, and the trap: a region tagged only `name` |
| 171 | // gets a `name_en` too, a city does not. Adding the fallback would be a |
| 172 | // silent change to what the layer contains. |
| 173 | let keys: Vec<String> = props_of(&city(Some("Oakland"), None)) |
| 174 | .into_iter() |
| 175 | .map(|(k, _)| k) |
| 176 | .collect(); |
| 177 | assert_eq!(keys, ["admin_level", "name", "osm_id"]); |
| 178 | assert!(!keys.contains(&"name_en".to_string())); |
| 179 | |
| 180 | // With name:en, both appear. |
| 181 | let keys: Vec<String> = props_of(&city(Some("Ciudad de México"), Some("Mexico City"))) |
| 182 | .into_iter() |
| 183 | .map(|(k, _)| k) |
| 184 | .collect(); |
| 185 | assert_eq!(keys, ["admin_level", "name", "name_en", "osm_id"]); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn an_unnamed_boundary_is_dropped() { |
| 190 | assert!(feature(&city(None, None), poly(), 1).is_none()); |
| 191 | assert!(feature(&city(Some(""), None), poly(), 1).is_none()); |
| 192 | // An empty name:en falls through to `name`. |
| 193 | let f = feature(&city(Some("Oakland"), Some("")), poly(), 1).unwrap(); |
| 194 | assert_eq!(f.props.len(), 3, "name from `name`, and no name_en"); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn a_non_administrative_boundary_is_rejected() { |
| 199 | for b in ["postal_code", "political", "maritime"] { |
| 200 | let t = AdminTags { boundary: Some(b), ..city(Some("X"), None) }; |
| 201 | assert!(!is_city(&t), "boundary={b}"); |
| 202 | } |
| 203 | // An absent boundary key defaults to administrative. |
| 204 | let t = AdminTags { boundary: None, ..city(Some("X"), None) }; |
| 205 | assert!(is_city(&t)); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn a_malformed_admin_level_is_not_level_eight() { |
| 210 | for v in ["", "eight", "8a", "8.0", "-8"] { |
| 211 | let t = AdminTags { admin_level: Some(v), ..city(Some("X"), None) }; |
| 212 | assert!(!is_city(&t), "admin_level={v:?}"); |
| 213 | } |
| 214 | // Whitespace around a good value is tolerated. |
| 215 | let t = AdminTags { admin_level: Some(" 8 "), ..city(Some("X"), None) }; |
| 216 | assert!(is_city(&t)); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn osm_id_is_the_relation_form() { |
| 221 | let p = props_of(&city(Some("Oakland"), None)); |
| 222 | assert_eq!(p.last().unwrap(), &("osm_id".to_string(), "relation/1".to_string())); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn the_pbf_filter_covers_what_the_level_test_accepts() { |
| 227 | let select = crate::select::Select::parse(&FILTERS).unwrap(); |
| 228 | let t = city(Some("Oakland"), None); |
| 229 | assert!(is_city(&t)); |
| 230 | assert!(select.matches(|k| if k == "boundary" { t.boundary } else { None })); |
| 231 | } |
| 232 | } |