From 775e5240bc3d69e21f01b2924385facc841d811d Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 24 Nov 2026 16:44:56 +0100 Subject: [PATCH 1/6] Field Package return by a list of changes in each layer's own system (#2032 C2f-4b-5) --- crates/data/src/io.rs | 4 +- crates/engine/src/cmd/field_audit_tests.rs | 99 +++++++++++++++ crates/engine/src/cmd/field_changes.rs | 136 +++++++++++++++++---- docs/user-guide/data-formats.md | 3 +- docs/user-guide/field-work.md | 8 +- 5 files changed, 220 insertions(+), 30 deletions(-) diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 13b04eaf17..013a568742 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -1663,8 +1663,8 @@ pub(crate) fn places_lonlat_extent(g: &Geometry, t: &crate::raster::Transformer) /// them, held in `crs`, though a place of theirs can't be given in longitude/latitude: they don't /// draw (the map says so too), and a step that needs longitude/latitude leaves them out and says /// so, or refuses naming one where leaving it out would lose it (a write-back, a sync of an -/// offline area holding it, a Field Package's list of changes) or a contract says so (the plugin -/// host's layers) (#2032 C2f-4b). `all` when none of the file's has a place there: then its system +/// offline area holding it) or a contract says so (the plugin host's layers) (#2032 C2f-4b). A +/// Field Package's list of changes gives it in its layer's own system. `all` when none of the file's has a place there: then its system /// doesn't seem to match its coordinates, and Define Projection can set the one that does, as /// nothing was lost. `None` for none. pub(crate) fn kept_unplaced_note(what: &str, n: usize, all: bool, crs: Crs) -> Option { diff --git a/crates/engine/src/cmd/field_audit_tests.rs b/crates/engine/src/cmd/field_audit_tests.rs index fa3366608c..a9d4f46869 100644 --- a/crates/engine/src/cmd/field_audit_tests.rs +++ b/crates/engine/src/cmd/field_audit_tests.rs @@ -716,6 +716,105 @@ fn a_layer_held_in_its_own_system_comes_back_with_only_its_changes() { assert_eq!(home.map().layer(hc).unwrap().dataset.coord_crs(), rustgis_data::raster::Crs::WebMercator); } +/// A layer held in its own system (Equal Earth) comes back by a list of version 2 giving its shapes +/// in that system (#2032 C2f-4b): a point moved in the field lands at home exactly as the field +/// holds it, and one moved where it has no place in longitude/latitude (which a list in +/// longitude/latitude refused to send) comes back too, as it is held. +#[test] +fn a_layer_held_in_its_own_system_comes_back_exact_by_a_list_in_that_system() { + use rustgis_data::{Coord, Geometry, raster::Crs}; + let equal_earth = Crs::from_epsg(8857).unwrap(); + let temp = Temp::new("native-list"); + let mut home = home(&temp); + let hc = layer(&home, "Cities"); + home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(equal_earth).unwrap(); + let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap(); + let mut a = phone(&temp, &out, "a"); + let cities = layer(&a, "Cities"); + let (_, unedited) = package(&mut a, &Value::Null).unwrap(); + assert_eq!(changes_in(&unedited).unwrap()["version"], json!(1), "nothing changed: a list any release reads"); + let (moved, far) = { + let ds = &mut a.map_mut().layer_mut(cities).unwrap().dataset; + let moved = ds.features[0].geometry.clone(); + let Some(Geometry::Point(c)) = moved else { panic!("{moved:?}") }; + let to = Geometry::Point(Coord::new(c.x + 0.123_456_789, c.y - 0.987_654_321)); + ds.features[0].geometry = Some(to.clone()); + // No place in longitude/latitude in Equal Earth. + ds.features[1].geometry = Some(Geometry::Point(Coord::new(1e9, 0.0))); + ((ds.features[0].id, to), ds.features[1].id) + }; + let (_, back) = package(&mut a, &Value::Null).unwrap(); + let changes = changes_in(&back).unwrap(); + assert_eq!(changes["version"], json!(2), "{changes}"); + assert_eq!(changes["layers"][0]["crs"], serde_json::to_value(equal_earth).unwrap(), "{changes}"); + let path = temp.0.join("a-back.rgisfield"); + std::fs::write(&path, &back).unwrap(); + let done = home.run("offline.bringBack", &json!({"path": path.display().to_string()})).unwrap(); + assert_eq!((done["updated"].as_u64(), done["conflicts"].as_u64()), (Some(2), Some(0)), "{done}"); + let place = |id: u64| { + let l = home.map().layer(hc).unwrap(); + l.dataset.features.iter().find(|f| f.id == id).and_then(|f| f.geometry.clone()) + }; + assert_eq!(place(moved.0), Some(moved.1), "exactly as the field holds it"); + assert_eq!(place(far), Some(Geometry::Point(Coord::new(1e9, 0.0)))); + assert_eq!(home.map().layer(hc).unwrap().dataset.coord_crs(), equal_earth); +} + +/// A list giving a layer's shapes in its own system comes back while home holds the layer in +/// another (Define Projection meanwhile): its shapes are put in longitude/latitude to be brought +/// back there, as a list of version 1's are. +#[test] +fn a_list_in_a_layers_own_system_comes_back_to_a_layer_held_in_another() { + use rustgis_data::{Geometry, raster::Crs}; + let temp = Temp::new("native-list-elsewhere"); + let mut home = home(&temp); + let hc = layer(&home, "Cities"); + home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); + let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap(); + let mut a = phone(&temp, &out, "a"); + let cities = layer(&a, "Cities"); + let name = text_field(&a, cities); + let edited = a.map().layer(cities).unwrap().dataset.features[0].id; + a.run("edit.setValue", &json!({"layer": cities, "row": 0, "field": name, "value": "Moved too"})).unwrap(); + let ds = &mut a.map_mut().layer_mut(cities).unwrap().dataset; + let Some(Geometry::Point(c)) = ds.features[0].geometry else { panic!() }; + ds.features[0].geometry = Some(Geometry::Point(rustgis_data::Coord::new(c.x + 1000.0, c.y))); + // Home holds the layer in Web Mercator now. + home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(Crs::WebMercator).unwrap(); + let (done, said) = bring_back(&temp, &mut a, &mut home, "a"); + // Home's shapes were converted too (a round trip through longitude/latitude), so the moved + // one reads as changed at home as well: the field's wins, and that is said, as with a list in + // longitude/latitude. + assert_eq!(done["updated"].as_u64(), Some(1), "{done} {said:?}"); + assert_eq!(value_of(&home, hc, edited, &name).as_deref(), Some("Moved too")); + assert_eq!(home.map().layer(hc).unwrap().dataset.coord_crs(), Crs::WebMercator); + let l = home.map().layer(hc).unwrap(); + let Some(Geometry::Point(now)) = l.dataset.features.iter().find(|f| f.id == edited).and_then(|f| f.geometry.clone()) else { panic!() }; + // 1 km east in Equal Earth, about 1 km east in Web Mercator too. + let Geometry::Point(was) = Crs::from_epsg(8857).unwrap().to(Crs::WebMercator).unwrap().geometry(&Geometry::Point(c)) else { panic!() }; + assert!((now.x - was.x) > 500.0 && (now.x - was.x) < 2000.0 && (now.y - was.y).abs() < 100.0, "{now:?} {was:?}"); +} + +/// A list of a version this RustGIS doesn't write is refused, as is one of version 1 that gives a +/// layer's system, which none of version 1 has. +#[test] +fn a_list_of_an_unknown_version_or_a_system_version_1_hasnt_is_refused() { + let list = |version: u32, crs: Value| { + let mut layer = json!({"layer": 1, "added": [5]}); + if !crs.is_null() { + layer["crs"] = crs; + } + serde_json::to_vec(&json!({"version": version, "package": "p", "layers": [layer]})).unwrap() + }; + let utm = serde_json::to_value(rustgis_data::raster::Crs::from_epsg(32632).unwrap()).unwrap(); + assert!(crate::cmd::field_changes::Changes::read(&list(1, Value::Null), "p").is_ok()); + assert!(crate::cmd::field_changes::Changes::read(&list(2, utm.clone()), "p").is_ok()); + let newer = crate::cmd::field_changes::Changes::read(&list(3, Value::Null), "p").unwrap_err(); + assert!(newer.contains("newer RustGIS"), "{newer}"); + let damaged = crate::cmd::field_changes::Changes::read(&list(1, utm), "p").unwrap_err(); + assert!(damaged.contains("damaged"), "{damaged}"); +} + /// Two packages of one area. A adds a field and fills it in for a city; B, given the city before /// the field was there, deletes it. The city has a change B never saw, so it is kept, and said. #[test] diff --git a/crates/engine/src/cmd/field_changes.rs b/crates/engine/src/cmd/field_changes.rs index 46f5ef7be2..4e7e11605e 100644 --- a/crates/engine/src/cmd/field_changes.rs +++ b/crates/engine/src/cmd/field_changes.rs @@ -23,6 +23,7 @@ use std::collections::{HashMap, HashSet}; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; +use rustgis_data::raster::Crs; use rustgis_data::{Geometry, RowRef, Value}; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -36,8 +37,10 @@ pub(crate) const ENTRY: &str = "changes.v1.json"; /// given. A returned package keeps the identity but never has it, so a device opening a return /// never takes its edited rows for those home gave. pub(crate) const GIVEN: &str = "given.v1.json"; -/// The version of the list this RustGIS writes and reads. -const VERSION: u32 = 1; +/// The newest version of the list this RustGIS writes and reads. Version 2 gives each layer's +/// shapes in its own system ([`LayerChanges::crs`]); a list in which every layer's are in +/// longitude/latitude is written as version 1, which releases before it read too. +const VERSION: u32 = 2; /// The bytes of one index record: id, place in the rows file, length, fingerprint. const RECORD: usize = 8 + 8 + 4 + 16; /// The longest row read back from a given copy, in bytes. @@ -80,6 +83,12 @@ pub(crate) struct LayerChanges { /// to the home layer, so a field removed at home meanwhile isn't added back. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub fields: Vec, + /// The system the shapes given here are in: the layer's own when the package gave it held in + /// one ([`rustgis_data::Dataset::held_in`]), so a feature whose place can't be given in + /// longitude/latitude comes back exact (#2032 C2f-4b). `None` for longitude/latitude, as + /// every list of version 1. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub crs: Option, } /// A feature's values as the package gave them. @@ -112,13 +121,16 @@ impl Changes { if header.version > VERSION { return Err("the field package's list of changes was made by a newer RustGIS: update RustGIS to bring it back".into()); } - if header.version != VERSION { + if header.version == 0 { return Err(format!( "the field package's list of changes is of version {}, which no RustGIS writes: it is damaged", header.version )); } let changes: Changes = serde_json::from_slice(bytes).map_err(damaged)?; + if changes.version == 1 && changes.layers.iter().any(|l| l.crs.is_some()) { + return Err("the field package's list of changes gives a layer's system in a version that has none: it is damaged".into()); + } if changes.package != package { return Err("the field package's list of changes is of another package: make a new field package".into()); } @@ -169,6 +181,10 @@ struct BaseLayer { /// Its fields' names, in the order the rows hold their values. fields: Vec, rows: usize, + /// The system the rows' shapes are in: the layer's own, as it was given held in it. `None` + /// for longitude/latitude, as every copy written before #2032 C2f-4b. + #[serde(default, skip_serializing_if = "Option::is_none")] + crs: Option, } /// A row as the rows file holds it. @@ -182,7 +198,8 @@ struct BaseRow { /// holds the copy's field `i` (`None`: Null). struct Print<'a> { row: RowRef<'a>, - /// The row's shape in longitude/latitude (converted one row at a time, never the layer). + /// The row's shape in the copy's system ([`BaseLayer::crs`]; converted one row at a time when + /// the layer is held in another, never the layer). geometry: Option<&'a Geometry>, fields: &'a [Option], } @@ -223,7 +240,7 @@ impl Write for Fnv { /// FNV-1a's 128-bit offset basis. const FNV_BASIS: u128 = 0x6c62_272e_07bb_0142_62b8_2175_6295_c58d; -/// The fingerprint of a row as [`Print`] writes it, in longitude/latitude. Rows are fingerprinted +/// The fingerprint of a row as [`Print`] writes it, in the copy's system. Rows are fingerprinted /// as they are held once read, given and sent alike, so a value that reads back as it was saved /// (as every one does) can't make an unedited row look edited. fn fingerprint(print: &Print<'_>) -> Result<[u8; 16], String> { @@ -408,7 +425,7 @@ pub(crate) fn keep_rows(s: &mut Session, _p: &serde_json::Value) -> crate::CmdRe /// Keeps the rows `given` holds on this device under its package's identity, unless it keeps them /// already. The files are written beside the folder and moved into place once complete, so a copy /// cut short (cancelled, or failed) is never read and the next open of the package writes it anew. -/// Reads each row once and writes it once, in longitude/latitude as a returning map is compared; +/// Reads each row once and writes it once, in its layer's own system as a returning map is compared; /// Cancel is looked at between rows, and progress counts them. pub(crate) fn keep_base(given: &GivenRows) -> Kept { rustgis_data::progress::check().map_err(|e| e.to_string())?; @@ -479,9 +496,10 @@ fn write_base(given: &GivenRows, dir: &Path) -> Result<(), String> { progress::begin(given.layers.iter().map(|(_, _, d)| d.features.len()).sum()); let mut layers = Vec::new(); for (id, name, dataset) in &given.layers { - // Each row's shape is put in longitude/latitude as it is written: a layer held in another - // system isn't copied whole. - let to = dataset.lonlat_conversion().map_err(|e| format!("{name}: {e}"))?; + // Each row's shape is kept in the layer's own system, as it is held: a feature whose place + // can't be given in longitude/latitude comes back exact too. + let crs = dataset.coord_crs(); + let to = crs.to(crs).map_err(|e| format!("{name}: {e}"))?; let ds = dataset; let fields: Vec> = (0..ds.fields.len()).map(Some).collect(); let rows_path = dir.join(format!("{id}.rows")); @@ -491,7 +509,7 @@ fn write_base(given: &GivenRows, dir: &Path) -> Result<(), String> { for row in ds.features.rows() { progress::step(1).map_err(|e| e.to_string())?; line.clear(); - let geometry = in_lonlat(&to, row, name)?; + let geometry = in_system(&to, row, name)?; serde_json::to_writer(&mut line, &Print { row, geometry: geometry.as_deref(), fields: &fields }).map_err(|e| e.to_string())?; let len = u32::try_from(line.len()) .ok() @@ -523,6 +541,7 @@ fn write_base(given: &GivenRows, dir: &Path) -> Result<(), String> { name: name.clone(), fields: ds.fields.iter().map(|f| f.name.clone()).collect(), rows: index.len(), + crs: (crs != Crs::LonLat).then_some(crs), }); } progress::check().map_err(|e| e.to_string())?; @@ -530,9 +549,9 @@ fn write_base(given: &GivenRows, dir: &Path) -> Result<(), String> { std::fs::write(&list, serde_json::to_vec(&layers).map_err(|e| e.to_string())?).map_err(io(&list)) } -/// `row`'s shape in longitude/latitude by `to` (the layer's conversion, made once): borrowed when it -/// is held there already; refused, naming the feature, when one of its places can't be. -fn in_lonlat<'a>( +/// `row`'s shape by `to` (the layer's conversion to the copy's system, made once): borrowed when it +/// is held there already; refused, naming the feature, when one of its places can't be given there. +fn in_system<'a>( to: &rustgis_data::raster::Reprojection, row: RowRef<'a>, layer: &str, @@ -540,11 +559,16 @@ fn in_lonlat<'a>( row.geometry() .map(|g| { rustgis_data::Dataset::converted_to_lonlat(to, g) - .ok_or_else(|| format!("{layer}: feature {} has a place that can't be given in longitude/latitude", row.id())) + .ok_or_else(|| format!("{layer}: feature {} has a place that can't be given in {}", row.id(), system_name(to.target()))) }) .transpose() } +/// A system as a notice names it: longitude/latitude by that name. +fn system_name(crs: Crs) -> String { + if crs == Crs::LonLat { "longitude/latitude".into() } else { crs.label() } +} + /// A file error, named by the file. fn io(path: &Path) -> impl Fn(std::io::Error) -> String + '_ { move |e| format!("{}: {e}", path.display()) @@ -603,8 +627,9 @@ pub(crate) fn list(map: &rustgis_map::Map, package: &str) -> Result> = g.fields.iter().map(|n| ds.field_index(n)).collect(); @@ -619,7 +644,7 @@ pub(crate) fn list(map: &rustgis_map::Map, package: &str) -> Result Result Result = g.fields.iter().zip(&fields).filter(|(_, j)| j.is_none()).map(|(n, _)| n.clone()).collect(); if !(added.is_empty() && deleted.is_empty() && edited.is_empty() && removed.is_empty() && added_fields.is_empty()) { let fields = added_fields.iter().filter_map(|j| ds.fields.get(*j)).cloned().collect(); - changes.layers.push(LayerChanges { layer: g.layer, added, deleted, edited, removed, fields }); + changes.layers.push(LayerChanges { layer: g.layer, added, deleted, edited, removed, fields, crs: g.crs }); } } + // Version 1 while every layer's shapes are in longitude/latitude, so a release before 2 reads + // the list; one with a layer's own system refuses version 2, saying a newer RustGIS made it. + changes.version = if changes.layers.iter().any(|l| l.crs.is_some()) { VERSION } else { 1 }; Ok(Some(changes)) } @@ -760,13 +788,33 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang }; let name = sl.name.clone(); let error = |e: String| super::workspace::sync_error(&ol.name, e); - let theirs = ol.dataset.in_lonlat().map_err(error)?; - // The source is changed in longitude/latitude, as the package holds its features, and its - // own coordinates put back after, those it left as they were as stored. - let held = (sl.dataset.coord_crs() != rustgis_data::raster::Crs::LonLat).then(|| sl.dataset.clone()); + // The list's shapes are in its layer's system ([`LayerChanges::crs`]). While the home layer + // and the package's are held in it too, the changes go in as they are held, converting + // nothing, so a feature whose place can't be given in longitude/latitude comes back exact + // (#2032 C2f-4b). Otherwise the list's shapes are put in longitude/latitude, and the source + // is changed there, its own coordinates put back after: those it left as they were. + let listed = lc.crs.unwrap_or(Crs::LonLat); + let native = listed != Crs::LonLat && sl.dataset.coord_crs() == listed && ol.dataset.coord_crs() == listed; + let converted; + let lc = if native || listed == Crs::LonLat { + lc + } else { + converted = given_in_lonlat(lc, listed).map_err(error)?; + &converted + }; + let theirs = if native { std::borrow::Cow::Borrowed(&ol.dataset) } else { ol.dataset.in_lonlat().map_err(error)? }; + let held = (!native && sl.dataset.coord_crs() != Crs::LonLat).then(|| sl.dataset.clone()); if held.is_some() { sl.dataset = sl.dataset.in_lonlat().map_err(error)?.into_owned(); } + // A shape that lost is kept in longitude/latitude ([`AreaConflict::geometry`]): from the + // layer's own system when it is changed there, a place that can't be given there as NaN, + // which the list of conflicts says. + let to_lonlat = if native { Some(listed.to(Crs::LonLat).map_err(error)?) } else { None }; + let kept_shape = |g: Option>| match &to_lonlat { + Some(to) => g.map(|g| g.map(|g| to.geometry(&g))), + None => g, + }; // Every id the layer had, or kept new ones above, before this return. let home_top = sl.dataset.features.highest_id(); let wanted: HashSet = @@ -786,7 +834,8 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang let field_value = |row: usize, field: &str| theirs.field_index(field).map_or(&Value::Null, |i| theirs.features.value(row, i)).clone(); let mut written = HashSet::new(); - let lost = |kind, id, values, shape| super::workspace::conflict(&changes.package, lc.layer, &name, id, kind, values, shape); + let lost = + |kind, id, values, shape| super::workspace::conflict(&changes.package, lc.layer, &name, id, kind, values, kept_shape(shape)); for g in &lc.edited { // An edit naming a feature the package's map hasn't has no values to bring back or keep. let Some(&p) = at_field.get(&g.id) else { @@ -905,6 +954,39 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang })) } +/// `lc` with the shapes it gives put in longitude/latitude from `crs`, the system it gives them in, +/// for a layer changed there; refused, naming the feature, when one of its places can't be. +fn given_in_lonlat(lc: &LayerChanges, crs: Crs) -> Result { + let to = crs.to(Crs::LonLat)?; + let convert = |given: &[Given]| -> Result, String> { + given + .iter() + .map(|g| { + let geometry = match &g.geometry { + Some(Some(shape)) => Some(Some( + rustgis_data::Dataset::converted_to_lonlat(&to, shape) + .ok_or_else(|| { + format!("feature {} has a place in {} that can't be given in longitude/latitude", g.id, crs.label()) + })? + .into_owned(), + )), + other => other.clone(), + }; + Ok(Given { id: g.id, values: g.values.clone(), geometry }) + }) + .collect() + }; + Ok(LayerChanges { + layer: lc.layer, + added: lc.added.clone(), + deleted: convert(&lc.deleted)?, + edited: convert(&lc.edited)?, + removed: lc.removed.clone(), + fields: lc.fields.clone(), + crs: None, + }) +} + /// Adds to offline area `ai`'s source layers, and to the area's own copy of them, the fields /// `changes` says the field added that they haven't. Returns them as `layer: field`. pub(crate) fn add_fields(s: &mut Session, ai: usize, changes: &Changes) -> Vec { @@ -1263,13 +1345,15 @@ mod tests { assert!(let_go(&dir, "the rows").is_none()); } - /// A list of any version but this one is refused: a newer one asks for a newer RustGIS. + /// A list of a version RustGIS writes (1 and 2) is read; any other is refused: a newer one asks + /// for a newer RustGIS. #[test] fn a_list_of_another_version_is_refused() { let id = "0123456789abcdef0123456789abcdef"; let list = |v: u32| format!(r#"{{"version":{v},"package":"{id}","layers":[]}}"#); assert!(Changes::read(list(1).as_bytes(), id).is_ok()); - assert!(Changes::read(list(2).as_bytes(), id).unwrap_err().contains("newer RustGIS")); + assert!(Changes::read(list(2).as_bytes(), id).is_ok()); + assert!(Changes::read(list(3).as_bytes(), id).unwrap_err().contains("newer RustGIS")); assert!(Changes::read(list(0).as_bytes(), id).unwrap_err().contains("damaged")); } } diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 1c0313d02e..c082fd8ade 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -801,7 +801,8 @@ CSV's, LiDAR cloud's, MapInfo, DXF, LandXML or city model's features has a place Projection can set the one that does. Such a feature doesn't draw (the map says how many it leaves out), and each step that needs longitude/latitude (an export in WGS 84, a tool, an edit) leaves it out and says so, or refuses naming it where leaving it out would lose it (a write-back, a sync of an -offline area holding it, a Field Package's list of changes) or a plugin tool takes the layer; +offline area holding it) or a plugin tool takes the layer; a Field Package's list of changes brings +it back in its own system; written back in its own system, it is exact. A system that converts a place to beyond ±180.5° longitude or ±90.5° latitude (Web Mercator a billion metres east is 8983°) doesn't fit either: such a feature is kept and draws out of place. Shapefile, GeoJSON, CSV, MapInfo, DXF, LandXML, ArcGIS diff --git a/docs/user-guide/field-work.md b/docs/user-guide/field-work.md index 965df02179..9d01aaf8d4 100644 --- a/docs/user-guide/field-work.md +++ b/docs/user-guide/field-work.md @@ -215,7 +215,13 @@ Where a change meets one made on the computer since the package was made: given in longitude/latitude (one far past where that system reaches) stays as stored when it lies outside the area when the area is synced (**Sync**, or a package that goes back as a whole map); one the area holds can't be compared with the phone's, and the sync is refused, naming it. - A return by the list of changes is refused for any such feature of the layer, naming it. + A return by the list of changes brings such a feature back as it is held: the list gives the + shapes of a layer held in its own coordinate system in that system, so they come back exactly, + with no round trip through longitude/latitude. If the computer now holds the layer in another + system, the shapes are converted, and such a feature refuses the return, naming it. A list in a + layer's own system needs a RustGIS that reads it (an older one refuses it, saying a newer + RustGIS made it); a package whose layers are all in longitude/latitude sends one any release + reads. Each of these keeps the version that lost on the offline area: the computer's values and shape the phone's replaced, the phone's edits to a feature deleted on the computer, the computer's feature From b50038005b6a83bbb8a8b17cfc1c2bfef27eaaf6 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 24 Nov 2026 17:03:51 +0100 Subject: [PATCH 2/6] Review: a copy with a layer in its own system listed where older releases don't look; only a feature the list touches can refuse a converted return; docs --- crates/data/src/io.rs | 3 +- crates/engine/src/cmd/field.rs | 3 +- crates/engine/src/cmd/field_audit_tests.rs | 51 +++++++++++++- crates/engine/src/cmd/field_changes.rs | 78 +++++++++++++++++++--- docs/user-guide/data-formats.md | 2 +- docs/user-guide/field-work.md | 8 ++- 6 files changed, 127 insertions(+), 18 deletions(-) diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 013a568742..052c6c52f4 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -1664,7 +1664,8 @@ pub(crate) fn places_lonlat_extent(g: &Geometry, t: &crate::raster::Transformer) /// draw (the map says so too), and a step that needs longitude/latitude leaves them out and says /// so, or refuses naming one where leaving it out would lose it (a write-back, a sync of an /// offline area holding it) or a contract says so (the plugin host's layers) (#2032 C2f-4b). A -/// Field Package's list of changes gives it in its layer's own system. `all` when none of the file's has a place there: then its system +/// Field Package's list of changes gives it in its layer's own system, which brings it back while +/// the computer holds the layer there. `all` when none of the file's has a place there: then its system /// doesn't seem to match its coordinates, and Define Projection can set the one that does, as /// nothing was lost. `None` for none. pub(crate) fn kept_unplaced_note(what: &str, n: usize, all: bool, crs: Crs) -> Option { diff --git a/crates/engine/src/cmd/field.rs b/crates/engine/src/cmd/field.rs index a84ddadedf..b1d193a523 100644 --- a/crates/engine/src/cmd/field.rs +++ b/crates/engine/src/cmd/field.rs @@ -352,7 +352,8 @@ fn build_package(s: &mut Session, p: &Value, notes: &mut rustgis_data::loss::Not } Ok(None) => losses.push(WHOLE_MAP.replace("{why}", "this device doesn't keep the rows the package was given")), Err(e) if e == rustgis_data::DataError::Cancelled.to_string() => return Err(e), - Err(e) => losses.push(WHOLE_MAP.replace("{why}", &format!("the rows the package was given couldn't be read ({e})"))), + Err(e) => losses + .push(WHOLE_MAP.replace("{why}", &format!("the rows the package was given couldn't be compared with the map ({e})"))), } } } diff --git a/crates/engine/src/cmd/field_audit_tests.rs b/crates/engine/src/cmd/field_audit_tests.rs index a9d4f46869..6a819e1052 100644 --- a/crates/engine/src/cmd/field_audit_tests.rs +++ b/crates/engine/src/cmd/field_audit_tests.rs @@ -783,8 +783,7 @@ fn a_list_in_a_layers_own_system_comes_back_to_a_layer_held_in_another() { home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(Crs::WebMercator).unwrap(); let (done, said) = bring_back(&temp, &mut a, &mut home, "a"); // Home's shapes were converted too (a round trip through longitude/latitude), so the moved - // one reads as changed at home as well: the field's wins, and that is said, as with a list in - // longitude/latitude. + // one may read as changed at home as well; the field's wins either way. assert_eq!(done["updated"].as_u64(), Some(1), "{done} {said:?}"); assert_eq!(value_of(&home, hc, edited, &name).as_deref(), Some("Moved too")); assert_eq!(home.map().layer(hc).unwrap().dataset.coord_crs(), Crs::WebMercator); @@ -795,6 +794,54 @@ fn a_list_in_a_layers_own_system_comes_back_to_a_layer_held_in_another() { assert!((now.x - was.x) > 500.0 && (now.x - was.x) < 2000.0 && (now.y - was.y).abs() < 100.0, "{now:?} {was:?}"); } +/// A layer the phone holds in its own system while home holds it in longitude/latitude: a feature +/// there with no place in longitude/latitude that the list doesn't touch doesn't refuse the +/// return; once the list touches it, it does, naming it. +#[test] +fn only_a_feature_the_list_touches_can_refuse_a_converted_return() { + use rustgis_data::{Coord, Geometry, raster::Crs}; + let temp = Temp::new("native-list-untouched"); + let mut home = home(&temp); + let hc = layer(&home, "Cities"); + home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); + let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap(); + // Home holds the layer in longitude/latitude now. + home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(Crs::LonLat).unwrap(); + // The phone's copy as given holds a feature with no place in longitude/latitude (Equal Earth + // a billion metres east), as one edited in the layer's own system would be. + let dir = temp.0.join("a"); + let (text, _) = unpack_into(&out, &dir).unwrap(); + let mut project = project_in(&text, &dir).unwrap(); + let package_id = project.field_origin.as_ref().unwrap().package.clone(); + let ci = project.maps[0].layers.iter().position(|l| l.name == "Cities").unwrap(); + let far = project.maps[0].layers[ci].dataset.features[1].id; + project.maps[0].layers[ci].dataset.features[1].geometry = Some(Geometry::Point(Coord::new(1e9, 0.0))); + project.given = Some(std::sync::Arc::new(crate::cmd::field_changes::GivenRows::of(&project, &package_id))); + let mut a = Session::new(project); + a.opened_package = Some(temp.0.join("a.rgisfield")); + a.run("offline.keepRows", &Value::Null).unwrap(); + let cities = layer(&a, "Cities"); + let name = text_field(&a, cities); + let edited = a.map().layer(cities).unwrap().dataset.features[0].id; + a.run("edit.setValue", &json!({"layer": cities, "row": 0, "field": name, "value": "Kept"})).unwrap(); + // Untouched: the edit comes back. + let (_, back) = package(&mut a, &Value::Null).unwrap(); + let path = temp.0.join("a-back.rgisfield"); + std::fs::write(&path, &back).unwrap(); + let mut trial = Session::new(home.project.clone()); + trial.path = home.path.clone(); + let done = trial.run("offline.bringBack", &json!({"path": path.display().to_string()})).unwrap(); + assert_eq!(done["updated"].as_u64(), Some(1), "{done}"); + assert_eq!(value_of(&trial, hc, edited, &name).as_deref(), Some("Kept")); + // Touched (its value edited too): it refuses, named, and nothing changes. + a.run("edit.setValue", &json!({"layer": cities, "row": 1, "field": name, "value": "Far"})).unwrap(); + let (_, back) = package(&mut a, &Value::Null).unwrap(); + std::fs::write(&path, &back).unwrap(); + let err = home.run("offline.bringBack", &json!({"path": path.display().to_string()})).unwrap_err(); + assert!(err.contains(&format!("feature {far} has a place")), "{err}"); + assert_ne!(value_of(&home, hc, edited, &name).as_deref(), Some("Kept")); +} + /// A list of a version this RustGIS doesn't write is refused, as is one of version 1 that gives a /// layer's system, which none of version 1 has. #[test] diff --git a/crates/engine/src/cmd/field_changes.rs b/crates/engine/src/cmd/field_changes.rs index 4e7e11605e..a626bdff57 100644 --- a/crates/engine/src/cmd/field_changes.rs +++ b/crates/engine/src/cmd/field_changes.rs @@ -47,6 +47,15 @@ const RECORD: usize = 8 + 8 + 4 + 16; const MAX_ROW: u32 = 256 << 20; /// The file listing a given copy's layers, written last: a folder without it is incomplete. const LAYERS: &str = "layers.json"; +/// [`LAYERS`] for a copy with a layer in its own system ([`BaseLayer::crs`]), which a release +/// before #2032 C2f-4b-5 doesn't look for: it would read that layer's rows as longitude/latitude, +/// so it finds no copy instead, and the package goes back as a whole map, which it says. +const LAYERS_V2: &str = "layers.v2.json"; + +/// The file listing the copy in `dir`, when it is complete. +fn layers_file(dir: &Path) -> Option { + [LAYERS_V2, LAYERS].into_iter().map(|name| dir.join(name)).find(|p| p.is_file()) +} /// The changes of one returning package. #[derive(Debug, Default, Serialize, Deserialize)] @@ -292,7 +301,7 @@ impl GivenRows { /// Whether this device keeps them already. pub(crate) fn kept(&self) -> bool { - base_dir(&self.package).is_some_and(|d| d.join(LAYERS).is_file()) + base_dir(&self.package).is_some_and(|d| layers_file(&d).is_some()) } } @@ -430,7 +439,7 @@ pub(crate) fn keep_rows(s: &mut Session, _p: &serde_json::Value) -> crate::CmdRe pub(crate) fn keep_base(given: &GivenRows) -> Kept { rustgis_data::progress::check().map_err(|e| e.to_string())?; let dir = base_dir(&given.package).ok_or("this device gives RustGIS no folder of its own to keep the field package's rows in")?; - if dir.join(LAYERS).is_file() { + if layers_file(&dir).is_some() { return Ok(Vec::new()); } // Its return sent before it was opened again, maybe in an earlier run: nothing is kept. @@ -484,7 +493,7 @@ fn publish(part: &Path, dir: &Path) -> Result<(), String> { } match std::fs::rename(part, dir) { Ok(()) => Ok(()), - Err(_) if dir.join(LAYERS).is_file() => Ok(()), + Err(_) if layers_file(dir).is_some() => Ok(()), Err(e) => Err(format!("{}: {e}", dir.display())), } } @@ -545,7 +554,7 @@ fn write_base(given: &GivenRows, dir: &Path) -> Result<(), String> { }); } progress::check().map_err(|e| e.to_string())?; - let list = dir.join(LAYERS); + let list = dir.join(if layers.iter().any(|l| l.crs.is_some()) { LAYERS_V2 } else { LAYERS }); std::fs::write(&list, serde_json::to_vec(&layers).map_err(|e| e.to_string())?).map_err(io(&list)) } @@ -616,8 +625,8 @@ fn read_row(file: &mut std::fs::File, e: &Entry, fields: usize) -> Result Result, String> { use rustgis_data::progress; - let Some(dir) = base_dir(package).filter(|d| d.join(LAYERS).is_file()) else { return Ok(None) }; - let list_path = dir.join(LAYERS); + let Some(dir) = base_dir(package) else { return Ok(None) }; + let Some(list_path) = layers_file(&dir) else { return Ok(None) }; let text = std::fs::read(&list_path).map_err(|e| format!("{}: {e}", list_path.display()))?; let given: Vec = serde_json::from_slice(&text).map_err(|e| format!("{}: {e}", list_path.display()))?; let mut changes = Changes { version: VERSION, package: package.to_string(), layers: Vec::new(), gone: Vec::new() }; @@ -802,10 +811,23 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang converted = given_in_lonlat(lc, listed).map_err(error)?; &converted }; - let theirs = if native { std::borrow::Cow::Borrowed(&ol.dataset) } else { ol.dataset.in_lonlat().map_err(error)? }; + // Converted, a feature with a place that can't be given in longitude/latitude loses its + // shape there: one the list doesn't touch stays as it is (at home as stored, by + // `merged_from_lonlat`); one it touches refuses the return, naming it. + let touched: HashSet = + lc.added.iter().copied().chain(lc.deleted.iter().map(|g| g.id)).chain(lc.edited.iter().map(|g| g.id)).collect(); + let theirs = if native { + std::borrow::Cow::Borrowed(&ol.dataset) + } else { + let (lonlat, _) = ol.dataset.in_lonlat_leaving_out().map_err(error)?; + unplaced_touched(&ol.dataset, &lonlat, &touched).map_err(error)?; + lonlat + }; let held = (!native && sl.dataset.coord_crs() != Crs::LonLat).then(|| sl.dataset.clone()); - if held.is_some() { - sl.dataset = sl.dataset.in_lonlat().map_err(error)?.into_owned(); + if let Some(own) = &held { + let (lonlat, _) = own.in_lonlat_leaving_out().map_err(error)?; + unplaced_touched(own, &lonlat, &touched).map_err(error)?; + sl.dataset = lonlat.into_owned(); } // A shape that lost is kept in longitude/latitude ([`AreaConflict::geometry`]): from the // layer's own system when it is changed there, a place that can't be given there as NaN, @@ -954,6 +976,22 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang })) } +/// Refuses, naming it, a feature `touched` names whose shape `own` holds but its conversion to +/// longitude/latitude, `lonlat` (row for row), left out: one of its places can't be given there. +fn unplaced_touched(own: &rustgis_data::Dataset, lonlat: &rustgis_data::Dataset, touched: &HashSet) -> Result<(), String> { + match own + .features + .rows() + .zip(lonlat.features.rows()) + .find(|(o, l)| touched.contains(&o.id()) && o.geometry().is_some() && l.geometry().is_none()) + { + Some((o, _)) => { + Err(format!("feature {} has a place in {} that can't be given in longitude/latitude", o.id(), own.coord_crs().label())) + } + None => Ok(()), + } +} + /// `lc` with the shapes it gives put in longitude/latitude from `crs`, the system it gives them in, /// for a layer changed there; refused, naming the feature, when one of its places can't be. fn given_in_lonlat(lc: &LayerChanges, crs: Crs) -> Result { @@ -1229,6 +1267,26 @@ mod tests { assert!(refused.contains("damaged") && refused.contains("feature 7"), "{refused}"); } + /// A copy with a layer held in its own system lists its layers in [`LAYERS_V2`], which a release + /// before it doesn't look for, so that release sends the package back as a whole map rather + /// than read the layer's rows as longitude/latitude; one in longitude/latitude only, in + /// [`LAYERS`], as before. This release reads either. + #[test] + fn a_copy_with_a_layer_in_its_own_system_is_listed_where_older_releases_dont_look() { + let held = crate::sample::world_project(); + let mut native = held.clone(); + native.maps[0].layers[0].dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); + for (project, file, other) in [(&held, LAYERS, LAYERS_V2), (&native, LAYERS_V2, LAYERS)] { + let id = crate::project::field_id().unwrap(); + assert!(keep_base(&GivenRows::of(project, &id)).unwrap().is_empty()); + let dir = base_dir(&id).unwrap(); + assert!(dir.join(file).is_file() && !dir.join(other).exists(), "{file}"); + assert!(GivenRows::of(project, &id).kept()); + let map = project.maps.first().unwrap(); + assert!(list(map, &id).unwrap().unwrap().layers.is_empty(), "{file}: nothing changed"); + } + } + /// A folder a racing send removed first is gone, not a failure to remove it. #[test] fn letting_go_of_a_folder_already_gone_says_nothing() { @@ -1348,7 +1406,7 @@ mod tests { /// A list of a version RustGIS writes (1 and 2) is read; any other is refused: a newer one asks /// for a newer RustGIS. #[test] - fn a_list_of_another_version_is_refused() { + fn a_list_of_a_version_rustgis_writes_is_read_and_another_refused() { let id = "0123456789abcdef0123456789abcdef"; let list = |v: u32| format!(r#"{{"version":{v},"package":"{id}","layers":[]}}"#); assert!(Changes::read(list(1).as_bytes(), id).is_ok()); diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index c082fd8ade..64bea88732 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -802,7 +802,7 @@ Projection can set the one that does. Such a feature doesn't draw (the map says out), and each step that needs longitude/latitude (an export in WGS 84, a tool, an edit) leaves it out and says so, or refuses naming it where leaving it out would lose it (a write-back, a sync of an offline area holding it) or a plugin tool takes the layer; a Field Package's list of changes brings -it back in its own system; +it back in its own system while the computer holds the layer there; written back in its own system, it is exact. A system that converts a place to beyond ±180.5° longitude or ±90.5° latitude (Web Mercator a billion metres east is 8983°) doesn't fit either: such a feature is kept and draws out of place. Shapefile, GeoJSON, CSV, MapInfo, DXF, LandXML, ArcGIS diff --git a/docs/user-guide/field-work.md b/docs/user-guide/field-work.md index 9d01aaf8d4..f0363c7a71 100644 --- a/docs/user-guide/field-work.md +++ b/docs/user-guide/field-work.md @@ -218,10 +218,12 @@ Where a change meets one made on the computer since the package was made: A return by the list of changes brings such a feature back as it is held: the list gives the shapes of a layer held in its own coordinate system in that system, so they come back exactly, with no round trip through longitude/latitude. If the computer now holds the layer in another - system, the shapes are converted, and such a feature refuses the return, naming it. A list in a + system, the shapes are converted, and such a feature the list changes refuses the return, + naming it. A list in a layer's own system needs a RustGIS that reads it (an older one refuses it, saying a newer - RustGIS made it); a package whose layers are all in longitude/latitude sends one any release - reads. + RustGIS made it); a list that changes no layer held in its own system is one any release + reads. A phone with an older RustGIS than the one that opened the package finds no copy of a + layer held in its own system, and sends the package back as a whole map, saying so. Each of these keeps the version that lost on the offline area: the computer's values and shape the phone's replaced, the phone's edits to a feature deleted on the computer, the computer's feature From 89e70530728752d142b75ce7059eb1ae3d9246b0 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 24 Nov 2026 17:39:56 +0100 Subject: [PATCH 3/6] Review: version 2 read now, written in a later release (copies and lists stay in longitude/latitude); a renumbered addition doesn't refuse; refused return changes nothing (tested); wording --- crates/data/src/io.rs | 5 +- crates/engine/src/cmd/field.rs | 5 +- crates/engine/src/cmd/field_audit_tests.rs | 38 +++++++++++- crates/engine/src/cmd/field_changes.rs | 68 +++++++++++++++++++--- docs/user-guide/data-formats.md | 3 +- docs/user-guide/field-work.md | 13 ++--- 6 files changed, 106 insertions(+), 26 deletions(-) diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 052c6c52f4..13b04eaf17 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -1663,9 +1663,8 @@ pub(crate) fn places_lonlat_extent(g: &Geometry, t: &crate::raster::Transformer) /// them, held in `crs`, though a place of theirs can't be given in longitude/latitude: they don't /// draw (the map says so too), and a step that needs longitude/latitude leaves them out and says /// so, or refuses naming one where leaving it out would lose it (a write-back, a sync of an -/// offline area holding it) or a contract says so (the plugin host's layers) (#2032 C2f-4b). A -/// Field Package's list of changes gives it in its layer's own system, which brings it back while -/// the computer holds the layer there. `all` when none of the file's has a place there: then its system +/// offline area holding it, a Field Package's list of changes) or a contract says so (the plugin +/// host's layers) (#2032 C2f-4b). `all` when none of the file's has a place there: then its system /// doesn't seem to match its coordinates, and Define Projection can set the one that does, as /// nothing was lost. `None` for none. pub(crate) fn kept_unplaced_note(what: &str, n: usize, all: bool, crs: Crs) -> Option { diff --git a/crates/engine/src/cmd/field.rs b/crates/engine/src/cmd/field.rs index b1d193a523..d7253177c8 100644 --- a/crates/engine/src/cmd/field.rs +++ b/crates/engine/src/cmd/field.rs @@ -352,8 +352,9 @@ fn build_package(s: &mut Session, p: &Value, notes: &mut rustgis_data::loss::Not } Ok(None) => losses.push(WHOLE_MAP.replace("{why}", "this device doesn't keep the rows the package was given")), Err(e) if e == rustgis_data::DataError::Cancelled.to_string() => return Err(e), - Err(e) => losses - .push(WHOLE_MAP.replace("{why}", &format!("the rows the package was given couldn't be compared with the map ({e})"))), + Err(e) => losses.push( + WHOLE_MAP.replace("{why}", &format!("the rows the package was given couldn't be read or compared with the map ({e})")), + ), } } } diff --git a/crates/engine/src/cmd/field_audit_tests.rs b/crates/engine/src/cmd/field_audit_tests.rs index 6a819e1052..6d3a08f5be 100644 --- a/crates/engine/src/cmd/field_audit_tests.rs +++ b/crates/engine/src/cmd/field_audit_tests.rs @@ -722,6 +722,7 @@ fn a_layer_held_in_its_own_system_comes_back_with_only_its_changes() { /// longitude/latitude refused to send) comes back too, as it is held. #[test] fn a_layer_held_in_its_own_system_comes_back_exact_by_a_list_in_that_system() { + crate::cmd::field_changes::test_bases::write_own_systems(); use rustgis_data::{Coord, Geometry, raster::Crs}; let equal_earth = Crs::from_epsg(8857).unwrap(); let temp = Temp::new("native-list"); @@ -765,6 +766,7 @@ fn a_layer_held_in_its_own_system_comes_back_exact_by_a_list_in_that_system() { /// back there, as a list of version 1's are. #[test] fn a_list_in_a_layers_own_system_comes_back_to_a_layer_held_in_another() { + crate::cmd::field_changes::test_bases::write_own_systems(); use rustgis_data::{Geometry, raster::Crs}; let temp = Temp::new("native-list-elsewhere"); let mut home = home(&temp); @@ -799,6 +801,7 @@ fn a_list_in_a_layers_own_system_comes_back_to_a_layer_held_in_another() { /// return; once the list touches it, it does, naming it. #[test] fn only_a_feature_the_list_touches_can_refuse_a_converted_return() { + crate::cmd::field_changes::test_bases::write_own_systems(); use rustgis_data::{Coord, Geometry, raster::Crs}; let temp = Temp::new("native-list-untouched"); let mut home = home(&temp); @@ -837,9 +840,42 @@ fn only_a_feature_the_list_touches_can_refuse_a_converted_return() { a.run("edit.setValue", &json!({"layer": cities, "row": 1, "field": name, "value": "Far"})).unwrap(); let (_, back) = package(&mut a, &Value::Null).unwrap(); std::fs::write(&path, &back).unwrap(); + let before = serde_json::to_value(&home.project).unwrap(); let err = home.run("offline.bringBack", &json!({"path": path.display().to_string()})).unwrap_err(); assert!(err.contains(&format!("feature {far} has a place")), "{err}"); - assert_ne!(value_of(&home, hc, edited, &name).as_deref(), Some("Kept")); + assert_eq!(serde_json::to_value(&home.project).unwrap(), before, "a refused return changes nothing"); +} + +/// A feature added in the field whose id home has meanwhile given to a feature of its own with no +/// place in longitude/latitude takes a new id: home's feature stays as stored, and doesn't refuse +/// the return. +#[test] +fn a_home_feature_an_addition_renumbers_around_doesnt_refuse_a_converted_return() { + use rustgis_data::{Coord, Geometry, raster::Crs}; + let temp = Temp::new("renumbered-unplaced"); + let mut home = home(&temp); + let hc = layer(&home, "Cities"); + let name = text_field(&home, hc); + let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap(); + // Home holds the layer in Equal Earth and adds a feature there with no place in + // longitude/latitude: it takes the next id, which the phone's addition takes too. + home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); + let at_home = add_point(&mut home, hc, &name, "Home's", [10.0, 10.0]); + let ds = &mut home.map_mut().layer_mut(hc).unwrap().dataset; + let row = ds.features.iter().position(|f| f.id == at_home).unwrap(); + ds.features.set_geometry(row, Some(Geometry::Point(Coord::new(1e9, 0.0)))); + let mut a = phone(&temp, &out, "a"); + let cities = layer(&a, "Cities"); + let added = add_point(&mut a, cities, &name, "Field's", [11.0, 11.0]); + assert_eq!(added, at_home, "the same id"); + let (done, said) = bring_back(&temp, &mut a, &mut home, "a"); + assert_eq!((done["added"].as_u64(), done["renumbered"].as_u64()), (Some(1), Some(1)), "{done} {said:?}"); + assert_eq!(with_label(&home, hc, &name, "Field's"), 1); + let l = home.map().layer(hc).unwrap(); + assert_eq!( + l.dataset.features.iter().find(|f| f.id == at_home).and_then(|f| f.geometry.clone()), + Some(Geometry::Point(Coord::new(1e9, 0.0))) + ); } /// A list of a version this RustGIS doesn't write is refused, as is one of version 1 that gives a diff --git a/crates/engine/src/cmd/field_changes.rs b/crates/engine/src/cmd/field_changes.rs index a626bdff57..698ed001e3 100644 --- a/crates/engine/src/cmd/field_changes.rs +++ b/crates/engine/src/cmd/field_changes.rs @@ -52,6 +52,22 @@ const LAYERS: &str = "layers.json"; /// so it finds no copy instead, and the package goes back as a whole map, which it says. const LAYERS_V2: &str = "layers.v2.json"; +/// Whether a copy keeps a layer held in its own system in that system ([`BaseLayer::crs`]), so its +/// list of changes is of version 2. Off until a release that reads version 2 has shipped +/// (`docs/maintaining.md`: a new version is read a release before anything writes it): copies are +/// kept, and lists written, in longitude/latitude as before. Tests turn it on +/// ([`test_bases::write_own_systems`]). +fn writes_own_systems() -> bool { + #[cfg(test)] + { + test_bases::OWN_SYSTEMS.load(std::sync::atomic::Ordering::Relaxed) + } + #[cfg(not(test))] + { + false + } +} + /// The file listing the copy in `dir`, when it is complete. fn layers_file(dir: &Path) -> Option { [LAYERS_V2, LAYERS].into_iter().map(|name| dir.join(name)).find(|p| p.is_file()) @@ -449,7 +465,7 @@ pub(crate) fn keep_base(given: &GivenRows) -> Kept { let parent = dir.parent().ok_or("no folder for the field package's rows")?; std::fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?; let part = parent.join(format!("{}.{}.part", given.package, crate::project::field_id()?)); - let written = write_base(given, &part).and_then(|()| publish(&part, &dir)); + let written = write_base(given, &part, writes_own_systems()).and_then(|()| publish(&part, &dir)); let mut notes = Vec::new(); if part.exists() { notes.extend(let_go(&part, "a copy of the field package's rows cut short")); @@ -499,16 +515,17 @@ fn publish(part: &Path, dir: &Path) -> Result<(), String> { } /// Writes the rows `given` holds into `dir`: see [`keep_base`]. -fn write_base(given: &GivenRows, dir: &Path) -> Result<(), String> { +fn write_base(given: &GivenRows, dir: &Path, own_systems: bool) -> Result<(), String> { use rustgis_data::progress; std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; progress::begin(given.layers.iter().map(|(_, _, d)| d.features.len()).sum()); let mut layers = Vec::new(); for (id, name, dataset) in &given.layers { - // Each row's shape is kept in the layer's own system, as it is held: a feature whose place - // can't be given in longitude/latitude comes back exact too. - let crs = dataset.coord_crs(); - let to = crs.to(crs).map_err(|e| format!("{name}: {e}"))?; + // Each row's shape is kept in the layer's own system, as it is held, so a feature whose + // place can't be given in longitude/latitude comes back exact too ([`writes_own_systems`]); + // else in longitude/latitude, as before. + let crs = if own_systems { dataset.coord_crs() } else { Crs::LonLat }; + let to = dataset.coord_crs().to(crs).map_err(|e| format!("{name}: {e}"))?; let ds = dataset; let fields: Vec> = (0..ds.fields.len()).map(Some).collect(); let rows_path = dir.join(format!("{id}.rows")); @@ -560,6 +577,7 @@ fn write_base(given: &GivenRows, dir: &Path) -> Result<(), String> { /// `row`'s shape by `to` (the layer's conversion to the copy's system, made once): borrowed when it /// is held there already; refused, naming the feature, when one of its places can't be given there. +/// (`Dataset::converted_to_lonlat` converts into `to`'s target, whatever system that is.) fn in_system<'a>( to: &rustgis_data::raster::Reprojection, row: RowRef<'a>, @@ -814,8 +832,10 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang // Converted, a feature with a place that can't be given in longitude/latitude loses its // shape there: one the list doesn't touch stays as it is (at home as stored, by // `merged_from_lonlat`); one it touches refuses the return, naming it. - let touched: HashSet = - lc.added.iter().copied().chain(lc.deleted.iter().map(|g| g.id)).chain(lc.edited.iter().map(|g| g.id)).collect(); + // Home's features the list replaces or removes; a field addition whose id home uses takes + // a new one, leaving home's feature as it is, so it counts only in the package's map. + let changed: HashSet = lc.deleted.iter().map(|g| g.id).chain(lc.edited.iter().map(|g| g.id)).collect(); + let touched: HashSet = changed.iter().copied().chain(lc.added.iter().copied()).collect(); let theirs = if native { std::borrow::Cow::Borrowed(&ol.dataset) } else { @@ -826,7 +846,7 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang let held = (!native && sl.dataset.coord_crs() != Crs::LonLat).then(|| sl.dataset.clone()); if let Some(own) = &held { let (lonlat, _) = own.in_lonlat_leaving_out().map_err(error)?; - unplaced_touched(own, &lonlat, &touched).map_err(error)?; + unplaced_touched(own, &lonlat, &changed).map_err(error)?; sl.dataset = lonlat.into_owned(); } // A shape that lost is kept in longitude/latitude ([`AreaConflict::geometry`]): from the @@ -1273,6 +1293,7 @@ mod tests { /// [`LAYERS`], as before. This release reads either. #[test] fn a_copy_with_a_layer_in_its_own_system_is_listed_where_older_releases_dont_look() { + test_bases::write_own_systems(); let held = crate::sample::world_project(); let mut native = held.clone(); native.maps[0].layers[0].dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); @@ -1287,6 +1308,25 @@ mod tests { } } + /// As a release keeps them ([`writes_own_systems`] off): a layer held in its own system is kept + /// in longitude/latitude, listed in [`LAYERS`], and an unchanged return's list is of version 1, + /// which every release reads. + #[test] + fn a_release_keeps_the_copy_in_longitude_latitude_until_version_2_is_written() { + let mut project = crate::sample::world_project(); + project.maps[0].layers[0].dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); + let id = crate::project::field_id().unwrap(); + let dir = base_dir(&id).unwrap(); + std::fs::create_dir_all(&dir).unwrap(); + write_base(&GivenRows::of(&project, &id), &dir, false).unwrap(); + assert!(dir.join(LAYERS).is_file() && !dir.join(LAYERS_V2).exists()); + let text = std::fs::read(dir.join(LAYERS)).unwrap(); + let layers: Vec = serde_json::from_slice(&text).unwrap(); + assert!(layers.iter().all(|l| l.crs.is_none())); + let changes = list(project.maps.first().unwrap(), &id).unwrap().unwrap(); + assert_eq!((changes.version, changes.layers.len()), (1, 0)); + } + /// A folder a racing send removed first is gone, not a failure to remove it. #[test] fn letting_go_of_a_folder_already_gone_says_nothing() { @@ -1419,6 +1459,16 @@ mod tests { /// In tests, given copies go in a folder of each test's own thread, removed when it ends. #[cfg(test)] pub(crate) mod test_bases { + /// Turns on keeping copies, and writing lists, in each layer's own system + /// ([`super::writes_own_systems`]) for this test process; off by default, as in a release. + /// Every test passes either way, so tests sharing a process don't depend on it. + pub(crate) static OWN_SYSTEMS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + + /// [`OWN_SYSTEMS`] on. + pub(crate) fn write_own_systems() { + OWN_SYSTEMS.store(true, std::sync::atomic::Ordering::Relaxed); + } + use std::path::PathBuf; struct Root(PathBuf); diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 64bea88732..1c0313d02e 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -801,8 +801,7 @@ CSV's, LiDAR cloud's, MapInfo, DXF, LandXML or city model's features has a place Projection can set the one that does. Such a feature doesn't draw (the map says how many it leaves out), and each step that needs longitude/latitude (an export in WGS 84, a tool, an edit) leaves it out and says so, or refuses naming it where leaving it out would lose it (a write-back, a sync of an -offline area holding it) or a plugin tool takes the layer; a Field Package's list of changes brings -it back in its own system while the computer holds the layer there; +offline area holding it, a Field Package's list of changes) or a plugin tool takes the layer; written back in its own system, it is exact. A system that converts a place to beyond ±180.5° longitude or ±90.5° latitude (Web Mercator a billion metres east is 8983°) doesn't fit either: such a feature is kept and draws out of place. Shapefile, GeoJSON, CSV, MapInfo, DXF, LandXML, ArcGIS diff --git a/docs/user-guide/field-work.md b/docs/user-guide/field-work.md index f0363c7a71..b6345e5888 100644 --- a/docs/user-guide/field-work.md +++ b/docs/user-guide/field-work.md @@ -215,15 +215,10 @@ Where a change meets one made on the computer since the package was made: given in longitude/latitude (one far past where that system reaches) stays as stored when it lies outside the area when the area is synced (**Sync**, or a package that goes back as a whole map); one the area holds can't be compared with the phone's, and the sync is refused, naming it. - A return by the list of changes brings such a feature back as it is held: the list gives the - shapes of a layer held in its own coordinate system in that system, so they come back exactly, - with no round trip through longitude/latitude. If the computer now holds the layer in another - system, the shapes are converted, and such a feature the list changes refuses the return, - naming it. A list in a - layer's own system needs a RustGIS that reads it (an older one refuses it, saying a newer - RustGIS made it); a list that changes no layer held in its own system is one any release - reads. A phone with an older RustGIS than the one that opened the package finds no copy of a - layer held in its own system, and sends the package back as a whole map, saying so. + Send Field Package can't list the changes of a layer holding such a feature: the package goes + back as a whole map, and the phone says why, naming it. Bring Back of a list of changes refuses + only for such a feature of the computer's layer that the list changes, naming it; the others stay + as stored. Each of these keeps the version that lost on the offline area: the computer's values and shape the phone's replaced, the phone's edits to a feature deleted on the computer, the computer's feature From 4323f6ce7bbcacbb984e329ac12ee3a64d7af65b Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 24 Nov 2026 18:07:07 +0100 Subject: [PATCH 4/6] Review: the own-system switch scoped to the test that turns it on; the release default checked through keep_base; a kept shape without its place said; wording --- crates/engine/src/cmd/field_audit_tests.rs | 33 ++++++++++- crates/engine/src/cmd/field_changes.rs | 66 ++++++++++++++++------ docs/user-guide/field-work.md | 4 +- 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/crates/engine/src/cmd/field_audit_tests.rs b/crates/engine/src/cmd/field_audit_tests.rs index 6d3a08f5be..795b48cacd 100644 --- a/crates/engine/src/cmd/field_audit_tests.rs +++ b/crates/engine/src/cmd/field_audit_tests.rs @@ -722,7 +722,7 @@ fn a_layer_held_in_its_own_system_comes_back_with_only_its_changes() { /// longitude/latitude refused to send) comes back too, as it is held. #[test] fn a_layer_held_in_its_own_system_comes_back_exact_by_a_list_in_that_system() { - crate::cmd::field_changes::test_bases::write_own_systems(); + let _own = crate::cmd::field_changes::test_bases::write_own_systems(); use rustgis_data::{Coord, Geometry, raster::Crs}; let equal_earth = Crs::from_epsg(8857).unwrap(); let temp = Temp::new("native-list"); @@ -766,7 +766,7 @@ fn a_layer_held_in_its_own_system_comes_back_exact_by_a_list_in_that_system() { /// back there, as a list of version 1's are. #[test] fn a_list_in_a_layers_own_system_comes_back_to_a_layer_held_in_another() { - crate::cmd::field_changes::test_bases::write_own_systems(); + let _own = crate::cmd::field_changes::test_bases::write_own_systems(); use rustgis_data::{Geometry, raster::Crs}; let temp = Temp::new("native-list-elsewhere"); let mut home = home(&temp); @@ -801,7 +801,7 @@ fn a_list_in_a_layers_own_system_comes_back_to_a_layer_held_in_another() { /// return; once the list touches it, it does, naming it. #[test] fn only_a_feature_the_list_touches_can_refuse_a_converted_return() { - crate::cmd::field_changes::test_bases::write_own_systems(); + let _own = crate::cmd::field_changes::test_bases::write_own_systems(); use rustgis_data::{Coord, Geometry, raster::Crs}; let temp = Temp::new("native-list-untouched"); let mut home = home(&temp); @@ -878,6 +878,33 @@ fn a_home_feature_an_addition_renumbers_around_doesnt_refuse_a_converted_return( ); } +/// A shape the computer moved where it has no place in longitude/latitude, which the field's then +/// replaces, is kept among the conflicts without that place (they are in longitude/latitude), and +/// the return says so. +#[test] +fn a_displaced_shape_with_no_place_in_longitude_latitude_is_said() { + use rustgis_data::{Coord, Geometry, raster::Crs}; + let _own = crate::cmd::field_changes::test_bases::write_own_systems(); + let temp = Temp::new("native-list-placeless-conflict"); + let mut home = home(&temp); + let hc = layer(&home, "Cities"); + home.map_mut().layer_mut(hc).unwrap().dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); + let (_, out) = package(&mut home, &json!({"name": "Day", "extent": AREA})).unwrap(); + let mut a = phone(&temp, &out, "a"); + let cities = layer(&a, "Cities"); + let moved = a.map().layer(cities).unwrap().dataset.features[0].id; + let ds = &mut a.map_mut().layer_mut(cities).unwrap().dataset; + let Some(Geometry::Point(c)) = ds.features[0].geometry else { panic!() }; + ds.features[0].geometry = Some(Geometry::Point(Coord::new(c.x + 10.0, c.y))); + // Home moves the same city where it has no place in longitude/latitude. + let ds = &mut home.map_mut().layer_mut(hc).unwrap().dataset; + let row = ds.features.iter().position(|f| f.id == moved).unwrap(); + ds.features.set_geometry(row, Some(Geometry::Point(Coord::new(1e9, 0.0)))); + let (done, said) = bring_back(&temp, &mut a, &mut home, "a"); + assert_eq!((done["updated"].as_u64(), done["conflicts"].as_u64()), (Some(1), Some(1)), "{done} {said:?}"); + assert!(said.iter().any(|t| t.contains("1 shape(s) that lost") && t.contains("don't hold that place")), "{said:?}"); +} + /// A list of a version this RustGIS doesn't write is refused, as is one of version 1 that gives a /// layer's system, which none of version 1 has. #[test] diff --git a/crates/engine/src/cmd/field_changes.rs b/crates/engine/src/cmd/field_changes.rs index 698ed001e3..d952783936 100644 --- a/crates/engine/src/cmd/field_changes.rs +++ b/crates/engine/src/cmd/field_changes.rs @@ -55,12 +55,12 @@ const LAYERS_V2: &str = "layers.v2.json"; /// Whether a copy keeps a layer held in its own system in that system ([`BaseLayer::crs`]), so its /// list of changes is of version 2. Off until a release that reads version 2 has shipped /// (`docs/maintaining.md`: a new version is read a release before anything writes it): copies are -/// kept, and lists written, in longitude/latitude as before. Tests turn it on +/// kept, and lists written, in longitude/latitude as before. A test turns it on for itself /// ([`test_bases::write_own_systems`]). fn writes_own_systems() -> bool { #[cfg(test)] { - test_bases::OWN_SYSTEMS.load(std::sync::atomic::Ordering::Relaxed) + test_bases::OWN_SYSTEMS.with(std::cell::Cell::get) } #[cfg(not(test))] { @@ -802,6 +802,8 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang )); } let mut done = Applied::default(); + // Shapes that lost kept without a place that can't be given in longitude/latitude: said. + let placeless = std::cell::Cell::new(0usize); let tag = |layer: &str, id: u64| format!("{layer} #{id}"); let src = s.project.maps.get_mut(si).ok_or("the source map is gone")?; for lc in &changes.layers { @@ -854,7 +856,15 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang // which the list of conflicts says. let to_lonlat = if native { Some(listed.to(Crs::LonLat).map_err(error)?) } else { None }; let kept_shape = |g: Option>| match &to_lonlat { - Some(to) => g.map(|g| g.map(|g| to.geometry(&g))), + Some(to) => g.map(|g| { + g.map(|g| { + let lonlat = to.geometry(&g); + if g.is_finite() && !lonlat.is_finite() { + placeless.set(placeless.get() + 1); + } + lonlat + }) + }), None => g, }; // Every id the layer had, or kept new ones above, before this return. @@ -984,6 +994,15 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang } super::workspace::renew_area(s, ai, si, offline_map, next_generation)?; super::workspace::keep_conflicts(s, ai, std::mem::take(&mut done.lost)); + if placeless.get() > 0 { + s.notify( + format!( + "Offline area '{area_name}': {} shape(s) that lost to the field's have a place that can't be given in longitude/latitude, so the versions kept in Data › Offline › Conflicts don't hold that place", + placeless.get() + ), + false, + ); + } done.say(s, &area_name, &source); Ok(json!({ "updated": done.updated, @@ -1293,7 +1312,7 @@ mod tests { /// [`LAYERS`], as before. This release reads either. #[test] fn a_copy_with_a_layer_in_its_own_system_is_listed_where_older_releases_dont_look() { - test_bases::write_own_systems(); + let _own = test_bases::write_own_systems(); let held = crate::sample::world_project(); let mut native = held.clone(); native.maps[0].layers[0].dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); @@ -1308,17 +1327,17 @@ mod tests { } } - /// As a release keeps them ([`writes_own_systems`] off): a layer held in its own system is kept - /// in longitude/latitude, listed in [`LAYERS`], and an unchanged return's list is of version 1, - /// which every release reads. + /// As a release keeps them ([`writes_own_systems`] off, as on any test's thread that doesn't + /// turn it on): a layer held in its own system is kept in longitude/latitude, listed in + /// [`LAYERS`], and an unchanged return's list is of version 1, which every release reads. #[test] fn a_release_keeps_the_copy_in_longitude_latitude_until_version_2_is_written() { + assert!(!writes_own_systems()); let mut project = crate::sample::world_project(); project.maps[0].layers[0].dataset.hold_in(Crs::from_epsg(8857).unwrap()).unwrap(); let id = crate::project::field_id().unwrap(); + assert!(keep_base(&GivenRows::of(&project, &id)).unwrap().is_empty()); let dir = base_dir(&id).unwrap(); - std::fs::create_dir_all(&dir).unwrap(); - write_base(&GivenRows::of(&project, &id), &dir, false).unwrap(); assert!(dir.join(LAYERS).is_file() && !dir.join(LAYERS_V2).exists()); let text = std::fs::read(dir.join(LAYERS)).unwrap(); let layers: Vec = serde_json::from_slice(&text).unwrap(); @@ -1459,14 +1478,27 @@ mod tests { /// In tests, given copies go in a folder of each test's own thread, removed when it ends. #[cfg(test)] pub(crate) mod test_bases { - /// Turns on keeping copies, and writing lists, in each layer's own system - /// ([`super::writes_own_systems`]) for this test process; off by default, as in a release. - /// Every test passes either way, so tests sharing a process don't depend on it. - pub(crate) static OWN_SYSTEMS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); - - /// [`OWN_SYSTEMS`] on. - pub(crate) fn write_own_systems() { - OWN_SYSTEMS.store(true, std::sync::atomic::Ordering::Relaxed); + std::thread_local! { + /// Whether copies are kept, and lists written, in each layer's own system + /// ([`super::writes_own_systems`]) on this test's thread: off, as in a release, unless + /// the test turns it on. A copy is kept on the thread that runs `offline.keepRows` in + /// tests, the test's own. + pub(crate) static OWN_SYSTEMS: std::cell::Cell = const { std::cell::Cell::new(false) }; + } + + /// [`OWN_SYSTEMS`] on for this test, until what it returns is dropped. + #[must_use = "the switch is off again once this is dropped"] + pub(crate) fn write_own_systems() -> OwnSystems { + OwnSystems(OWN_SYSTEMS.with(|on| on.replace(true))) + } + + /// Puts [`OWN_SYSTEMS`] back as it was when dropped. + pub(crate) struct OwnSystems(bool); + + impl Drop for OwnSystems { + fn drop(&mut self) { + OWN_SYSTEMS.with(|on| on.set(self.0)); + } } use std::path::PathBuf; diff --git a/docs/user-guide/field-work.md b/docs/user-guide/field-work.md index b6345e5888..39a30d67bc 100644 --- a/docs/user-guide/field-work.md +++ b/docs/user-guide/field-work.md @@ -217,8 +217,8 @@ Where a change meets one made on the computer since the package was made: map); one the area holds can't be compared with the phone's, and the sync is refused, naming it. Send Field Package can't list the changes of a layer holding such a feature: the package goes back as a whole map, and the phone says why, naming it. Bring Back of a list of changes refuses - only for such a feature of the computer's layer that the list changes, naming it; the others stay - as stored. + only for such a feature that the list changes, naming it: one of the computer's layer the list + edits or deletes, or one of the package's map it adds or edits. The others stay as stored. Each of these keeps the version that lost on the offline area: the computer's values and shape the phone's replaced, the phone's edits to a feature deleted on the computer, the computer's feature From 172fd5a9f9bb362a29878acd6097d18200a3bb63 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 24 Nov 2026 19:02:41 +0100 Subject: [PATCH 5/6] Review: the guide names the features the list changes --- docs/user-guide/field-work.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/user-guide/field-work.md b/docs/user-guide/field-work.md index 39a30d67bc..0364433ec2 100644 --- a/docs/user-guide/field-work.md +++ b/docs/user-guide/field-work.md @@ -217,8 +217,9 @@ Where a change meets one made on the computer since the package was made: map); one the area holds can't be compared with the phone's, and the sync is refused, naming it. Send Field Package can't list the changes of a layer holding such a feature: the package goes back as a whole map, and the phone says why, naming it. Bring Back of a list of changes refuses - only for such a feature that the list changes, naming it: one of the computer's layer the list - edits or deletes, or one of the package's map it adds or edits. The others stay as stored. + only for such a feature that the list changes, naming it: one of the computer's features that + the list edits or deletes, or one of the package's features that it adds or edits. The others + stay as stored. Each of these keeps the version that lost on the offline area: the computer's values and shape the phone's replaced, the phone's edits to a feature deleted on the computer, the computer's feature From 7fa3a90511f083bb948e9641d7a6ab621947a525 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Tue, 24 Nov 2026 19:51:45 +0100 Subject: [PATCH 6/6] Review: a version with no place in longitude/latitude that a conflict would keep refuses the return (conflicts are saved finite), tested through save and open; guide --- crates/engine/src/cmd/field_audit_tests.rs | 37 +++++++++++----- crates/engine/src/cmd/field_changes.rs | 50 ++++++++++------------ docs/user-guide/field-work.md | 7 +-- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/crates/engine/src/cmd/field_audit_tests.rs b/crates/engine/src/cmd/field_audit_tests.rs index 795b48cacd..3698ae186a 100644 --- a/crates/engine/src/cmd/field_audit_tests.rs +++ b/crates/engine/src/cmd/field_audit_tests.rs @@ -878,11 +878,12 @@ fn a_home_feature_an_addition_renumbers_around_doesnt_refuse_a_converted_return( ); } -/// A shape the computer moved where it has no place in longitude/latitude, which the field's then -/// replaces, is kept among the conflicts without that place (they are in longitude/latitude), and -/// the return says so. +/// A shape the computer moved where it has no place in longitude/latitude, which the field's would +/// replace, can't be kept among the conflicts (they are in longitude/latitude, saved finite): the +/// return is refused, naming the feature, and nothing changes. With the computer's shape placed, +/// the same return comes back, the conflict kept, and the project saves and opens again. #[test] -fn a_displaced_shape_with_no_place_in_longitude_latitude_is_said() { +fn a_displaced_shape_with_no_place_in_longitude_latitude_refuses_the_return() { use rustgis_data::{Coord, Geometry, raster::Crs}; let _own = crate::cmd::field_changes::test_bases::write_own_systems(); let temp = Temp::new("native-list-placeless-conflict"); @@ -896,13 +897,29 @@ fn a_displaced_shape_with_no_place_in_longitude_latitude_is_said() { let ds = &mut a.map_mut().layer_mut(cities).unwrap().dataset; let Some(Geometry::Point(c)) = ds.features[0].geometry else { panic!() }; ds.features[0].geometry = Some(Geometry::Point(Coord::new(c.x + 10.0, c.y))); + let set_home = |home: &mut Session, at: Coord| { + let ds = &mut home.map_mut().layer_mut(hc).unwrap().dataset; + let row = ds.features.iter().position(|f| f.id == moved).unwrap(); + ds.features.set_geometry(row, Some(Geometry::Point(at))); + }; // Home moves the same city where it has no place in longitude/latitude. - let ds = &mut home.map_mut().layer_mut(hc).unwrap().dataset; - let row = ds.features.iter().position(|f| f.id == moved).unwrap(); - ds.features.set_geometry(row, Some(Geometry::Point(Coord::new(1e9, 0.0)))); - let (done, said) = bring_back(&temp, &mut a, &mut home, "a"); - assert_eq!((done["updated"].as_u64(), done["conflicts"].as_u64()), (Some(1), Some(1)), "{done} {said:?}"); - assert!(said.iter().any(|t| t.contains("1 shape(s) that lost") && t.contains("don't hold that place")), "{said:?}"); + set_home(&mut home, Coord::new(1e9, 0.0)); + let (_, back) = package(&mut a, &Value::Null).unwrap(); + let path = temp.0.join("a-back.rgisfield"); + std::fs::write(&path, &back).unwrap(); + let before = serde_json::to_value(&home.project).unwrap(); + let err = home.run("offline.bringBack", &json!({"path": path.display().to_string()})).unwrap_err(); + assert!(err.contains(&format!("feature {moved}: the version a conflict would keep")), "{err}"); + assert_eq!(serde_json::to_value(&home.project).unwrap(), before, "a refused return changes nothing"); + // Placed at home: it comes back, the conflict kept, with no such refusal, and saves. + set_home(&mut home, Coord::new(c.x - 10.0, c.y)); + let done = home.run("offline.bringBack", &json!({"path": path.display().to_string()})).unwrap(); + assert_eq!((done["updated"].as_u64(), done["conflicts"].as_u64()), (Some(1), Some(1)), "{done}"); + let saved = temp.0.join("home-after.rgis"); + home.run("project.saveAs", &json!({"path": saved})).unwrap(); + let mut again = Session::new(Project::default()); + again.run("project.open", &json!({"path": saved})).unwrap(); + assert_eq!(again.project.offline_areas[0].conflicts.len(), 1); } /// A list of a version this RustGIS doesn't write is refused, as is one of version 1 that gives a diff --git a/crates/engine/src/cmd/field_changes.rs b/crates/engine/src/cmd/field_changes.rs index d952783936..452ae8fe1a 100644 --- a/crates/engine/src/cmd/field_changes.rs +++ b/crates/engine/src/cmd/field_changes.rs @@ -802,8 +802,6 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang )); } let mut done = Applied::default(); - // Shapes that lost kept without a place that can't be given in longitude/latitude: said. - let placeless = std::cell::Cell::new(0usize); let tag = |layer: &str, id: u64| format!("{layer} #{id}"); let src = s.project.maps.get_mut(si).ok_or("the source map is gone")?; for lc in &changes.layers { @@ -851,21 +849,25 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang unplaced_touched(own, &lonlat, &changed).map_err(error)?; sl.dataset = lonlat.into_owned(); } - // A shape that lost is kept in longitude/latitude ([`AreaConflict::geometry`]): from the - // layer's own system when it is changed there, a place that can't be given there as NaN, - // which the list of conflicts says. + // A shape that lost is kept in longitude/latitude ([`AreaConflict::geometry`], which a + // saved project holds finite): from the layer's own system when it is changed there. One + // with a place that can't be given there can't be kept, so it refuses the return, naming + // the feature, and nothing changes (the command puts the project back). let to_lonlat = if native { Some(listed.to(Crs::LonLat).map_err(error)?) } else { None }; - let kept_shape = |g: Option>| match &to_lonlat { - Some(to) => g.map(|g| { + let kept_shape = |id: u64, g: Option>| -> Result>, String> { + let Some(to) = &to_lonlat else { return Ok(g) }; + g.map(|g| { g.map(|g| { - let lonlat = to.geometry(&g); - if g.is_finite() && !lonlat.is_finite() { - placeless.set(placeless.get() + 1); - } - lonlat + rustgis_data::Dataset::converted_to_lonlat(to, &g).map(std::borrow::Cow::into_owned).ok_or_else(|| { + error(format!( + "feature {id}: the version a conflict would keep has a place in {} that can't be given in longitude/latitude, where conflicts are kept, so nothing was changed", + listed.label() + )) + }) }) - }), - None => g, + .transpose() + }) + .transpose() }; // Every id the layer had, or kept new ones above, before this return. let home_top = sl.dataset.features.highest_id(); @@ -886,8 +888,9 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang let field_value = |row: usize, field: &str| theirs.field_index(field).map_or(&Value::Null, |i| theirs.features.value(row, i)).clone(); let mut written = HashSet::new(); - let lost = - |kind, id, values, shape| super::workspace::conflict(&changes.package, lc.layer, &name, id, kind, values, kept_shape(shape)); + let lost = |kind, id, values, shape| -> Result<_, String> { + Ok(super::workspace::conflict(&changes.package, lc.layer, &name, id, kind, values, kept_shape(id, shape)?)) + }; for g in &lc.edited { // An edit naming a feature the package's map hasn't has no values to bring back or keep. let Some(&p) = at_field.get(&g.id) else { @@ -900,7 +903,7 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang // The field's edits lost: the values and shape it changed. let values = g.values.iter().map(|(field, _)| (field.clone(), field_value(p, field))).collect(); let shape = g.geometry.as_ref().map(|_| theirs.features.geometry(p).cloned()); - done.lost.push(lost(ConflictKind::Dropped, g.id, values, shape)); + done.lost.push(lost(ConflictKind::Dropped, g.id, values, shape)?); continue; }; let (mut changed, mut values, mut shape) = (false, Vec::new(), None); @@ -934,7 +937,7 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang } if !values.is_empty() || shape.is_some() { done.conflicts.push(tag(&name, g.id)); - done.lost.push(lost(ConflictKind::Replaced, g.id, values, shape)); + done.lost.push(lost(ConflictKind::Replaced, g.id, values, shape)?); } } // A deletion goes in while the feature is as it was given; one changed at home stays. @@ -958,7 +961,7 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang gone.insert(g.id); } else { done.kept.push(tag(&name, g.id)); - done.lost.push(lost(ConflictKind::Kept, g.id, Vec::new(), None)); + done.lost.push(lost(ConflictKind::Kept, g.id, Vec::new(), None)?); } } if !gone.is_empty() { @@ -994,15 +997,6 @@ pub(crate) fn apply(s: &mut Session, ai: usize, edited: &rustgis_map::Map, chang } super::workspace::renew_area(s, ai, si, offline_map, next_generation)?; super::workspace::keep_conflicts(s, ai, std::mem::take(&mut done.lost)); - if placeless.get() > 0 { - s.notify( - format!( - "Offline area '{area_name}': {} shape(s) that lost to the field's have a place that can't be given in longitude/latitude, so the versions kept in Data › Offline › Conflicts don't hold that place", - placeless.get() - ), - false, - ); - } done.say(s, &area_name, &source); Ok(json!({ "updated": done.updated, diff --git a/docs/user-guide/field-work.md b/docs/user-guide/field-work.md index 0364433ec2..856c366cf6 100644 --- a/docs/user-guide/field-work.md +++ b/docs/user-guide/field-work.md @@ -215,11 +215,12 @@ Where a change meets one made on the computer since the package was made: given in longitude/latitude (one far past where that system reaches) stays as stored when it lies outside the area when the area is synced (**Sync**, or a package that goes back as a whole map); one the area holds can't be compared with the phone's, and the sync is refused, naming it. - Send Field Package can't list the changes of a layer holding such a feature: the package goes - back as a whole map, and the phone says why, naming it. Bring Back of a list of changes refuses + Send Field Package can't list the changes of a layer holding such a feature the package gave: + the package goes back as a whole map, and the phone says why, naming it. Bring Back of a list of changes refuses only for such a feature that the list changes, naming it: one of the computer's features that the list edits or deletes, or one of the package's features that it adds or edits. The others - stay as stored. + stay as stored. A change that would keep among the conflicts a version with such a place + refuses it too, naming the feature, as conflicts are kept in longitude/latitude. Each of these keeps the version that lost on the offline area: the computer's values and shape the phone's replaced, the phone's edits to a feature deleted on the computer, the computer's feature