Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / safety.rs
safety.rs64 lines · 2.3 KiBRaw
| 1 | //! The `safety` layer: speed cameras, ALPR, surveillance cameras, stop signs and traffic signals. |
| 2 | //! |
| 3 | //! Classification is [`osm_ingest::safety::classify`], the port of `normalize_safety.py`; this |
| 4 | //! module only adapts tags to it and interns the result. Nodes only — extract calls it from the |
| 5 | //! node pass, never for a way or a relation. |
| 6 | |
| 7 | use osm_ingest::safety::{self, SafetyTags}; |
| 8 | use tilecodec::mamaps::dict; |
| 9 | |
| 10 | use super::{kind, Class, TagSource}; |
| 11 | |
| 12 | /// Carried at the archive max only: dense at every intersection and drawn from z16 by overzoom. |
| 13 | pub const MIN_ZOOM: u8 = 14; |
| 14 | |
| 15 | #[cfg(test)] |
| 16 | pub const KINDS: &[&str] = |
| 17 | &["speed_camera", "alpr", "surveillance", "stop_sign", "traffic_signals"]; |
| 18 | |
| 19 | pub const FILTERS: &[&str] = &["highway", "man_made", "enforcement"]; |
| 20 | |
| 21 | pub fn classify(tags: &(impl TagSource + ?Sized)) -> Option<Class> { |
| 22 | let t = SafetyTags { |
| 23 | highway: tags.get("highway"), |
| 24 | man_made: tags.get("man_made"), |
| 25 | enforcement: tags.get("enforcement"), |
| 26 | surveillance_type: tags.get("surveillance:type"), |
| 27 | camera_type: tags.get("camera:type"), |
| 28 | operator: tags.get("operator"), |
| 29 | manufacturer: tags.get("manufacturer"), |
| 30 | ..SafetyTags::default() |
| 31 | }; |
| 32 | let found = safety::classify(&t)?; |
| 33 | Some(Class::line(dict::LAYER_SAFETY, kind(found.as_str()), MIN_ZOOM)) |
| 34 | } |
| 35 | |
| 36 | #[cfg(test)] |
| 37 | mod tests { |
| 38 | use super::*; |
| 39 | |
| 40 | fn kind_of(tags: &[(&str, &str)]) -> Option<&'static str> { |
| 41 | classify(tags).map(|c| { |
| 42 | assert_eq!(c.layer, dict::LAYER_SAFETY); |
| 43 | dict::KINDS[c.kind as usize - 1] |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn each_kind_classifies() { |
| 49 | assert_eq!(kind_of(&[("highway", "stop")]), Some("stop_sign")); |
| 50 | assert_eq!(kind_of(&[("highway", "traffic_signals")]), Some("traffic_signals")); |
| 51 | assert_eq!(kind_of(&[("highway", "speed_camera")]), Some("speed_camera")); |
| 52 | assert_eq!( |
| 53 | kind_of(&[("man_made", "surveillance"), ("enforcement", "maxspeed")]), |
| 54 | Some("speed_camera"), |
| 55 | ); |
| 56 | assert_eq!( |
| 57 | kind_of(&[("man_made", "surveillance"), ("surveillance:type", "ALPR")]), |
| 58 | Some("alpr"), |
| 59 | ); |
| 60 | assert_eq!(kind_of(&[("man_made", "surveillance")]), Some("surveillance")); |
| 61 | assert_eq!(kind_of(&[("highway", "residential")]), None); |
| 62 | assert_eq!(kind_of(&[("amenity", "cafe")]), None); |
| 63 | } |
| 64 | } |