fn way_blob(state: &mut WayPass, block: &pbf::PrimitiveBlock, outer_wanted: &[i64]) -> Result { let mut kinds = 0u8; visit_block(block, KIND_WAYS, &mut kinds, &mut |el: Element| { if let Element::Way(w) = el { if outer_wanted.binary_search(&w.id).is_ok() { state.outer_refs.push((w.id, w.refs.to_vec())); } // libosmium's `MultipolygonManager::after_way` builds a standalone // area from every closed way with more than 3 nodes that is not // tagged `area=no` — including ways that are also members of a // multipolygon relation, so a campus tagged on both the relation and // its rings yields a POI either way. It compares end *locations*; // comparing the end refs is equivalent for real OSM data and needs no // location index here. let closed = w.refs.len() > 3 && w.refs.first() == w.refs.last(); if !closed || w.tags.get_str("area") == Some("no") { return Ok(()); } let name = match w.tags.get("name") { Some(n) if !n.is_empty() => n, _ => return Ok(()), }; if let Some(type_) = tags::classify(&poi_tags(&w.tags)) { let self_region = if w.tags.get_str("boundary") == Some("administrative") { poi_side::tag_way(w.id) } else { poi_side::REGION_NONE }; state.areas.push(WayArea { id: w.id, type_, name: name.to_vec(), attrs: crate::poi_attrs::encode(|k| w.tags.get(k)), refs: w.refs.to_vec(), self_region, place_cap: place_level_cap(w.tags.get_str("place")), }); } } Ok(()) })?; Ok(kinds) } fn node_blob( state: &mut NodePass, block: &pbf::PrimitiveBlock, needed: &[i64], bbox: Option<&crate::bbox::BBox>, ) -> Result { let mut kinds = 0u8; visit_block(block, KIND_NODES, &mut kinds, &mut |el: Element| { if let Element::Node(n) = el { if let Ok(idx) = needed.binary_search(&n.id) { state.locs.push((idx as u32, n.lat_e7, n.lon_e7)); } if n.tags.is_empty() { return Ok(()); } // A node POI outside the region is dropped here; its own location // arrives with it, so no location table is needed. if !crate::bbox::keep_e7(bbox, n.lat_e7, n.lon_e7) { return Ok(()); } let name = match n.tags.get("name") { Some(v) if !v.is_empty() => v, _ => return Ok(()), }; if let Some(type_) = tags::classify(&poi_tags(&n.tags)) { state.pois.push(make_poi( (n.lat_e7 as f64 * 1e-7, n.lon_e7 as f64 * 1e-7), type_, n.id, name, crate::poi_attrs::encode(|k| n.tags.get(k)), poi_side::REGION_NONE, place_level_cap(n.tags.get_str("place")), )); } } Ok(()) })?; Ok(kinds) } /// Average of an area ring's vertices, reproducing libosmium's representative /// point. /// /// `refs` is the ring's *distinct* vertices. libosmium stored a ring closed — the /// start vertex appears again at the end — and its Assembler sorted the ring's /// segments before walking them, so the ring always starts at the vertex with the /// smallest `(lon, lat)`. That vertex is therefore the one counted twice, which is /// why it is added again here: averaging the way's own node list instead would /// double the way's *first* node and land metres off on a small building. fn ring_centroid(refs: I, location: &L) -> Option<(f64, f64)> where I: Iterator, L: Fn(i64) -> Option<(i32, i32)>, { let mut sum_lat = 0.0f64; let mut sum_lon = 0.0f64; let mut n = 0u64; let mut start: Option<(i32, i32)> = None; for id in refs { if let Some((lat_e7, lon_e7)) = location(id) { sum_lat += lat_e7 as f64 * 1e-7; sum_lon += lon_e7 as f64 * 1e-7; n += 1; // libosmium's Location orders by x (lon) then y (lat). if start.is_none_or(|(slat, slon)| (lon_e7, lat_e7) < (slon, slat)) { start = Some((lat_e7, lon_e7)); } } } let (slat, slon) = start?; sum_lat += slat as f64 * 1e-7; sum_lon += slon as f64 * 1e-7; Some((sum_lat / (n + 1) as f64, sum_lon / (n + 1) as f64)) }