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

Modern-Apps.git / scripts / maps / osm_ingest / src / poi_build_s2.rs

poi_build_s2.rs120 lines · 4.7 KiBRaw
1fn way_blob(state: &mut WayPass, block: &pbf::PrimitiveBlock, outer_wanted: &[i64]) -> Result<u8> {
2 let mut kinds = 0u8;
3 visit_block(block, KIND_WAYS, &mut kinds, &mut |el: Element| {
4 if let Element::Way(w) = el {
5 if outer_wanted.binary_search(&w.id).is_ok() {
6 state.outer_refs.push((w.id, w.refs.to_vec()));
7 }
8 // libosmium's `MultipolygonManager::after_way` builds a standalone
9 // area from every closed way with more than 3 nodes that is not
10 // tagged `area=no` — including ways that are also members of a
11 // multipolygon relation, so a campus tagged on both the relation and
12 // its rings yields a POI either way. It compares end *locations*;
13 // comparing the end refs is equivalent for real OSM data and needs no
14 // location index here.
15 let closed = w.refs.len() > 3 && w.refs.first() == w.refs.last();
16 if !closed || w.tags.get_str("area") == Some("no") {
17 return Ok(());
18 }
19 let name = match w.tags.get("name") {
20 Some(n) if !n.is_empty() => n,
21 _ => return Ok(()),
22 };
23 if let Some(type_) = tags::classify(&poi_tags(&w.tags)) {
24 let self_region = if w.tags.get_str("boundary") == Some("administrative") {
25 poi_side::tag_way(w.id)
26 } else {
27 poi_side::REGION_NONE
28 };
29 state.areas.push(WayArea {
30 id: w.id,
31 type_,
32 name: name.to_vec(),
33 attrs: crate::poi_attrs::encode(|k| w.tags.get(k)),
34 refs: w.refs.to_vec(),
35 self_region,
36 place_cap: place_level_cap(w.tags.get_str("place")),
37 });
38 }
39 }
40 Ok(())
41 })?;
42 Ok(kinds)
43}
44
45fn node_blob(
46 state: &mut NodePass,
47 block: &pbf::PrimitiveBlock,
48 needed: &[i64],
49 bbox: Option<&crate::bbox::BBox>,
50) -> Result<u8> {
51 let mut kinds = 0u8;
52 visit_block(block, KIND_NODES, &mut kinds, &mut |el: Element| {
53 if let Element::Node(n) = el {
54 if let Ok(idx) = needed.binary_search(&n.id) {
55 state.locs.push((idx as u32, n.lat_e7, n.lon_e7));
56 }
57 if n.tags.is_empty() {
58 return Ok(());
59 }
60 // A node POI outside the region is dropped here; its own location
61 // arrives with it, so no location table is needed.
62 if !crate::bbox::keep_e7(bbox, n.lat_e7, n.lon_e7) {
63 return Ok(());
64 }
65 let name = match n.tags.get("name") {
66 Some(v) if !v.is_empty() => v,
67 _ => return Ok(()),
68 };
69 if let Some(type_) = tags::classify(&poi_tags(&n.tags)) {
70 state.pois.push(make_poi(
71 (n.lat_e7 as f64 * 1e-7, n.lon_e7 as f64 * 1e-7),
72 type_,
73 n.id,
74 name,
75 crate::poi_attrs::encode(|k| n.tags.get(k)),
76 poi_side::REGION_NONE,
77 place_level_cap(n.tags.get_str("place")),
78 ));
79 }
80 }
81 Ok(())
82 })?;
83 Ok(kinds)
84}
85
86/// Average of an area ring's vertices, reproducing libosmium's representative
87/// point.
88///
89/// `refs` is the ring's *distinct* vertices. libosmium stored a ring closed — the
90/// start vertex appears again at the end — and its Assembler sorted the ring's
91/// segments before walking them, so the ring always starts at the vertex with the
92/// smallest `(lon, lat)`. That vertex is therefore the one counted twice, which is
93/// why it is added again here: averaging the way's own node list instead would
94/// double the way's *first* node and land metres off on a small building.
95fn ring_centroid<I, L>(refs: I, location: &L) -> Option<(f64, f64)>
96where
97 I: Iterator<Item = i64>,
98 L: Fn(i64) -> Option<(i32, i32)>,
99{
100 let mut sum_lat = 0.0f64;
101 let mut sum_lon = 0.0f64;
102 let mut n = 0u64;
103 let mut start: Option<(i32, i32)> = None;
104 for id in refs {
105 if let Some((lat_e7, lon_e7)) = location(id) {
106 sum_lat += lat_e7 as f64 * 1e-7;
107 sum_lon += lon_e7 as f64 * 1e-7;
108 n += 1;
109 // libosmium's Location orders by x (lon) then y (lat).
110 if start.is_none_or(|(slat, slon)| (lon_e7, lat_e7) < (slon, slat)) {
111 start = Some((lat_e7, lon_e7));
112 }
113 }
114 }
115 let (slat, slon) = start?;
116 sum_lat += slat as f64 * 1e-7;
117 sum_lon += slon as f64 * 1e-7;
118 Some((sum_lat / (n + 1) as f64, sum_lon / (n + 1) as f64))
119}
120