1use std::collections::{BTreeMap, HashMap};
2
3use crate::domains::geospatial::adapters::AdapterHttpRequest;
4use crate::net::disclosure::NetworkDisclosureRegistry;
5use crate::NQuin;
6
7const OSM_BASE: &str = "https://www.openstreetmap.org";
8const OSM_WIKI: &str = "https://wiki.openstreetmap.org/wiki/Key:";
9const OSM_LICENSE: &str = "https://opendatacommons.org/licenses/odbl/1-0/";
10const GEO: &str = "http://www.opengis.net/ont/geosparql#";
11
12#[derive(Debug, Clone, Copy, PartialEq)]
14pub struct OsmCoordinate {
15 pub lat: f64,
16 pub lon: f64,
17}
18
19#[derive(Debug, Clone, PartialEq)]
21pub struct OsmPolygon {
22 pub exterior: Vec<OsmCoordinate>,
23 pub interiors: Vec<Vec<OsmCoordinate>>,
24}
25
26#[derive(Debug, Clone, PartialEq)]
28pub enum OsmGeometry {
29 Point(OsmCoordinate),
30 LineString(Vec<OsmCoordinate>),
31 MultiLineString(Vec<Vec<OsmCoordinate>>),
32 Polygon(OsmPolygon),
33 MultiPolygon(Vec<OsmPolygon>),
34 GeometryCollection(Vec<OsmGeometry>),
35}
36
37#[derive(Debug, Clone, PartialEq)]
39pub struct OsmVectorFeature {
40 pub id: u64,
41 pub element_type: String,
42 pub version: Option<u64>,
43 pub changeset: Option<u64>,
44 pub timestamp: Option<String>,
45 pub tags: BTreeMap<String, String>,
46 pub node_refs: Vec<u64>,
48 pub members: Vec<OsmRelationMember>,
50 pub geometry: Option<OsmGeometry>,
51 pub issues: Vec<String>,
53}
54
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct OsmRelationMember {
57 pub element_type: String,
58 pub reference: u64,
59 pub role: String,
60}
61
62impl OsmVectorFeature {
63 pub fn source_iri(&self) -> String {
65 format!("{}/{}/{}", OSM_BASE, self.element_type, self.id)
66 }
67
68 pub fn as_wkt(&self) -> Option<String> {
70 self.geometry.as_ref().map(geometry_wkt)
71 }
72}
73
74pub struct OsmAdapter {
76 pub id: &'static str,
77 pub overpass_endpoint: String,
78 pub tile_endpoint: String,
79}
80
81impl OsmAdapter {
82 pub fn new(id: &'static str, overpass_endpoint: &str, tile_endpoint: &str) -> Self {
84 Self {
85 id,
86 overpass_endpoint: overpass_endpoint.to_string(),
87 tile_endpoint: tile_endpoint.to_string(),
88 }
89 }
90
91 pub fn adapter_id(&self) -> &'static str {
93 self.id
94 }
95
96 pub fn build_fetch_request(
99 &self,
100 bbox: (f64, f64, f64, f64),
101 time_range: (u64, u64),
102 registry: &NetworkDisclosureRegistry,
103 ) -> Result<AdapterHttpRequest, String> {
104 validate_bbox(bbox)?;
105 let primary = &self.overpass_endpoint;
106 if !registry.check_egress_consent(self.adapter_id(), primary) {
107 return Err(format!(
108 "Consent denied or unregistered for endpoint {} by adapter {}",
109 primary,
110 self.adapter_id()
111 ));
112 }
113
114 let date_filter = if time_range.1 > 0 {
115 format!("[date:\"{}\"]", unix_date(time_range.1))
116 } else {
117 String::new()
118 };
119 let query = format!(
120 "{date_filter}[out:json];(node({south},{west},{north},{east});way({south},{west},{north},{east});relation({south},{west},{north},{east}););out geom;",
121 date_filter = date_filter,
122 south = bbox.1,
123 west = bbox.0,
124 north = bbox.3,
125 east = bbox.2
126 );
127 Ok(AdapterHttpRequest::post_form(
128 primary.clone(),
129 format!("data={}", form_encode(&query)),
130 "OSM Overpass",
131 ))
132 }
133
134 #[cfg(not(target_arch = "wasm32"))]
138 pub fn fetch_region_vector_features(
139 &self,
140 bbox: (f64, f64, f64, f64),
141 time_range: (u64, u64),
142 registry: &NetworkDisclosureRegistry,
143 ) -> Result<Vec<OsmVectorFeature>, String> {
144 let request = self.build_fetch_request(bbox, time_range, registry)?;
145 let body = super::execute_http_request_text(&request)?;
146 self.parse_vector_features(&body)
147 }
148
149 #[cfg(target_arch = "wasm32")]
152 pub async fn fetch_region_vector_features_async(
153 &self,
154 bbox: (f64, f64, f64, f64),
155 time_range: (u64, u64),
156 registry: &NetworkDisclosureRegistry,
157 ) -> Result<Vec<OsmVectorFeature>, String> {
158 let request = self.build_fetch_request(bbox, time_range, registry)?;
159 let body = super::execute_http_request_text_async(&request).await?;
160 self.parse_vector_features(&body)
161 }
162
163 pub fn parse_vector_features(&self, body: &str) -> Result<Vec<OsmVectorFeature>, String> {
166 let document: serde_json::Value =
167 serde_json::from_str(body).map_err(|e| format!("invalid Overpass JSON: {e}"))?;
168 if let Some(error) = document.get("remark").and_then(|v| v.as_str()) {
169 return Err(format!("Overpass query failed: {error}"));
170 }
171 let elements = document
172 .get("elements")
173 .and_then(|v| v.as_array())
174 .ok_or_else(|| "Overpass response is missing its elements array".to_string())?;
175
176 let mut node_coords = HashMap::new();
177 for element in elements {
178 if element.get("type").and_then(|v| v.as_str()) != Some("node") {
179 continue;
180 }
181 let Some(id) = element.get("id").and_then(|v| v.as_u64()) else {
182 continue;
183 };
184 if let Some(coord) = coordinate_from_node(element) {
185 node_coords.insert(id, coord);
186 }
187 }
188
189 let mut way_coords = HashMap::new();
190 for element in elements {
191 if element.get("type").and_then(|v| v.as_str()) != Some("way") {
192 continue;
193 }
194 let Some(id) = element.get("id").and_then(|v| v.as_u64()) else {
195 continue;
196 };
197 if let Some(coords) = element_coordinates(element, &node_coords) {
198 way_coords.insert(id, coords);
199 }
200 }
201
202 let mut features = Vec::with_capacity(elements.len());
203 for element in elements {
204 let Some(element_type) = element.get("type").and_then(|v| v.as_str()) else {
205 continue;
206 };
207 let Some(id) = element.get("id").and_then(|v| v.as_u64()) else {
208 continue;
209 };
210 if !matches!(element_type, "node" | "way" | "relation") {
211 continue;
212 }
213
214 let tags = parse_tags(element.get("tags"));
215 let mut issues = Vec::new();
216 let geometry = match element_type {
217 "node" => coordinate_from_node(element).map(OsmGeometry::Point),
218 "way" => way_geometry(element, &tags, &node_coords, &mut issues),
219 "relation" => {
220 relation_geometry(element, &tags, &node_coords, &way_coords, &mut issues)
221 }
222 _ => None,
223 };
224 if geometry.is_none() {
225 issues.push("source element has no reconstructable geometry".into());
226 }
227
228 features.push(OsmVectorFeature {
229 id,
230 element_type: element_type.to_string(),
231 version: element.get("version").and_then(|v| v.as_u64()),
232 changeset: element.get("changeset").and_then(|v| v.as_u64()),
233 timestamp: element
234 .get("timestamp")
235 .and_then(|v| v.as_str())
236 .map(str::to_string),
237 tags,
238 node_refs: element
239 .get("nodes")
240 .and_then(|v| v.as_array())
241 .into_iter()
242 .flatten()
243 .filter_map(|v| v.as_u64())
244 .collect(),
245 members: element
246 .get("members")
247 .and_then(|v| v.as_array())
248 .into_iter()
249 .flatten()
250 .filter_map(|member| {
251 Some(OsmRelationMember {
252 element_type: member.get("type")?.as_str()?.to_string(),
253 reference: member.get("ref")?.as_u64()?,
254 role: member
255 .get("role")
256 .and_then(|v| v.as_str())
257 .unwrap_or("")
258 .to_string(),
259 })
260 })
261 .collect(),
262 geometry,
263 issues,
264 });
265 }
266 Ok(features)
267 }
268
269 pub fn parse_features(&self, body: &str) -> Result<Vec<NQuin>, String> {
273 let p_type = token(b"http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
274 let p_source = token(b"http://purl.org/dc/terms/source");
275 let p_title = token(b"http://purl.org/dc/terms/title");
276 let p_license = token(b"http://purl.org/dc/terms/license");
277 let p_lat = token(b"http://www.w3.org/2003/01/geo/wgs84_pos#lat");
278 let p_lon = token(b"http://www.w3.org/2003/01/geo/wgs84_pos#long");
279 let p_as_wkt = token(format!("{GEO}asWKT").as_bytes());
280 let p_version = token(b"https://www.openstreetmap.org/wiki/Elements#version");
281 let p_changeset = token(b"https://www.openstreetmap.org/wiki/Elements#changeset");
282 let p_timestamp = token(b"http://purl.org/dc/terms/modified");
283 let p_member = token(b"https://www.openstreetmap.org/wiki/Elements#member");
284 let p_role = token(b"https://www.openstreetmap.org/wiki/Elements#role");
285
286 let mut quins = Vec::new();
287 for feature in self.parse_vector_features(body)? {
288 let source = feature.source_iri();
289 let subject = token(source.as_bytes());
290 let kind_iri = format!("{OSM_BASE}/{}", feature.element_type);
291 quins.push(quin(subject, p_type, token(kind_iri.as_bytes())));
292 quins.push(quin(subject, p_source, subject));
293 quins.push(quin(subject, p_license, token(OSM_LICENSE.as_bytes())));
294 if let Some(version) = feature.version {
295 quins.push(quin(
296 subject,
297 p_version,
298 token(version.to_string().as_bytes()),
299 ));
300 }
301 if let Some(changeset) = feature.changeset {
302 quins.push(quin(
303 subject,
304 p_changeset,
305 token(changeset.to_string().as_bytes()),
306 ));
307 }
308 if let Some(timestamp) = &feature.timestamp {
309 quins.push(quin(subject, p_timestamp, token(timestamp.as_bytes())));
310 }
311 if let Some(name) = feature.tags.get("name") {
312 quins.push(quin(subject, p_title, token(name.as_bytes())));
313 }
314 for (key, value) in &feature.tags {
315 let predicate = token(format!("{OSM_WIKI}{key}").as_bytes());
316 quins.push(quin(subject, predicate, token(value.as_bytes())));
317 }
318 if let Some(OsmGeometry::Point(point)) = &feature.geometry {
319 quins.push(quin(subject, p_lat, point.lat.to_bits()));
320 quins.push(quin(subject, p_lon, point.lon.to_bits()));
321 }
322 if let Some(wkt) = feature.as_wkt() {
323 quins.push(quin(subject, p_as_wkt, token(wkt.as_bytes())));
324 }
325
326 if feature.element_type == "way" {
329 let p_node = token(b"https://www.openstreetmap.org/wiki/Elements#node");
330 for node_id in &feature.node_refs {
331 let iri = format!("{OSM_BASE}/node/{node_id}");
332 quins.push(quin(subject, p_node, token(iri.as_bytes())));
333 }
334 } else if feature.element_type == "relation" {
335 for (index, member) in feature.members.iter().enumerate() {
336 let member_iri =
337 format!("{OSM_BASE}/{}/{}", member.element_type, member.reference);
338 let member_node = token(format!("{source}#member/{index}").as_bytes());
339 quins.push(quin(subject, p_member, member_node));
340 quins.push(quin(member_node, p_source, token(member_iri.as_bytes())));
341 quins.push(quin(member_node, p_role, token(member.role.as_bytes())));
342 }
343 }
344 }
345 Ok(quins)
346 }
347}
348
349fn token(bytes: &[u8]) -> u64 {
350 crate::lexicon::generate_60bit_token(bytes)
351}
352
353fn quin(subject: u64, predicate: u64, object: u64) -> NQuin {
354 NQuin {
355 subject,
356 predicate,
357 object,
358 context: 0,
359 metadata: 0,
360 parity: subject ^ predicate ^ object,
361 }
362}
363
364fn validate_bbox(bbox: (f64, f64, f64, f64)) -> Result<(), String> {
365 let (west, south, east, north) = bbox;
366 if ![west, south, east, north].iter().all(|v| v.is_finite())
367 || west < -180.0
368 || east > 180.0
369 || south < -90.0
370 || north > 90.0
371 || west >= east
372 || south >= north
373 {
374 return Err("invalid WGS84 bounding box; expected west < east and south < north".into());
375 }
376 Ok(())
377}
378
379fn form_encode(value: &str) -> String {
380 let mut encoded = String::with_capacity(value.len());
381 for byte in value.bytes() {
382 if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'~') {
383 encoded.push(byte as char);
384 } else if byte == b' ' {
385 encoded.push('+');
386 } else {
387 encoded.push('%');
388 encoded.push(
389 char::from_digit((byte >> 4) as u32, 16)
390 .unwrap()
391 .to_ascii_uppercase(),
392 );
393 encoded.push(
394 char::from_digit((byte & 0x0f) as u32, 16)
395 .unwrap()
396 .to_ascii_uppercase(),
397 );
398 }
399 }
400 encoded
401}
402
403fn unix_date(seconds: u64) -> String {
404 let days = (seconds / 86_400) as i64;
405 let z = days + 719_468;
406 let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
407 let day_of_era = z - era * 146_097;
408 let year_of_era =
409 (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
410 let mut year = year_of_era + era * 400;
411 let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
412 let month_prime = (5 * day_of_year + 2) / 153;
413 let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
414 let month = month_prime + if month_prime < 10 { 3 } else { -9 };
415 year += i64::from(month <= 2);
416 format!("{year:04}-{month:02}-{day:02}T00:00:00Z")
417}
418
419fn parse_tags(value: Option<&serde_json::Value>) -> BTreeMap<String, String> {
420 value
421 .and_then(|v| v.as_object())
422 .into_iter()
423 .flatten()
424 .filter_map(|(key, value)| value.as_str().map(|s| (key.clone(), s.to_string())))
425 .collect()
426}
427
428fn valid_coordinate(lat: f64, lon: f64) -> Option<OsmCoordinate> {
429 if lat.is_finite()
430 && lon.is_finite()
431 && (-90.0..=90.0).contains(&lat)
432 && (-180.0..=180.0).contains(&lon)
433 {
434 Some(OsmCoordinate { lat, lon })
435 } else {
436 None
437 }
438}
439
440fn coordinate_from_node(value: &serde_json::Value) -> Option<OsmCoordinate> {
441 valid_coordinate(value.get("lat")?.as_f64()?, value.get("lon")?.as_f64()?)
442}
443
444fn coordinate_array(value: &serde_json::Value) -> Option<OsmCoordinate> {
445 valid_coordinate(value.get("lat")?.as_f64()?, value.get("lon")?.as_f64()?)
446}
447
448fn element_coordinates(
449 element: &serde_json::Value,
450 nodes: &HashMap<u64, OsmCoordinate>,
451) -> Option<Vec<OsmCoordinate>> {
452 if let Some(coords) = element.get("geometry").and_then(|v| v.as_array()) {
453 let parsed: Vec<_> = coords.iter().filter_map(coordinate_array).collect();
454 if parsed.len() >= 2 {
455 return Some(parsed);
456 }
457 }
458 let refs = element.get("nodes")?.as_array()?;
459 let parsed: Vec<_> = refs
460 .iter()
461 .filter_map(|id| id.as_u64().and_then(|id| nodes.get(&id).copied()))
462 .collect();
463 (parsed.len() >= 2).then_some(parsed)
464}
465
466fn way_geometry(
467 element: &serde_json::Value,
468 tags: &BTreeMap<String, String>,
469 nodes: &HashMap<u64, OsmCoordinate>,
470 issues: &mut Vec<String>,
471) -> Option<OsmGeometry> {
472 let coords = element_coordinates(element, nodes)?;
473 if coords.len() < 2 {
474 issues.push("way has fewer than two resolved coordinates".into());
475 return None;
476 }
477 let closed = coords.first() == coords.last();
478 if is_area(tags, closed) {
479 if !closed {
480 issues.push("area-tagged way is not closed; preserved as a line".into());
481 return Some(OsmGeometry::LineString(coords));
482 }
483 let validation_ring = ring_points_for_validation(&coords, true);
484 let report = crate::specialized_libs::computational_geometry::validate_simple_polygon(
485 &validation_ring,
486 );
487 if !report.is_valid {
488 issues.push(format!(
489 "area way polygon validation found {} issue(s)",
490 report.issues.len()
491 ));
492 }
493 Some(OsmGeometry::Polygon(OsmPolygon {
494 exterior: coords,
495 interiors: Vec::new(),
496 }))
497 } else {
498 Some(OsmGeometry::LineString(coords))
499 }
500}
501
502fn is_area(tags: &BTreeMap<String, String>, closed: bool) -> bool {
503 if tags.get("area").map(String::as_str) == Some("yes") {
504 return true;
505 }
506 if tags.get("area").map(String::as_str) == Some("no") || !closed {
507 return false;
508 }
509 tags.contains_key("building")
510 || tags.contains_key("landuse")
511 || tags.contains_key("leisure")
512 || tags.contains_key("amenity")
513 || tags.contains_key("boundary")
514 || tags.contains_key("water")
515 || tags.get("waterway").map(String::as_str) == Some("riverbank")
516 || matches!(
517 tags.get("natural").map(String::as_str),
518 Some(
519 "water"
520 | "wood"
521 | "scrub"
522 | "grassland"
523 | "wetland"
524 | "heath"
525 | "bare_rock"
526 | "sand"
527 )
528 )
529}
530
531fn relation_geometry(
532 element: &serde_json::Value,
533 tags: &BTreeMap<String, String>,
534 nodes: &HashMap<u64, OsmCoordinate>,
535 ways: &HashMap<u64, Vec<OsmCoordinate>>,
536 issues: &mut Vec<String>,
537) -> Option<OsmGeometry> {
538 let members = element.get("members")?.as_array()?;
539 let mut lines = Vec::new();
540 let mut outer_parts = Vec::new();
541 let mut inner_parts = Vec::new();
542 for member in members {
543 let kind = member.get("type").and_then(|v| v.as_str()).unwrap_or("");
544 let role = member.get("role").and_then(|v| v.as_str()).unwrap_or("");
545 if kind == "node" {
546 if let Some(id) = member.get("ref").and_then(|v| v.as_u64()) {
547 if let Some(point) = nodes.get(&id) {
548 lines.push(OsmGeometry::Point(*point));
549 }
550 }
551 continue;
552 }
553 if kind != "way" {
554 issues.push(format!(
555 "relation member type '{kind}' is retained only by source identity"
556 ));
557 continue;
558 }
559 let coords = member
560 .get("geometry")
561 .and_then(|v| v.as_array())
562 .map(|arr| arr.iter().filter_map(coordinate_array).collect::<Vec<_>>())
563 .filter(|coords| coords.len() >= 2)
564 .or_else(|| {
565 member
566 .get("ref")
567 .and_then(|v| v.as_u64())
568 .and_then(|id| ways.get(&id).cloned())
569 });
570 let Some(coords) = coords else {
571 issues.push("relation member way has no complete geometry".into());
572 continue;
573 };
574 if matches!(
575 tags.get("type").map(String::as_str),
576 Some("multipolygon" | "boundary")
577 ) {
578 match role {
579 "outer" => outer_parts.push(coords),
580 "inner" => inner_parts.push(coords),
581 _ => issues.push(format!(
582 "multipolygon way member has unrecognized role '{role}'"
583 )),
584 }
585 } else {
586 lines.push(OsmGeometry::LineString(coords));
587 }
588 }
589
590 if !outer_parts.is_empty() || !inner_parts.is_empty() {
591 let outers = stitch_rings(outer_parts, "outer", issues);
592 let inners = stitch_rings(inner_parts, "inner", issues);
593 if outers.is_empty() {
594 issues.push("multipolygon has no complete outer ring".into());
595 return None;
596 }
597 let mut polygons: Vec<OsmPolygon> = outers
598 .into_iter()
599 .map(|exterior| OsmPolygon {
600 exterior,
601 interiors: Vec::new(),
602 })
603 .collect();
604 for hole in inners {
605 let probe = hole.first().copied();
606 let owner = probe.and_then(|point| {
607 polygons.iter().position(|polygon| {
608 let points = to_points(&polygon.exterior);
609 crate::specialized_libs::computational_geometry::point_in_polygon(
610 crate::specialized_libs::computational_geometry::Point2::new(
611 point.lon, point.lat,
612 ),
613 &points,
614 )
615 })
616 });
617 if let Some(owner) = owner {
618 polygons[owner].interiors.push(hole);
619 } else {
620 issues.push("multipolygon inner ring is not contained by an outer ring".into());
621 }
622 }
623 for polygon in &polygons {
624 let report =
627 crate::specialized_libs::computational_geometry::validate_polygon_with_holes(
628 &crate::specialized_libs::computational_geometry::PolygonWithHoles {
629 outer: ring_points_for_validation(&polygon.exterior, true),
630 holes: polygon
631 .interiors
632 .iter()
633 .map(|ring| ring_points_for_validation(ring, false))
634 .collect(),
635 },
636 );
637 if !report.is_valid {
638 issues.push(format!(
639 "multipolygon ring validation found {} issue(s)",
640 report.issues.len()
641 ));
642 }
643 }
644 return Some(OsmGeometry::MultiPolygon(polygons));
645 }
646 match (tags.get("type").map(String::as_str), lines.len()) {
647 (Some("route"), 1) => lines.into_iter().next(),
648 (_, 1) => lines.into_iter().next(),
649 (_, 0) => None,
650 _ => Some(OsmGeometry::GeometryCollection(lines)),
651 }
652}
653
654fn to_points(
655 ring: &[OsmCoordinate],
656) -> Vec<crate::specialized_libs::computational_geometry::Point2> {
657 ring.iter()
658 .map(|p| crate::specialized_libs::computational_geometry::Point2::new(p.lon, p.lat))
659 .collect()
660}
661
662fn ring_points_for_validation(
663 ring: &[OsmCoordinate],
664 counterclockwise: bool,
665) -> Vec<crate::specialized_libs::computational_geometry::Point2> {
666 let mut points = to_points(ring);
667 if points.len() > 1 && points.first() == points.last() {
668 points.pop();
669 }
670 let twice_area: f64 = points
671 .iter()
672 .zip(points.iter().cycle().skip(1))
673 .take(points.len())
674 .map(|(a, b)| a.x * b.y - b.x * a.y)
675 .sum();
676 if (twice_area > 0.0) != counterclockwise {
677 points.reverse();
678 }
679 points
680}
681
682fn stitch_rings(
683 mut parts: Vec<Vec<OsmCoordinate>>,
684 role: &str,
685 issues: &mut Vec<String>,
686) -> Vec<Vec<OsmCoordinate>> {
687 let mut rings = Vec::new();
688 while let Some(mut ring) = parts.pop() {
689 let mut attempts = 0;
690 while ring.first() != ring.last() && attempts <= parts.len() {
691 let Some(tail) = ring.last().copied() else {
692 break;
693 };
694 let next_index = parts
695 .iter()
696 .position(|part| part.first() == Some(&tail) || part.last() == Some(&tail));
697 let Some(next_index) = next_index else { break };
698 let mut next = parts.swap_remove(next_index);
699 if next.last() == Some(&tail) {
700 next.reverse();
701 }
702 ring.extend(next.into_iter().skip(1));
703 attempts = 0;
704 }
705 if ring.first() == ring.last() && ring.len() >= 4 {
706 rings.push(ring);
707 } else {
708 issues.push(format!(
709 "multipolygon {role} member chains do not form a closed ring"
710 ));
711 }
712 }
713 rings
714}
715
716fn geometry_wkt(geometry: &OsmGeometry) -> String {
717 match geometry {
718 OsmGeometry::Point(point) => format!("POINT({} {})", point.lon, point.lat),
719 OsmGeometry::LineString(points) => format!("LINESTRING({})", wkt_points(points)),
720 OsmGeometry::MultiLineString(lines) => format!(
721 "MULTILINESTRING({})",
722 lines
723 .iter()
724 .map(|line| format!("({})", wkt_points(line)))
725 .collect::<Vec<_>>()
726 .join(",")
727 ),
728 OsmGeometry::Polygon(poly) => format!("POLYGON({})", wkt_rings(poly)),
729 OsmGeometry::MultiPolygon(polygons) => format!(
730 "MULTIPOLYGON({})",
731 polygons
732 .iter()
733 .map(|poly| format!("({})", wkt_rings(poly)))
734 .collect::<Vec<_>>()
735 .join(",")
736 ),
737 OsmGeometry::GeometryCollection(parts) => format!(
738 "GEOMETRYCOLLECTION({})",
739 parts.iter().map(geometry_wkt).collect::<Vec<_>>().join(",")
740 ),
741 }
742}
743
744fn wkt_rings(poly: &OsmPolygon) -> String {
745 std::iter::once(&poly.exterior)
746 .chain(poly.interiors.iter())
747 .map(|ring| format!("({})", wkt_points(ring)))
748 .collect::<Vec<_>>()
749 .join(",")
750}
751
752fn wkt_points(points: &[OsmCoordinate]) -> String {
753 points
754 .iter()
755 .map(|p| format!("{} {}", p.lon, p.lat))
756 .collect::<Vec<_>>()
757 .join(",")
758}
759
760#[cfg(test)]
761mod tests {
762 use super::*;
763 use crate::domains::geospatial::adapters::{AdapterHttpMethod, DataAdapter};
764
765 const FIXTURE: &str = r#"{
766 "version":0.6,"generator":"Overpass API","elements":[
767 {"type":"node","id":1,"lat":-37.8183,"lon":144.9671,"version":3,"tags":{"name":"Flinders Street Station","amenity":"station"}},
768 {"type":"node","id":2,"lat":-37.8180,"lon":144.9680},
769 {"type":"node","id":3,"lat":-37.8176,"lon":144.9687},
770 {"type":"way","id":10,"nodes":[2,3],"geometry":[{"lat":-37.8180,"lon":144.9680},{"lat":-37.8176,"lon":144.9687}],"version":8,"tags":{"highway":"primary","name":"Example Road"}},
771 {"type":"way","id":11,"nodes":[20,21,22,23,20],"geometry":[{"lat":-37.8190,"lon":144.9660},{"lat":-37.8190,"lon":144.9664},{"lat":-37.8186,"lon":144.9664},{"lat":-37.8186,"lon":144.9660},{"lat":-37.8190,"lon":144.9660}],"tags":{"building":"yes","name":"Town Hall"}},
772 {"type":"way","id":30,"geometry":[{"lat":-37.8200,"lon":144.9650},{"lat":-37.8200,"lon":144.9655},{"lat":-37.8195,"lon":144.9655}],"tags":{"type":"route"}},
773 {"type":"relation","id":40,"members":[
774 {"type":"way","ref":50,"role":"outer","geometry":[{"lat":-37.8210,"lon":144.9640},{"lat":-37.8210,"lon":144.9650},{"lat":-37.8200,"lon":144.9650},{"lat":-37.8200,"lon":144.9640},{"lat":-37.8210,"lon":144.9640}]},
775 {"type":"way","ref":51,"role":"inner","geometry":[{"lat":-37.8208,"lon":144.9642},{"lat":-37.8206,"lon":144.9642},{"lat":-37.8206,"lon":144.9644},{"lat":-37.8208,"lon":144.9644},{"lat":-37.8208,"lon":144.9642}]}
776 ],"tags":{"type":"multipolygon","landuse":"forest"}}
777 ]
778 }"#;
779
780 fn adapter() -> OsmAdapter {
781 OsmAdapter::new(
782 "osm_adapter",
783 "https://overpass-api.de/api/interpreter",
784 "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
785 )
786 }
787
788 #[test]
789 fn request_is_consent_gated_valid_form_data_and_includes_geometry() {
790 let adapter = adapter();
791 let registry = NetworkDisclosureRegistry::new();
792 assert!(adapter
793 .build_fetch_request((144.9, -37.9, 145.0, -37.8), (0, 0), ®istry)
794 .is_err());
795 let mut registry = registry;
796 registry.register_egress(
797 "osm_adapter",
798 "https://overpass-api.de/api/interpreter",
799 "Fetch OSM features",
800 "User builds a local world pack",
801 );
802 let request = adapter
803 .build_fetch_request((144.9, -37.9, 145.0, -37.8), (0, 0), ®istry)
804 .unwrap();
805 assert_eq!(request.method, AdapterHttpMethod::Post);
806 assert_eq!(
807 request.content_type,
808 Some("application/x-www-form-urlencoded")
809 );
810 let body = request.body.unwrap();
811 assert!(body.starts_with("data=%5Bout%3Ajson%5D%3B"));
812 assert!(body.contains("out+geom%3B"));
813 assert_eq!(unix_date(1_700_000_000), "2023-11-14T00:00:00Z");
814
815 let dated = adapter
816 .build_fetch_request((144.9, -37.9, 145.0, -37.8), (0, 1_700_000_000), ®istry)
817 .unwrap();
818 assert!(dated
819 .body
820 .unwrap()
821 .contains("%5Bdate%3A%222023-11-14T00%3A00%3A00Z%22%5D"));
822 }
823
824 #[test]
825 fn parses_nodes_ways_polygons_and_multipolygon_holes() {
826 let features = adapter().parse_vector_features(FIXTURE).unwrap();
827 assert_eq!(features.len(), 7);
828 assert!(matches!(features[0].geometry, Some(OsmGeometry::Point(_))));
829 let road = features.iter().find(|feature| feature.id == 10).unwrap();
830 assert!(matches!(road.geometry, Some(OsmGeometry::LineString(_))));
831 assert_eq!(
832 road.tags.get("highway").map(String::as_str),
833 Some("primary")
834 );
835 let area = features.iter().find(|feature| feature.id == 11).unwrap();
836 assert!(matches!(area.geometry, Some(OsmGeometry::Polygon(_))));
837 assert!(area.issues.is_empty(), "area-way issues: {:?}", area.issues);
838 let relation = features.iter().find(|feature| feature.id == 40).unwrap();
839 let Some(OsmGeometry::MultiPolygon(polygons)) = &relation.geometry else {
840 panic!("expected multipolygon")
841 };
842 assert_eq!(polygons.len(), 1);
843 assert_eq!(polygons[0].interiors.len(), 1);
844 assert!(relation.issues.is_empty(), "issues: {:?}", relation.issues);
845 assert!(relation
846 .as_wkt()
847 .unwrap()
848 .starts_with("MULTIPOLYGON(((144.964"));
849 }
850
851 #[test]
852 fn graph_response_contains_wkt_licence_source_and_way_node_links() {
853 let adapter = adapter();
854 assert!(adapter.needs_fetch_body());
855 let quins = adapter.parse_response(FIXTURE).unwrap();
856 let p_source = token(b"http://purl.org/dc/terms/source");
857 let p_licence = token(b"http://purl.org/dc/terms/license");
858 let p_wkt = token(format!("{GEO}asWKT").as_bytes());
859 let p_nodes = token(b"https://www.openstreetmap.org/wiki/Elements#node");
860 let way = token(b"https://www.openstreetmap.org/way/10");
861 assert!(quins
862 .iter()
863 .any(|q| q.subject == way && q.predicate == p_source));
864 assert!(quins.iter().any(|q| q.subject == way
865 && q.predicate == p_licence
866 && q.object == token(OSM_LICENSE.as_bytes())));
867 assert!(quins
868 .iter()
869 .any(|q| q.subject == way && q.predicate == p_wkt));
870 assert_eq!(
871 quins
872 .iter()
873 .filter(|q| q.subject == way && q.predicate == p_nodes)
874 .count(),
875 2
876 );
877 }
878
879 #[test]
880 fn rejects_bad_bbox_and_overpass_error_payloads() {
881 assert!(validate_bbox((2.0, 1.0, 1.0, 2.0)).is_err());
882 assert!(adapter()
883 .parse_vector_features(r#"{"remark":"runtime error"}"#)
884 .unwrap_err()
885 .contains("query failed"));
886 assert!(adapter()
887 .parse_vector_features(r#"{}"#)
888 .unwrap_err()
889 .contains("missing its elements"));
890 assert!(adapter()
891 .parse_vector_features(r#"{"elements":[],"remark":"runtime error"}"#)
892 .unwrap_err()
893 .contains("query failed"));
894 assert!(adapter().parse_vector_features("not json").is_err());
895 }
896}