Skip to content
12 changes: 11 additions & 1 deletion crates/engine/src/cmd/gpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ fn add_layer(s: &mut Session, p: &Value) -> CmdResult {
/// with no table named) read it again, in every map: its rows were written, or (with `reopen`)
/// the table was made anew, so its fields and system are read again too. Returns how many read
/// it again, and the layers that couldn't reopen it with why (each also keeps its error).
/// Replacement notes are reported immediately, including for callers without a frame loop.
pub(crate) fn reset_sources(s: &mut Session, path: &std::path::Path, table: &str, reopen: bool) -> (usize, Vec<String>) {
let (mut n, mut failed) = (0, Vec::new());
let mut notes = Vec::new();
let canonical = std::fs::canonicalize(path).ok();
for map in &mut s.project.maps {
for l in map.layers.iter_mut() {
Expand All @@ -164,14 +166,22 @@ pub(crate) fn reset_sources(s: &mut Session, path: &std::path::Path, table: &str
source.reset();
n += 1;
} else {
match source.reopen() {
let reopened = l.refresh_replaced_source();
if let Some(source) = &mut l.source {
notes.extend(source.take_read_notes().into_iter().map(|note| note.prefixed(&l.name)));
}
match reopened {
Ok(()) => n += 1,
Err(e) => failed.push(format!("{} ({e})", l.name)),
}
}
}
}
}
// Replacement notes must land with the command, including in callers without a frame loop.
for note in notes {
s.say(note);
}
(n, failed)
}

Expand Down
20 changes: 18 additions & 2 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31446,9 +31446,24 @@ fn exporting_features_adds_or_replaces_a_table_in_an_existing_geopackage() {
l.sync_source(&rustgis_geom::Extent::new(-20.0, -20.0, 20.0, 20.0), true);
});
assert_eq!(kind(&s), Some(rustgis_data::GeometryType::Polygon));
let in_place = s.map_mut().layers.iter_mut().find(|layer| layer.name == "InPlace").unwrap();
let moved = rustgis_map::symbology::MovedLabel { id: in_place.dataset.features.row(0).unwrap().id(), at: [4.0, 5.0] };
in_place.labels.default.moved.push(moved);
let mut class = in_place.labels.default.clone();
class.name = "Subset".into();
in_place.labels.classes.push(class);
let mark = s.notice_mark();
s.run("share.exportLayer", &json!({"layer": "Cities", "path": path, "mode": "replaceTable", "tableName": "Countries"})).unwrap();
assert_eq!(kind(&s), Some(rustgis_data::GeometryType::Point), "the new table's");
assert!(s.map().layer_by_name("InPlace").unwrap().labels.in_order().all(|class| class.moved.is_empty()));
assert_eq!(
s.notices_since(mark)
.iter()
.filter(|note| note.text == "InPlace: 2 saved label move(s) reset: replacement data may reuse feature ids")
.count(),
1,
"the replacement command says the reset without requiring a render frame"
);
let read = s.map().layer_by_name("InPlace").map(|l| l.load_all().map(|ds| ds.features.len()));
assert_eq!(read, Some(Ok(cities)), "its features are the new table's");
assert!(s.notices_since(mark).iter().any(|n| n.text.ends_with("1 layer reads Countries in place, now the new table")));
Expand Down Expand Up @@ -57467,7 +57482,7 @@ fn reads_made_ahead_are_taken_by_the_command() {
assert_eq!(unread(&s), before + 1, "{:?}", s.notices.iter().map(|n| &n.text).collect::<Vec<_>>());
}

// A field added while the read ran: it is read again, with that field kept.
// A saved field absent from the source must not receive positional source values.
let path = dir.join("fields.geojson");
std::fs::write(&path, points).unwrap();
let id = s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap()["layer"].as_u64().unwrap();
Expand All @@ -57477,7 +57492,8 @@ fn reads_made_ahead_are_taken_by_the_command() {
extra.name = "added".into();
s.map_mut().layer_mut(id as u32).unwrap().dataset.fields.push(extra);
s.run("layer.rename", &json!({"layer": id, "name": "Renamed while reading"})).unwrap();
crate::cmd::reads::read_land(&mut s, read).unwrap();
let error = crate::cmd::reads::read_land(&mut s, read).unwrap_err();
assert!(error.contains("added") && error.contains("Reload Data"), "{error}");
assert_eq!(s.map().layer(id as u32).unwrap().name, "Renamed while reading");
assert!(s.map().layer(id as u32).unwrap().dataset.fields.iter().any(|f| f.name == "added"));

Expand Down
Loading
Loading