Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / maps / mamaps_build / src / schema / safety.rs

safety.rs62 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
7use osm_ingest::safety::{self, SafetyTags};
8use tilecodec::mamaps::dict;
9
10use super::{kind, Class, TagSource};
11
12/// Carried at the archive max only: dense at every intersection and drawn from z16 by overzoom.
13pub const MIN_ZOOM: u8 = 14;
14
15pub const KINDS: &[&str] = &["speed_camera", "alpr", "surveillance", "stop_sign", "traffic_signals"];
16
17pub const FILTERS: &[&str] = &["highway", "man_made", "enforcement"];
18
19pub fn classify(tags: &(impl TagSource + ?Sized)) -> Option<Class> {
20 let t = SafetyTags {
21 highway: tags.get("highway"),
22 man_made: tags.get("man_made"),
23 enforcement: tags.get("enforcement"),
24 surveillance_type: tags.get("surveillance:type"),
25 camera_type: tags.get("camera:type"),
26 operator: tags.get("operator"),
27 manufacturer: tags.get("manufacturer"),
28 ..SafetyTags::default()
29 };
30 let found = safety::classify(&t)?;
31 Some(Class::line(dict::LAYER_SAFETY, kind(found.as_str()), MIN_ZOOM))
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 fn kind_of(tags: &[(&str, &str)]) -> Option<&'static str> {
39 classify(tags).map(|c| {
40 assert_eq!(c.layer, dict::LAYER_SAFETY);
41 dict::KINDS[c.kind as usize - 1]
42 })
43 }
44
45 #[test]
46 fn each_kind_classifies() {
47 assert_eq!(kind_of(&[("highway", "stop")]), Some("stop_sign"));
48 assert_eq!(kind_of(&[("highway", "traffic_signals")]), Some("traffic_signals"));
49 assert_eq!(kind_of(&[("highway", "speed_camera")]), Some("speed_camera"));
50 assert_eq!(
51 kind_of(&[("man_made", "surveillance"), ("enforcement", "maxspeed")]),
52 Some("speed_camera"),
53 );
54 assert_eq!(
55 kind_of(&[("man_made", "surveillance"), ("surveillance:type", "ALPR")]),
56 Some("alpr"),
57 );
58 assert_eq!(kind_of(&[("man_made", "surveillance")]), Some("surveillance"));
59 assert_eq!(kind_of(&[("highway", "residential")]), None);
60 assert_eq!(kind_of(&[("amenity", "cafe")]), None);
61 }
62}