Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/data/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct Exported {
/// Vertices beyond 85.05° north or south, written at 85.05° by an export in Web Mercator,
/// where it ends (0 in another system). See [`crate::web_mercator_moved_note`].
pub beyond_web_mercator: usize,
/// Geometry a CSV can't hold, counted over all chunks (none for the other formats).
/// Geometry and text changed by CSV export, counted over all chunks.
pub csv: crate::io::CsvLosses,
/// Per field of the layer, values of the source's date fields that weren't dates, so are
/// written as null ([`crate::source::Batch::unread_dates`], said by
Expand Down Expand Up @@ -183,7 +183,7 @@ fn write_chunks_as(
total = total.saturating_add(chunk.len());
null_numbers = null_numbers.saturating_add(numbers_written_as_null(&chunk, &layer.fields, ext));
if ext == "csv" {
csv += crate::io::CsvLosses::of(&chunk, geometry_type);
csv += crate::io::CsvLosses::of(&chunk, geometry_type, &layer.fields)?;
}
if ext == "gpkg" {
crate::source::add_unread_dates(&mut utc_dates, &crate::io::offset_dates(chunk.iter(), &layer.fields));
Expand Down
20 changes: 19 additions & 1 deletion crates/data/src/export/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ fn an_explicit_export_format_ignores_case_and_the_filename() {
std::fs::remove_dir_all(d).unwrap();
}

#[test]
fn a_streamed_csv_counts_guarded_values_across_chunks() {
let d = folder("csv-guarded");
let mut ds = Dataset::new("places", GeometryType::None, vec![Field::new("value", FieldType::Text)]);
for value in ["-007", "-093.1445780", "=SUM(A1)"] {
ds.push_next(None, vec![Value::Text(value.into())]);
}
let path = d.join("places.csv");
let exported = write_chunks_in(&Runs::new(&ds, 1), &ds, &path, Crs::LonLat, 1).unwrap();
assert_eq!(exported.csv.guarded, [2]);
assert_eq!(std::fs::read_to_string(path).unwrap(), "value\n'-007\n-093.1445780\n'=SUM(A1)\n");
std::fs::remove_dir_all(d).unwrap();
}

/// An export in Web Mercator written a chunk at a time counts the vertices beyond 85.05° it
/// writes at 85.05°, as it writes them: from a source that declares no extent, whose features
/// beyond aren't in its first chunk. In longitude/latitude, or within 85.05°, it counts none.
Expand Down Expand Up @@ -204,7 +218,11 @@ fn a_streamed_export_writes_what_a_whole_export_writes() {
unfit,
beyond_web_mercator: 0,
// Counted over the chunks as the whole layer counts it.
csv: if ext == "csv" { crate::io::CsvLosses::of(&whole.features, whole.geometry_type) } else { Default::default() },
csv: if ext == "csv" {
crate::io::CsvLosses::of(&whole.features, whole.geometry_type, &whole.fields).unwrap()
} else {
Default::default()
},
unread_dates: Vec::new(),
rounded_dates: Vec::new(),
null_text: Vec::new(),
Expand Down
193 changes: 153 additions & 40 deletions crates/data/src/io.rs

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions crates/engine/src/cmd/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option<Crs>, gpkg: Gpk
return Err(format!("{path}: {e}"));
}
let parquet = matches!(ext.as_str(), "parquet" | "geoparquet");
let (written, left_out, skipped, table, unfit, null_numbers, moved, csv, dates) = match &chunks {
let (written, left_out, skipped, table, unfit, null_numbers, moved, format_notes, dates) = match &chunks {
Some((src, layer)) => {
let (e, table) =
rustgis_data::export::write_source_to(src.as_ref(), layer, Path::new(&path), crs, &gpkg).map_err(|e| e.to_string())?;
Expand All @@ -731,16 +731,19 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option<Crs>, gpkg: Gpk
}
dates.extend(e.date_messages.iter().map(|n| format!("{path}: {n}")));
dates.extend(e.read_notes.iter().map(|n| format!("{path}: {}: {n} when read for the export", layer.name)));
(e.written, e.left_out, e.skipped, table, unfit, e.null_numbers, moved, e.csv, dates)
let mut format_notes = e.csv.notes(&ds.fields, path);
format_notes.extend(e.csv.coordinates_note(&ds.fields, path));
(e.written, e.left_out, e.skipped, table, unfit, e.null_numbers, moved, format_notes, dates)
}
None => {
let format_notes =
rustgis_data::io::export_losses_notes(ds, &ext, rustgis_data::io::name_field(ds), path).map_err(|e| e.to_string())?;
let (left_out, table) = rustgis_data::io::write_path_to(ds, Path::new(&path), crs, &gpkg).map_err(|e| e.to_string())?;
let unfit = if parquet { rustgis_data::source::parquet::values_note(ds, path) } else { None };
let null_numbers = rustgis_data::export::numbers_written_as_null(&ds.features, &ds.fields, &ext);
let csv = if ext == "csv" { rustgis_data::io::CsvLosses::of(&ds.features, ds.geometry_type) } else { Default::default() };
let mut dates = read_notes(path, &ds.name, &unreadable);
dates.extend(gpkg_dates_note(ds, &ext, path));
(ds.features.len().saturating_sub(left_out), left_out, unread, table, unfit, null_numbers, None, csv, dates)
(ds.features.len().saturating_sub(left_out), left_out, unread, table, unfit, null_numbers, None, format_notes, dates)
}
};
// The features written give their own extent. A streamed layer written a chunk at a time has
Expand Down Expand Up @@ -770,14 +773,7 @@ fn write_layer(l: &rustgis_map::Layer, path: &str, asked: Option<Crs>, gpkg: Gpk
// geometry it can draw, of the layer's kind).
let mut said = Vec::new();
said.extend(rustgis_data::io::left_out_note(path, &ext, left_out));
// 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(csv.coordinates_note(&ds.fields, path));
}
if ext == "gpx" {
said.extend(rustgis_data::io::gpx_notes(ds, rustgis_data::io::name_field(ds), path));
}
said.extend(format_notes);
if skipped > 0 {
said.push(format!("{path}: {skipped} rows couldn't be read and were left out"));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/share/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub(in crate::cmd) fn table_work(s: &Session, p: &Value) -> Result<Work, String>
notes.extend(rustgis_data::io::dates_as_text_note(&ds.fields, &path));
notes.extend(super::gpkg_dates_note(&ds, &ext, &path));
let name_field = rustgis_data::io::name_field(&ds);
notes.extend(rustgis_data::io::export_losses_notes(&ds, &ext, name_field, &path));
notes.extend(rustgis_data::io::export_losses_notes(&ds, &ext, name_field, &path).map_err(|e| e.to_string())?);
let (left_out, table) =
rustgis_data::io::write_path_to(&ds, Path::new(&path), rustgis_data::raster::Crs::LonLat, &gpkg).map_err(|e| e.to_string())?;
if let Some(note) = rustgis_data::io::left_out_note(&path, &ext, left_out) {
Expand Down
5 changes: 2 additions & 3 deletions crates/engine/src/cmd/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ pub(crate) fn convert_work(s: &Session, p: &Value) -> Result<super::exports::Wor
.map_err(|e| e.to_string())?;
let mut notes = rustgis_data::export_notes([(&ds, layer.extent())], &path, rustgis_data::raster::Crs::LonLat);
if format == "csv" {
notes.extend(result.csv.notes(&path));
notes.extend(result.csv.notes(&ds.fields, &path));
}
if result.skipped > 0 {
notes.push(format!("{path}: {} rows couldn't be read and were left out", result.skipped));
Expand Down Expand Up @@ -2014,8 +2014,7 @@ fn converted(ds: &Dataset, p: &Value) -> Result<(String, Vec<String>), String> {
// Every format here is longitude/latitude.
let target = ps(p, "path").unwrap_or(&ds.name);
let mut notes = rustgis_data::export_notes([(ds, ds.extent())], target, rustgis_data::raster::Crs::LonLat);
// CSV holds a point a row, GPX a name for each feature and no areas.
notes.extend(rustgis_data::io::export_losses_notes(ds, format, name_field, target));
notes.extend(rustgis_data::io::export_losses_notes(ds, format, name_field, target).map_err(|e| e.to_string())?);
// KML and GPX hold features with geometry only.
if let ext @ ("kml" | "gpx") = format {
let without =
Expand Down
34 changes: 20 additions & 14 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24631,18 +24631,24 @@ fn csv_and_gpx_exports_say_what_they_leave_out() {
"{n:?}"
);
let n = said(&mut s, "share.exportLayer", json!({"layer": "Sites", "path": out("sites.gpx")}));
assert!(n.len() == 1 && n[0].contains("1 multipoint was written as a waypoint for each"), "{n:?}");
assert!(n.len() == 2 && n[0].contains("'height'") && n[1].contains("1 multipoint was written as a waypoint for each"), "{n:?}");
let n = said(&mut s, "data.convert", json!({"layer": "Areas", "format": "gpx"}));
assert!(n.iter().any(|t| t.contains("'height'")) && n.iter().any(|t| t.contains("tracks")), "{n:?}");
let n = said(&mut s, "data.convert", json!({"layer": "Areas", "format": "csv"}));
assert!(n.iter().any(|t| t.contains("wasn't written, only the attributes")), "{n:?}");
// A row without geometry, which KML and GPX can't hold, is counted.
// Both formats count the row without geometry and name the omitted null field.
let mut rows = layer("Rows", GeometryType::Point, vec![(Geometry::Point(Coord::new(1.0, 2.0)), vec![V::Text("P".into()), V::Null])]);
rows.push(None, vec![V::Text("no place".into()), V::Null]);
s.map_mut().add_dataset(rows);
for format in ["kml", "gpx"] {
let n = said(&mut s, "data.convert", json!({"layer": "Rows", "format": format}));
assert!(n.len() == 1 && n[0].contains("1 features without drawable geometry were left out"), "{format}: {n:?}");
assert!(n.iter().any(|t| t.contains("1 features without drawable geometry were left out")), "{format}: {n:?}");
assert_eq!(n.len(), 2, "{format}: {n:?}");
if format == "kml" {
assert!(n.iter().any(|t| t.contains("KML omitted 1 field with no values in the placemarks written ('height')")), "{n:?}");
} else {
assert!(n.iter().any(|t| t.contains("1 field wasn't written: 'height'")), "{n:?}");
}
}
// A standalone table keeps geometry too: Export Table says the same, and nothing for a clean one.
let areas = layer("Area table", GeometryType::Polygon, vec![(square(0.0, true), vec![V::Text("A".into()), V::Float(2.5)])]);
Expand All @@ -24654,17 +24660,17 @@ fn csv_and_gpx_exports_say_what_they_leave_out() {
assert!(n.len() == 1 && n[0].contains("the geometry of 1 feature wasn't written"), "{n:?}");
let n = said(&mut s, "share.exportTable", json!({"table": 9001, "path": out("table.gpx")}));
assert!(n.iter().any(|t| t.contains("'height'")) && n.iter().any(|t| t.contains("1 hole left out")), "{n:?}");
for ext in ["csv", "gpx"] {
let n = said(&mut s, "share.exportTable", json!({"table": 9002, "path": out(&format!("clean-table.{ext}"))}));
assert_eq!(n, Vec::<String>::new(), "{ext}");
}
// Points alone, their other field empty: CSV and GPX hold them whole.
for ext in ["csv", "gpx"] {
assert_eq!(
said(&mut s, "share.exportLayer", json!({"layer": "Clean", "path": out(&format!("clean.{ext}"))})),
Vec::<String>::new()
);
assert_eq!(said(&mut s, "data.convert", json!({"layer": "Clean", "format": ext})), Vec::<String>::new());
let n = said(&mut s, "share.exportTable", json!({"table": 9002, "path": out("clean-table.csv")}));
assert_eq!(n, Vec::<String>::new());
// Points alone, their other field empty: CSV holds them whole; GPX names the field it leaves out.
assert_eq!(said(&mut s, "share.exportLayer", json!({"layer": "Clean", "path": out("clean.csv")})), Vec::<String>::new());
assert_eq!(said(&mut s, "data.convert", json!({"layer": "Clean", "format": "csv"})), Vec::<String>::new());
for n in [
said(&mut s, "share.exportTable", json!({"table": 9002, "path": out("clean-table.gpx")})),
said(&mut s, "share.exportLayer", json!({"layer": "Clean", "path": out("clean.gpx")})),
said(&mut s, "data.convert", json!({"layer": "Clean", "format": "gpx"})),
] {
assert!(n.len() == 1 && n[0].contains("1 field wasn't written: 'height'"), "{n:?}");
}
std::fs::remove_dir_all(dir).unwrap();
}
Expand Down
8 changes: 3 additions & 5 deletions crates/engine/tests/never_silent_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const GEOMETRY_ONLY: [&str; 4] = ["fgb", "shp", "kml", "gpx"];

/// Changes a format makes without a notice, as (format, the start of a change from [`changes`],
/// why). Each is what the format can hold or how its reader works, not a loss of a value's
/// number or text, except trimming (Shapefile, KML, GPX), blanks read as null and (KML, GPX) a
/// field of nulls left out, which are kept here until they are said (#1907). CSV keeps the spaces
/// number or text, except trimming (Shapefile, KML, GPX) and blanks read as null, which are kept
/// here until they are said (#1907). CSV keeps the spaces
/// around a value (#2059).
const LIMITS: &[(&str, &str, &str)] = &[
// CSV holds text: reading types each column from its values (`text_type`) and reads an empty
Expand Down Expand Up @@ -55,14 +55,12 @@ const LIMITS: &[(&str, &str, &str)] = &[
];

/// KML and GPX hold text, read with types from the values as for CSV (`type_text_columns`); the
/// name field is written as the feature's name and read back as `name`, and a field that is
/// null everywhere isn't in the file.
/// name field is written as the feature's name and read back as `name`.
const TEXT_FORMAT_LIMITS: &[&str] = &[
"text trimmed",
"blank text became null",
"field type changed",
"field of nulls type changed",
"field of nulls missing",
"field renamed",
"boolean became text",
"date became text",
Expand Down
3 changes: 1 addition & 2 deletions crates/ui-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4970,8 +4970,7 @@ impl RustGisApp {
dbf_notes.push(format!("{name}: {} rows couldn't be read and were left out", lost.rows));
}
dbf_notes.extend(rustgis_engine::cmd::share::read_notes(&name, &l.name, &lost));
// CSV holds a point a row, GPX a name for each feature and no areas.
dbf_notes.extend(io::export_losses_notes(ds, &ext, io::name_field(ds), &name));
dbf_notes.extend(io::export_losses_notes(ds, &ext, io::name_field(ds), &name).map_err(|e| e.to_string())?);
if let Some(note) = io::left_out_note(&name, &ext, left_out) {
self.session.notify(note, false);
}
Expand Down
Loading
Loading