From 0ee8c16b48de24b67a3cd91d2a47a091e94ff52a Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 12 Nov 2026 11:23:42 -0600 Subject: [PATCH 1/3] fix(data): a CSV export writes its coordinate columns first A point layer's CSV put longitude and latitude after the fields, and reading takes the first header named like a coordinate, so a field named lat, x or longitude was read back as one. The coordinate columns now come first, named with the first recognised name no field has. Closes #1856 Co-Authored-By: Claude Opus 5.5 --- crates/data/src/export/tests.rs | 2 +- crates/data/src/io.rs | 55 ++++++++++++++++++++++++--------- crates/engine/src/tests.rs | 6 ++-- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/crates/data/src/export/tests.rs b/crates/data/src/export/tests.rs index d2a7c8b216..7f893dbecb 100644 --- a/crates/data/src/export/tests.rs +++ b/crates/data/src/export/tests.rs @@ -285,7 +285,7 @@ fn a_layer_without_a_declared_type_takes_its_first_geometrys() { let layer = Dataset::new("points", GeometryType::None, points.fields.clone()); write_chunks_in(&Runs::new(&points, 2), &layer, &d.join("points.csv"), Crs::LonLat, 1).unwrap(); let text = std::fs::read_to_string(d.join("points.csv")).unwrap(); - assert!(text.starts_with("n,longitude,latitude"), "{text}"); + assert!(text.starts_with("longitude,latitude,n"), "{text}"); } /// Rows a source couldn't read are counted, so an export can say how many it left out; a clean diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 863a48059c..c200361cd2 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -1438,8 +1438,10 @@ impl GeoJsonWriter { // --------------------------------------------------------------------------------------------- // CSV -const LON_NAMES: &[&str] = &["lon", "lng", "long", "longitude", "x", "lon_dd", "longitude_dd"]; -const LAT_NAMES: &[&str] = &["lat", "latitude", "y", "lat_dd", "latitude_dd"]; +/// Longitude and latitude columns, recognised in any order; CSV export names its coordinate +/// columns with the first of each that no field has. +const LON_NAMES: &[&str] = &["longitude", "lon", "lng", "long", "x", "lon_dd", "longitude_dd"]; +const LAT_NAMES: &[&str] = &["latitude", "lat", "y", "lat_dd", "latitude_dd"]; /// Projected coordinate columns, recognised as well as longitude/latitude ones. const X_NAMES: &[&str] = &["easting", "point_x", "xcoord", "x_coord"]; @@ -1876,7 +1878,7 @@ pub fn to_csv(ds: &Dataset) -> Result { } /// Writes CSV a run of features at a time: a header of the fields, and for a point layer -/// longitude and latitude columns after them. +/// longitude and latitude columns before them. pub(crate) struct CsvWriter { w: csv::Writer, point: bool, @@ -1887,19 +1889,24 @@ impl CsvWriter { pub(crate) fn new(out: W, fields: &[Field], geometry_type: GeometryType) -> Result { let mut w = csv::Writer::from_writer(out); let point = geometry_type == GeometryType::Point; - let mut head: Vec = fields.iter().map(|f| f.name.clone()).collect(); + let names: Vec = fields.iter().map(|f| f.name.clone()).collect(); + let mut head = Vec::new(); if point { - // Coordinate columns get names that don't collide with existing fields. - for base in ["longitude", "latitude"] { - let mut name = base.to_string(); - let mut i = 1; - while head.iter().any(|h| h.eq_ignore_ascii_case(&name)) { - name = format!("{base}_{i}"); - i += 1; - } + // The coordinate columns come first, named as reading looks for them and unlike any + // field: reading takes the first such column, so a field named `lat` or `x` stays a + // field when the file is added back. A layer with a field of every name gets + // `longitude_1` and the like, which reading doesn't recognise. + let taken = |name: &str| names.iter().any(|h| h.eq_ignore_ascii_case(name)); + for (base, known) in [("longitude", LON_NAMES), ("latitude", LAT_NAMES)] { + let name = known + .iter() + .find(|n| !taken(n)) + .map(|n| n.to_string()) + .unwrap_or_else(|| (1..).map(|i| format!("{base}_{i}")).find(|n| !taken(n)).unwrap_or_default()); head.push(name); } } + head.extend(names); w.write_record(&head).map_err(|e| DataError::Format(e.to_string()))?; Ok(CsvWriter { w, point }) } @@ -1908,12 +1915,13 @@ impl CsvWriter { pub(crate) fn write(&mut self, features: &[crate::Feature]) -> Result<()> { for f in features { crate::progress::step(1)?; - let mut row: Vec = f.attrs.iter().map(csv_cell).collect(); + let mut row = Vec::with_capacity(f.attrs.len() + 2); if self.point { let c = f.geometry.as_ref().and_then(|g| g.label_point()); row.push(c.map(|c| c.x.to_string()).unwrap_or_default()); row.push(c.map(|c| c.y.to_string()).unwrap_or_default()); } + row.extend(f.attrs.iter().map(csv_cell)); self.w.write_record(&row).map_err(|e| DataError::Format(e.to_string()))?; } Ok(()) @@ -4129,6 +4137,25 @@ mod tests { assert_eq!(write_bytes(&ds, "gpx").unwrap().1, 2); } + /// A point layer's fields named like coordinates keep their values when its CSV is read back: + /// the coordinate columns come first, so reading takes them, not the fields. (Reading keeps the + /// coordinate columns as fields too.) + #[test] + fn csv_fields_named_like_coordinates_stay_fields() { + let names = ["lat", "x", "longitude"]; + let fields: Vec = names.iter().map(|n| Field::new(*n, FieldType::Double)).collect(); + let mut ds = Dataset::new("t", GeometryType::Point, fields); + ds.push(Some(Geometry::Point(Coord::new(20.0, 10.0))), vec![Value::Float(1.5), Value::Float(2.5), Value::Float(3.5)]); + let out = to_csv(&ds).unwrap(); + assert_eq!(out.lines().next(), Some("lon,latitude,lat,x,longitude"), "{out}"); + let (back, _) = read_csv(&out, "t").unwrap(); + assert_eq!(back.features[0].geometry, Some(Geometry::Point(Coord::new(20.0, 10.0)))); + for (name, v) in names.iter().zip([1.5, 2.5, 3.5]) { + let i = back.field_index(name).unwrap(); + assert_eq!(back.value(0, i), &Value::Float(v), "{name}"); + } + } + #[test] fn csv_leading_zeros_and_guard_roundtrip() { let (ds, _) = read_csv("zip,n,note\n02134,0,-abc\n10001,0.5,=x\n", "t").unwrap(); @@ -4156,7 +4183,7 @@ mod tests { assert_eq!(ds.geometry_type, GeometryType::Point, "BOM must not hide the lon header"); let out = to_csv(&ds).unwrap(); assert!(out.contains("'=SUM(A1)"), "{out}"); - assert!(out.lines().next().unwrap().contains("longitude_1"), "{out}"); + assert_eq!(out.lines().next(), Some("lng,latitude,lon,lat,longitude,note"), "{out}"); let (ds, _) = read_csv("a\nNaN\nx\n", "t").unwrap(); assert_eq!(ds.fields[0].ty, FieldType::Text); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c94bcb35d5..c922680e6e 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -34165,13 +34165,13 @@ fn geopackage_past_the_count_limit_streams_exports_and_reads_whole() { let output = fixture.dir.join("whole.csv"); let exported = s.run("share.exportLayer", &json!({"layer": id, "path": output})).unwrap(); assert_eq!(exported["features"].as_u64(), Some(count as u64)); - // A header, then every row once and in order (the first column is the road's name). + // A header, then every row once and in order (the road's name after its coordinates). let mut lines = std::io::BufRead::lines(std::io::BufReader::new(std::fs::File::open(&output).unwrap())); - assert!(lines.next().unwrap().unwrap().starts_with("name,")); + assert!(lines.next().unwrap().unwrap().starts_with("longitude,latitude,name")); let mut rows = 0; for (k, line) in lines.enumerate() { let line = line.unwrap(); - assert!(line.starts_with(&format!("road {},", k + 1)), "row {k}: {line}"); + assert_eq!(line.split(',').nth(2), Some(format!("road {}", k + 1).as_str()), "row {k}: {line}"); rows += 1; } assert_eq!(rows, count, "every row"); From f8116465e40a711436c4827e047a36c83e236c32 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 12 Nov 2026 12:58:12 -0600 Subject: [PATCH 2/3] fix(data): say when a CSV's coordinate columns aren't read back as such A point layer with a field of every longitude or latitude name has its coordinates in longitude_1 or latitude_1, which reading doesn't take; the export now says so. The name check trims field names as reading does. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/io.rs | 87 +++++++++++++++++++++++++++------- crates/engine/src/cmd/share.rs | 1 + 2 files changed, 71 insertions(+), 17 deletions(-) diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index c200361cd2..b57be9380f 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -1870,6 +1870,34 @@ fn csv_cell(v: &Value) -> String { } } +/// The names of a point layer's longitude and latitude columns in CSV: the first of the names +/// reading takes for each that no field has, ignoring case and the spaces around a name (reading +/// trims them), and whether there was one of each. +/// A layer with a field of every such name gets `longitude_1` or `latitude_1`, which reading +/// doesn't take as coordinates ([`csv_coordinates_note`]). +fn csv_coordinate_names(fields: &[Field]) -> ([String; 2], bool) { + let taken = |name: &str| fields.iter().any(|f| f.name.trim().eq_ignore_ascii_case(name)); + let pick = |base: &str, known: &[&str]| { + let name = known.iter().map(|n| n.to_string()).chain((1..).map(|i| format!("{base}_{i}"))).find(|n| !taken(n)); + name.unwrap_or_default() + }; + let names = [pick("longitude", LON_NAMES), pick("latitude", LAT_NAMES)]; + let read = LON_NAMES.contains(&names[0].as_str()) && LAT_NAMES.contains(&names[1].as_str()); + (names, read) +} + +/// What a CSV export of a point layer (`gt`) to `target` says when its fields take every name +/// reading takes for a longitude or a latitude: the coordinates are in columns reading doesn't +/// find on its own. `None` otherwise. +pub fn csv_coordinates_note(fields: &[Field], gt: GeometryType, target: &str) -> Option { + let ([x, y], read) = csv_coordinate_names(fields); + (gt == GeometryType::Point && !read).then(|| { + format!( + "{target}: the fields have every name read as a longitude or latitude, so the coordinates are in columns '{x}' and '{y}', which adding the file back doesn't take as coordinates unless they are picked" + ) + }) +} + /// A dataset as CSV text: its fields, and a point layer's longitude and latitude ([`CsvWriter`]). pub fn to_csv(ds: &Dataset) -> Result { let mut w = CsvWriter::new(Vec::new(), &ds.fields, ds.geometry_type)?; @@ -1889,24 +1917,13 @@ impl CsvWriter { pub(crate) fn new(out: W, fields: &[Field], geometry_type: GeometryType) -> Result { let mut w = csv::Writer::from_writer(out); let point = geometry_type == GeometryType::Point; - let names: Vec = fields.iter().map(|f| f.name.clone()).collect(); let mut head = Vec::new(); if point { - // The coordinate columns come first, named as reading looks for them and unlike any - // field: reading takes the first such column, so a field named `lat` or `x` stays a - // field when the file is added back. A layer with a field of every name gets - // `longitude_1` and the like, which reading doesn't recognise. - let taken = |name: &str| names.iter().any(|h| h.eq_ignore_ascii_case(name)); - for (base, known) in [("longitude", LON_NAMES), ("latitude", LAT_NAMES)] { - let name = known - .iter() - .find(|n| !taken(n)) - .map(|n| n.to_string()) - .unwrap_or_else(|| (1..).map(|i| format!("{base}_{i}")).find(|n| !taken(n)).unwrap_or_default()); - head.push(name); - } + // The coordinate columns come first, so reading takes them rather than a field + // named `lat` or `x`. + head.extend(csv_coordinate_names(fields).0); } - head.extend(names); + head.extend(fields.iter().map(|f| f.name.clone())); w.write_record(&head).map_err(|e| DataError::Format(e.to_string()))?; Ok(CsvWriter { w, point }) } @@ -2751,11 +2768,15 @@ impl std::ops::AddAssign for CsvLosses { } /// What a CSV or GPX file (extension `ext`) of the whole dataset `ds` can't hold, written to -/// `target`: [`CsvLosses`]' notices, or [`gpx_notes`] with each feature named from `name_field`; +/// `target`: [`CsvLosses`]' notices and [`csv_coordinates_note`], or [`gpx_notes`] with each feature named from `name_field`; /// none for another format. A streamed export counts CSV's a chunk at a time instead. pub fn export_losses_notes(ds: &Dataset, ext: &str, name_field: Option, target: &str) -> Vec { match ext { - "csv" => CsvLosses::of(&ds.features, ds.geometry_type).notes(target), + "csv" => { + let mut notes = CsvLosses::of(&ds.features, ds.geometry_type).notes(target); + notes.extend(csv_coordinates_note(&ds.fields, ds.geometry_type, target)); + notes + } "gpx" => gpx_notes(ds, name_field, target), _ => Vec::new(), } @@ -4154,6 +4175,38 @@ mod tests { let i = back.field_index(name).unwrap(); assert_eq!(back.value(0, i), &Value::Float(v), "{name}"); } + // A name with spaces around it is the name reading gives it. + let mut padded = Dataset::new("t", GeometryType::Point, vec![Field::new(" longitude ", FieldType::Double)]); + padded.push(Some(Geometry::Point(Coord::new(20.0, 10.0))), vec![Value::Float(1.5)]); + let out = to_csv(&padded).unwrap(); + assert_eq!(out.lines().next(), Some("lon,latitude, longitude "), "{out}"); + let (back, _) = read_csv(&out, "t").unwrap(); + assert_eq!(back.features[0].geometry, Some(Geometry::Point(Coord::new(20.0, 10.0)))); + assert_eq!(back.value(0, back.field_index("longitude").unwrap()), &Value::Float(1.5)); + } + + /// A point layer with a field of every longitude name has its longitudes in a column reading + /// doesn't take, and its export says so; one with a name left says nothing. + #[test] + fn csv_coordinates_in_columns_reading_doesnt_take_are_said() { + let layer = |names: &[&str]| { + let fields: Vec = names.iter().map(|n| Field::new(*n, FieldType::Integer)).collect(); + let mut ds = Dataset::new("t", GeometryType::Point, fields); + ds.push(Some(Geometry::Point(Coord::new(20.0, 10.0))), names.iter().map(|_| Value::Int(1)).collect()); + ds + }; + let every = layer(LON_NAMES); + let out = to_csv(&every).unwrap(); + assert!(out.starts_with("longitude_1,latitude,"), "{out}"); + let notes = export_losses_notes(&every, "csv", None, "t.csv"); + assert_eq!(notes.len(), 1, "{notes:?}"); + assert!(notes[0].contains("columns 'longitude_1' and 'latitude'"), "{notes:?}"); + // As said: reading takes a field for the longitude. + let (back, _) = read_csv(&out, "t").unwrap(); + assert_ne!(back.features[0].geometry, Some(Geometry::Point(Coord::new(20.0, 10.0)))); + // A name left, or a layer that isn't of points: nothing to say. + assert!(export_losses_notes(&layer(&LON_NAMES[1..]), "csv", None, "t.csv").is_empty()); + assert_eq!(csv_coordinates_note(&every.fields, GeometryType::Polyline, "t.csv"), None); } #[test] diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index 6d927274de..f86e2d68e0 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -709,6 +709,7 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk // CSV holds a point a row, GPX a name for each feature and no areas. if ext == "csv" { said.extend(csv.notes(path)); + said.extend(rustgis_data::io::csv_coordinates_note(&ds.fields, ds.geometry_type, path)); } if ext == "gpx" { said.extend(rustgis_data::io::gpx_notes(ds, rustgis_data::io::name_field(ds), path)); From 1ec3f8832e9ba8261f99c4dc865d1e4afadfd2aa Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 12 Nov 2026 17:37:54 -0600 Subject: [PATCH 3/3] fix(data): a CSV export says which columns re-reading takes as coordinates When a point layer's fields have every name read as a longitude or latitude, adding the file back takes those fields as the coordinates, which moves the points. The note now says so, and names the columns. A streamed export of a layer that declares no type knows from its chunks that it wrote coordinates, so it says so too. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/io.rs | 81 +++++++++++++++++++++------------- crates/engine/src/cmd/share.rs | 2 +- 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index b57be9380f..1c1e64552b 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -1872,30 +1872,16 @@ fn csv_cell(v: &Value) -> String { /// The names of a point layer's longitude and latitude columns in CSV: the first of the names /// reading takes for each that no field has, ignoring case and the spaces around a name (reading -/// trims them), and whether there was one of each. -/// A layer with a field of every such name gets `longitude_1` or `latitude_1`, which reading -/// doesn't take as coordinates ([`csv_coordinates_note`]). -fn csv_coordinate_names(fields: &[Field]) -> ([String; 2], bool) { +/// trims them). +/// A layer with a field of every such name gets `longitude_1` or `latitude_1`, and reading takes +/// that field instead ([`CsvLosses::coordinates_note`]). +fn csv_coordinate_names(fields: &[Field]) -> [String; 2] { let taken = |name: &str| fields.iter().any(|f| f.name.trim().eq_ignore_ascii_case(name)); let pick = |base: &str, known: &[&str]| { let name = known.iter().map(|n| n.to_string()).chain((1..).map(|i| format!("{base}_{i}"))).find(|n| !taken(n)); name.unwrap_or_default() }; - let names = [pick("longitude", LON_NAMES), pick("latitude", LAT_NAMES)]; - let read = LON_NAMES.contains(&names[0].as_str()) && LAT_NAMES.contains(&names[1].as_str()); - (names, read) -} - -/// What a CSV export of a point layer (`gt`) to `target` says when its fields take every name -/// reading takes for a longitude or a latitude: the coordinates are in columns reading doesn't -/// find on its own. `None` otherwise. -pub fn csv_coordinates_note(fields: &[Field], gt: GeometryType, target: &str) -> Option { - let ([x, y], read) = csv_coordinate_names(fields); - (gt == GeometryType::Point && !read).then(|| { - format!( - "{target}: the fields have every name read as a longitude or latitude, so the coordinates are in columns '{x}' and '{y}', which adding the file back doesn't take as coordinates unless they are picked" - ) - }) + [pick("longitude", LON_NAMES), pick("latitude", LAT_NAMES)] } /// A dataset as CSV text: its fields, and a point layer's longitude and latitude ([`CsvWriter`]). @@ -1921,7 +1907,7 @@ impl CsvWriter { if point { // The coordinate columns come first, so reading takes them rather than a field // named `lat` or `x`. - head.extend(csv_coordinate_names(fields).0); + head.extend(csv_coordinate_names(fields)); } head.extend(fields.iter().map(|f| f.name.clone())); w.write_record(&head).map_err(|e| DataError::Format(e.to_string()))?; @@ -2721,12 +2707,15 @@ pub struct CsvLosses { /// Features whose geometry isn't written: in a line or polygon layer (or one that declares /// no type), and in a point layer those with no point to write (an empty multipoint). pub unwritten: usize, + /// Written with coordinate columns, as a point layer: a streamed export knows this from its + /// chunks when the layer declares no type ([`CsvLosses::coordinates_note`]). + pub points: bool, } impl CsvLosses { /// The losses of `features` in a layer of type `gt`; a streamed export adds up its chunks'. pub fn of(features: &[crate::Feature], gt: GeometryType) -> Self { - let mut out = Self::default(); + let mut out = Self { points: gt == GeometryType::Point, ..Self::default() }; for g in features.iter().filter_map(|f| f.geometry.as_ref()) { match (gt, g) { (GeometryType::Point, Geometry::Point(_)) => {} @@ -2757,6 +2746,31 @@ impl CsvLosses { } notes } + + /// What a CSV export with these losses says when reading the file back wouldn't take its + /// coordinate columns: its `fields` have every name reading takes for a longitude or a + /// latitude, and reading takes those fields instead. `None` otherwise. + pub fn coordinates_note(self, fields: &[Field], target: &str) -> Option { + if !self.points { + return None; + } + let header: Vec = csv_coordinate_names(fields).into_iter().chain(fields.iter().map(|f| f.name.clone())).collect(); + let found = found_xy(&header); + if found == Some((0, 1)) { + return None; + } + let name = |i: usize| dbf_notice_name(header.get(i).map_or("", String::as_str)); + let read = match found { + Some((x, y)) => format!("reads columns '{}' and '{}' as the coordinates, which moves the points", name(x), name(y)), + None => "reads no coordinates".to_string(), + }; + let target: String = target.chars().map(dbf_notice_char).collect(); + Some(format!( + "{target}: the fields have every name read as a longitude or latitude, so the coordinates are in columns '{}' and '{}'; adding the file back {read} unless those columns are picked", + name(0), + name(1) + )) + } } impl std::ops::AddAssign for CsvLosses { @@ -2764,17 +2778,19 @@ impl std::ops::AddAssign for CsvLosses { fn add_assign(&mut self, other: Self) { self.as_one_point += other.as_one_point; self.unwritten += other.unwritten; + self.points |= other.points; } } /// What a CSV or GPX file (extension `ext`) of the whole dataset `ds` can't hold, written to -/// `target`: [`CsvLosses`]' notices and [`csv_coordinates_note`], or [`gpx_notes`] with each feature named from `name_field`; +/// `target`: [`CsvLosses`]' notices, or [`gpx_notes`] with each feature named from `name_field`; /// none for another format. A streamed export counts CSV's a chunk at a time instead. pub fn export_losses_notes(ds: &Dataset, ext: &str, name_field: Option, target: &str) -> Vec { match ext { "csv" => { - let mut notes = CsvLosses::of(&ds.features, ds.geometry_type).notes(target); - notes.extend(csv_coordinates_note(&ds.fields, ds.geometry_type, target)); + let losses = CsvLosses::of(&ds.features, ds.geometry_type); + let mut notes = losses.notes(target); + notes.extend(losses.coordinates_note(&ds.fields, target)); notes } "gpx" => gpx_notes(ds, name_field, target), @@ -4136,14 +4152,14 @@ mod tests { ds.push(Some(square), vec![]); ds.push(None, vec![]); let losses = CsvLosses::of(&ds.features, ds.geometry_type); - assert_eq!(losses, CsvLosses { as_one_point: 0, unwritten: 1 }); + assert_eq!(losses, CsvLosses { as_one_point: 0, unwritten: 1, points: false }); assert!(losses.notes("t.csv")[0].contains("the geometry of 1 feature wasn't written")); assert!(!to_csv(&ds).unwrap().contains("longitude")); let points = [crate::Feature { id: 0, geometry: Some(Geometry::Point(Coord::new(1.0, 2.0))), attrs: vec![] }]; - assert_eq!(CsvLosses::of(&points, GeometryType::Point), CsvLosses::default()); + assert_eq!(CsvLosses::of(&points, GeometryType::Point), CsvLosses { points: true, ..CsvLosses::default() }); // A multipoint without a point in a point layer gets empty coordinate cells. let empty = [crate::Feature { id: 1, geometry: Some(Geometry::MultiPoint(vec![])), attrs: vec![] }]; - assert_eq!(CsvLosses::of(&empty, GeometryType::Point), CsvLosses { as_one_point: 0, unwritten: 1 }); + assert_eq!(CsvLosses::of(&empty, GeometryType::Point), CsvLosses { as_one_point: 0, unwritten: 1, points: true }); } /// GPX leaves out a feature without geometry and a multipoint without a point (no waypoint @@ -4200,13 +4216,16 @@ mod tests { assert!(out.starts_with("longitude_1,latitude,"), "{out}"); let notes = export_losses_notes(&every, "csv", None, "t.csv"); assert_eq!(notes.len(), 1, "{notes:?}"); - assert!(notes[0].contains("columns 'longitude_1' and 'latitude'"), "{notes:?}"); - // As said: reading takes a field for the longitude. + assert_eq!( + notes[0], + "t.csv: the fields have every name read as a longitude or latitude, so the coordinates are in columns 'longitude_1' and 'latitude'; adding the file back reads columns 'longitude' and 'latitude' as the coordinates, which moves the points unless those columns are picked" + ); + // As said: reading takes the field `longitude` (1) for the longitude. let (back, _) = read_csv(&out, "t").unwrap(); - assert_ne!(back.features[0].geometry, Some(Geometry::Point(Coord::new(20.0, 10.0)))); + assert_eq!(back.features[0].geometry, Some(Geometry::Point(Coord::new(1.0, 10.0)))); // A name left, or a layer that isn't of points: nothing to say. assert!(export_losses_notes(&layer(&LON_NAMES[1..]), "csv", None, "t.csv").is_empty()); - assert_eq!(csv_coordinates_note(&every.fields, GeometryType::Polyline, "t.csv"), None); + assert_eq!(CsvLosses::of(&[], GeometryType::Polyline).coordinates_note(&every.fields, "t.csv"), None); } #[test] diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index f86e2d68e0..da948ce856 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -709,7 +709,7 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option, gpkg: Gpk // CSV holds a point a row, GPX a name for each feature and no areas. if ext == "csv" { said.extend(csv.notes(path)); - said.extend(rustgis_data::io::csv_coordinates_note(&ds.fields, ds.geometry_type, path)); + said.extend(csv.coordinates_note(&ds.fields, path)); } if ext == "gpx" { said.extend(rustgis_data::io::gpx_notes(ds, rustgis_data::io::name_field(ds), path));