diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 7753df0b33..24b71ec969 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -2200,12 +2200,20 @@ fn dissolve(inp: &dyn Inputs, p: &Json) -> Result { // Each group's key, polygons and records, in the order first met; found by its key's hash. let mut groups: Vec<(Value, Vec>, Vec)> = Vec::new(); let mut index: HashMap, usize> = HashMap::new(); + // Features with no polygon to dissolve (no geometry, an empty polygon, or a point or line in + // the polygon layer, as Minimum Bounding Geometry's flat hulls are): left out of the groups, + // their counts and statistics, and counted. + let mut not_polygons = 0usize; let input_rows = each_once(rows(inp, p, "input", ds)); crate::control::begin(input_rows.len()); for (done, r) in input_rows.into_iter().enumerate() { crate::control::check()?; crate::control::reached(done); - let Some(mp) = ds.features.get(r).and_then(|f| f.geometry.as_ref()).and_then(to_multipolygon) else { continue }; + let polygon = ds.features.get(r).and_then(|f| f.geometry.as_ref()).and_then(to_multipolygon); + let Some(mp) = polygon.filter(|mp| mp.0.iter().any(|p| !p.exterior().0.is_empty())) else { + not_polygons += 1; + continue; + }; let value = fi.map_or(&Value::Null, |fi| ds.value(r, fi)); // A NaN equals no value, so each one stays a group of its own; −0 is 0, as it compares. let part = crate::stats::equal_key(value); @@ -2248,11 +2256,28 @@ fn dissolve(inp: &dyn Inputs, p: &Json) -> Result { out.push(from_multipolygon(&u), attrs); } let n = out.features.len(); + // Features read, none with a polygon: an unusable result, so an error saying why. + if n == 0 && not_polygons > 0 { + return Err(format!( + "Dissolve: none of the {not_polygons} feature(s) has a polygon: no geometry, an empty polygon, or a point or line" + )); + } let mut messages = vec![format!("Dissolve: {n} features")]; + if not_polygons > 0 { + messages.push(format!( + "Dissolve: {not_polygons} feature(s) left out, with their values: no geometry, an empty polygon, or a point or line rather than a polygon" + )); + } messages.extend(rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default())); Ok(ToolResult { output: Output::Layer(out), messages }) } +/// The farthest (meters, either way) Minimum Bounding Geometry takes a point from the analysis +/// plane's origin: about 9 million degrees of longitude, far past any real data and far short of +/// where the hull's products of coordinate differences overflow. (The plane's y is bounded: a +/// latitude past ±90° is taken as the pole.) +const HULL_LIMIT_M: f64 = 1e12; + fn convex_hull(inp: &dyn Inputs, p: &Json) -> Result { let ds = input(inp, p, "input")?; let each = s(p, "group") == "None"; @@ -2261,20 +2286,59 @@ fn convex_hull(inp: &dyn Inputs, p: &Json) -> Result { } else { Dataset::new(out_name(p, format!("{}_Hull", ds.name)), GeometryType::Polygon, vec![Field::new("count", FieldType::Integer)]) }; + // A feature's points in the analysis plane, and how many of its vertices (a closed ring's + // repeated last point left out) are passed over: a coordinate missing or infinite, which the + // hull can't order, or a longitude past [`HULL_LIMIT_M`] (about 9 million degrees), which no + // real data reaches, well before the hull's arithmetic could overflow (near 1e154 m). A + // latitude past ±90° is taken as the pole, as the analysis plane takes it, and counted too. let points = |g: &Geometry| { - let mut these = Vec::new(); - g.for_each_coord(|c| { - let m = rustgis_geom::lonlat_to_plane(c); - these.push(geo::Point::new(m.x, m.y)); + let (mut these, mut gaps, mut poles) = (Vec::new(), 0usize, 0usize); + g.for_each_vertex(|_, c| { + let m = if c.is_finite() { Some(rustgis_geom::lonlat_to_plane(c)) } else { None }; + match m { + Some(m) if m.x.abs() <= HULL_LIMIT_M && m.y.abs() <= HULL_LIMIT_M => { + poles += usize::from(c.y.abs() > 90.0); + these.push(geo::Point::new(m.x, m.y)); + } + _ => gaps += 1, + } }); - these + (these, gaps, poles) }; - // The features with a geometry, each with it. + // Each row once, however often a selection lists it, in the selection's order. + let mut seen = std::collections::HashSet::new(); + let picked: Vec = rows(inp, p, "input", ds).into_iter().filter(|r| seen.insert(*r)).collect(); + // The features with a geometry, each with it; the rest are left out, and counted. let features: Vec<(&Feature, &Geometry)> = - rows(inp, p, "input", ds).into_iter().filter_map(|r| ds.features.get(r)).filter_map(|f| Some((f, f.geometry.as_ref()?))).collect(); - let mut pts: Vec> = Vec::new(); - // Hulls of at least one point that came out with no area. - let mut flat = 0usize; + picked.iter().filter_map(|&r| ds.features.get(r).and_then(|f| Some((f, f.geometry.as_ref()?)))).collect(); + // Features skipped: no geometry, or no point the hull can use. + let mut skipped = picked.len().saturating_sub(features.len()); + // A hull with no area isn't a polygon (its ring would have fewer than the four positions one + // needs): points in one place have a point for their hull, and points on one line the segment + // between the two farthest apart, so it's written as that point or line. Those two are found by + // taking the point farthest from any one of them, then the point farthest from that. + let is_flat = |hull: &geo::Polygon| geo::Area::unsigned_area(hull) == 0.0; + let shape = |hull: geo::Polygon, flat: &mut usize| { + if !is_flat(&hull) { + return from_multipolygon(&MultiPolygon::new(vec![hull])); + } + let ring = &hull.exterior().0; + // (A hull of a point or more always has one; if not, the caller counts the feature skipped.) + ring.first()?; + *flat += 1; + let farthest = |from: geo::Coord| { + ring.iter().copied().fold((from, 0.0), |(best, d), c| { + let dc = (c.x - from.x).hypot(c.y - from.y); + if dc > d { (c, dc) } else { (best, d) } + }) + }; + let first = *ring.first()?; + let (a, _) = farthest(first); + let (b, span) = farthest(a); + let back = |c: geo::Coord| rustgis_geom::plane_to_lonlat(Coord::new(c.x, c.y)); + Some(if span > 0.0 { Geometry::Line(vec![vec![back(a), back(b)]]) } else { Geometry::Point(back(a)) }) + }; + let (mut flat, mut gaps, mut poles) = (0usize, 0usize, 0usize); if each { // Each feature's hull made on several cores for many (#1252), placed in their order. crate::control::begin(features.len()); @@ -2283,33 +2347,83 @@ fn convex_hull(inp: &dyn Inputs, p: &Json) -> Result { PARALLEL_FEATURES, || (), |_, _, (_, g)| { - let these = points(g); - let had = !these.is_empty(); - let hull = geo::MultiPoint::new(these).convex_hull(); - Ok::<_, String>((had && geo::Area::unsigned_area(&hull) == 0.0, hull)) + let (these, passed, past) = points(g); + // A feature with no point to give has no hull: none is written for it. + let hull = (!these.is_empty()).then(|| geo::MultiPoint::new(these).convex_hull()); + Ok::<_, String>((passed, past, hull)) }, )?; - for ((f, _), (is_flat, hull)) in features.iter().zip(hulls) { - flat += usize::from(is_flat); - out.push(from_multipolygon(&MultiPolygon::new(vec![hull])), f.attrs.clone()); + for ((f, _), (passed, past, hull)) in features.iter().zip(hulls) { + gaps += passed; + poles += past; + match hull.and_then(|hull| shape(hull, &mut flat)) { + Some(g) => out.push(Some(g), f.attrs.clone()), + None => skipped += 1, + } } } else { + // All together; `count` is the features hulled, those with a point to give. With none, + // no hull is written, as one feature each. + let (mut pts, mut hulled) = (Vec::new(), 0usize); for (_, g) in &features { - pts.extend(points(g)); + let (these, passed, past) = points(g); + gaps += passed; + poles += past; + if these.is_empty() { + skipped += 1; + } else { + hulled += 1; + } + pts.extend(these); + } + if hulled > 0 { + match shape(geo::MultiPoint::new(pts).convex_hull(), &mut flat) { + Some(g) => out.push(Some(g), vec![Value::Int(i64::try_from(hulled).unwrap_or(i64::MAX))]), + None => skipped += hulled, + } } } - if !each { - let n = ds.features.len() as i64; - let had = !pts.is_empty(); - let hull = geo::MultiPoint::new(pts).convex_hull(); - flat += usize::from(had && geo::Area::unsigned_area(&hull) == 0.0); - out.push(from_multipolygon(&MultiPolygon::new(vec![hull])), vec![Value::Int(n)]); + // The layer's type follows its hulls: points or lines when every hull is one (so a tool + // taking points takes them), polygons otherwise, holding any points and lines among them. + let kinds: Vec = out.features.iter().filter_map(|f| f.geometry.as_ref().map(Geometry::kind)).collect(); + if let Some(&first) = kinds.first() + && first != GeometryType::Polygon + && kinds.iter().all(|k| *k == first) + { + out.geometry_type = first; + } + let out_kind = out.geometry_type; + let gaps_note = (gaps > 0).then(|| { + format!("{gaps} vertex(es) passed over: a coordinate missing or infinite, or so far out that the hull can't take it (past about 9 million degrees)") + }); + let poles_note = (poles > 0).then(|| format!("{poles} vertex(es) with a latitude past a pole found (taken as that pole)")); + // Nothing hulled, every feature skipped: an unusable result, so an error saying why, with the + // vertices it passed over. + if out.features.is_empty() && skipped > 0 { + let why = gaps_note.iter().chain(poles_note.iter()).map(|n| format!("; {n}")).collect::(); + return Err(format!("Minimum Bounding Geometry: none of the {} feature(s) has a point it can hull{why}", picked.len())); } let n = out.features.len(); let mut r = result(out, format!("Minimum Bounding Geometry: {n} hulls"))?; + if skipped > 0 { + r.messages.push(format!("Minimum Bounding Geometry: {skipped} feature(s) skipped: no geometry, or no point it can use")); + } + r.messages.extend(gaps_note.into_iter().chain(poles_note).map(|n| format!("Minimum Bounding Geometry: {n}"))); if flat > 0 { + // Where they go: a layer of their kind when every hull is flat, else the polygon layer, + // where tools taking polygons pass them over. + let layer = match out_kind { + GeometryType::Point => "the output is a point layer".to_string(), + GeometryType::Polyline => "the output is a line layer".to_string(), + // Points and lines, every hull flat: a polygon layer, there being no one kind. + _ if flat >= n => "they are kept in the output's polygon layer, points and lines together, where Dissolve leaves them out and as Clip masks they cover nothing".to_string(), + _ => format!( + "they are kept in the output's polygon layer, among its {} polygon(s), where Dissolve leaves them out and as Clip masks they cover nothing", + n.saturating_sub(flat) + ), + }; r.messages.push(format!( - "Minimum Bounding Geometry: {flat} of {n} hulls have no area: their points lie on one straight line on the map (a single point, or points on one meridian or parallel; beyond 85.05° that includes points around a pole, which enclose a cap on the ground)" + "Minimum Bounding Geometry: {flat} of {n} hulls have no area, so they are written as a point or a line: a single point, or points in one place or on one straight line on the map (on one meridian or parallel, say; beyond 85.05° that includes points around a pole, which enclose a cap on the ground); {layer}" )); } Ok(r) @@ -3690,8 +3804,8 @@ fn kernel_density_tool(inp: &dyn Inputs, p: &Json) -> Result // which would land on the map's edge rather than where it is (#1033): its latitude doesn't // come back. let mut beyond: usize = 0; - // A multipoint's members with non-finite coordinates, in the features whose other members - // were used (a feature with none usable is counted in `skipped`). + // A multipoint's members with non-finite coordinates, in the features with finite ones (a + // feature with none is counted in `skipped`). let (mut non_finite, mut non_finite_features) = (0usize, 0usize); let project = |c: Coord, beyond: &mut usize| { // Web Mercator's limit is known exactly: the round trip alone would let a latitude a hair @@ -3729,18 +3843,20 @@ fn kernel_density_tool(inp: &dyn Inputs, p: &Json) -> Result } } Geometry::MultiPoint(cs) => { - let (mut any, mut bad) = (false, 0usize); - for c in cs { - if !c.is_finite() { - bad += 1; - } else if let Some(p_proj) = project(*c, &mut beyond) { + // As for a point: a feature with no finite member is skipped, and a member the + // system can't hold counts as beyond (not as skipped too); a non-finite member of a + // feature with finite ones is left out and counted on its own. + let bad = cs.iter().filter(|c| !c.is_finite()).count(); + if bad == cs.len() { + skipped += 1; + continue; + } + for c in cs.iter().filter(|c| c.is_finite()) { + if let Some(p_proj) = project(*c, &mut beyond) { pts.push((p_proj, w)); - any = true; } } - if !any { - skipped += 1; - } else if bad > 0 { + if bad > 0 { non_finite = non_finite.saturating_add(bad); non_finite_features += 1; } @@ -3755,11 +3871,14 @@ fn kernel_density_tool(inp: &dyn Inputs, p: &Json) -> Result let why = if crs == Crs::WebMercator { " (past 85.05° of latitude)" } else { "" }; let beyond_note = (beyond > 0).then(|| format!("{beyond} point(s) beyond what {} can hold{why} were left out", crs.label())); let non_finite_note = (non_finite > 0).then(|| { - format!("{non_finite} point(s) with non-finite coordinates were left out of {non_finite_features} multipoint feature(s) whose other points were used") + format!("{non_finite} point(s) with non-finite coordinates were left out of {non_finite_features} multipoint feature(s) that have other points") }); if pts.is_empty() { let none = "input layer has no valid points with positive weights to analyze"; - return Err(beyond_note.map_or_else(|| none.to_string(), |note| format!("{none}: {note}"))); + let skipped_note = (skipped > 0) + .then(|| format!("{skipped} feature(s) skipped (no point geometry, a weight that isn't positive, or no finite coordinate)")); + let notes: Vec<&str> = skipped_note.iter().chain(beyond_note.iter()).chain(non_finite_note.iter()).map(String::as_str).collect(); + return Err(if notes.is_empty() { none.to_string() } else { format!("{none}: {}", notes.join("; ")) }); } let pt_extent = crate::density::points_extent(&pts).ok_or("input layer has no valid points with positive weights to analyze")?; @@ -6666,6 +6785,49 @@ mod tests { } } + /// Dissolve says how many features it left out for having no polygon: a point and a line in a + /// polygon layer (as Minimum Bounding Geometry's flat hulls are), an empty polygon and a + /// feature without geometry. Those in a group add nothing to its count or statistics. A layer + /// of polygons says nothing of the kind. + #[test] + fn dissolve_counts_features_with_no_polygon() { + let fields = vec![Field::new("k", FieldType::Integer), Field::new("v", FieldType::Double)]; + let notice = |extra: bool| { + let mut polys = Dataset::new("polys", GeometryType::Polygon, fields.clone()); + polys.push(Some(sq(0.0, 0.0, 1.0)), vec![Value::Int(1), Value::Float(1.0)]); + polys.push(Some(sq(3.0, 0.0, 1.0)), vec![Value::Int(1), Value::Float(2.0)]); + if extra { + // In group 1 with the polygons, with values that would show in its sum. + polys.push(Some(Geometry::Point(Coord::new(5.0, 5.0))), vec![Value::Int(1), Value::Float(100.0)]); + polys.push(Some(Geometry::Polygon(vec![])), vec![Value::Int(1), Value::Float(1000.0)]); + polys.push(Some(Geometry::Line(vec![vec![Coord::new(0.0, 5.0), Coord::new(1.0, 5.0)]])), vec![Value::Int(2), Value::Null]); + polys.push(None, vec![Value::Int(3), Value::Null]); + } + let m = Map(HashMap::from([("polys".to_string(), polys)])); + let params = serde_json::json!({"input": "polys", "field": "k", "statistics": [{"field": "v", "statistic": "SUM"}]}); + let res = crate::run("dissolve", &m, ¶ms).unwrap(); + let note = res.messages.iter().find(|m| m.contains("left out")).cloned(); + let out = layer(res); + (out.features.len(), out.value(0, 1).as_f64(), out.value(0, 2).as_f64(), note) + }; + let (groups, count, sum, note) = notice(true); + assert_eq!((groups, count, sum), (1, Some(2.0), Some(3.0))); + assert_eq!( + note.as_deref(), + Some( + "Dissolve: 4 feature(s) left out, with their values: no geometry, an empty polygon, or a point or line rather than a polygon" + ) + ); + assert_eq!(notice(false), (1, Some(2.0), Some(3.0), None)); + // None with a polygon: an error saying why, not an empty layer. + let mut flat = Dataset::new("flat", GeometryType::Polygon, vec![]); + flat.push(Some(Geometry::Point(Coord::new(5.0, 5.0))), vec![]); + flat.push(None, vec![]); + let m = Map(HashMap::from([("flat".to_string(), flat)])); + let e = crate::run("dissolve", &m, &serde_json::json!({"input": "flat"})).err().unwrap_or_default(); + assert!(e.contains("none of the 2 feature(s) has a polygon"), "{e}"); + } + /// Dissolve's groups, found by their keys' hash: in the order first met, −0 with 0, each NaN /// on its own (it equals no value), and a number apart from the same text and a whole number /// apart from the same fraction. @@ -8552,7 +8714,8 @@ mod tests { /// Minimum Bounding Geometry says how many hulls have no area: points around a pole on one /// parallel (one line in the analysis plane), all together or one feature each, and points - /// spread over an area say nothing of the kind. + /// spread over an area say nothing of the kind. Single points, one feature each, and points + /// stacked in one place have hulls with no area too, and say so. #[test] fn convex_hull_counts_hulls_with_no_area() { let layer = |coords: &[(f64, f64)], multi: bool| { @@ -8575,12 +8738,167 @@ mod tests { let polar = [(0.0, 89.0), (90.0, 89.0), (180.0, 89.0), (-90.0, 89.0)]; let note_all = note(&layer(&polar, false), "All").unwrap_or_default(); assert!(note_all.contains(": 1 of 1 hulls have no area"), "{note_all}"); - // One feature each: the polar multipoint's hull is flat, the other's isn't. + // Every hull flat, and a line: the notice says the output is a line layer. + assert!(note_all.ends_with("; the output is a line layer"), "{note_all}"); + // One feature each: the polar multipoint's hull is flat, the other's isn't, so the flat one + // stays in the polygon layer, and the notice says where and what polygon tools do with it. let note_each = note(&layer(&polar, true), "None").unwrap_or_default(); assert!(note_each.contains(": 1 of 2 hulls have no area"), "{note_each}"); + assert!(note_each.contains("kept in the output's polygon layer, among its 1 polygon(s)"), "{note_each}"); + // Every hull flat, a point and a line: no one kind, so a polygon layer, said as such. + let mut both = Dataset::new("pts", GeometryType::Point, vec![]); + both.push(Some(Geometry::Point(Coord::new(10.0, 20.0))), vec![]); + both.push(Some(Geometry::MultiPoint(vec![Coord::new(0.0, 0.0), Coord::new(5.0, 0.0)])), vec![]); + let note_both = note(&Map(HashMap::from([("pts".to_string(), both)])), "None").unwrap_or_default(); + assert!(note_both.contains(": 2 of 2 hulls have no area"), "{note_both}"); + assert!(note_both.contains("polygon layer, points and lines together"), "{note_both}"); let spread = [(0.0, 60.0), (10.0, 60.0), (5.0, 65.0), (0.0, 89.0), (90.0, 88.0)]; assert_eq!(note(&layer(&spread, false), "All"), None); assert_eq!(note(&layer(&spread, true), "None"), None); + let note_single = note(&layer(&spread, false), "None").unwrap_or_default(); + assert!( + note_single.contains(": 5 of 5 hulls have no area, so they are written as a point or a line: a single point"), + "{note_single}" + ); + assert!(note_single.ends_with("; the output is a point layer"), "{note_single}"); + // Features stacked in one place, together, have a hull with no area, and say so. + let stacked = [(10.0, 20.0), (10.0, 20.0), (10.0, 20.0)]; + let note_stacked = note(&layer(&stacked, false), "All").unwrap_or_default(); + assert!(note_stacked.contains(": 1 of 1 hulls have no area"), "{note_stacked}"); + // A hull with no area is written as what it is (its ring would have fewer than the four + // positions a polygon needs): a point for points in one place, the segment between the + // farthest two for points on one line. A hull with area keeps a closed ring. + let geoms = |m: &Map, group: &str| { + let res = crate::run("convexHull", m, &serde_json::json!({"input": "pts", "group": group})).unwrap_or_else(|e| panic!("{e}")); + let Output::Layer(out) = res.output else { panic!("not a layer") }; + out.features.into_iter().map(|f| f.geometry).collect::>() + }; + let singles = geoms(&layer(&spread, false), "None"); + assert!(singles.len() == 5 && singles.iter().all(|g| matches!(g, Some(Geometry::Point(_)))), "{singles:?}"); + // The layer's type follows its hulls: all points, a point layer; all lines, a line layer; + // a mix, a polygon layer. + let kind = |m: &Map, group: &str| { + let res = crate::run("convexHull", m, &serde_json::json!({"input": "pts", "group": group})).unwrap_or_else(|e| panic!("{e}")); + let Output::Layer(out) = res.output else { panic!("not a layer") }; + out.geometry_type + }; + assert_eq!(kind(&layer(&spread, false), "None"), GeometryType::Point); + assert_eq!(kind(&layer(&polar, false), "All"), GeometryType::Polyline); + assert_eq!(kind(&layer(&polar, true), "None"), GeometryType::Polygon); + assert_eq!(kind(&layer(&spread, false), "All"), GeometryType::Polygon); + let near = |a: Coord, b: (f64, f64)| (a.x - b.0).abs() < 1e-9 && (a.y - b.1).abs() < 1e-9; + assert!(matches!(geoms(&layer(&stacked, false), "All").first(), Some(Some(Geometry::Point(c))) if near(*c, (10.0, 20.0)))); + // The polar points on 89°N, as the analysis plane draws them: from 90°W to 180°. + let each = geoms(&layer(&polar, true), "None"); + let ends = match each.first() { + Some(Some(Geometry::Line(parts))) => parts.first().cloned().unwrap_or_default(), + _ => vec![], + }; + let mut lons: Vec = ends.iter().map(|c| c.x).collect(); + lons.sort_by(f64::total_cmp); + assert!(ends.len() == 2 && near(Coord::new(lons[0], ends[0].y), (-90.0, 89.0)) && (lons[1] - 180.0).abs() < 1e-9, "{each:?}"); + let ring_len = |g: &Option| match g { + Some(Geometry::Polygon(p)) => p.first().and_then(|rings| rings.first()).map_or(0, Vec::len), + _ => 0, + }; + assert!(each.get(1).map_or(0, ring_len) >= 4, "{each:?}"); + } + + /// Minimum Bounding Geometry passes over a vertex with a coordinate missing or infinite, which + /// the hull can't order (it failed on one before), or so far out that its arithmetic would + /// overflow, and skips a feature without geometry or with no point it can use (writing no hull + /// for it), saying how many of each, all together or one feature each; with nothing left to + /// enclose, it is an error saying why. A latitude past a pole is taken as that pole, and says + /// so. A bad vertex in a closed ring counts once, a repeated NaN point included. Clean points + /// say nothing of the kind. + #[test] + fn convex_hull_passes_over_missing_coordinates() { + let run = |geoms: Vec>, group: &str| { + let mut pts = Dataset::new("pts", GeometryType::Point, vec![]); + for g in geoms { + pts.push(g, vec![]); + } + let m = Map(HashMap::from([("pts".to_string(), pts)])); + crate::run("convexHull", &m, &serde_json::json!({"input": "pts", "group": group})) + }; + let (gap, far) = (Coord::new(f64::NAN, 40.0), Coord::new(1e308, 0.0)); + let stations = Geometry::MultiPoint(vec![Coord::new(-75.0, 40.0), Coord::new(-74.99, 40.0), Coord::new(-75.0, 40.01), gap, far]); + for group in ["All", "None"] { + let res = run(vec![Some(stations.clone()), Some(Geometry::Point(gap)), None], group).unwrap_or_else(|e| panic!("{group}: {e}")); + let all = res.messages.join(" | "); + assert!( + all.contains( + "3 vertex(es) passed over: a coordinate missing or infinite, or so far out that the hull can't take it (past about 9 million degrees)" + ), + "{group}: {all}" + ); + assert!(all.contains("2 feature(s) skipped: no geometry, or no point it can use"), "{group}: {all}"); + let Output::Layer(out) = res.output else { panic!("{group}: not a layer") }; + assert_eq!(out.features.len(), 1, "{group}: a hull for the stations only"); + let clean = run(vec![Some(Geometry::Point(Coord::new(0.0, 0.0))), Some(Geometry::Point(Coord::new(1.0, 1.0)))], group) + .unwrap_or_else(|e| panic!("{group}: {e}")) + .messages + .join(" | "); + assert!(!clean.contains("passed over") && !clean.contains("skipped"), "{group}: {clean}"); + } + for group in ["All", "None"] { + let err = run(vec![Some(Geometry::Point(gap)), None], group).err().unwrap_or_default(); + assert!(err.contains("none of the 2 feature(s) has a point it can hull; 1 vertex(es) passed over"), "{group}: {err}"); + } + // A ring with one vertex too far out, its closing point the same: one vertex passed over. + let far_ring = Geometry::Polygon(vec![vec![vec![ + Coord::new(1e15, 0.0), + Coord::new(1.0, 0.0), + Coord::new(1.0, 1.0), + Coord::new(0.0, 1.0), + Coord::new(1e15, 0.0), + ]]]); + let all = run(vec![Some(far_ring)], "All").unwrap_or_else(|e| panic!("{e}")).messages.join(" | "); + assert!(all.contains(": 1 vertex(es) passed over"), "{all}"); + // A ring that repeats its NaN start point as its end: that vertex is passed over once. + let nan_ring = Geometry::Polygon(vec![vec![vec![gap, Coord::new(1.0, 0.0), Coord::new(1.0, 1.0), Coord::new(0.0, 1.0), gap]]]); + let all = run(vec![Some(nan_ring)], "All").unwrap_or_else(|e| panic!("{e}")).messages.join(" | "); + assert!(all.contains(": 1 vertex(es) passed over"), "{all}"); + // A latitude past a pole is taken as that pole, and says so; a pole's own latitude says nothing. + let past = run(vec![Some(Geometry::Point(Coord::new(10.0, 95.0)))], "All").unwrap_or_else(|e| panic!("{e}")).messages.join(" | "); + assert!(past.contains("1 vertex(es) with a latitude past a pole found (taken as that pole)"), "{past}"); + let at_pole = run(vec![Some(Geometry::Point(Coord::new(10.0, 90.0)))], "All").unwrap_or_else(|e| panic!("{e}")); + assert!(!at_pole.messages.join(" | ").contains("past a pole"), "{:?}", at_pole.messages); + } + + /// Minimum Bounding Geometry's `count`, all features together, is the features it hulled: with a + /// selection, the selected ones, and never a feature without geometry. + #[test] + fn convex_hull_counts_the_features_it_hulls() { + let mut pts = Dataset::new("pts", GeometryType::Point, vec![]); + for c in [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (5.0, 5.0)] { + pts.push(Some(Geometry::Point(Coord::new(c.0, c.1))), vec![]); + } + pts.push(None, vec![]); + // A feature whose only point is missing a coordinate gives the hull nothing either. + pts.push(Some(Geometry::Point(Coord::new(f64::NAN, 0.0))), vec![]); + let count = |sel: Vec| { + let m = Selected(Map(HashMap::from([("pts".to_string(), pts.clone())])), HashMap::from([("pts".to_string(), sel)])); + let out = layer(crate::run("convexHull", &m, &serde_json::json!({"input": "pts"})).unwrap_or_else(|e| panic!("{e}"))); + out.value(0, 0).as_f64() + }; + // No selection: every feature with a usable point (not the one without geometry, nor the + // one whose point is missing a coordinate). + assert_eq!(count(vec![]), Some(4.0)); + assert_eq!(count(vec![0, 5]), Some(1.0)); + assert_eq!(count(vec![0, 1, 2]), Some(3.0)); + // A row a selection lists twice is one feature. + assert_eq!(count(vec![0, 0, 1]), Some(2.0)); + // One feature each, the hulls follow the selection's order, each row once. + let mut named = Dataset::new("pts", GeometryType::Point, vec![Field::new("name", FieldType::Text)]); + for (name, x) in [("a", 0.0), ("b", 1.0), ("c", 2.0)] { + named.push(Some(Geometry::Point(Coord::new(x, 0.0))), vec![Value::Text(name.into())]); + } + let m = Selected(Map(HashMap::from([("pts".to_string(), named)])), HashMap::from([("pts".to_string(), vec![2, 0, 2])])); + let out = + layer(crate::run("convexHull", &m, &serde_json::json!({"input": "pts", "group": "None"})).unwrap_or_else(|e| panic!("{e}"))); + let names: Vec = (0..out.features.len()).map(|r| out.value(r, 0).to_string()).collect(); + assert_eq!(names, ["c", "a"]); } /// Buffer says how many features' buffers kept edges coarser than their outline allows: a line @@ -8858,6 +9176,38 @@ mod tests { assert!(note.starts_with("1 point(s) beyond what") && !note.contains("85.05°"), "{:?}", res.messages); } + /// Kernel Density skips a multipoint with no finite member; one wholly past 85.05° counts its + /// points as beyond, not as skipped too, as a point does. With nothing left to place, the + /// error names every cause. Whole multipoints say nothing of the kind. + #[test] + fn kernel_density_counts_a_multipoint_beyond_once() { + let run = |members: Vec>| { + let mut pts = Dataset::new("pts", GeometryType::Point, vec![]); + pts.push(Some(Geometry::MultiPoint(vec![Coord::new(0.0, 60.0), Coord::new(0.01, 60.0)])), vec![]); + for m in members { + pts.push(Some(Geometry::MultiPoint(m)), vec![]); + } + let m = Map(HashMap::from([("pts".to_string(), pts)])); + let params = serde_json::json!({"input": "pts", "crs": "EPSG:3857", "radius": 5000.0, "pixelSize": 1000.0}); + crate::run("kernelDensity", &m, ¶ms).unwrap_or_else(|e| panic!("{e}")).messages + }; + let (gap, inf) = (Coord::new(f64::NAN, 60.0), Coord::new(0.0, f64::INFINITY)); + let messages = run(vec![vec![Coord::new(0.02, 60.0), gap, inf], vec![gap], vec![Coord::new(0.0, 87.0), Coord::new(1.0, 88.0)]]); + let all = messages.join(" | "); + assert!(all.contains("2 point(s) with non-finite coordinates were left out of 1 multipoint feature(s)"), "{all}"); + assert!(all.contains("from 3 points (1 skipped"), "{all}"); + assert!(all.contains("2 point(s) beyond what"), "{all}"); + let clean = run(vec![vec![Coord::new(0.02, 60.0), Coord::new(0.03, 60.0)]]).join(" | "); + assert!(!clean.contains("left out") && !clean.contains("skipped"), "{clean}"); + // With nothing left to place, the error says both why. + let mut only = Dataset::new("only", GeometryType::Point, vec![]); + only.push(Some(Geometry::MultiPoint(vec![gap, Coord::new(0.0, 87.0)])), vec![]); + let m = Map(HashMap::from([("only".to_string(), only)])); + let params = serde_json::json!({"input": "only", "crs": "EPSG:3857", "radius": 5000.0, "pixelSize": 1000.0}); + let err = crate::run("kernelDensity", &m, ¶ms).err().unwrap_or_default(); + assert!(err.contains("1 point(s) beyond what") && err.contains("1 point(s) with non-finite coordinates were left out"), "{err}"); + } + /// A multipoint's non-finite members are left out and counted when its other members are /// used; a clean multipoint says nothing of the kind. #[test] diff --git a/crates/data/src/export.rs b/crates/data/src/export.rs index a4f8428e4e..40fad08759 100644 --- a/crates/data/src/export.rs +++ b/crates/data/src/export.rs @@ -116,6 +116,7 @@ fn write_chunks_to( let left_out = match ext.as_str() { #[cfg(not(target_arch = "wasm32"))] "gpkg" => { + // Every feature is written: one of another kind makes the column GEOMETRY. table = Some(crate::source::gpkg::write_features_in(&layer.name, geometry_type, &layer.fields, path, crs, gpkg, &mut feed)?); 0 } diff --git a/crates/data/src/export/tests.rs b/crates/data/src/export/tests.rs index 3fc019f4b2..b7e05cd1a6 100644 --- a/crates/data/src/export/tests.rs +++ b/crates/data/src/export/tests.rs @@ -370,3 +370,138 @@ fn geoparquet_counts_values_written_as_null() { assert_eq!(note, format!("{want}, '{}…'", "x".repeat(40))); let _ = std::fs::remove_dir_all(d); } + +/// A streamed GeoPackage export of a layer holding another kind of geometry than its own writes +/// every feature in a GEOMETRY column, as the export of the whole layer does, even when the +/// other kind comes only in a late chunk (the export starts again for it): squares with points +/// among them, a few features a chunk, give the same table. +#[test] +fn a_streamed_geopackage_of_a_mixed_layer_keeps_every_kind() { + let d = folder("mixed-kinds"); + let mut mixed = squares(); + mixed.name = "mixed".into(); + for i in [0usize, 5, 6, 299] { + if let Some(f) = mixed.features.get_mut(i) { + f.geometry = Some(Geometry::Point(Coord::new(i as f64, 1.0))); + } + } + let layer = Dataset::new(mixed.name.clone(), mixed.geometry_type, mixed.fields.clone()); + let (a, b) = (d.join("streamed.gpkg"), d.join("whole.gpkg")); + let streamed = write_chunks_in(&Runs::new(&mixed, 4), &layer, &a, Crs::LonLat, 1).unwrap(); + assert_eq!((streamed.left_out, streamed.written), (0, 301)); + assert_eq!(crate::io::write_path(&mixed, &b).unwrap(), 0); + let table = |p: &Path| { + let c = rusqlite::Connection::open(p).unwrap(); + let kind = c.query_row("SELECT geometry_type_name FROM gpkg_geometry_columns", [], |r| r.get::<_, String>(0)).unwrap(); + let blobs = c + .query_row("SELECT count(*) FROM mixed WHERE geom IS NOT NULL AND substr(hex(geom), 1, 4) = '4750'", [], |r| r.get::<_, i64>(0)) + .unwrap(); + (kind, blobs) + }; + // Every geometry row is a GeoPackage blob: the 296 squares and the 4 points. + assert_eq!(table(&a), ("GEOMETRY".to_string(), 300)); + assert_eq!(table(&b), ("GEOMETRY".to_string(), 300)); + assert_eq!(read_back(&a).features, read_back(&b).features); + let points = read_back(&a).features.iter().filter(|f| matches!(f.geometry, Some(Geometry::Point(_)))).count(); + assert_eq!(points, 4); + // A multipoint among them comes back a multipoint, with its members, streamed and whole. + let pair = Geometry::MultiPoint(vec![Coord::new(1.5, 2.5), Coord::new(3.5, 4.5)]); + if let Some(f) = mixed.features.get_mut(10) { + f.geometry = Some(pair.clone()); + } + let (c, e) = (d.join("streamed-multi.gpkg"), d.join("whole-multi.gpkg")); + write_chunks_in(&Runs::new(&mixed, 4), &layer, &c, Crs::LonLat, 1).unwrap(); + crate::io::write_path(&mixed, &e).unwrap(); + for p in [&c, &e] { + let back = read_back(p); + let multis: Vec<_> = + back.features.iter().filter_map(|f| f.geometry.as_ref()).filter(|g| matches!(g, Geometry::MultiPoint(_))).collect(); + assert_eq!(multis, [&pair], "{}", p.display()); + } +} + +/// A layer that declares no geometry type but carries geometry exports to the same GeoPackage +/// streamed as whole: a table of its geometry's kind when it holds one, a GEOMETRY table when it +/// holds several, nothing left out either way. +#[test] +fn a_typeless_layer_exports_the_same_geopackage_streamed_or_whole() { + let d = folder("typeless"); + let mut ds = squares(); + ds.name = "typeless".into(); + ds.geometry_type = GeometryType::None; + let kind = |p: &Path| { + let c = rusqlite::Connection::open(p).unwrap(); + c.query_row("SELECT geometry_type_name FROM gpkg_geometry_columns", [], |r| r.get::<_, String>(0)).unwrap() + }; + let layer = Dataset::new(ds.name.clone(), ds.geometry_type, ds.fields.clone()); + for (with_point, want) in [(false, "MULTIPOLYGON"), (true, "GEOMETRY")] { + if with_point && let Some(f) = ds.features.get_mut(3) { + f.geometry = Some(Geometry::Point(Coord::new(3.0, 1.0))); + } + let (a, b) = (d.join(format!("streamed-{with_point}.gpkg")), d.join(format!("whole-{with_point}.gpkg"))); + assert_eq!(write_chunks_in(&Runs::new(&ds, 4), &layer, &a, Crs::LonLat, 1).unwrap().left_out, 0); + assert_eq!(crate::io::write_path(&ds, &b).unwrap(), 0); + assert_eq!((kind(&a), kind(&b)), (want.to_string(), want.to_string())); + assert_eq!(read_back(&a).features, read_back(&b).features); + assert_eq!(read_back(&a).features.len(), 300); + } +} + +/// A projected streamed GeoPackage export of a mixed layer writes every feature, projected, in a +/// GEOMETRY column, as the whole export does. +#[test] +fn a_projected_streamed_geopackage_of_a_mixed_layer_keeps_every_kind() { + let d = folder("mixed-projected"); + let mut mixed = squares(); + mixed.name = "mixed".into(); + for i in [2usize, 7] { + if let Some(f) = mixed.features.get_mut(i) { + f.geometry = Some(Geometry::Point(Coord::new(3.0, 1.0))); + } + } + let utm = Crs::from_epsg(32631).unwrap(); + let layer = Dataset::new(mixed.name.clone(), mixed.geometry_type, mixed.fields.clone()); + let (a, b) = (d.join("streamed.gpkg"), d.join("whole.gpkg")); + let streamed = write_chunks_in(&Runs::new(&mixed, 4), &layer, &a, utm, 1).unwrap(); + assert_eq!((streamed.left_out, streamed.written), (0, 301)); + assert_eq!(crate::io::write_path_in(&mixed, &b, utm).unwrap(), 0); + assert_eq!(read_back(&a).features, read_back(&b).features); + assert_eq!(read_back(&a).features.len(), 300); +} + +/// A mixed layer added as a table to an existing GeoPackage, streamed or whole, gets a GEOMETRY +/// column holding every feature, the file's other table left as it was. +#[test] +fn a_mixed_layer_added_to_a_geopackage_keeps_every_kind() { + let d = folder("mixed-added"); + let mut mixed = squares(); + mixed.name = "mixed".into(); + for i in [1usize, 298] { + if let Some(f) = mixed.features.get_mut(i) { + f.geometry = Some(Geometry::Point(Coord::new(i as f64, 1.0))); + } + } + let layer = Dataset::new(mixed.name.clone(), mixed.geometry_type, mixed.fields.clone()); + let add = GpkgTable { mode: crate::io::GpkgMode::AddTable, name: None }; + let mut other = squares(); + other.name = "other".into(); + other.features.truncate(3); + for (i, streamed) in [false, true].into_iter().enumerate() { + let path = d.join(format!("county-{i}.gpkg")); + crate::io::write_path(&other, &path).unwrap(); + if streamed { + let (e, _) = write_chunks_to(&Runs::new(&mixed, 4), &layer, &path, Crs::LonLat, &add, 1).unwrap(); + assert_eq!((e.left_out, e.written), (0, 301)); + } else { + assert_eq!(crate::io::write_path_to(&mixed, &path, Crs::LonLat, &add).unwrap().0, 0); + } + let c = rusqlite::Connection::open(&path).unwrap(); + let kind = |t: &str| { + c.query_row("SELECT geometry_type_name FROM gpkg_geometry_columns WHERE table_name = ?1", [t], |r| r.get::<_, String>(0)) + .unwrap() + }; + assert_eq!((kind("mixed"), kind("other")), ("GEOMETRY".to_string(), "MULTIPOLYGON".to_string())); + let rows: i64 = c.query_row("SELECT count(*) FROM mixed WHERE geom IS NOT NULL", [], |r| r.get(0)).unwrap(); + assert_eq!(rows, 300); + } +} diff --git a/crates/data/src/geometry.rs b/crates/data/src/geometry.rs index ed09623c68..77b399638f 100644 --- a/crates/data/src/geometry.rs +++ b/crates/data/src/geometry.rs @@ -334,11 +334,19 @@ impl VertexId { /// How many vertices a path has: a closed ring's repeated last point doesn't count. fn open_len(path: &[Coord], ring: bool) -> usize { match (path.first(), path.last()) { - (Some(a), Some(b)) if ring && path.len() > 1 && a == b => path.len() - 1, + (Some(a), Some(b)) if ring && path.len() > 1 && same_point(a, b) => path.len() - 1, _ => path.len(), } } +/// Whether two points are the same place. A NaN coordinate equals nothing as a number, so NaN is +/// matched by its bits: a ring that repeats a NaN point closes on it. +fn same_point(a: &Coord, b: &Coord) -> bool { + // Each coordinate on its own: equal as numbers (so 0 and −0 match), or a NaN by its bits. + let same = |p: f64, q: f64| p == q || p.to_bits() == q.to_bits(); + same(a.x, b.x) && same(a.y, b.y) +} + impl Geometry { /// Calls `f` with each path that vertices lie on: its part and ring, and whether it is a polygon /// ring. Nothing is allocated, so a geometry of many parts costs no memory to walk. @@ -762,6 +770,30 @@ mod tests { assert!(polygon_contains(&[square(0.0, 0.0, 10.0), square(4.0, 4.0, 2.0)], lp)); } + /// A polygon ring that starts and ends on the same NaN point is closed on it (NaN equals + /// nothing as a number, so it's matched by its bits): its last point isn't a vertex of its own, + /// so every ring reader sees four vertices and four edges, and deleting the first vertex keeps + /// the ring closed on the next one. A line with the same ends isn't a ring: all three count. + #[test] + fn a_ring_closed_on_a_nan_point_counts_it_once() { + let gap = Coord::new(f64::NAN, 0.0); + let ring = vec![gap, Coord::new(1.0, 0.0), Coord::new(1.0, 1.0), Coord::new(0.0, 1.0), gap]; + let g = Geometry::Polygon(vec![vec![ring]]); + assert_eq!((g.vertices().len(), g.edges().len()), (4, 4)); + let Ok(Geometry::Polygon(after)) = g.delete_vertex(VertexId { part: 0, ring: 0, index: 0 }) else { panic!("not deleted") }; + let ring = after.first().and_then(|rings| rings.first()).cloned().unwrap_or_default(); + assert_eq!(ring.len(), 4); + assert_eq!((ring.first(), ring.last()), (Some(&Coord::new(1.0, 0.0)), Some(&Coord::new(1.0, 0.0)))); + let line = Geometry::Line(vec![vec![gap, Coord::new(1.0, 0.0), gap]]); + assert_eq!(line.vertices().len(), 3); + // The same NaN point written once with 0 and once with −0 still closes the ring. + let signed = vec![Coord::new(f64::NAN, 0.0), Coord::new(1.0, 0.0), Coord::new(1.0, 1.0), Coord::new(f64::NAN, -0.0)]; + assert_eq!(Geometry::Polygon(vec![vec![signed]]).vertices().len(), 3); + // And the other way round: a signed zero in x, the NaN in y. + let mirrored = vec![Coord::new(0.0, f64::NAN), Coord::new(1.0, 0.0), Coord::new(1.0, 1.0), Coord::new(-0.0, f64::NAN)]; + assert_eq!(Geometry::Polygon(vec![vec![mirrored]]).vertices().len(), 3); + } + #[test] fn line_hit_and_box() { let g = Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(10.0, 0.0)]]); diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 685c329a83..c0797ba5ec 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -546,6 +546,8 @@ pub fn write_path_to(ds: &Dataset, path: &Path, crs: Crs, gpkg: &GpkgTable) -> R #[cfg(not(target_arch = "wasm32"))] { crate::progress::begin(ds.features.len()); + // Every feature is written: a layer of several kinds of geometry gets a GEOMETRY + // column, and one that declares no type takes its geometry's. let table = crate::source::gpkg::write_table_in(ds, path, crs, gpkg)?; Ok((0, Some(table))) } @@ -2516,6 +2518,20 @@ fn dbf_boolean(value: &Value) -> Option { } } +/// The notice for the `left_out` features an export to `name` (a file of extension `ext`) left +/// out, as [`write_path_in`] and [`write_bytes_in`] count them; `None` when none were. A +/// Shapefile export (`.shp`, or zipped) writes only drawable geometry of the layer's one kind (it +/// writes no null shapes, though the format has them); FlatGeobuf, KML and GPX exports write only +/// features with geometry; the other formats leave nothing out. +pub fn left_out_note(name: &str, ext: &str, left_out: usize) -> Option { + (left_out > 0).then(|| match ext { + "shp" | "zip" => format!( + "{name}: {left_out} features left out of the Shapefile: without drawable geometry, or with geometry of another kind than the layer's" + ), + _ => format!("{name}: {left_out} features without drawable geometry were left out"), + }) +} + /// Whether [`shapefile_parts_in`] writes a feature with geometry `g` to a layer of type `gt`: one /// of the layer's kind with something drawable (a finite point, a line part of two finite points, /// a polygon whose outer ring has three). @@ -2682,6 +2698,102 @@ mod tests { use super::*; use std::io::Write; + /// A Shapefile export of a polygon layer holding a point and a feature without geometry writes + /// the polygon and counts both left out, the point for its kind; a layer of polygons loses none. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_shapefile_counts_geometry_of_another_kind_left_out() -> std::result::Result<(), Box> { + let geojson = r#"{"type":"FeatureCollection","features":[ + {"type":"Feature","properties":{"name":"area"},"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,0]]]}}, + {"type":"Feature","properties":{"name":"spot"},"geometry":{"type":"Point","coordinates":[5,5]}}, + {"type":"Feature","properties":{"name":"none"},"geometry":null}]}"#; + let (mixed, _) = read_geojson(geojson, "mixed")?; + let dir = tempfile::tempdir()?; + assert_eq!(write_path(&mixed, &dir.path().join("mixed.shp"))?, 2); + let (back, _) = read_path(&dir.path().join("mixed.shp"))?; + assert_eq!(back.features.len(), 1); + let mut areas = mixed.clone(); + areas.features.truncate(1); + assert_eq!(write_path(&areas, &dir.path().join("areas.shp"))?, 0); + Ok(()) + } + + /// The left-out notice names what each format can't hold: a Shapefile, features without + /// drawable geometry or of another kind; the others, features without drawable geometry. + /// Nothing left out, no notice. + #[test] + fn left_out_notes_say_what_the_format_cant_hold() { + for ext in ["shp", "zip"] { + assert_eq!( + left_out_note("out", ext, 2).as_deref(), + Some( + "out: 2 features left out of the Shapefile: without drawable geometry, or with geometry of another kind than the layer's" + ) + ); + } + for ext in ["fgb", "kml", "gpx"] { + assert_eq!(left_out_note("out", ext, 1).as_deref(), Some("out: 1 features without drawable geometry were left out")); + } + assert_eq!(left_out_note("out", "shp", 0), None); + assert_eq!(left_out_note("out", "gpkg", 0), None); + } + + /// A GeoPackage export loses no geometry: a layer read from a GeoJSON of a polygon and a point + /// (declared a polygon layer, by its most common kind) gets a GEOMETRY column holding both, and + /// a feature without geometry stays, as a table holds it. A layer of one kind keeps its kind's + /// column, whether it declares its type or not. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_geopackage_of_a_mixed_layer_keeps_every_kind() -> std::result::Result<(), Box> { + let geojson = r#"{"type":"FeatureCollection","features":[ + {"type":"Feature","properties":{"name":"area"},"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,0]]]}}, + {"type":"Feature","properties":{"name":"spot"},"geometry":{"type":"Point","coordinates":[5,5]}}, + {"type":"Feature","properties":{"name":"none"},"geometry":null}]}"#; + let (mixed, _) = read_geojson(geojson, "mixed")?; + assert_eq!(mixed.geometry_type, GeometryType::Polygon); + let dir = tempfile::tempdir()?; + // The column's type, and the rows written with whether each has geometry, read with SQL. + let table = |path: &Path| -> std::result::Result<(String, Vec<(String, bool)>), Box> { + let conn = rusqlite::Connection::open(path)?; + let kind: String = conn.query_row("SELECT geometry_type_name FROM gpkg_geometry_columns", [], |r| r.get(0))?; + let mut rows = conn.prepare(r#"SELECT "name", "geom" IS NOT NULL FROM "mixed" ORDER BY "fid""#)?; + let written = rows.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?.collect::>()?; + Ok((kind, written)) + }; + let all = vec![("area".to_string(), true), ("spot".to_string(), true), ("none".to_string(), false)]; + let path = dir.path().join("mixed.gpkg"); + assert_eq!(write_path(&mixed, &path)?, 0); + assert_eq!(table(&path)?, ("GEOMETRY".to_string(), all.clone())); + // Read back, the point and the polygon are both there. + let (back, _) = read_path(&path)?; + let mut kinds: Vec = back.features.iter().filter_map(|f| f.geometry.as_ref().map(Geometry::kind)).collect(); + kinds.sort_by_key(|k| format!("{k:?}")); + assert_eq!(kinds, [GeometryType::Point, GeometryType::Polygon]); + // A layer that declares no geometry type does the same. + let mut typeless = mixed.clone(); + typeless.geometry_type = GeometryType::None; + let at = dir.path().join("typeless.gpkg"); + assert_eq!(write_path(&typeless, &at)?, 0); + assert_eq!(table(&at)?, ("GEOMETRY".to_string(), all)); + // Projected, every feature is written too. + let utm = Crs::from_epsg(32631).ok_or("no UTM zone 31N")?; + let projected = dir.path().join("projected.gpkg"); + assert_eq!(write_path_in(&mixed, &projected, utm)?, 0); + assert_eq!(table(&projected)?.0, "GEOMETRY"); + // One kind, declared or not: that kind's column, nothing left out. + let mut areas = mixed.clone(); + areas.features.retain(|f| !matches!(f.geometry, Some(Geometry::Point(_)))); + let one = vec![("area".to_string(), true), ("none".to_string(), false)]; + let at = dir.path().join("areas.gpkg"); + assert_eq!(write_path(&areas, &at)?, 0); + assert_eq!(table(&at)?, ("MULTIPOLYGON".to_string(), one.clone())); + areas.geometry_type = GeometryType::None; + let at = dir.path().join("typeless-areas.gpkg"); + assert_eq!(write_path(&areas, &at)?, 0); + assert_eq!(table(&at)?, ("MULTIPOLYGON".to_string(), one)); + Ok(()) + } + /// Exercises caller cleanup through the public Shapefile writer, one fresh process per collision. #[cfg(not(target_arch = "wasm32"))] #[test] diff --git a/crates/data/src/source/gpkg.rs b/crates/data/src/source/gpkg.rs index 099ad5f266..aa646942dc 100644 --- a/crates/data/src/source/gpkg.rs +++ b/crates/data/src/source/gpkg.rs @@ -607,7 +607,7 @@ pub fn write_in(ds: &crate::Dataset, path: &Path, crs: Crs) -> Result<()> { pub fn write_table_in(ds: &crate::Dataset, path: &Path, crs: Crs, target: &GpkgTable) -> Result { let name = target.name.as_deref().unwrap_or(&ds.name); if let Some(replace) = into_existing(path, target)? { - return add::write_features(path, name, ds.geometry_type, &ds.fields, crs, replace, &mut |put| put(&ds.features)); + return add::write_features(path, name, table_kind(ds), &ds.fields, crs, replace, &mut |put| put(&ds.features)); } check_new_destination(path)?; let tmp = temp_database_beside(path)?; @@ -617,6 +617,24 @@ pub fn write_table_in(ds: &crate::Dataset, path: &Path, crs: Crs, target: &GpkgT Ok(GpkgWritten { table, replaced: false, dependents: Vec::new() }) } +/// The kind of geometry a GeoPackage table of `ds` is made for: its type or, for a layer that +/// declares none but carries geometry, its first geometry's (as a streamed export takes it), so +/// that geometry isn't dropped for want of a column. A layer holding other kinds as well gets a +/// GEOMETRY column, which holds any kind ([`Redo::Mixed`]). +fn table_kind(ds: &crate::Dataset) -> GeometryType { + match ds.geometry_type { + GeometryType::None => ds.features.iter().find_map(|f| f.geometry.as_ref()).map_or(GeometryType::None, crate::Geometry::kind), + t => t, + } +} + +/// Whether a table made for geometry of `kind` can't hold `f`'s in a column of that type, so it +/// needs a GEOMETRY column: a feature of another kind (a mixed layer's, as a GeoJSON of points +/// and polygons is). One without geometry fits any table. +fn other_kind(f: &crate::Feature, kind: GeometryType) -> bool { + kind != GeometryType::None && f.geometry.as_ref().is_some_and(|g| g.kind() != kind) +} + /// Whether an export goes into the existing GeoPackage at `path` (anything there, which opening /// it as one then checks) and, if so, whether it replaces a table: `None` for a new file. A /// table is added to a new file when there is none; one can't be replaced in a file not there. @@ -632,9 +650,10 @@ fn into_existing(path: &Path, target: &GpkgTable) -> Result> { /// As [`write_table_in`], for a layer `name` of `geometry_type` with `fields`: the features `feed` /// hands to the function it is given, a run at a time, already in `crs`'s coordinates, so a -/// whole layer needn't be held. A point layer's table type and whether ids are kept are chosen -/// from the features so far; should a later one contradict that choice, the export starts -/// again with it settled (feeding the features again), giving the file [`write_in`] would. +/// whole layer needn't be held. A point layer's table type, whether the column holds any kind +/// (a feature of another kind than `geometry_type`) and whether ids are kept are chosen from the +/// features so far; should a later one contradict that choice, the export starts again with it +/// settled (feeding the features again), giving the file [`write_in`] would. pub(crate) fn write_features_in( name: &str, geometry_type: GeometryType, @@ -649,14 +668,15 @@ pub(crate) fn write_features_in( return add::write_features(path, name, geometry_type, fields, crs, replace, feed); } check_new_destination(path)?; - let (mut multi_points, mut keep_ids) = (None, true); + let (mut multi_points, mut mixed, mut keep_ids) = (None, false, true); // Each choice changes at most once. - for _ in 0..3 { + for _ in 0..4 { let tmp = temp_database_beside(path)?; let conn = Connection::open(&tmp).map_err(err)?; - let mut make = |multi| create(&conn, name, geometry_type, fields, crs, multi); - match fill(&conn, multi_points, keep_ids, &mut make, feed)? { + let mut make = |multi, mixed| create(&conn, name, geometry_type, fields, crs, multi, mixed); + match fill(&conn, multi_points, mixed, keep_ids, &mut make, feed)? { Err(Redo::MultiPoints) => multi_points = Some(true), + Err(Redo::Mixed) => mixed = true, Err(Redo::Renumber) => keep_ids = false, Ok((layout, extent)) => { finish(&conn, &layout, extent)?; @@ -669,21 +689,22 @@ pub(crate) fn write_features_in( } /// Feeds the features into the table `make` creates on `conn`, made once the first run shows -/// whether a point layer holds multipoints (unless `multi_points` already says): the table and -/// the extent of what went in, or what the export must start again for. With `keep_ids` each -/// feature keeps its id. +/// whether a point layer holds multipoints (unless `multi_points` already says), its column one +/// for any kind of geometry when `mixed`: the table and the extent of what went in, or what the +/// export must start again for. With `keep_ids` each feature keeps its id. fn fill( conn: &Connection, multi_points: Option, + mixed: bool, keep_ids: bool, - make: &mut dyn FnMut(bool) -> Result, + make: &mut dyn FnMut(bool, bool) -> Result, feed: &mut dyn FnMut(&mut dyn FnMut(&[crate::Feature]) -> Result<()>) -> Result<()>, ) -> Result> { let (mut layout, mut extent, mut redo) = (None, Extent::EMPTY, None); let fed = feed(&mut |features| { if layout.is_none() { let multi = multi_points.unwrap_or_else(|| features.iter().any(|f| matches!(f.geometry, Some(crate::Geometry::MultiPoint(_))))); - layout = Some(make(multi)?); + layout = Some(make(multi, mixed)?); } let Some(l) = layout.as_ref() else { return Err(DataError::Format("GeoPackage: no table to write".into())) }; for g in features.iter().filter_map(|f| f.geometry.as_ref()) { @@ -702,7 +723,7 @@ fn fill( (Ok(()), None) => { let layout = match layout { Some(l) => l, - None => make(multi_points.unwrap_or(false))?, + None => make(multi_points.unwrap_or(false), mixed)?, }; Ok(Ok((layout, extent))) } @@ -772,7 +793,9 @@ fn destination_error(path: &Path, error: std::io::Error) -> DataError { /// returns the table's name as written. fn build(conn: &Connection, ds: &crate::Dataset, name: &str, crs: Crs) -> Result { let multi_points = ds.features.iter().any(|f| matches!(f.geometry, Some(crate::Geometry::MultiPoint(_)))); - let layout = create(conn, name, ds.geometry_type, &ds.fields, crs, multi_points)?; + let kind = table_kind(ds); + let mixed = ds.features.iter().any(|f| other_kind(f, kind)); + let layout = create(conn, name, kind, &ds.fields, crs, multi_points, mixed)?; // Feature ids are kept when they are unique and fit; otherwise SQLite numbers the rows. let mut seen = std::collections::HashSet::new(); let keep_ids = ds.features.iter().all(|f| i64::try_from(f.id).is_ok() && seen.insert(f.id)); @@ -793,6 +816,9 @@ struct Layout { fields: Vec, has_geom: bool, multi_points: bool, + /// The kind of geometry the table is made for, and whether its column holds any kind. + kind: GeometryType, + mixed: bool, geom_type: &'static str, srs_id: i32, /// The table, geometry and id column, and spatial index, quoted for SQL. @@ -823,14 +849,23 @@ const FEATURE_METADATA: &str = "CREATE TABLE IF NOT EXISTS gpkg_geometry_columns /// Creates the GeoPackage's tables and the empty feature table (and its spatial index) for a /// layer, in a transaction [`finish`] commits. A point layer holding any multipoint is a -/// MULTIPOINT table, whose single points [`insert`] writes as one-point multipoints. -fn create(conn: &Connection, name: &str, geometry_type: GeometryType, fields: &[Field], crs: Crs, multi_points: bool) -> Result { +/// MULTIPOINT table, whose single points [`insert`] writes as one-point multipoints; a `mixed` +/// layer's is a GEOMETRY table. +fn create( + conn: &Connection, + name: &str, + geometry_type: GeometryType, + fields: &[Field], + crs: Crs, + multi_points: bool, + mixed: bool, +) -> Result { // An EPSG system by its code; a custom one numbered as GDAL numbers them. let srs_id = match crs.epsg() { 0 => 100_000, c => i32::from(c), }; - let layout = plan(name, geometry_type, fields, multi_points, srs_id); + let layout = plan(name, geometry_type, fields, multi_points, mixed, srs_id); conn.execute_batch(&format!( "PRAGMA journal_mode = OFF; PRAGMA synchronous = OFF; PRAGMA application_id = 1196444487; PRAGMA user_version = 10400; @@ -862,8 +897,9 @@ fn create(conn: &Connection, name: &str, geometry_type: GeometryType, fields: &[ /// The names a layer's feature table is written with, in spatial reference system `srs_id`: the /// table named for the layer (not a reserved name, never empty), its id and geometry columns -/// clear of its fields, and its fields' columns unique without regard to case. -fn plan(name: &str, geometry_type: GeometryType, fields: &[Field], multi_points: bool, srs_id: i32) -> Layout { +/// clear of its fields, and its fields' columns unique without regard to case. A `mixed` layer's +/// geometry column is GEOMETRY, which holds every kind, its features written as they are. +fn plan(name: &str, geometry_type: GeometryType, fields: &[Field], multi_points: bool, mixed: bool, srs_id: i32) -> Layout { let mut table: String = name.chars().filter(|c| !c.is_control()).collect::().trim().to_string(); if table.is_empty() { table = "layer".into(); @@ -889,7 +925,11 @@ fn plan(name: &str, geometry_type: GeometryType, fields: &[Field], multi_points: .unwrap_or_else(|| f.name.clone()); columns.push(name); } + let mixed = mixed && has_geom; + // A GEOMETRY column holds single points as they are. + let multi_points = multi_points && !mixed; let geom_type = match geometry_type { + _ if mixed => "GEOMETRY", GeometryType::Point if multi_points => "MULTIPOINT", GeometryType::Point => "POINT", GeometryType::Polyline => "MULTILINESTRING", @@ -905,6 +945,8 @@ fn plan(name: &str, geometry_type: GeometryType, fields: &[Field], multi_points: fields: fields.to_vec(), has_geom, multi_points, + kind: geometry_type, + mixed, geom_type, srs_id, table_q: t, @@ -942,6 +984,8 @@ fn create_table(conn: &Connection, layout: &Layout) -> Result<()> { enum Redo { /// A multipoint in a POINT table: the table is MULTIPOINT. MultiPoints, + /// Geometry of another kind than the table's: its column is GEOMETRY, holding every kind. + Mixed, /// An id that doesn't fit or is taken: SQLite numbers every row. Renumber, } @@ -949,7 +993,8 @@ enum Redo { /// Inserts features into the table [`create`] made, and their boxes into its spatial index. /// With `keep_ids` each keeps its id; otherwise SQLite numbers it. Stops at the first feature /// the table as made can't hold as the whole layer would have it (a multipoint in a POINT -/// table, an id that doesn't fit or is taken), saying what to [`Redo`]. +/// table, geometry of another kind, an id that doesn't fit or is taken), saying what to +/// [`Redo`]. fn insert(conn: &Connection, layout: &Layout, features: &[crate::Feature], keep_ids: bool) -> Result> { use rusqlite::types::Value as Sql; let Layout { columns, fields, has_geom, multi_points, srs_id, table_q: t, geom_q: g, fid_q: i, rtree_q: rt, .. } = layout; @@ -964,6 +1009,9 @@ fn insert(conn: &Connection, layout: &Layout, features: &[crate::Feature], keep_ if has_geom { Some(conn.prepare_cached(&format!("INSERT INTO {rt} VALUES (?1, ?2, ?3, ?4, ?5)")).map_err(err)?) } else { None }; for f in features { crate::progress::step(1)?; + if has_geom && !layout.mixed && other_kind(f, layout.kind) { + return Ok(Some(Redo::Mixed)); + } if has_geom && !multi_points && matches!(f.geometry, Some(crate::Geometry::MultiPoint(_))) && layout.geom_type == "POINT" { return Ok(Some(Redo::MultiPoints)); } diff --git a/crates/data/src/source/gpkg/add.rs b/crates/data/src/source/gpkg/add.rs index b324783a5f..6e42965930 100644 --- a/crates/data/src/source/gpkg/add.rs +++ b/crates/data/src/source/gpkg/add.rs @@ -37,17 +37,17 @@ pub(super) fn write_features( // records) into deletes elsewhere; a table referring to the replaced one is refused instead. conn.execute_batch("PRAGMA foreign_keys = OFF").map_err(err)?; conn.busy_timeout(Duration::from_secs(5)).map_err(err)?; - let (mut multi_points, mut keep_ids) = (None, true); + let (mut multi_points, mut mixed, mut keep_ids) = (None, false, true); // Each choice changes at most once; an export starting again rolls back what it wrote. - for _ in 0..3 { + for _ in 0..4 { let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate).map_err(err)?; check_geopackage(&tx, &file)?; // An attribute table has no spatial reference system. let srs_id = if geometry_type == GeometryType::None { 0 } else { srs_id(&tx, crs)? }; // The views and triggers naming a replaced table, when one is. let replaced = Cell::new(None); - let mut make = |multi| { - let layout = plan(name, geometry_type, fields, multi, srs_id); + let mut make = |multi, mixed| { + let layout = plan(name, geometry_type, fields, multi, mixed, srs_id); replaced.set(make_room(&tx, &file, &layout.table, replace)?); if layout.has_geom { tx.execute_batch(FEATURE_METADATA).map_err(err)?; @@ -62,8 +62,9 @@ pub(super) fn write_features( create_table(&tx, &layout)?; Ok(layout) }; - match fill(&tx, multi_points, keep_ids, &mut make, feed)? { + match fill(&tx, multi_points, mixed, keep_ids, &mut make, feed)? { Err(Redo::MultiPoints) => multi_points = Some(true), + Err(Redo::Mixed) => mixed = true, Err(Redo::Renumber) => keep_ids = false, Ok((layout, extent)) => { register(&tx, &layout, extent)?; diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index b181989a9a..e8a5afb121 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -53,8 +53,8 @@ pub fn commands() -> Vec { let (left_out, table) = rustgis_data::io::write_path_to(&t.dataset, Path::new(path), Crs::LonLat, &gpkg).map_err(|e| e.to_string())?; s.check_continuation(&owner).map_err(|why| super::exports::completion_refused(Some(path), why))?; - if left_out > 0 { - s.notify(format!("{path}: {left_out} features without drawable geometry were left out"), false); + if let Some(note) = rustgis_data::io::left_out_note(path, &ext, left_out) { + s.notify(note, false); } for note in notes.into_iter().chain(dbf_notes) { s.notify(note, false); @@ -643,11 +643,9 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk dbf_notes.extend(rustgis_data::io::shapefile_values_notes(ds, path)); } // FlatGeobuf, KML, GPX and Shapefile hold features with geometry only (a Shapefile, only - // geometry it can draw). + // geometry it can draw, of the layer's kind). let mut said = Vec::new(); - if left_out > 0 { - said.push(format!("{path}: {left_out} features without drawable geometry were left out")); - } + said.extend(rustgis_data::io::left_out_note(path, &ext, left_out)); if skipped > 0 { said.push(format!("{path}: {skipped} rows couldn't be read and were left out")); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 01825dabda..e4bce1231b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -32296,7 +32296,7 @@ fn shapefile_value_losses_reach_export_notices() { notices.iter().any(|n| n.contains("3 values") && n.contains("id") && n.contains("number") && n.contains("left empty")), "{command}: {notices:?}" ); - assert!(notices.iter().any(|n| n.contains("1 features without drawable geometry were left out")), "{command}: {notices:?}"); + assert!(notices.iter().any(|n| n.contains("1 features left out of the Shapefile")), "{command}: {notices:?}"); } } // A clean table gives neither loss notice; other formats keep the lossy attributes. diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 59102e35e4..dafd2bbee1 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3954,8 +3954,8 @@ impl RustGisApp { if matches!(ext.as_str(), "parquet" | "geoparquet") { dbf_notes.extend(rustgis_engine::data::source::parquet::values_note(ds, &name)); } - if left_out > 0 { - self.session.notify(format!("{name}: {left_out} features without drawable geometry were left out"), false); + if let Some(note) = io::left_out_note(&name, &ext, left_out) { + self.session.notify(note, false); } if ext == "zip" && rustgis_engine::data::raster::wkt::to_wkt(crs).is_none() { self.session.notify(format!("{name}: no .prj written, as {} has no WKT form", crs.label()), false); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index c30ce9baed..6928e8ceab 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -342,7 +342,8 @@ surface from point features using a Quartic (biweight, default) or Gaussian kern renormalised to integrate to 1). Points can optionally be weighted by a numeric population field (invalid or non-positive weights are excluded and reported in the tool messages; for multipoint features, each constituent point part -receives the feature's weight). Calculations are performed in the linear units +receives the feature's weight, a part with a coordinate missing or infinite is left out and +counted, and a feature with no finite part is skipped). Calculations are performed in the linear units of a projected coordinate system (defaults to the layer's projected system; if the input layer lacks a coordinate system or is in degrees, or to project into a specific system, supply a projected system via `crs`, e.g. `EPSG:32618`). @@ -765,9 +766,24 @@ notice. A Double field's values are decimals already, rounded as decimal arithme get no notice. Without `statistics`, `field` gives that field's `COUNT`, `SUM`, `MEAN`, `MIN`, `MAX` and `STD`, reading its values as numbers. +**Minimum Bounding Geometry** (`convexHull`) encloses each feature in its convex hull (Group +Option `None`), or all of them in one (`All`, with `count`, the features it enclosed). It passes +over a vertex with a coordinate missing or infinite, or so far out the hull can't take it (past +about 9 million degrees), and skips a feature without geometry or with no point left to enclose, +writing no hull for it; if every feature is skipped, it is an error. A latitude past a pole is +taken as that pole. A hull with no area is written as what it is: a point for points in one place, +or the line between the two farthest apart for points on one straight line on the map. The layer is +a point or line layer when every hull is one, and the tool says so; otherwise it's a polygon layer +holding any points and lines among its polygons. Dissolve leaves those out, saying how many; as +Clip masks they cover nothing; a Shapefile export leaves them out, saying how many; and a +GeoPackage export keeps them in a column that holds any kind of geometry. It says how many of +each. + **Dissolve** (`dissolve`) merges the polygons sharing a value of `field` into one feature for each value, or all of them into one without a field. Each feature has the value and `count`, the -polygons merged; records with no value form a feature of their own. **Summarize Within** +polygons merged; records with no value form a feature of their own. A feature with no polygon (no +geometry, or a point or line in the polygon layer) is left out, with its values, and the tool says +how many; if none has a polygon, it is an error. **Summarize Within** (`countPoints`) copies the polygons and adds `point_count`, the points inside each polygon or on its edge; a point where polygons overlap counts in each of them. diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index e74829a9c6..ebd87bcd43 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -578,7 +578,7 @@ were rounded, and Export Raster, which writes the rounded values, says so again. | Format | How to write it | Notes | | --- | --- | --- | | GeoJSON (`.geojson`, `.json`), CSV | Data › Export (`share.exportLayer`) | The Export Features dialog picks the format; the chosen extension is appended when needed. In the web app it downloads any vector format below except GeoPackage and FlatGeobuf (a Shapefile as one zip). A streamed layer exports its whole source: on the desktop, to GeoJSON, CSV, FlatGeobuf, GeoPackage or GeoParquet a chunk at a time with no limit on its size (a GeoParquet source's row groups are read whole, so on the desktop each holds at most 16,777,216 rows; a layer read in place from the GeoPackage it goes into is read whole first), otherwise loaded whole (see [Streamed layers](data.md)); remote sources on the web must be downloaded first. | -| GeoPackage | Data › Export to `.gpkg` (desktop) | A new file with one feature table (an attributes table for a layer without geometry) in the layer's coordinate system (see below), with an R-tree and GeoPackage 1.4 triggers to maintain it. An existing destination is refused unless the export adds a table to it or replaces one of its feature or attribute tables (`mode`, see [Exporting](sharing.md)), leaving its other tables as they were. Unique feature ids that fit signed 64-bit integers are kept; otherwise SQLite assigns new ids. | +| GeoPackage | Data › Export to `.gpkg` (desktop) | A new file with one feature table (an attributes table for a layer without geometry) in the layer's coordinate system (see below), with an R-tree and GeoPackage 1.4 triggers to maintain it. An existing destination is refused unless the export adds a table to it or replaces one of its feature or attribute tables (`mode`, see [Exporting](sharing.md)), leaving its other tables as they were. A layer holding more than one kind of geometry (a GeoJSON of points and polygons, say) is written whole, in a `GEOMETRY` column, which holds any kind; a layer of one kind gets that kind's column. Unique feature ids that fit signed 64-bit integers are kept; otherwise SQLite assigns new ids. | | GeoParquet (`.parquet`, `.geoparquet`) | Data › Export | GeoParquet 1.1: WKB geometry in WGS 84, Zstd level 9 compression, row groups of up to 65,536 features and a `bbox` covering column with byte-stream-split encoding. The covering keeps spatial range reads available; compression favours smaller files over export speed. Reading the file needs Zstd support, and reading its `bbox` column needs byte-stream-split support, which current GDAL, PyArrow and DuckDB have and some older readers lack (some of those refuse the file). A standalone table without geometry writes plain Parquet. | | Shapefile | Data › Export to `.shp` (or `.zip` for the files zipped) | Writes `.shp`, `.shx`, `.dbf`, `.prj` (the coordinate system, see below) and `.cpg` (UTF-8). Field names are cut to 10 bytes (made unique ignoring case, and reported), text to at most 254 bytes, and numbers that do not fit their dBase field are left empty. Export notices count shortened text, non-null values left empty (including incompatible types), and numbers rounded to the field's precision, and name the affected source fields; features without geometry, and lines of a single point, are left out (and reported). | | FlatGeobuf | Data › Export to `.fgb` | In the layer's coordinate system (see below), with a spatial index; features without geometry are left out (and reported). Desktop only: the writer needs a temporary file, which the browser doesn't have. |