diff --git a/crates/data/src/cityjson/tests.rs b/crates/data/src/cityjson/tests.rs index bdd40c0ccd..3a0005a34f 100644 --- a/crates/data/src/cityjson/tests.rs +++ b/crates/data/src/cityjson/tests.rs @@ -253,6 +253,21 @@ fn instances_and_tin_reliefs_are_said() { assert!(read("m.city.json", &only, None).unwrap_err().to_string().contains("TINRelief")); } +/// An instance whose reference point can't be drawn isn't said to be drawn as a point. +#[test] +fn an_instance_kept_without_geometry_isnt_said_to_be_drawn() { + let instance = |at: usize| { + json!({"tree": {"type": "SolitaryVegetationObject", "geometry": [ + {"type": "GeometryInstance", "template": 0, "boundaries": [at], "transformationMatrix": []}]}}) + }; + let vertices = json!([[0, 0, 0], [1e200, 0, 0]]); + let out = read("m.city.json", &doc(vertices.clone(), instance(1)), Crs::from_epsg(3857)).unwrap(); + assert!(geometry(&out[0].0, 0).is_none(), "{:?}", geometry(&out[0].0, 0)); + assert!(!says(&out, "geometry instances are drawn"), "{:?}", messages_of(&out)); + let out = read("m.city.json", &doc(vertices, instance(0)), Crs::from_epsg(3857)).unwrap(); + assert!(says(&out, "1 geometry instances are drawn as a point"), "{:?}", messages_of(&out)); +} + /// An attribute named as one of the reader's fields keeps its value, under a name that says so. #[test] fn an_attribute_named_as_a_field_is_renamed_and_said() { @@ -520,6 +535,26 @@ fn a_lone_face_seen_edge_on_has_no_footprint() { assert!(says(&out, "1 city objects have a shape that can't be drawn"), "{:?}", messages_of(&out)); } +/// A level 0 face leaning a little off vertical shows a sliver from above, which isn't kept as the +/// footprint as it stands: it is measured as [`footprint`] measures any face, and is a wall. +#[test] +fn a_lone_face_nearly_on_edge_is_a_wall() { + let bytes = doc( + json!([[0.0, 0.0, 0.0], [10.0, 0.0, 0.0], [10.0, 0.3, 10.0], [0.0, 0.3, 10.0]]), + json!({"w": {"type": "Building", "geometry": [{"type": "MultiSurface", "lod": "0", "boundaries": [[[0, 1, 2, 3]]]}]}}), + ); + let out = read("m.city.json", &bytes, None).unwrap(); + assert!(says(&out, "1 city objects with no faces seen from above"), "{:?}", messages_of(&out)); + // Lying flat, the same face is the footprint and says nothing. + let bytes = doc( + json!([[0.0, 0.0, 0.0], [10.0, 0.0, 0.0], [10.0, 0.3, 0.0], [0.0, 0.3, 0.0]]), + json!({"w": {"type": "Building", "geometry": [{"type": "MultiSurface", "lod": "0", "boundaries": [[[0, 1, 2, 3]]]}]}}), + ); + let out = read("m.city.json", &bytes, None).unwrap(); + assert!((area(geometry(&out[0].0, 0)) - 3.0).abs() < 1e-9, "{:?}", geometry(&out[0].0, 0)); + assert!(messages_of(&out).is_empty(), "{:?}", messages_of(&out)); +} + /// Lines and points show the level of detail they came from, not the footprint's. Their own heights /// differing is more than `z_min` and `z_max` keep, which is said; all at one height, it isn't. #[test] @@ -981,6 +1016,7 @@ fn a_system_given_over_the_one_named_is_said() { // ...and without one given, it is an error, not a note. let e = read("m.city.json", root.to_string().as_bytes(), None).unwrap_err().to_string(); assert!(e.contains("it names a coordinate system RustGIS doesn't know"), "{e}"); + assert!(e.contains("give one with the crs parameter of layer.addData"), "{e}"); } /// One building whose LoD 2 geometry passes the face cap: a face with a courtyard first (marked diff --git a/crates/data/src/citymodel.rs b/crates/data/src/citymodel.rs index ba84312aeb..b3b48b525d 100644 --- a/crates/data/src/citymodel.rs +++ b/crates/data/src/citymodel.rs @@ -14,7 +14,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use serde_json::Value as Json; -use crate::footprint::{Hull, P3, footprint, usable_xy}; +use crate::footprint::{Hull, P3, footprint, shows, usable_xy}; use crate::raster::Crs; use crate::{Coord, DataError, Dataset, Field, Geometry, GeometryType, ReadReport, Result, Value}; @@ -211,7 +211,7 @@ fn lone_face(face: &[Vec]) -> Option { hi - lo }; let size = span(|c| c.x).max(span(|c| c.y)); - (crate::geometry::ring_area(outer).abs() > size * size * 5e-5).then(|| Geometry::Polygon(vec![rings])) + shows(face.first()?, size).then(|| Geometry::Polygon(vec![rings])) } /// An object's footprint and the level of detail it came from: the first of its LoD 0, the @@ -355,7 +355,7 @@ pub(crate) fn coordinate_system( } (None, Some(system)) => known_system(system).ok_or_else(|| { let system: String = system.chars().take(80).collect(); - DataError::Format(format!("{file_name}: it names a coordinate system RustGIS doesn't know ({system})")) + DataError::Format(format!("{file_name}: it names a coordinate system RustGIS doesn't know ({system}); give one with the crs parameter of layer.addData (from the command line, Python or the AI Assistant)")) })?, (None, None) if within(180.0, 90.0) => { notes.push("the file names no coordinate system: its coordinates are read as longitude/latitude".into()); @@ -642,7 +642,10 @@ pub(crate) fn object_rows(parts: ObjectParts, object: usize, counts: &mut Counts push(Family::Line, line_lod, geometry, heights_differ(lines.iter().flatten()), lines_draw); } let Highest { lod: point_lod, parts: points, usable: points_usable, .. } = points; - counts.instances += points.iter().filter(|(_, instance)| *instance).count(); + // Only those drawn: a point row kept without geometry draws no instance. + if points_usable { + counts.instances += points.iter().filter(|(_, instance)| *instance).count(); + } let points: Vec = points.into_iter().map(|(p, _)| p).collect(); if !points.is_empty() { let geometry = points_usable.then(|| match points.as_slice() { diff --git a/crates/data/src/footprint.rs b/crates/data/src/footprint.rs index 70634f34be..c33d57a158 100644 --- a/crates/data/src/footprint.rs +++ b/crates/data/src/footprint.rs @@ -43,6 +43,28 @@ pub(crate) enum Hull { Unmerged, } +/// Whether a face's outer `ring` shows from above, in a shape `size` across (its larger X or Y +/// span), by the cosine of its tilt (its projected area over its area): walls, even leaning ones, +/// show as slivers, which cover nothing and which the union can't always take. Faces narrower than +/// a ten-thousandth of the shape are left out too (a flat shape). +pub(crate) fn shows(ring: &[P3], size: f64) -> bool { + let (mut nx, mut ny, mut nz) = (0.0, 0.0, 0.0); + let (mut longest, mut area2) = (0.0f64, 0.0); + // From the ring's first point: projected coordinates run to millions, whose products would + // lose the millimetres a small face is measured in. + let o = ring.first().copied().unwrap_or([0.0; 3]); + let local: Vec = ring.iter().map(|p| [p[0] - o[0], p[1] - o[1], p[2] - o[2]]).collect(); + for (a, b) in local.iter().zip(local.iter().cycle().skip(1)) { + nx += (a[1] - b[1]) * (a[2] + b[2]); + ny += (a[2] - b[2]) * (a[0] + b[0]); + nz += (a[0] - b[0]) * (a[1] + b[1]); + longest = longest.max((b[0] - a[0]).hypot(b[1] - a[1])); + area2 += a[0] * b[1] - b[0] * a[1]; + } + let n = nx.hypot(ny).hypot(nz); + n > 0.0 && nz.abs() / n > 0.05 && longest > 0.0 && area2.abs() / longest > size * 1e-4 +} + /// A shape's footprint: the union of its `faces` (each its outer ring and then its holes) seen from /// above. Faces standing on edge, such as walls, show nothing and are left out. The footprint is the /// hull of `points` when no face shows, or when its faces aren't merged: `truncated` (the reader left @@ -72,26 +94,6 @@ pub(crate) fn footprint]>>( if !size.is_finite() { return (None, None); } - // A face shows from above by the cosine of its tilt (its projected area over its area): walls, - // even leaning ones, show as slivers, which cover nothing and which the union can't always - // take. Faces narrower than a ten-thousandth of the shape are left out too (a flat shape). - let shows = |ring: &[P3]| { - let (mut nx, mut ny, mut nz) = (0.0, 0.0, 0.0); - let (mut longest, mut area2) = (0.0f64, 0.0); - // From the ring's first point: projected coordinates run to millions, whose products would - // lose the millimetres a small face is measured in. - let o = ring.first().copied().unwrap_or([0.0; 3]); - let local: Vec = ring.iter().map(|p| [p[0] - o[0], p[1] - o[1], p[2] - o[2]]).collect(); - for (a, b) in local.iter().zip(local.iter().cycle().skip(1)) { - nx += (a[1] - b[1]) * (a[2] + b[2]); - ny += (a[2] - b[2]) * (a[0] + b[0]); - nz += (a[0] - b[0]) * (a[1] + b[1]); - longest = longest.max((b[0] - a[0]).hypot(b[1] - a[1])); - area2 += a[0] * b[1] - b[0] * a[1]; - } - let n = nx.hypot(ny).hypot(nz); - n > 0.0 && nz.abs() / n > 0.05 && longest > 0.0 && area2.abs() / longest > size * 1e-4 - }; let ring2 = |r: &[P3]| geo::LineString::from(r.iter().map(|p| (p[0], p[1])).collect::>()); // Past the cap, a partial union would be wrong: the hull below stands in. let seen: Vec> = faces @@ -99,7 +101,7 @@ pub(crate) fn footprint]>>( .filter(|_| !truncated) .filter_map(|face| { let (outer, holes) = face.as_ref().split_first()?; - (outer.len() >= 3 && shows(outer)) + (outer.len() >= 3 && shows(outer, size)) .then(|| geo::Polygon::new(ring2(outer), holes.iter().map(|h| ring2(h)).collect()).orient(Direction::Default)) }) .collect(); diff --git a/crates/data/src/gml.rs b/crates/data/src/gml.rs index 2056737a30..ba9edbde90 100644 --- a/crates/data/src/gml.rs +++ b/crates/data/src/gml.rs @@ -179,26 +179,34 @@ fn ring(mut r: Vec) -> Option> { (r.len() >= 4).then_some(r) } -/// The rings of a polygon or surface patch: exterior (outerBoundaryIs) first. -fn polygon(p: &Node, zm: &Cell) -> Option>> { - // Each ring `ring` keeps (at least three points, closed if it wasn't), and whether it had Z or M. +/// The rings of a polygon or surface patch: exterior (outerBoundaryIs) first. `lost` counts the +/// rings left out: too few points or unreadable, and exterior rings past the first. +fn polygon(p: &Node, zm: &Cell, lost: &Cell) -> Option>> { + // Each ring `ring` keeps (at least four positions once closed, closed if it wasn't), and whether + // it had Z or M. let boundary = |names: &[&str]| -> Vec<(Vec, bool)> { let kept = |b: Node| { let dims = Cell::new(false); - ring(coords(&b, &dims)?).map(|r| (r, dims.get())) + let r = coords(&b, &dims).and_then(ring).map(|r| (r, dims.get())); + lost.set(lost.get() + usize::from(r.is_none())); + r }; p.descendants().filter(|d| d.is_element() && is_gml(d) && names.contains(&local(d))).filter_map(kept).collect() }; // The first exterior ring, then the interiors: only the rings kept mark Z or M. - let outer = boundary(&["exterior", "outerBoundaryIs"]).into_iter().next()?; + let mut outers = boundary(&["exterior", "outerBoundaryIs"]).into_iter(); + let outer = outers.next()?; + lost.set(lost.get() + outers.count()); let mut rings = vec![outer]; rings.extend(boundary(&["interior", "innerBoundaryIs"])); zm.set(zm.get() || rings.iter().any(|(_, d)| *d)); Some(rings.into_iter().map(|(r, _)| r).collect()) } -/// A GML geometry element as a geometry; `zm` is set when Z or M values are dropped. -fn geometry(g: &Node, zm: &Cell) -> Option { +/// A GML geometry element as a geometry; `zm` is set when Z or M values are dropped, and `lost` +/// counts the rings and surface patches left out (too few points, or unreadable). Members of a +/// multi-geometry left out are counted by `parts_lost`. +fn geometry(g: &Node, zm: &Cell, lost: &Cell) -> Option { let kids = |names: &[&str]| -> Vec { g.descendants().filter(|d| d.is_element() && is_gml(d) && names.contains(&local(d))).collect() }; match local(g) { @@ -222,9 +230,20 @@ fn geometry(g: &Node, zm: &Cell) -> Option { let parts: Vec> = kids(&["LineString", "Curve"]).iter().filter_map(kept).collect(); (!parts.is_empty()).then_some(Geometry::Line(parts)) } - "Polygon" => Some(Geometry::Polygon(vec![polygon(g, zm)?])), + "Polygon" => Some(Geometry::Polygon(vec![polygon(g, zm, lost)?])), "Surface" | "MultiPolygon" | "MultiSurface" => { - let polys: Vec>> = kids(&["Polygon", "PolygonPatch"]).iter().filter_map(|p| polygon(p, zm)).collect(); + let polys: Vec>> = kids(&["Polygon", "PolygonPatch"]) + .iter() + .filter_map(|p| { + // A polygon left out whole counts once, not by its rings; a multi-geometry's + // member left out is counted by `parts_lost`, so only a surface's patch here. + let rings = Cell::new(0); + let poly = polygon(p, zm, &rings); + let whole = usize::from(!local(g).starts_with("Multi")); + lost.set(lost.get() + if poly.is_some() { rings.get() } else { whole }); + poly + }) + .collect(); (!polys.is_empty()).then_some(Geometry::Polygon(polys)) } "Envelope" | "Box" => { @@ -260,7 +279,7 @@ fn parts_lost(g: &Node) -> usize { && match (kind, local(&x)) { ("MultiPoint", "Point") => coords(&x, &scratch).is_some_and(|c| !c.is_empty()), ("MultiLineString" | "MultiCurve", "LineString" | "Curve") => coords(&x, &scratch).is_some_and(|c| c.len() >= 2), - ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch") => polygon(&x, &scratch).is_some(), + ("MultiPolygon" | "MultiSurface", "Polygon" | "PolygonPatch") => polygon(&x, &scratch, &Cell::new(0)).is_some(), _ => false, } }) @@ -297,6 +316,8 @@ struct Kind { unread_more: bool, /// Members of multi-geometries that gave nothing to keep. parts_lost: usize, + /// Rings and surface patches of its geometries left out. + lost: usize, } /// Reads a GML file into a layer per feature type (those with features). @@ -323,6 +344,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> let (mut skipped, mut file_srs): (usize, Option<(Crs, bool)>) = (0, None); let mut unknown_srs: Option = None; let mut over = 0usize; + // Rings and parts left out of the geometries kept. for f in members { let name = local(&f).to_string(); if !kinds.contains_key(&name) { @@ -338,6 +360,8 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> let mut bad = false; // Whether the geometry kept had Z or M values. let mut zm = false; + // Rings and surface patches left out of the geometry kept. + let mut rings_lost = 0; // Properties not read, and those holding GML of a kind not read (another kind of // geometry): the feature's unreadable shape when it has none, else not read. let (mut unread, mut shapes_not_read) = (Vec::new(), Vec::new()); @@ -363,10 +387,11 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> None => unknown_srs = Some(s.to_string()), } } - let dims = Cell::new(false); - match geometry(&g, &dims) { + let (dims, lost) = (Cell::new(false), Cell::new(0)); + match geometry(&g, &dims, &lost) { Some(gm) => { zm = dims.get(); + rings_lost += lost.get(); geom = Some(gm); kind.parts_lost += parts_lost(&g); } @@ -419,6 +444,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> // A geometry that couldn't be read, unless another property gave the feature its shape. skipped += usize::from(bad && geom.is_none()); kind.zm += usize::from(zm && geom.is_some()); + kind.lost += rings_lost; kind.rows.push((geom, attrs)); } let mut report = ReadReport { skipped, ..ReadReport::default() }; @@ -473,6 +499,15 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result> if file_srs.is_none() && outside { rep.messages.push("the GML doesn't say its coordinate system (srsName) and its coordinates aren't longitude/latitude".into()); } + // Said with the layer it happened in, which may be the only one added. + if kind.lost > 0 { + let what = + if kind.lost == 1 { "ring or surface patch of a geometry was" } else { "rings or surface patches of geometries were" }; + rep.messages.push(format!( + "{}: {} {what} left out: too few points, unreadable coordinates, a patch with no exterior ring, or an exterior ring past the first", + ds.name, kind.lost + )); + } if zm > 0 { ds.zm_dropped = true; rep.messages.push(format!("{}: {}", ds.name, crate::zm_note(Some(zm as u64)))); @@ -571,7 +606,8 @@ mod tests { rep.messages ); - // A one-point 3D part is left out, and its height with it: the 2D line kept says nothing. + // A one-point 3D part is left out, and its height with it: the 2D line kept drops no Z, + // and the part left out is said. let gml = r#" 10 48 312 10 48 11 49 @@ -590,7 +626,8 @@ mod tests { ); assert_eq!(layers[0].0.features[0].geometry, Some(Geometry::Line(vec![vec![Coord::new(10.0, 48.0), Coord::new(11.0, 49.0)]]))); - // A two-point 3D interior ring is left out: the 2D polygon kept says nothing. + // A two-point 3D interior ring is left out: the 2D polygon kept drops no Z, and the ring + // left out is said. let gml = r#" 10 48 11 48 11 49 10 48 @@ -599,9 +636,68 @@ mod tests { "#; let layers = read("p.gml", gml.as_bytes()).unwrap(); assert!(!layers[0].0.zm_dropped, "{:?}", layers[0].1.messages); + assert!( + layers[0].1.messages.iter().any(|m| m.contains(": 1 ring or surface patch of a geometry was left out")), + "{:?}", + layers[0].1.messages + ); assert!(matches!(&layers[0].0.features[0].geometry, Some(Geometry::Polygon(p)) if p.len() == 1 && p[0].len() == 1)); } + /// Rings with too few points are left out and counted, once a kind, and a multi-geometry's + /// member left out only as a part; a clean file says nothing of them. + #[test] + fn rings_and_parts_left_out_are_counted() { + let layers = read("w.gml", WFS.as_bytes()).unwrap(); + assert!(layers.iter().all(|(_, r)| !r.messages.iter().any(|m| m.contains("or surface patch"))), "{:?}", layers[0].1.messages); + // A second exterior ring is a ring left out; a multi-surface's polygon whose only ring is + // two points is a part left out, said once, by the parts notice. + let gml = r#" + + 10 48 11 48 11 49 10 48 + 20 48 21 48 21 49 20 48 + + + 10 48 11 48 11 49 10 48 + 10 48 11 48 + +"#; + let layers = read("p.gml", gml.as_bytes()).unwrap(); + let (ds, rep) = &layers[0]; + assert_eq!((ds.features.len(), rep.skipped), (2, 0)); + let said: Vec<&String> = rep.messages.iter().filter(|m| m.contains("or surface patch")).collect(); + assert_eq!(said.len(), 1, "{:?}", rep.messages); + assert!(said[0].contains(": 1 ring or surface patch of a geometry was left out"), "{said:?}"); + assert!(rep.messages.iter().any(|m| m.contains(": 1 part of multi-geometries")), "{:?}", rep.messages); + + // With two feature types, the notice goes with the one that lost a part, not the first. + let gml = r#" +10 48 11 49 + + 10 48 11 48 11 49 10 48 + 10 48 11 48 + +"#; + let layers = read("two.gml", gml.as_bytes()).unwrap(); + let said = |i: usize| layers[i].1.messages.iter().any(|m| m.contains("multi-geometries")); + assert_eq!(layers.len(), 2); + assert!(!said(0) && said(1), "{:?}", layers.iter().map(|l| &l.1.messages).collect::>()); + assert!(layers[1].1.messages.iter().any(|m| m.starts_with(&format!("{}: 1 part", layers[1].0.name))), "{:?}", layers[1].1.messages); + + // A multi-surface's polygon with only an interior ring is left out and counted once, as a + // part, not again as a ring. + let gml = r#" + + 10 48 11 48 11 49 10 48 + 10 48 11 48 11 49 10 48 + +"#; + let layers = read("hole.gml", gml.as_bytes()).unwrap(); + let messages = &layers[0].1.messages; + assert!(!messages.iter().any(|m| m.contains("or surface patch")), "{messages:?}"); + assert!(messages.iter().any(|m| m.contains(": 1 part of multi-geometries")), "{messages:?}"); + } + /// A multi-geometry member that is only a reference gives nothing, and is said; a whole one /// says nothing. #[test] diff --git a/crates/data/src/ogc.rs b/crates/data/src/ogc.rs index 8467669a03..7074d8b126 100644 --- a/crates/data/src/ogc.rs +++ b/crates/data/src/ogc.rs @@ -3532,9 +3532,27 @@ impl Pages { fn land_page(mut self, answer: Result, String>) -> Step { if matches!(self.phase, Phase::WfsHits) { - let matched = answer.ok().and_then(|b| wfs_matched(&b)); - let Some(matched) = matched else { - return self.stop("the WFS total couldn't be read; the features received may be incomplete".into()); + // Why the request failed is kept, so the notice says why completeness is unknown. + let matched = match answer { + Ok(b) => wfs_matched(&b).ok_or(None), + // An error holding an address isn't repeated: `redact` keeps its path, which can + // hold a key. + Err(e) => Err(Some(e).filter(|e| !e.contains("://"))), + }; + let matched = match matched { + Ok(matched) => matched, + Err(why) => { + // Without the credentials an address in the error may hold, and not all of a + // long one. + let why = why + .map(|e| { + let mut e = crate::diagnostics::redact(&e); + crate::diagnostics::cut_with_ellipsis(&mut e, MAX_REASON); + format!(" ({e})") + }) + .unwrap_or_default(); + return self.stop(format!("the WFS total couldn't be read{why}; the features received may be incomplete")); + } }; let offset = self.features.len(); if offset > self.max { @@ -3839,8 +3857,12 @@ impl Pages { if let Some((ds, ..)) = &mut self.decoded { ds.features.truncate(max); } - let note = format!("the first {max} features are read; the rest aren't"); - if matches!(self.phase, Phase::WfsHits | Phase::WfsOffsets { .. }) { self.stop(note) } else { self.finish(Some(note)) } + // Not through `stop`, whose "N WFS features read:" would say the count twice. + let note = match self.phase { + Phase::WfsOffsets { matched, .. } => format!("the first {max} of {matched} WFS features are read; the rest aren't"), + _ => format!("the first {max} features are read; the rest aren't"), + }; + self.finish(Some(note)) } /// The pages' features in the first page's collection, without its paging members. @@ -5002,7 +5024,17 @@ mod tests { { let (bytes, note) = join_pages(url, first.to_vec(), 100, |_| answer.clone()); assert_eq!(count(&bytes), 2); - assert!(note.unwrap().contains("incomplete")); + let note = note.unwrap(); + assert!(note.contains("incomplete")); + // The failed request's reason is kept. + assert_eq!(note.contains("(timeout)"), answer.is_err(), "{note}"); + } + // A credential in the failed request's address isn't repeated in the notice. + { + let failed = Err::, String>("error sending request for url (https://x.test/wfs?token=abc123)".to_string()); + let (_, note) = join_pages(url, first.to_vec(), 100, |_| failed.clone()); + let note = note.unwrap(); + assert!(note.contains("incomplete") && !note.contains("abc123"), "{note}"); } for last in [ Err("timeout".into()), @@ -5026,10 +5058,14 @@ mod tests { if calls == 1 { Ok(hits.to_vec()) } else { Ok(br#"{"features":[{"id":3},{"id":4}]}"#.to_vec()) } }); assert_eq!(count(&bytes), 3); - assert!(note.unwrap().contains("3 of 4 WFS features read")); + let note = note.unwrap(); + assert!(note.contains("the first 3 of 4 WFS features are read"), "{note}"); + assert!(!note.contains("WFS features read:"), "{note}"); let (bytes, note) = join_pages(url, first.to_vec(), 1, |_| panic!("limit checked before hits")); assert_eq!(count(&bytes), 1); - assert!(note.unwrap().contains("first 1")); + let note = note.unwrap(); + assert!(note.contains("first 1"), "{note}"); + assert!(!note.contains("WFS features read:"), "{note}"); for suffix in ["&STARTINDEX=0", "&RESULTTYPE=hits"] { let (bytes, note) = join_pages(&format!("{url}{suffix}"), first.to_vec(), 100, |_| panic!("explicit request must stay unchanged")); diff --git a/crates/data/src/raster/esri.rs b/crates/data/src/raster/esri.rs index 4af3d4204d..baceede8f6 100644 --- a/crates/data/src/raster/esri.rs +++ b/crates/data/src/raster/esri.rs @@ -24,7 +24,7 @@ //! (Molodensky–Badekas for Korean 1985, a chain of operations for NGO 1948 …), and the 11 //! planetary ones (Moon, Mars), whose bodies the map can't hold; //! - the seven PROJ can't write as a PROJ string (Hammer–Aitoff, Fuller, Cube, Tobler …) and the -//! two two-point oblique Mercators (53025, 54025), for which RustGIS has no WKT form. +//! two two-point oblique Mercators (53025, 54025), which RustGIS refuses. //! //! The three Earth sinusoidal systems (Sphere and World Sinusoidal, 53008 and 54008, and Africa //! Sinusoidal, 102011) were added afterwards (opengeos/RustGIS#563): RustGIS computes the diff --git a/crates/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index 76053e3ce5..692d6282a0 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -577,8 +577,10 @@ pub struct Raster { /// left out. pub lost: Vec, /// How many values of each band changed when read as `f32` (Int32 IDs past 16,777,216, Float64 - /// values with more digits than `f32` holds), by band; empty when none did. NaN and no-data - /// values aren't counted. + /// values with more digits than `f32` holds), by band; empty when none did. NaN values and + /// samples that are the no-data value aren't counted (one that rounds onto it is). The two are + /// compared once read as `f32`: a long decimal whose `f32` reading differs from the no-data + /// value's (rounded through `f64`) is counted, as it isn't masked as no-data either. pub rounded: Vec, /// Who holds the rights to the picture (the TIFF Copyright tag): the credits a map's tile /// layers ask for, on a GeoTIFF of the view. Read from a GeoTIFF that has it, and written diff --git a/crates/data/src/raster/wkt.rs b/crates/data/src/raster/wkt.rs index 34174fb88d..6001bf792b 100644 --- a/crates/data/src/raster/wkt.rs +++ b/crates/data/src/raster/wkt.rs @@ -814,7 +814,10 @@ fn custom(p: &Node, method: &str) -> Result { // arrives as the plain name through the METHODS table. "HOTINE_OBLIQUE_MERCATOR" | "HOTINE_OBLIQUE_MERCATOR_AZIMUTH_NATURAL_ORIGIN" if get(&["AZIMUTH"]).is_some() => omerc(true)?, // EPSG's methods 9806 and 9818 have no scale factor; PROJ's cass and poly ignore one. - "CASSINI_SOLDNER" => format!("+proj=cass +lat_0={lat0} {at}"), + // ESRI writes plain `Cassini` to `.prj` files for the same ellipsoidal method (EPSG 9806): + // ESRI's projection reference describes it as Cassini-Soldner, and GDAL's ESRI WKT + // morphing maps `Cassini` and `Cassini_Soldner` to each other. + "CASSINI_SOLDNER" | "CASSINI" => format!("+proj=cass +lat_0={lat0} {at}"), "POLYCONIC" => format!("+proj=poly +lat_0={lat0} {at}"), "EQUAL_EARTH" => format!("+proj=eqearth {at}"), // The implementation fixes the cone axis and pseudo standard parallel. Refuse other @@ -1606,6 +1609,26 @@ mod tests { assert!((ll.x - 3.0).abs() < 1e-6 && (ll.y - 45.153_477).abs() < 1e-5, "{ll:?}"); } + #[test] + fn esri_cassini_name() { + // ArcGIS writes Cassini-Soldner as plain `Cassini` in a `.prj`: Berlin's Soldner grid + // (EPSG:3068) under that name and a name RustGIS doesn't know reads as the EPSG system. + let epsg = Crs::from_epsg(3068).unwrap(); + let wkt = to_wkt(epsg).unwrap().replace(",AUTHORITY[\"EPSG\",\"3068\"]", ""); + let start = wkt.find('"').unwrap() + 1; + let end = start + wkt[start..].find('"').unwrap(); + let esri = + format!("{}My_Soldner{}", &wkt[..start], &wkt[end..]).replace("PROJECTION[\"Cassini_Soldner\"]", "PROJECTION[\"Cassini\"]"); + assert!(esri.contains("PROJECTION[\"Cassini\"]"), "{esri}"); + let crs = crs_from_wkt(&esri).unwrap_or_else(|e| panic!("{e}: {esri}")); + // Near the grid origin and a few degrees from it, where a spherical form would differ. + for lonlat in [Coord::new(13.4, 52.4), Coord::new(17.5, 55.5)] { + let a = epsg.transformer().unwrap().from_lonlat(lonlat).unwrap(); + let b = crs.transformer().unwrap().from_lonlat(lonlat).unwrap(); + assert!((a.x - b.x).abs() < 0.01 && (a.y - b.y).abs() < 0.01, "{a:?} {b:?}"); + } + } + #[test] fn datums_and_prime_meridians() { // A prime meridian east of Greenwich moves longitudes east. diff --git a/crates/data/src/raster/zarr.rs b/crates/data/src/raster/zarr.rs index 68ada6a52f..0bf5d6e087 100644 --- a/crates/data/src/raster/zarr.rs +++ b/crates/data/src/raster/zarr.rs @@ -1060,7 +1060,6 @@ fn values_1d(a: &Array, chunks: &HashMap>) -> Result> { Ok(out) } -/// A number attribute (`scale_factor`, `_FillValue`). /// A JSON number exactly: a whole one as one. fn exact_number(n: &serde_json::Number) -> Option { n.as_i64() @@ -1087,6 +1086,7 @@ fn attr_exact(a: &Array, k: &str) -> Option { } } +/// A number attribute (`scale_factor`, `_FillValue`). fn attr_f64(a: &Array, k: &str) -> Option { match a.attrs.get(k)? { Value::Number(n) => n.as_f64(), diff --git a/crates/engine/src/cmd/design.rs b/crates/engine/src/cmd/design.rs index 0d1e37152f..2f4f170d01 100644 --- a/crates/engine/src/cmd/design.rs +++ b/crates/engine/src/cmd/design.rs @@ -986,7 +986,7 @@ fn modify(s: &mut Session, mut change: impl FnMut((u32, usize), &Geometry) -> Re fn set_geometry(s: &mut Session, p: &Json) -> CmdResult { let id = s.layer_id(p)?; let row = p.get("row").and_then(Json::as_u64).ok_or("row is required")? as usize; - let g = p.get("geometry").and_then(rustgis_data::io::geometry_from_geojson).ok_or("geometry must be a GeoJSON geometry")?; + let g = super::edit::geometry_param(p)?; replace_geometry(s, id, row, g)?; Ok(Json::Null) } diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 53fdedae7d..0c219a0d1b 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -32,7 +32,7 @@ pub fn commands() -> Vec { pub(crate) fn add_feature(s: &mut Session, p: &Json) -> CmdResult { let id = s.layer_id(p)?; - let g = p.get("geometry").and_then(rustgis_data::io::geometry_from_geojson).ok_or("geometry must be a GeoJSON geometry")?; + let g = geometry_param(p)?; let l = s.map_mut().layer_mut(id).ok_or("no layer")?; let want = l.geometry_type(); let ok = want == g.kind() || want == GeometryType::None; @@ -68,6 +68,25 @@ pub(crate) fn add_feature(s: &mut Session, p: &Json) -> CmdResult { Ok(json!({"row": row})) } +/// The `geometry` parameter as a geometry. A GeometryCollection of more or fewer than one member +/// (at any depth) is refused as such, not as something that isn't GeoJSON. +pub(crate) fn geometry_param(p: &Json) -> Result { + let given = p.get("geometry"); + if let Some(g) = given.and_then(rustgis_data::io::geometry_from_geojson) { + return Ok(g); + } + let mut g = given; + // Bounded by the JSON's own depth: each round goes one level in. + while let Some(c) = g.filter(|g| g.get("type").and_then(Json::as_str) == Some("GeometryCollection")) { + match c.get("geometries").and_then(Json::as_array).map(Vec::as_slice) { + Some([only]) => g = Some(only), + Some(_) => return Err("a GeometryCollection must hold exactly one geometry".into()), + None => break, + } + } + Err("geometry must be a GeoJSON geometry".into()) +} + /// Rejects empty or degenerate geometry, which would break extents, labels and rendering. pub(crate) fn validate(g: &Geometry) -> Result<(), String> { match g { diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 32ff9fbf3f..ddbada654d 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -16,13 +16,13 @@ use crate::{CmdResult, CommandSpec as C, Session}; pub fn commands() -> Vec { vec![ C::new("layer.addData", "Add Data", "Home › Add", add_data).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) - .params(r#"{"path": string, "name"?: string, "layer"?: "GeoPackage, GDAL, DuckDB or DXF layer", "paperSpace"?: "bool: a DXF's layout entities too", "stream"?: bool, "as"?: "scan" (a TIFF, PNG or JPEG, even a photo or a GeoTIFF, as a picture with no coordinates), "crs"?: "EPSG:… (or WKT) for Shapefile coordinates (overrides .prj), a CSV, a CityJSON model, or a GDAL or DuckDB file that doesn't say", "x"?: "CSV x/longitude column", "y"?: "CSV y/latitude column", "wkt"?: "CSV WKT geometry column", "delimiter"?: ","|";"|"\\t"|"|"}"#), + .params(r#"{"path": string, "name"?: string, "layer"?: "GeoPackage, GDAL, DuckDB or DXF layer", "paperSpace"?: "bool: a DXF's layout entities too", "stream"?: bool, "as"?: "scan" (a TIFF, PNG or JPEG, even a photo or a GeoTIFF, as a picture with no coordinates), "crs"?: "EPSG:… (or WKT) for Shapefile coordinates (overrides .prj), a CSV, a CityJSON model (it replaces the system a model file names; in a zip, it is used for a model naming none RustGIS knows), or a GDAL or DuckDB file that doesn't say", "x"?: "CSV x/longitude column", "y"?: "CSV y/latitude column", "wkt"?: "CSV WKT geometry column", "delimiter"?: ","|";"|"\\t"|"|"}"#), C::new("layer.addPhotos", "Add Photos", "Home › Add", add_photos).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"paths"?: [JPEG photo files], "folder"?: "a folder of photos (every JPEG in it)", "name"?: "the layer (default Photos; photos join it when it exists)"}"#, ), C::new("info.fileLayers", "List Layers in File", "Home › Add › Add Data", file_layers).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() - .params(r#"{"path": string, "crs"?: "EPSG:… for a .city.json that doesn't say (as layer.addData)"}"#), + .params(r#"{"path": string, "crs"?: "EPSG:… for a CityJSON model (.city.json, a .json, or in a zip): it replaces the system a model file names; in a zip, it is used for a model naming none RustGIS knows (as layer.addData)"}"#), C::new("info.fileColumns", "List Columns in File", "Home › Add › Add Data › Delimited Text", file_columns).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view().params( r#"{"path": "a delimited text file (CSV, TSV, TXT) or a spreadsheet", "delimiter"?: ","|";"|"\\t"|"|", "layer"?: "a spreadsheet's sheet (the first by default)"}"#, ), @@ -1594,7 +1594,7 @@ pub(crate) fn add_layers(s: &mut Session, p: &Value, file_name: &str, bytes: &[u .map(|(dataset, report)| rustgis_data::io::LayerRead { dataset, report, styles: Vec::new() }) .collect() } - // A CityJSON model that names no coordinate system, or names one it doesn't have. + // A CityJSON model with a coordinate system given, which takes the place of any it names. _ if crs.is_some() && rustgis_data::io::is_cityjson(file_name, bytes) => { let crs = crs.map(rustgis_data::raster::Crs::parse).transpose()?; rustgis_data::cityjson::read(file_name, bytes, crs) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 9be8093b66..3ad11800d0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -21919,6 +21919,36 @@ fn unknown_params_are_said() { assert!(said.len() < 500 && said.contains("995 more"), "{said}"); } +/// A GeometryCollection of several members is refused by what is wrong with it. +#[test] +fn a_geometry_collection_of_several_is_refused_as_such() { + use rustgis_data::{Coord, Geometry}; + let mut s = world(); + let point = json!({"type": "Point", "coordinates": [1, 2]}); + let two = json!({"type": "GeometryCollection", "geometries": [point, point]}); + let nested = json!({"type": "GeometryCollection", "geometries": [two]}); + let empty = json!({"type": "GeometryCollection", "geometries": []}); + for cmd in ["edit.addFeature", "edit.setGeometry"] { + for g in [&two, &nested, &empty] { + let e = s.run(cmd, &json!({"layer": "Cities", "row": 0, "geometry": g})).unwrap_err(); + assert!(e.contains("a GeometryCollection must hold exactly one geometry"), "{cmd}: {e}"); + } + let e = s.run(cmd, &json!({"layer": "Cities", "row": 0, "geometry": {"type": "Blob"}})).unwrap_err(); + assert!(e.contains("geometry must be a GeoJSON geometry"), "{cmd}: {e}"); + } + // One member, at any depth, is the geometry. + let one = json!({"type": "GeometryCollection", "geometries": [{"type": "GeometryCollection", "geometries": [point]}]}); + let before = s.map().layer_by_name("Cities").unwrap().dataset.features.len(); + let added = s.run("edit.addFeature", &json!({"layer": "Cities", "geometry": one})).unwrap(); + let row = added["row"].as_u64().unwrap() as usize; + assert_eq!((row, s.map().layer_by_name("Cities").unwrap().dataset.features.len()), (before, before + 1)); + s.run("edit.setGeometry", &json!({"layer": "Cities", "row": 0, "geometry": one})).unwrap(); + let cities = &s.map().layer_by_name("Cities").unwrap().dataset; + for r in [row, 0] { + assert_eq!(cities.features[r].geometry, Some(Geometry::Point(Coord::new(1.0, 2.0))), "row {r}"); + } +} + #[test] fn geojson_collection_import_reports_loss_even_without_surviving_geometry() { let collection = json!({"type":"GeometryCollection", "geometries":[