Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / region.rs
region.rs116 lines · 4.2 KiBRaw
| 1 | //! Build region: `california` or `world`. |
| 2 | //! |
| 3 | //! One parameter, one meaning: which bbox filters the **built** layers |
| 4 | //! (`roads`, `poi`, `buildings`). Everything else — water, earth, boundaries, |
| 5 | //! landcover, landuse, places, transit, traffic, junction, DEM — is always |
| 6 | //! included regardless of this setting. |
| 7 | //! |
| 8 | //! `world` disables the filter entirely (no bbox). `california` keeps only the |
| 9 | //! features touching the California box below. Callers use touches-box, never |
| 10 | //! containment, for the same reason [`crate::bbox`] documents for |
| 11 | //! `osmium extract complete_ways`: truncating a way at the box boundary would |
| 12 | //! invent a vertex that is not in OSM. |
| 13 | |
| 14 | use crate::bbox::BBox; |
| 15 | use crate::proto::{Error, Result}; |
| 16 | |
| 17 | /// California, with margin for border-straddling ways: |
| 18 | /// |
| 19 | /// lon -124.5 .. -114.0, lat 32.0 .. 42.1. Covers the state plus a slim |
| 20 | /// offshore/inland margin so a road straddling the border is kept whole. |
| 21 | pub const CALIFORNIA_BBOX: (f64, f64, f64, f64) = (-124.5, 32.0, -114.0, 42.1); |
| 22 | |
| 23 | /// North America, with margin for border-straddling ways: |
| 24 | /// |
| 25 | /// lon -170.0 .. -55.0, lat 7.0 .. 72.0. Alaska through Panama plus the |
| 26 | /// Caribbean; Greenland excluded (huge empty tile area for no road/POI gain). |
| 27 | /// Same touches-box semantics as California below. |
| 28 | pub const NA_BBOX: (f64, f64, f64, f64) = (-170.0, 7.0, -55.0, 72.0); |
| 29 | |
| 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 31 | pub enum Region { |
| 32 | California, |
| 33 | Na, |
| 34 | World, |
| 35 | } |
| 36 | |
| 37 | impl Region { |
| 38 | pub fn parse(s: &str) -> Result<Region> { |
| 39 | match s.to_ascii_lowercase().as_str() { |
| 40 | "california" | "ca" | "cali" => Ok(Region::California), |
| 41 | "na" | "northamerica" | "north_america" | "north-america" => Ok(Region::Na), |
| 42 | "world" | "planet" | "global" => Ok(Region::World), |
| 43 | other => Err(Error(format!( |
| 44 | "--region wants `california`, `na` or `world`, got `{other}`" |
| 45 | ))), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// The bbox to filter by, or `None` for world (no filtering). |
| 50 | pub fn bbox(self) -> Option<BBox> { |
| 51 | match self { |
| 52 | Region::World => None, |
| 53 | Region::California => Some(BBox { |
| 54 | min_lon: CALIFORNIA_BBOX.0, |
| 55 | min_lat: CALIFORNIA_BBOX.1, |
| 56 | max_lon: CALIFORNIA_BBOX.2, |
| 57 | max_lat: CALIFORNIA_BBOX.3, |
| 58 | }), |
| 59 | Region::Na => Some(BBox { |
| 60 | min_lon: NA_BBOX.0, |
| 61 | min_lat: NA_BBOX.1, |
| 62 | max_lon: NA_BBOX.2, |
| 63 | max_lat: NA_BBOX.3, |
| 64 | }), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | pub fn name(self) -> &'static str { |
| 69 | match self { |
| 70 | Region::California => "california", |
| 71 | Region::Na => "na", |
| 72 | Region::World => "world", |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[cfg(test)] |
| 78 | mod tests { |
| 79 | use super::*; |
| 80 | |
| 81 | #[test] |
| 82 | fn parses_all_regions() { |
| 83 | assert_eq!(Region::parse("california").unwrap(), Region::California); |
| 84 | assert_eq!(Region::parse("CA").unwrap(), Region::California); |
| 85 | assert_eq!(Region::parse("na").unwrap(), Region::Na); |
| 86 | assert_eq!(Region::parse("NorthAmerica").unwrap(), Region::Na); |
| 87 | assert_eq!(Region::parse("world").unwrap(), Region::World); |
| 88 | assert!(Region::parse("europe").is_err()); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn world_has_no_bbox_and_california_covers_the_state() { |
| 93 | assert!(Region::World.bbox().is_none()); |
| 94 | let b = Region::California.bbox().unwrap(); |
| 95 | // San Francisco, Los Angeles, San Diego inside; New York, London outside. |
| 96 | assert!(b.contains(-122.4194, 37.7749)); |
| 97 | assert!(b.contains(-118.2437, 34.0522)); |
| 98 | assert!(b.contains(-117.1611, 32.7157)); |
| 99 | assert!(!b.contains(-74.0, 40.7)); |
| 100 | assert!(!b.contains(-0.12, 51.5)); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn na_covers_the_continent_but_not_greenland_or_europe() { |
| 105 | let b = Region::Na.bbox().unwrap(); |
| 106 | // Anchorage, Mexico City, Panama City, Havana inside. |
| 107 | assert!(b.contains(-149.9, 61.2)); |
| 108 | assert!(b.contains(-99.1, 19.4)); |
| 109 | assert!(b.contains(-79.5, 9.0)); |
| 110 | assert!(b.contains(-82.4, 23.1)); |
| 111 | // Nuuk (Greenland), London, Bogota outside. |
| 112 | assert!(!b.contains(-51.7, 64.2)); |
| 113 | assert!(!b.contains(-0.12, 51.5)); |
| 114 | assert!(!b.contains(-74.1, 4.7)); |
| 115 | } |
| 116 | } |