From 7db213abb5f3d3200ee6140fa1d5f2b8fde567e2 Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 11 Nov 2026 01:14:21 -0600 Subject: [PATCH 1/2] fix(data): a point layer mixing points and multipoints exports to FlatGeobuf The writer declares a point layer's type Unknown, but the flatgeobuf crate's detect_type takes the first feature's type for the header and then refuses the other kind. write_in now turns detect_type off for a layer that holds both, so each feature records its own type; every other layer keeps its one geometry type in the header. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/source/fgb.rs | 42 ++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/data/src/source/fgb.rs b/crates/data/src/source/fgb.rs index 2d097706b9..8b35e695bb 100644 --- a/crates/data/src/source/fgb.rs +++ b/crates/data/src/source/fgb.rs @@ -683,7 +683,9 @@ pub fn write(ds: &crate::Dataset) -> Result> { /// the file saying `crs`. pub fn write_in(ds: &crate::Dataset, crs: Crs) -> Result> { let mut out = Vec::new(); - write_features(&ds.name, ds.geometry_type, &ds.fields, crs, &mut out, &mut |add| add(&ds.features))?; + let has = + |multi: bool| ds.features.iter().filter_map(|f| f.geometry.as_ref()).any(|g| matches!(g, crate::Geometry::MultiPoint(_)) == multi); + write_features_typed(&ds.name, ds.geometry_type, &ds.fields, crs, has(true) && has(false), &mut out, &mut |add| add(&ds.features))?; Ok(out) } @@ -710,6 +712,23 @@ pub(crate) fn write_features( crs: Crs, out: &mut dyn std::io::Write, feed: &mut dyn FnMut(&mut dyn FnMut(&[Feature]) -> Result<()>) -> Result<()>, +) -> Result { + write_features_typed(name, geometry_type, fields, crs, false, out, feed) +} + +/// [`write_features`], each feature's geometry type in the file when `mixed_points`: a point +/// layer holding both points and multipoints, which a file of one geometry type can't (its type +/// is the first feature's). Otherwise the file has one type, as readers expect of a layer. A +/// layer read in place isn't looked over first, so one that mixes them still fails. +#[allow(clippy::too_many_arguments)] +fn write_features_typed( + name: &str, + geometry_type: GeometryType, + fields: &[Field], + crs: Crs, + mixed_points: bool, + out: &mut dyn std::io::Write, + feed: &mut dyn FnMut(&mut dyn FnMut(&[Feature]) -> Result<()>) -> Result<()>, ) -> Result { use flatgeobuf::geozero::wkb::Wkb; use flatgeobuf::{FgbCrs, FgbWriter, FgbWriterOptions, GeometryType as G}; @@ -724,6 +743,7 @@ pub(crate) fn write_features( let code = i32::from(crs.epsg()); let options = FgbWriterOptions { crs: FgbCrs { org: (code > 0).then_some("EPSG"), code, wkt: wkt.as_deref(), ..Default::default() }, + detect_type: !(mixed_points && geometry_type == GeometryType::Point), ..Default::default() }; let mut w = FgbWriter::create_with_options(name, ty, options).map_err(err)?; @@ -782,4 +802,24 @@ mod write_tests { assert!(matches!(writable(true), Err(DataError::Unsupported(m)) if m.contains("browser"))); assert!(writable(false).is_ok()); } + + /// A point layer with points and multipoints (as a GeoJSON file gives) is written whole, + /// each feature its own kind; a layer of points alone keeps its one geometry type. + #[test] + fn a_point_layer_mixing_points_and_multipoints_is_written() { + use crate::{Coord, Dataset, Geometry}; + let mut ds = Dataset::new("m", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]); + ds.push(Some(Geometry::Point(Coord::new(1.0, 2.0))), vec![Value::Int(1)]); + ds.push(Some(Geometry::MultiPoint(vec![Coord::new(3.0, 4.0), Coord::new(5.0, 6.0)])), vec![Value::Int(2)]); + let bytes = write_in(&ds, Crs::LonLat).unwrap(); + let (back, _) = crate::io::read_bytes("m.fgb", &bytes).unwrap(); + let mut got: Vec<(Value, Option)> = back.features.iter().map(|f| (f.attrs[0].clone(), f.geometry.clone())).collect(); + got.sort_by_key(|(n, _)| n.as_f64().map(|n| n as i64)); + let want: Vec<(Value, Option)> = ds.features.iter().map(|f| (f.attrs[0].clone(), f.geometry.clone())).collect(); + assert_eq!(got, want); + let mut points = Dataset::new("p", GeometryType::Point, vec![]); + points.push(Some(Geometry::Point(Coord::new(1.0, 2.0))), vec![]); + let header = flatgeobuf::FgbReader::open(std::io::Cursor::new(write_in(&points, Crs::LonLat).unwrap())).unwrap(); + assert_eq!(header.header().geometry_type(), flatgeobuf::GeometryType::Point); + } } From 3699534aaddb9c8b5ffeef12f2cb3982070f25f1 Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 11 Nov 2026 04:42:47 -0600 Subject: [PATCH 2/2] fix(data): FlatGeobuf mixed points start the export again, streamed too Review of #1778: the pre-scan also matched lines and polygons in a point layer, and a layer exported a chunk at a time still failed. The writer now settles a point layer's type from its first point and, should a point of the other kind follow, starts again (feeding the features again) with each feature's own type, as the GeoPackage writer does; nothing reaches the output before the last feature. On that run a line or polygon in a point layer is refused. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/source/fgb.rs | 94 +++++++++++++++++++++++++++++------ 1 file changed, 79 insertions(+), 15 deletions(-) diff --git a/crates/data/src/source/fgb.rs b/crates/data/src/source/fgb.rs index 8b35e695bb..5e8265dc9e 100644 --- a/crates/data/src/source/fgb.rs +++ b/crates/data/src/source/fgb.rs @@ -683,9 +683,7 @@ pub fn write(ds: &crate::Dataset) -> Result> { /// the file saying `crs`. pub fn write_in(ds: &crate::Dataset, crs: Crs) -> Result> { let mut out = Vec::new(); - let has = - |multi: bool| ds.features.iter().filter_map(|f| f.geometry.as_ref()).any(|g| matches!(g, crate::Geometry::MultiPoint(_)) == multi); - write_features_typed(&ds.name, ds.geometry_type, &ds.fields, crs, has(true) && has(false), &mut out, &mut |add| add(&ds.features))?; + write_features(&ds.name, ds.geometry_type, &ds.fields, crs, &mut out, &mut |add| add(&ds.features))?; Ok(out) } @@ -705,6 +703,11 @@ fn writable(browser: bool) -> Result<()> { /// temporary file and only its index entry in memory, so a whole layer needn't be held. The /// writer needs that file whether or not it writes an index, and the web app has no files, so /// there a FlatGeobuf is refused rather than written. +/// +/// A point layer's file takes the type of its first point. Should a later one be of the other +/// kind (a multipoint after points, or a point after multipoints), the export starts again +/// (feeding the features again) with each feature's type in the file, which a file of one type +/// can't hold. Nothing reaches `out` before the last feature, so the first run leaves it empty. pub(crate) fn write_features( name: &str, geometry_type: GeometryType, @@ -713,15 +716,21 @@ pub(crate) fn write_features( out: &mut dyn std::io::Write, feed: &mut dyn FnMut(&mut dyn FnMut(&[Feature]) -> Result<()>) -> Result<()>, ) -> Result { - write_features_typed(name, geometry_type, fields, crs, false, out, feed) + for mixed_points in [false, true] { + if let Some(left_out) = write_run(name, geometry_type, fields, crs, mixed_points, out, feed)? { + return Ok(left_out); + } + } + Err(DataError::Format("FlatGeobuf: the export didn't settle on a geometry type".into())) } -/// [`write_features`], each feature's geometry type in the file when `mixed_points`: a point -/// layer holding both points and multipoints, which a file of one geometry type can't (its type -/// is the first feature's). Otherwise the file has one type, as readers expect of a layer. A -/// layer read in place isn't looked over first, so one that mixes them still fails. +/// One run of [`write_features`], each feature's geometry type in the file when `mixed_points`: +/// how many were left out, or `None` when a point layer turned out to mix points and +/// multipoints and the export must start again so. With `mixed_points`, a point layer's +/// geometry of another kind (a line, a polygon) is refused: a file of mixed types is read as +/// the layer of its first feature's kind. #[allow(clippy::too_many_arguments)] -fn write_features_typed( +fn write_run( name: &str, geometry_type: GeometryType, fields: &[Field], @@ -729,7 +738,7 @@ fn write_features_typed( mixed_points: bool, out: &mut dyn std::io::Write, feed: &mut dyn FnMut(&mut dyn FnMut(&[Feature]) -> Result<()>) -> Result<()>, -) -> Result { +) -> Result> { use flatgeobuf::geozero::wkb::Wkb; use flatgeobuf::{FgbCrs, FgbWriter, FgbWriterOptions, GeometryType as G}; writable(cfg!(target_arch = "wasm32"))?; @@ -743,7 +752,7 @@ fn write_features_typed( let code = i32::from(crs.epsg()); let options = FgbWriterOptions { crs: FgbCrs { org: (code > 0).then_some("EPSG"), code, wkt: wkt.as_deref(), ..Default::default() }, - detect_type: !(mixed_points && geometry_type == GeometryType::Point), + detect_type: !mixed_points, ..Default::default() }; let mut w = FgbWriter::create_with_options(name, ty, options).map_err(err)?; @@ -756,14 +765,35 @@ fn write_features_typed( }; w.add_column(&f.name, t, |_, c| c.nullable = true); } - let mut left_out = 0; - feed(&mut |features| { + let (mut left_out, mut first_multi, mut again) = (0, None, false); + let fed = feed(&mut |features| { for feat in features { crate::progress::step(1)?; let Some(g) = &feat.geometry else { left_out += 1; continue; }; + if geometry_type == GeometryType::Point { + let multi = match g { + crate::Geometry::Point(_) => Some(false), + crate::Geometry::MultiPoint(_) => Some(true), + _ => None, + }; + match (multi, mixed_points) { + (Some(m), false) if *first_multi.get_or_insert(m) != m => { + again = true; + // Stops the traversal; the export starts again. + return Err(DataError::Format("FlatGeobuf: starting again".into())); + } + (None, true) => { + return Err(DataError::Format(format!( + "FlatGeobuf: feature {} of a point layer isn't a point or multipoint", + feat.id + ))); + } + _ => {} + } + } let mut failed = None; w.add_feature_geom(Wkb(wkb::encode(g)), |out| { use flatgeobuf::geozero::PropertyProcessor; @@ -786,9 +816,13 @@ fn write_features_typed( } } Ok(()) - })?; + }); + if again { + return Ok(None); + } + fed?; w.write(out).map_err(err)?; - Ok(left_out) + Ok(Some(left_out)) } #[cfg(test)] @@ -822,4 +856,34 @@ mod write_tests { let header = flatgeobuf::FgbReader::open(std::io::Cursor::new(write_in(&points, Crs::LonLat).unwrap())).unwrap(); assert_eq!(header.header().geometry_type(), flatgeobuf::GeometryType::Point); } + + /// Fed a chunk at a time (a layer read in place), the multipoint arrives after the point + /// has set the file's type: the export starts again and writes both. A line in a point layer + /// isn't written into a file of mixed types, which would read back as a line layer. + #[test] + fn a_streamed_point_layer_mixing_points_and_multipoints_starts_again() { + use crate::{Coord, Geometry}; + let chunks = [ + vec![Feature { id: 1, geometry: Some(Geometry::Point(Coord::new(1.0, 2.0))), attrs: vec![] }], + vec![Feature { id: 2, geometry: Some(Geometry::MultiPoint(vec![Coord::new(3.0, 4.0), Coord::new(5.0, 6.0)])), attrs: vec![] }], + ]; + let mut runs = 0; + let mut out = Vec::new(); + write_features("m", GeometryType::Point, &[], Crs::LonLat, &mut out, &mut |add| { + runs += 1; + chunks.iter().try_for_each(|c| add(c)) + }) + .unwrap(); + assert_eq!(runs, 2); + let (back, _) = crate::io::read_bytes("m.fgb", &out).unwrap(); + let mut got: Vec<_> = back.features.iter().filter_map(|f| f.geometry.clone()).collect(); + got.sort_by_key(|g| matches!(g, Geometry::MultiPoint(_))); + assert_eq!(got, chunks.iter().flatten().filter_map(|f| f.geometry.clone()).collect::>()); + let line = Feature { id: 3, geometry: Some(Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(1.0, 1.0)]])), attrs: vec![] }; + let with_line = [chunks[0].clone(), chunks[1].clone(), vec![line]]; + let e = write_features("m", GeometryType::Point, &[], Crs::LonLat, &mut Vec::new(), &mut |add| { + with_line.iter().try_for_each(|c| add(c)) + }); + assert!(matches!(&e, Err(DataError::Format(m)) if m.contains("feature 3 of a point layer isn't a point")), "{e:?}"); + } }