From e0a61c2dffe9c37e5f060315355728c5981a1d73 Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Mon, 16 Nov 2026 16:25:54 +0000 Subject: [PATCH 01/14] feat(annotation): add image-aware pane and original-pixel drawing --- crates/assistant/src/mcp.rs | 4 +- crates/assistant/src/relay/archive_tests.rs | 40 ++ crates/engine/src/cmd/raster.rs | 3 + crates/engine/src/cmd/reads.rs | 1 + crates/engine/src/image_grid.rs | 185 ++++++++ crates/engine/src/lib.rs | 1 + crates/engine/src/policy/dispatch.rs | 9 +- crates/engine/src/shape_conversion.rs | 22 +- crates/engine/src/tests.rs | 21 + crates/ui-egui/src/annotation.rs | 480 ++++++++++++++++++++ crates/ui-egui/src/boxedit.rs | 12 +- crates/ui-egui/src/chrome.rs | 2 + crates/ui-egui/src/circleedit.rs | 12 +- crates/ui-egui/src/editpanes.rs | 3 + crates/ui-egui/src/lib.rs | 20 + crates/ui-egui/src/panes.rs | 2 + crates/ui-egui/src/readjob.rs | 90 +++- crates/ui-egui/src/ribbon.rs | 25 + docs/user-guide/editing.md | 39 ++ 19 files changed, 944 insertions(+), 27 deletions(-) create mode 100644 crates/engine/src/image_grid.rs create mode 100644 crates/ui-egui/src/annotation.rs diff --git a/crates/assistant/src/mcp.rs b/crates/assistant/src/mcp.rs index e9752f7abd..78010c60af 100644 --- a/crates/assistant/src/mcp.rs +++ b/crates/assistant/src/mcp.rs @@ -164,7 +164,9 @@ impl ArchiveBatch { if name != "run_command" || !matches!( args.get("command").and_then(Value::as_str).map(str::trim), - Some("map.reloadTiles" | "map.repairTiles" | "table.statistics" | "edit.previewShapeConversion") + Some( + "map.reloadTiles" | "map.repairTiles" | "table.statistics" | "edit.previewShapeConversion" | "raster.imageGrid" + ) ) { return None; diff --git a/crates/assistant/src/relay/archive_tests.rs b/crates/assistant/src/relay/archive_tests.rs index 48a3c2a575..2f447f4c0e 100644 --- a/crates/assistant/src/relay/archive_tests.rs +++ b/crates/assistant/src/relay/archive_tests.rs @@ -608,6 +608,46 @@ fn archive_batch_resumes_same_context_after_validation_failure_or_cancellation() } } +#[test] +fn annotation_grid_request_uses_the_mcp_worker_and_returns_the_original_frame() { + use rustgis_engine::{ + box_edit::Frame, + data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}, + }; + let raster = Raster::new( + "Image", + 2, + 3, + vec![vec![1.; 6]], + GeoTransform { origin_x: 100., origin_y: 200., pixel_width: 2., pixel_height: -3. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .unwrap(); + let bytes = geotiff::write(&raster, 0).unwrap(); + let expected = Frame::from_grid(&geotiff::read_image_grid(std::io::Cursor::new(&bytes)).unwrap()).unwrap(); + let mut s = Session::new(Project::default()); + let id = s.map_mut().add_raster("Image", rustgis_engine::map::RasterLayer::from_bytes(raster, "image.tif", bytes)); + let depth = s.undo_depth(); + let relay = Relay::start(0).unwrap(); + let grant = relay.grant(true).unwrap(); + let (_, rx) = queue(&relay, &grant, command(1, "raster.imageGrid", json!({"layer":id}))); + assert_eq!(relay.poll(&mut s), 0); + assert!(rx.try_recv().is_err()); + let done = worker(relay.take_archive_read().unwrap()); + finish(&relay, &mut s, done); + let answer = reply(&rx); + assert!(answer.get("error").is_none(), "{answer}"); + let result = answer.get("result").expect("grid result missing"); + assert_eq!(result["isError"], false, "{answer}"); + let actual: Frame = serde_json::from_str(result["content"][0]["text"].as_str().unwrap()).unwrap(); + assert_eq!(actual, expected); + assert_eq!(s.undo_depth(), depth); + relay.stop(); +} + #[test] fn shape_conversion_preview_uses_the_worker_without_implicitly_applying() { use rustgis_engine::data::{Dataset, GeometryType}; diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 263e3ff04f..9c9360e1f4 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -33,6 +33,9 @@ pub(crate) fn need_raster(s: &Session) -> Option<&'static str> { pub fn commands() -> Vec { vec![ + C::new("raster.imageGrid", "Original Image Grid", "Analyze › Annotate", crate::image_grid::query) + .inputs(crate::cmd::Inputs::single_layer).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).view() + .params(r#"{"layer"?:name|id}"#), C::new("layer.addDataBytes", "Add Data (from file contents)", "Home › Add", add_bytes).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"fileName": "name.tif|.tiff|.asc|.geojson|.csv|.zip|.kml|.kmz|.gpx|.parquet|.fgb|.gpkg|.mbtiles|.jp2|.j2k|.dxf (JPEG 2000: desktop, through GDAL)", "data": base64, "zoom"?: bool, "layer"?: "one layer of a zip, KML or GPX by name", "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "blocks"?, "stream"?, "as"? (as layer.addData)}"#), C::new("raster.symbology", "Symbology", "Style › Rendering", symbology).when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params( diff --git a/crates/engine/src/cmd/reads.rs b/crates/engine/src/cmd/reads.rs index be8a45f937..a7e0af5380 100644 --- a/crates/engine/src/cmd/reads.rs +++ b/crates/engine/src/cmd/reads.rs @@ -214,6 +214,7 @@ pub(crate) fn prepare(s: &mut Session, op: Authorized, owner: Continu id if super::edit::table_edit_in_background(id).is_some() => ("Edit Table", super::edit::table_ahead(s, spec.id, params)?, None), "topology.validate" => ("Error Inspector", super::design::topology_ahead(s, params), None), "source.syncState" => ("Source File State", super::publish::sync_ahead(s, params), None), + "raster.imageGrid" => ("Read Original Image Grid", crate::image_grid::ahead(s, params), None), "edit.previewShapeConversion" => ("Preview Shape Conversion", crate::shape_conversion::ahead(s, params), None), "table.statistics" => ("Column Statistics", super::table_stats::stats_ahead(s, params), None), _ => return Err("only Add Data, Load All, Repair/Reload Data, Convert To Annotation, models, scripts, tile archives, service listings, topology, Source File State and Column Statistics prepare background work".into()), diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs new file mode 100644 index 0000000000..f23f83bf44 --- /dev/null +++ b/crates/engine/src/image_grid.rs @@ -0,0 +1,185 @@ +//! Original GeoTIFF construction grids, read independently of display overviews. +use std::sync::Arc; + +use crate::{CmdResult, Session, box_edit::Frame}; +use rustgis_data::progress; +use rustgis_map::raster::RasterLayer; +use serde_json::Value; + +/// Session-local identity of an image source. Style changes do not invalidate a grid; replacing +/// its pixels, file reference, embedded bytes, layer or map does. No file reads occur here. +#[derive(Clone)] +pub struct Source { + map: u64, + layer: u32, + instance: u64, + replacement: u64, + raster: RasterLayer, +} +fn same_arc(a: &Option>, b: &Option>) -> bool { + match (a, b) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + (None, None) => true, + _ => false, + } +} +impl Source { + pub fn capture(s: &Session, layer: u32) -> Result { + let l = s.map().layer(layer).ok_or("no image layer")?; + let raster = l.raster.clone().ok_or("choose a GeoTIFF image layer")?; + Ok(Self { map: s.map().instance_id(), layer, instance: l.instance_id(), replacement: l.replacement_revision(), raster }) + } + pub fn current(&self, s: &Session) -> bool { + s.map().instance_id() == self.map + && s.map().layer(self.layer).is_some_and(|l| { + l.instance_id() == self.instance + && l.replacement_revision() == self.replacement + && l.raster.as_ref().is_some_and(|r| { + r.path == self.raster.path && same_arc(&r.embedded, &self.raster.embedded) && same_arc(&r.data, &self.raster.data) + }) + }) + } +} + +/// Cancel is checked before/after the bounded header parse; a filesystem read/seek itself +/// cannot be interrupted until the OS returns. No raster pixel decoding takes place. +pub(crate) fn read(r: &RasterLayer) -> Result { + progress::begin(2); + progress::step(1).map_err(|e| e.to_string())?; + let grid = if let Some(bytes) = &r.embedded { + rustgis_data::raster::geotiff::read_image_grid(std::io::Cursor::new(bytes.as_slice())) + } else { + let path = r + .path + .as_deref() + .filter(|p| rustgis_map::paths::is_local(p)) + .ok_or("choose a local or embedded GeoTIFF; remote original grids must be downloaded and added first")?; + #[cfg(not(target_arch = "wasm32"))] + { + let input = std::fs::File::open(path).map_err(|e| e.to_string())?; + rustgis_data::raster::geotiff::read_image_grid(input) + } + #[cfg(target_arch = "wasm32")] + { + let _ = path; + return Err("upload the original GeoTIFF to use its grid in the browser".into()); + } + } + .map_err(|e| e.to_string())?; + progress::step(1).map_err(|e| e.to_string())?; + Frame::from_grid(&grid) +} +struct Prepared { + source: Source, + result: Result, +} +pub(crate) fn ahead(s: &Session, p: &Value) -> Option { + let source = Source::capture(s, s.layer_id(p).ok()?).ok()?; + Some(crate::cmd::reads::ahead("Read Original Image Grid", "imageGrid".into(), move || { + let result = read(&source.raster); + Prepared { source, result } + })) +} +/// Desktop UI calls require the cancellable read queue. Headless callers run on their own +/// worker; the browser reads uploaded bytes inline. Reads only headers, never display pixels. +pub(crate) fn query(s: &mut Session, p: &Value) -> CmdResult { + let source = Source::capture(s, s.layer_id(p)?)?; + let ready = crate::cmd::reads::take::(s, "imageGrid"); + #[cfg(not(target_arch = "wasm32"))] + if ready.is_none() && s.statistics_window_thread == Some(std::thread::current().id()) { + return Err("original image grids need the desktop background read queue".into()); + } + let frame = match ready { + Some(done) if done.source.current(s) && done.source.layer == source.layer => done.result?, + Some(_) => return Err("image source changed while reading its grid; select the image again".into()), + None => read(&source.raster)?, + }; + serde_json::to_value(frame).map_err(|e| e.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + use serde_json::json; + fn session() -> (Session, u32, Frame) { + let raster = Raster::new( + "original", + 4, + 6, + vec![vec![1.; 24]], + GeoTransform { origin_x: 100., origin_y: 500., pixel_width: 2., pixel_height: -3. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .unwrap(); + let bytes = geotiff::write(&raster, 0).unwrap(); + let frame = Frame::from_grid(&geotiff::read_image_grid(std::io::Cursor::new(&bytes)).unwrap()).unwrap(); + let overview = Raster::new("overview", 1, 1, vec![vec![1.]], raster.transform, raster.crs, None, ColorHint::Gray, "UInt8").unwrap(); + let mut s = Session::new(crate::Project::default()); + let id = s.map_mut().add_raster("Image", RasterLayer::from_bytes(overview, "image.tif", bytes)); + (s, id, frame) + } + #[test] + fn annotation_grid_command_reads_original_header_and_never_mutates_the_project() { + let (mut s, id, expected) = session(); + let depth = s.undo_depth(); + let frame: Frame = serde_json::from_value(s.run("raster.imageGrid", &json!({"layer":id})).unwrap()).unwrap(); + assert_eq!(frame, expected); + assert_eq!((frame.width, frame.height), (4, 6)); + assert_eq!(s.undo_depth(), depth); + s.statistics_need_read_ahead(true); + assert!(s.run("raster.imageGrid", &json!({"layer":id})).unwrap_err().contains("background")); + let ahead = crate::cmd::reads::read_ahead(&mut s, "raster.imageGrid", &json!({"layer":id})).unwrap(); + let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); + assert_eq!(serde_json::from_value::(crate::cmd::reads::read_land(&mut s, done).unwrap()).unwrap(), expected); + } + #[test] + fn annotation_grid_background_read_cancels_and_rejects_replaced_sources() { + let (mut s, id, _) = session(); + let ahead = crate::cmd::reads::read_ahead(&mut s, "raster.imageGrid", &json!({"layer":id})).unwrap(); + let control = rustgis_analysis::control::Control::new(); + control.cancel(); + assert!(ahead.run(&control).is_err()); + let ahead = crate::cmd::reads::read_ahead(&mut s, "raster.imageGrid", &json!({"layer":id})).unwrap(); + let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); + s.map_mut().layer_mut(id).unwrap().raster.as_mut().unwrap().embedded = Some(Arc::new(vec![0; 32])); + assert!(crate::cmd::reads::read_land(&mut s, done).is_err()); + } + #[test] + fn annotation_grid_scoped_reads_preserve_approved_selectors_and_refuse_retargeting() { + use crate::policy::{Call, Outcome, Scope}; + struct Live; + impl crate::policy::Revocation for Live { + fn allows(&self, _: crate::policy::Effects) -> bool { + true + } + } + for named in [false, true] { + for retarget in [false, true] { + let (mut s, id, expected) = session(); + s.active_layer = Some(id); + let params = if named { json!({"layer":"Image"}) } else { json!({}) }; + let call = Call::command("raster.imageGrid", params); + let scope = Scope::assistant(1717, Arc::new(Live)); + let Outcome::NeedsApproval(pending) = s.review(&scope, &call) else { panic!("image reads require Files approval") }; + let read = s.start_scoped_archive_read(scope, &call, Some(pending.allow())).unwrap(); + let done = read.run(&rustgis_analysis::control::Control::new()).unwrap(); + if retarget { + s.run("layer.rename", &json!({"layer":id,"name":"Old image"})).unwrap(); + let raster = s.map().layer(id).unwrap().raster.clone().unwrap(); + let other = s.map_mut().add_raster("Image", raster); + s.active_layer = Some(other); + } + let answer = crate::cmd::reads::read_land(&mut s, done); + if retarget { + assert!(answer.is_err()); + } else { + assert_eq!(serde_json::from_value::(answer.unwrap()).unwrap(), expected); + } + } + } + } +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 62b2e8c6ee..b54942498c 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -13,6 +13,7 @@ mod construction; pub mod durable; pub mod extensions; pub mod geometry_calc; +pub mod image_grid; pub mod model; mod notice_text; #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs index 52538155df..e024b9313b 100644 --- a/crates/engine/src/policy/dispatch.rs +++ b/crates/engine/src/policy/dispatch.rs @@ -124,7 +124,10 @@ impl Session { let CallKind::Command { id, params } = &call.kind else { return Err("only standalone archive or column-statistics commands can prepare a scoped read".into()); }; - if !matches!(id.as_str(), "map.reloadTiles" | "map.repairTiles" | "table.statistics" | "edit.previewShapeConversion") { + if !matches!( + id.as_str(), + "map.reloadTiles" | "map.repairTiles" | "table.statistics" | "edit.previewShapeConversion" | "raster.imageGrid" + ) { return Err("only standalone archive or column-statistics commands can prepare a scoped read".into()); } let capture = |s: &mut Session| s.capture_read(id, params); @@ -156,6 +159,7 @@ impl Session { | "service.add" | "source.syncState" | "table.statistics" + | "raster.imageGrid" | "edit.previewShapeConversion" ) && crate::cmd::edit::table_edit_in_background(command).is_none() { @@ -172,8 +176,9 @@ impl Session { | "labels.convert" | "source.syncState" | "table.statistics" + | "raster.imageGrid" | "edit.previewShapeConversion" - ) && (command != "edit.previewShapeConversion" || self.policy_user()) + ) && (!matches!(command, "edit.previewShapeConversion" | "raster.imageGrid") || self.policy_user()) || (command == "edit.convertField" && params.get("table").is_none_or(Value::is_null)) { let id = self.layer_id(&Value::Object(params.clone()))?; diff --git a/crates/engine/src/shape_conversion.rs b/crates/engine/src/shape_conversion.rs index d5c89fb9f6..ec64a2ab35 100644 --- a/crates/engine/src/shape_conversion.rs +++ b/crates/engine/src/shape_conversion.rs @@ -213,27 +213,7 @@ fn target(s: &Session, p: &Value) -> Result { } fn frame(raster: Option<&rustgis_map::raster::RasterLayer>, p: &Value) -> Result { if let Some(r) = raster { - let grid = if let Some(bytes) = &r.embedded { - rustgis_data::raster::geotiff::read_image_grid(std::io::Cursor::new(bytes.as_slice())) - } else { - let path = r - .path - .as_deref() - .filter(|p| rustgis_map::paths::is_local(p)) - .ok_or("choose a local or embedded GeoTIFF; remote original grids are not read by conversion")?; - #[cfg(not(target_arch = "wasm32"))] - { - let input = std::fs::File::open(path).map_err(|e| e.to_string())?; - rustgis_data::raster::geotiff::read_image_grid(input) - } - #[cfg(target_arch = "wasm32")] - { - let _ = path; - return Err("upload the original GeoTIFF to use its grid in the browser".into()); - } - } - .map_err(|e| e.to_string())?; - Frame::from_grid(&grid) + crate::image_grid::read(r) } else if let Some(f) = p.get("frame") { serde_json::from_value(f.clone()).map_err(|e| e.to_string()) } else { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 61364033f4..b3b4a60cfd 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -48537,6 +48537,27 @@ fn long_commands_stop_when_cancelled_while_they_run() { note("edit.previewShapeConversion", read.run(&control).map(|_| ()), &control); } + // Reading original TIFF headers also checks Cancel through the common read queue. + { + let raster = rustgis_data::raster::Raster::new( + "grid", + 1, + 1, + vec![vec![1.]], + rustgis_data::raster::GeoTransform { origin_x: 0., origin_y: 0., pixel_width: 1., pixel_height: -1. }, + rustgis_data::raster::Crs::from_epsg(3857).unwrap(), + None, + rustgis_data::raster::ColorHint::Gray, + "UInt8", + ) + .unwrap(); + let bytes = rustgis_data::raster::geotiff::write(&raster, 0).unwrap(); + let id = s.map_mut().add_raster("Original grid", rustgis_map::RasterLayer::from_bytes(raster, "grid.tif", bytes)); + let read = crate::cmd::reads::read_ahead(&mut s, "raster.imageGrid", &json!({"layer":id})).unwrap(); + let control = Control::cancelled_once_started(); + note("raster.imageGrid", read.run(&control).map(|_| ()), &control); + } + // Column Statistics counts every row of a layer on a thread of its own in the desktop app. { let read = crate::cmd::reads::read_ahead(&mut s, "table.statistics", &json!({"layer": "Cities", "field": "pop_max"})).unwrap(); diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs new file mode 100644 index 0000000000..6b8c3afdf1 --- /dev/null +++ b/crates/ui-egui/src/annotation.rs @@ -0,0 +1,480 @@ +//! Session-only image/grid choices shared by Annotate and the ordinary Edit tools. +use crate::{Landed, RightPane, RustGisApp}; +use rustgis_engine::{Tool, box_edit::Frame, image_grid::Source}; +use serde_json::{Value, json}; + +#[derive(Default)] +pub(crate) struct State { + context: Option<(u64, u64)>, + image: Option, + image_chosen: bool, + source: Option, + pixels: bool, + frame: Option, + pending: Option, + next: u64, + error: Option, +} +fn cancel(app: &mut RustGisApp) { + if let Some(request) = app.annotation.pending.take() { + #[cfg(not(target_arch = "wasm32"))] + app.reads.cancel_annotation_grid(request); + #[cfg(target_arch = "wasm32")] + let _ = request; + } + crate::boxedit::cancel(app); + crate::circleedit::cancel(app); +} +pub(crate) fn sync(app: &mut RustGisApp) { + let context = (app.session.opened, app.session.map().instance_id()); + if app.annotation.context != Some(context) { + cancel(app); + app.annotation = State { context: Some(context), next: app.annotation.next, ..State::default() }; + } else if app.annotation.source.as_ref().is_some_and(|s| !s.current(&app.session)) { + cancel(app); + app.annotation.frame = None; + app.annotation.source = None; + app.annotation.error = Some(tl!("The image changed or was removed. Select it again or reload its grid.").into()); + } +} +pub(crate) fn open(app: &mut RustGisApp) -> Result<(), String> { + app.ui.open_right(RightPane::Annotation); + if !app.annotation.image_chosen + && app.annotation.image.is_none() + && let Some(id) = app.session.active_layer.filter(|id| app.session.map().layer(*id).is_some_and(|l| l.raster.is_some())) + { + select_image(app, &json!({"image":id}))?; + } + Ok(()) +} +pub(crate) fn select_image(app: &mut RustGisApp, p: &Value) -> Result<(), String> { + sync(app); + let image = p.get("image").filter(|v| !v.is_null()).map(|v| app.session.layer_id(&json!({"layer":v}))).transpose()?; + let source = image.map(|id| Source::capture(&app.session, id)).transpose()?; + let request = app.annotation.next.checked_add(1).ok_or("image-grid request limit reached")?; + cancel(app); + app.annotation.next = request; + app.annotation.image = image; + app.annotation.image_chosen = true; + app.annotation.source = source; + app.annotation.pixels = image.is_some(); + app.annotation.frame = None; + app.annotation.error = None; + app.annotation.pending = image.map(|_| request); + if let Some(id) = image { + #[cfg(not(target_arch = "wasm32"))] + let _ = crate::readjob::run_image_grid(app, json!({"layer":id}), request); + #[cfg(target_arch = "wasm32")] + { + let result = app.run("raster.imageGrid", json!({"layer":id})); + landed(app, request, Landed::Ran(result)); + } + } + Ok(()) +} +pub(crate) fn select_grid(app: &mut RustGisApp, pixels: bool) -> Result<(), String> { + sync(app); + if pixels && app.annotation.image.is_none() { + return Err(tl!("Choose an image before using original-image pixels.").into()); + } + if pixels && app.annotation.frame.is_none() { + return select_image(app, &json!({"image":app.annotation.image})); + } + cancel(app); + app.annotation.pixels = pixels; + app.annotation.error = None; + Ok(()) +} +pub(crate) fn cancel_read(app: &mut RustGisApp) { + cancel(app); + app.annotation.error = Some(tl!("Grid read cancelled. Reload the grid to draw in image pixels.").into()); +} +pub(crate) fn landed(app: &mut RustGisApp, request: u64, result: Landed) { + if app.annotation.pending != Some(request) { + return; + } + sync(app); + if app.annotation.pending != Some(request) { + return; + } + app.annotation.pending = None; + match result { + Landed::Ran(Ok(value)) => match serde_json::from_value(value) { + Ok(frame) => app.annotation.frame = Some(frame), + Err(e) => app.annotation.error = Some(e.to_string()), + }, + Landed::Ran(Err(e)) | Landed::NotRun(e) => app.annotation.error = Some(e), + } +} +/// Only new objects use this choice. Existing objects keep their own frozen frame. +pub(crate) fn drawing_frame(app: &RustGisApp) -> Result { + if app.annotation.context != Some((app.session.opened, app.session.map().instance_id())) || !app.annotation.pixels { + return Ok(Frame::default()); + } + if !app.annotation.source.as_ref().is_some_and(|s| s.current(&app.session)) { + return Err(tl!("The image changed. Reload its grid before drawing.").into()); + } + app.annotation + .frame + .clone() + .ok_or_else(|| app.annotation.error.clone().unwrap_or_else(|| tl!("Wait for the original image grid before drawing.").into())) +} +fn polygon_layer(app: &RustGisApp, id: u32) -> bool { + app.session.map().layer(id).is_some_and(|l| { + !l.is_streamed() + && !l.graphics + && l.raster.is_none() + && matches!(l.geometry_type(), rustgis_engine::data::GeometryType::Polygon | rustgis_engine::data::GeometryType::None) + }) +} +pub(crate) fn select_layer(app: &mut RustGisApp, p: &Value) -> Result<(), String> { + let id = app.session.layer_id(p)?; + if !polygon_layer(app, id) { + return Err(tl!("Choose a loaded editable polygon layer.").into()); + } + app.run("layer.select", json!({"layer":id})).map(|_| ()) +} +pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) { + sync(app); + let mut image = app.annotation.image; + let mut picked = false; + let name = image + .and_then(|id| app.session.map().layer(id)) + .filter(|_| app.annotation.source.as_ref().is_some_and(|s| s.current(&app.session))) + .map_or_else(|| tl!("No image").to_string(), |l| l.name.clone()); + egui::ComboBox::from_label(tl!("Image")).selected_text(name).show_ui(ui, |ui| { + picked |= ui.selectable_value(&mut image, None, tl!("No image")).clicked(); + for l in app.session.map().layers.iter().filter(|l| l.raster.is_some()) { + picked |= ui.selectable_value(&mut image, Some(l.id), &l.name).clicked(); + } + }); + if picked { + let _ = app.run("ui.annotationImage", json!({"image":image})); + } + let mut pixels = app.annotation.pixels; + egui::ComboBox::from_label(tl!("New shape grid")) + .selected_text(if pixels { tl!("Original image pixels") } else { tl!("Web Mercator grid metres") }) + .show_ui(ui, |ui| { + ui.add_enabled_ui(image.is_some(), |ui| ui.selectable_value(&mut pixels, true, tl!("Original image pixels"))); + ui.selectable_value(&mut pixels, false, tl!("Web Mercator grid metres")); + }); + if pixels != app.annotation.pixels { + let _ = app.run("ui.annotationGrid", json!({"pixels":pixels})); + } + if app.annotation.pixels { + ui.small(tl!("New shapes use the original GeoTIFF pixels, including rotation and shear. Existing objects keep their own grid.")); + if let Some(f) = &app.annotation.frame { + ui.small(crate::i18n::fmt( + "{width} × {height} pixels · EPSG:{epsg}", + &[("width", &f.width.to_string()), ("height", &f.height.to_string()), ("epsg", &f.epsg.to_string())], + )); + } + if app.annotation.pending.is_some() { + ui.horizontal(|ui| { + ui.spinner(); + ui.label(tl!("Reading original image grid…")); + }); + if ui.button(tl!("Cancel Grid Read")).clicked() { + let _ = app.run("ui.annotationCancel", Value::Null); + } + } else if ui.button(tl!("Reload Grid")).clicked() { + let _ = app.run("ui.annotationImage", json!({"image":image})); + } + } else { + ui.small(tl!("Web Mercator grid metres, not ground metres. Image pixels are the default when an image is selected.")); + } + if let Some(error) = &app.annotation.error { + ui.colored_label(egui::Color32::RED, error); + } +} +pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) { + ui.heading(tl!("Image annotation")); + grid_controls(app, ui); + ui.separator(); + let active = app.session.active_layer.filter(|id| polygon_layer(app, *id)); + let mut layer = active; + let name = + layer.and_then(|id| app.session.map().layer(id)).map_or_else(|| tl!("Choose a polygon layer").to_string(), |l| l.name.clone()); + egui::ComboBox::from_label(tl!("Target layer")).selected_text(name).show_ui(ui, |ui| { + for l in app.session.map().layers.iter().filter(|l| polygon_layer(app, l.id)) { + ui.selectable_value(&mut layer, Some(l.id), &l.name); + } + }); + if layer != active + && let Some(id) = layer + { + let _ = app.run("ui.annotationLayer", json!({"layer":id})); + } + if layer.is_none() { + ui.label(tl!("Add or create a polygon feature layer using Data, then choose it here.")); + } + let mut enabled = app.session.map().editing.settings.enabled; + if ui.checkbox(&mut enabled, tl!("Enable editing")).changed() { + let _ = app.run("edit.enable", json!({"on":enabled})); + } + let ready = crate::boxedit::ready(app) && drawing_frame(app).is_ok(); + ui.add_enabled_ui(ready, |ui| { + ui.horizontal(|ui| { + for (tool, name) in [(Tool::RotatedBox, tl!("Rotated Box")), (Tool::Circle, tl!("Circle"))] { + if ui.selectable_label(app.session.tool == tool, name).clicked() { + let _ = app.run("tool.set", json!({"tool":tool.code()})); + } + } + }); + }); + if !crate::boxedit::ready(app) { + ui.small(tl!("Drawing requires editing enabled, a loaded polygon layer and a flat Earth map with zero pitch.")); + } + ui.small(tl!("Space or middle-drag pans; releasing Space returns to the active drawing tool. Undo/Redo use the existing commands.")); + crate::boxedit::panel(app, ui); + crate::circleedit::panel(app, ui); + ui.separator(); + ui.add_enabled_ui(layer.is_some(), |ui| { + if ui.button(tl!("Export GeoJSON…")).clicked() { + let _ = app.run("ui.exportLayer", json!({"format":"geojson"})); + } + }); + ui.small(tl!( + "Exports contain ordinary polygons and feature properties. Image/grid choices and exact constructions remain session-only." + )); +} + +#[cfg(test)] +mod tests { + use super::*; + use rustgis_engine::data::raster::{ColorHint, Crs, GeoTransform, Raster}; + use rustgis_engine::{ + Project, Session, + data::{Dataset, GeometryType}, + map::RasterLayer, + }; + + // A minimal uncompressed TIFF with the original ModelTransformation, not a north-up + // display raster. Encoded here so the end-to-end test needs no external image or library. + fn tiff(frame: &Frame) -> Vec { + let matrix_at = 8 + 2 + 11 * 12 + 4; + let keys_at = matrix_at + 16 * 8; + let pixels_at = keys_at + 16 * 2; + let entries: [(u16, u16, u32, u32); 11] = [ + (256, 4, 1, frame.width), + (257, 4, 1, frame.height), + (258, 3, 1, 8), + (259, 3, 1, 1), + (262, 3, 1, 1), + (273, 4, 1, pixels_at), + (277, 3, 1, 1), + (278, 4, 1, frame.height), + (279, 4, 1, frame.width * frame.height), + (34264, 12, 16, matrix_at), + (34735, 3, 16, keys_at), + ]; + let mut out = b"II\x2a\x00\x08\x00\x00\x00".to_vec(); + out.extend(11_u16.to_le_bytes()); + for (tag, kind, count, value) in entries { + out.extend(tag.to_le_bytes()); + out.extend(kind.to_le_bytes()); + out.extend(count.to_le_bytes()); + out.extend(value.to_le_bytes()); + } + out.extend(0_u32.to_le_bytes()); + let a = frame.affine; + for v in [a[1], a[2], 0., a[0], a[4], a[5], 0., a[3], 0., 0., 1., 0., 0., 0., 0., 1.] { + out.extend(v.to_le_bytes()); + } + for v in [1_u16, 1, 0, 3, 1024, 0, 1, 1, 1025, 0, 1, 1, 3072, 0, 1, frame.epsg as u16] { + out.extend(v.to_le_bytes()); + } + out.resize(pixels_at as usize + (frame.width * frame.height) as usize, 100); + out + } + fn app() -> (RustGisApp, u32, u32, Frame) { + let frame = Frame { epsg: 3857, width: 128, height: 128, affine: [0., 2., 0.4, 2000., 0.2, -3.] }; + let display = Raster::new( + "overview", + 1, + 1, + vec![vec![100.]], + GeoTransform { origin_x: 0., origin_y: 2000., pixel_width: 1000., pixel_height: -1000. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .unwrap(); + let mut s = Session::new(Project::default()); + let image = s.map_mut().add_raster("Test TIFF", RasterLayer::from_bytes(display, "test.tif", tiff(&frame))); + let layer = s.map_mut().add_dataset(Dataset::new("Objects", GeometryType::Polygon, vec![])); + s.active_layer = Some(layer); + s.map_mut().camera.center = frame.to_lonlat([64., 64.]).unwrap(); + s.map_mut().camera.res = 1.; + s.run("edit.enable", &json!({"on":true})).unwrap(); + (RustGisApp::new(s), image, layer, frame) + } + fn screen(app: &RustGisApp, frame: &Frame, point: [f64; 2]) -> egui::Pos2 { + crate::mapview::lonlat_to_screen(app, rect(), frame.to_lonlat(point).unwrap()).unwrap() + } + fn rect() -> egui::Rect { + egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1000., 800.)) + } + fn drag(app: &mut RustGisApp, frame: &Frame, start: [f64; 2], end: [f64; 2], new: bool) { + let start = screen(app, frame, start); + let end = screen(app, frame, end); + if app.session.tool == Tool::Circle { + assert!(crate::circleedit::drag_start(app, rect(), start, new)); + crate::circleedit::drag_to(app, rect(), end); + crate::circleedit::drag_stop(app); + } else { + assert!(crate::boxedit::drag_start(app, rect(), start, new)); + crate::boxedit::drag_to(app, rect(), end, false); + crate::boxedit::drag_stop(app); + } + } + #[test] + fn annotation_grid_defaults_to_original_header_and_explicit_mercator_is_respected() { + let (mut app, image, layer, frame) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), frame); + assert_eq!(app.session.active_layer, Some(layer)); + app.run("ui.annotationGrid", json!({"pixels":false})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), frame); + app.run("ui.annotationImage", json!({"image":null})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); + app.run("layer.select", json!({"layer":image})).unwrap(); + open(&mut app).unwrap(); + assert!(app.annotation.image.is_none(), "reopening the pane must respect an explicit No image choice"); + } + #[test] + fn annotation_grid_drags_in_rotated_sheared_pixels_and_edits_keep_their_frame() { + for tool in [Tool::Circle, Tool::RotatedBox] { + let (mut app, image, layer, frame) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("tool.set", json!({"tool":tool.code()})).unwrap(); + let depth = app.session.undo_depth(); + drag(&mut app, &frame, [20., 20.], [80., 60.], true); + assert_eq!(app.session.undo_depth(), depth + 1); + if tool == Tool::Circle { + let c = app.session.circle_spec(layer, 0).unwrap(); + assert_eq!(c.frame, frame); + assert!((c.center[0] - 20.).abs() < 0.01 && (c.radius - 60_f64.hypot(40.)).abs() < 0.01); + } else { + let b = app.session.box_spec(layer, 0).unwrap(); + assert_eq!(b.frame, frame); + assert!((b.width - 60.).abs() < 0.01 && (b.height - 40.).abs() < 0.01 && (b.center[0] - 50.).abs() < 0.01); + } + app.run("edit.undo", json!({})).unwrap(); + assert!(app.session.map().layer(layer).unwrap().dataset.features.is_empty()); + assert_eq!(drawing_frame(&app).unwrap(), frame); + app.run("edit.redo", json!({})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), frame); + app.run("ui.annotationGrid", json!({"pixels":false})).unwrap(); + if tool == Tool::Circle { + let c = app.session.circle_spec(layer, 0).unwrap().clone(); + drag(&mut app, &frame, [c.center[0] + c.radius, c.center[1]], [c.center[0] + 100., c.center[1]], false); + let edited = app.session.circle_spec(layer, 0).unwrap(); + assert_eq!(edited.frame, frame); + assert!((edited.radius - 100.).abs() < 0.01); + } else { + drag(&mut app, &frame, [80., 60.], [90., 70.], false); + let edited = app.session.box_spec(layer, 0).unwrap(); + assert_eq!(edited.frame, frame); + assert!((edited.width - 70.).abs() < 0.01 && (edited.height - 50.).abs() < 0.01); + } + } + } + #[test] + fn annotation_grid_failures_and_replaced_sources_never_fall_back_silently() { + let (mut app, image, _, _) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.session.map_mut().layer_mut(image).unwrap().raster.as_mut().unwrap().embedded = Some(std::sync::Arc::new(vec![0; 16])); + sync(&mut app); + assert!(app.annotation.source.is_none()); + assert!(drawing_frame(&app).is_err()); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + assert!(app.annotation.error.is_some() && drawing_frame(&app).is_err()); + app.run("tool.set", json!({"tool":"circle"})).unwrap(); + assert!(!crate::circleedit::drag_start(&mut app, rect(), egui::pos2(400., 350.), true)); + app.run("ui.annotationGrid", json!({"pixels":false})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); + } + #[test] + fn annotation_grid_source_changes_during_a_drag_cannot_commit_in_the_old_grid() { + for tool in [Tool::Circle, Tool::RotatedBox] { + let (mut app, image, layer, frame) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("tool.set", json!({"tool":tool.code()})).unwrap(); + let start = screen(&app, &frame, [20., 20.]); + let end = screen(&app, &frame, [80., 60.]); + let depth = app.session.undo_depth(); + if tool == Tool::Circle { + assert!(crate::circleedit::drag_start(&mut app, rect(), start, true)); + crate::circleedit::drag_to(&mut app, rect(), end); + } else { + assert!(crate::boxedit::drag_start(&mut app, rect(), start, true)); + crate::boxedit::drag_to(&mut app, rect(), end, false); + } + // Also protects a source replacement received directly through MCP before UI sync. + app.session.map_mut().layer_mut(image).unwrap().raster.as_mut().unwrap().embedded = Some(std::sync::Arc::new(vec![0; 32])); + if tool == Tool::Circle { + crate::circleedit::drag_stop(&mut app); + } else { + crate::boxedit::drag_stop(&mut app); + } + assert!(app.session.map().layer(layer).unwrap().dataset.features.is_empty()); + assert_eq!(app.session.undo_depth(), depth); + } + } + #[test] + fn annotation_grid_replaced_requests_ignore_old_failures_and_cancellation() { + let (mut app, image, _, frame) = app(); + app.reads_off_frame = true; + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + let old = app.annotation.pending.unwrap(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + let current = app.annotation.pending.unwrap(); + assert_ne!(old, current); + for r in [ + Landed::Ran(Err("old failure".into())), + Landed::NotRun("old cancel".into()), + Landed::Ran(Ok(serde_json::to_value(&frame).unwrap())), + ] { + landed(&mut app, old, r); + assert_eq!(app.annotation.pending, Some(current)); + assert!(app.annotation.error.is_none()); + } + app.run("ui.annotationCancel", Value::Null).unwrap(); + landed(&mut app, current, Landed::Ran(Ok(serde_json::to_value(&frame).unwrap()))); + assert!(drawing_frame(&app).is_err()); + app.run("project.new", json!({})).unwrap(); + assert!(app.annotation.image.is_none()); + assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); + } + #[test] + fn annotation_grid_pane_entry_defaults_and_layer_selection_share_commands() { + use egui_kittest::kittest::Queryable; + let (mut app, image, layer, _) = app(); + app.run("layer.select", json!({"layer":image})).unwrap(); + app.run("ui.tab", json!({"tab":"Analyze"})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1366., 900.)).with_max_steps(1000).build_ui_state( + |ui, app: &mut RustGisApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + app, + ); + h.get_by_role_and_label(egui::accesskit::Role::Button, "Annotate").click(); + h.run(); + assert_eq!(h.state().ui.right, Some(RightPane::Annotation)); + assert!(h.query_all_by_role(egui::accesskit::Role::ComboBox).any(|n| n.value().as_deref() == Some("Original image pixels"))); + h.get_by_label("Target layer").click(); + h.run(); + h.query_all_by_role_and_label(egui::accesskit::Role::Button, "Objects").find(|n| n.rect().min.x > 800.).unwrap().click(); + h.run(); + assert_eq!(h.state().session.active_layer, Some(layer)); + h.get_by_role_and_label(egui::accesskit::Role::Button, "Circle").click(); + h.run(); + assert_eq!(h.state().session.tool, Tool::Circle); + } +} diff --git a/crates/ui-egui/src/boxedit.rs b/crates/ui-egui/src/boxedit.rs index 4a5fd63483..43f9a64bf0 100644 --- a/crates/ui-egui/src/boxedit.rs +++ b/crates/ui-egui/src/boxedit.rs @@ -61,6 +61,7 @@ fn valid(app: &RustGisApp, g: &Gesture) -> bool { && g.map == app.session.project.active_map && g.map_name == app.session.map().name && app.session.active_layer == Some(g.layer) + && (g.row.is_some() || crate::annotation::drawing_frame(app).is_ok_and(|frame| frame == g.original.frame)) && app.session.map().layer(g.layer).is_some_and(|l| { l.instance_id() == g.instance && l.replacement_revision() == g.replacement @@ -142,7 +143,16 @@ pub(crate) fn drag_start(app: &mut RustGisApp, rect: Rect, origin: Pos2, force_n let Some(id) = app.session.active_layer else { return false }; let (row, original, handle) = match chosen { Some((_, row, b, h)) => (Some(row), b, h), - None => (None, BoxSpec { frame: Frame::default(), center: [0., 0.], width: 1., height: 1., angle: 0. }, Handle::New), + None => { + let frame = match crate::annotation::drawing_frame(app) { + Ok(frame) => frame, + Err(error) => { + app.boxes.error = Some(error); + return false; + } + }; + (None, BoxSpec { frame, center: [0., 0.], width: 1., height: 1., angle: 0. }, Handle::New) + } }; let Some(start) = original.frame.to_local(c) else { return false }; if let Some(row) = row { diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index dfd1af8079..6b302bcc0a 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -877,6 +877,7 @@ pub(crate) const UI_COMMANDS: &[(&str, &str, &str)] = &[ ("ui.mcpRelay", "MCP Relay", "Help › Help"), ("ui.pane.geoprocessing", "Tools Pane", "View › Windows"), ("ui.pane.symbology", "Symbology Pane", "Style › Drawing"), + ("ui.pane.annotation", "Annotate", "Analyze › Annotate"), ("ui.pane.labeling", "Labeling Pane", "View › Windows"), ("ui.pane.layerProperties", "Layer Properties Pane", "View › Windows"), ("ui.pane.diagnosticLog", "Messages", "View › Windows"), @@ -919,6 +920,7 @@ fn search_action(id: &str) -> Option { Some(match id { "project.open" => Command("ui.open"), "project.save" => Command("ui.save"), + "raster.imageGrid" => Command("ui.pane.annotation"), "edit.previewShapeConversion" => Command("ui.convertShape"), "project.saveAs" => Command("ui.saveAs"), "project.addFolder" => Command("ui.addFolder"), diff --git a/crates/ui-egui/src/circleedit.rs b/crates/ui-egui/src/circleedit.rs index 6425fd52cc..736fec713b 100644 --- a/crates/ui-egui/src/circleedit.rs +++ b/crates/ui-egui/src/circleedit.rs @@ -61,6 +61,7 @@ fn valid(app: &RustGisApp, g: &Gesture) -> bool { && g.map == app.session.project.active_map && g.map_name == app.session.map().name && app.session.active_layer == Some(g.layer) + && (g.row.is_some() || crate::annotation::drawing_frame(app).is_ok_and(|frame| frame == g.original.frame)) && app.session.map().layer(g.layer).is_some_and(|l| { l.instance_id() == g.instance && l.replacement_revision() == g.replacement @@ -110,7 +111,16 @@ pub(crate) fn drag_start(app: &mut RustGisApp, rect: Rect, origin: Pos2, force_n let Some(id) = app.session.active_layer else { return false }; let (row, original, handle) = match chosen { Some((_, row, b, h)) => (Some(row), b, h), - None => (None, CircleSpec { frame: Frame::default(), center: [0., 0.], radius: 1., tolerance: 0.25 }, Handle::New), + None => { + let frame = match crate::annotation::drawing_frame(app) { + Ok(frame) => frame, + Err(error) => { + app.circles.error = Some(error); + return false; + } + }; + (None, CircleSpec { frame, center: [0., 0.], radius: 1., tolerance: 0.25 }, Handle::New) + } }; let Some(start) = original.frame.to_local(c) else { return false }; if let Some(row) = row { diff --git a/crates/ui-egui/src/editpanes.rs b/crates/ui-egui/src/editpanes.rs index d597959b4e..300db4222a 100644 --- a/crates/ui-egui/src/editpanes.rs +++ b/crates/ui-egui/src/editpanes.rs @@ -109,6 +109,9 @@ fn combo(ui: &mut Ui, id: &str, name: &str, value: &mut String, options: &[Strin pub fn editing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { crate::shapeconvert::panel(app, ui); + if matches!(app.session.tool, rustgis_engine::Tool::Circle | rustgis_engine::Tool::RotatedBox) { + ui.collapsing(tl!("New shape grid"), |ui| crate::annotation::grid_controls(app, ui)); + } crate::boxedit::panel(app, ui); crate::circleedit::panel(app, ui); message(app, ui, t); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 9018cc3029..b39779ccdb 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -89,6 +89,7 @@ pub enum Landed { Ran(Result), NotRun(String), } +mod annotation; mod boxedit; mod circleedit; #[cfg(not(target_arch = "wasm32"))] @@ -655,6 +656,7 @@ pub enum RightPane { Symbology, History, Editing, + Annotation, Design, Workflows, Script, @@ -1165,6 +1167,7 @@ pub struct RustGisApp { pub(crate) boxes: boxedit::State, pub(crate) circles: circleedit::State, pub(crate) conversion: shapeconvert::State, + pub(crate) annotation: annotation::State, pub ui: UiState, pub map: mapview::MapView, pub dialogs: dialogs::Dialogs, @@ -1472,6 +1475,7 @@ impl RustGisApp { boxes: boxedit::State::default(), circles: circleedit::State::default(), conversion: shapeconvert::State::default(), + annotation: annotation::State::default(), ui, map: mapview::MapView::default(), dialogs: dialogs::Dialogs::default(), @@ -2746,6 +2750,9 @@ impl RustGisApp { } // Counting a column goes over every row of the layer: on a thread of its own, and the // answer shown once it lands (the table's Statistics window). + if id == "raster.imageGrid" { + return readjob::run(self, "raster.imageGrid", params); + } if id == "edit.previewShapeConversion" { return readjob::run_shape_conversion(self, params); } @@ -2864,6 +2871,7 @@ impl RustGisApp { // Nested commands (including scripts) can replace rows too; never retarget a chosen // vertex or held gesture to the replacement feature at the same row. crate::vertexedit::cancel_stale_drag(self); + annotation::sync(self); crate::boxedit::cancel_stale(self); crate::circleedit::cancel_stale(self); if let Err(e) = &r { @@ -3315,6 +3323,17 @@ impl RustGisApp { return self.run_ui("ui.pane.layerProperties", &json!({"tab": "source", "layer": layer})); } } + "ui.pane.annotation" => { + if self.ui.right == Some(RightPane::Annotation) { + self.ui.close_right(RightPane::Annotation); + } else { + annotation::open(self)?; + } + } + "ui.annotationImage" => annotation::select_image(self, p)?, + "ui.annotationGrid" => annotation::select_grid(self, p.get("pixels").and_then(Value::as_bool).ok_or("pixels is required")?)?, + "ui.annotationCancel" => annotation::cancel_read(self), + "ui.annotationLayer" => annotation::select_layer(self, p)?, "ui.pane.editing" => toggle_right(&mut self.ui, RightPane::Editing), "ui.pane.design" => toggle_right(&mut self.ui, RightPane::Design), "ui.pane.script" => toggle_right(&mut self.ui, RightPane::Script), @@ -5971,6 +5990,7 @@ impl RustGisApp { /// Per-frame work that doesn't draw (theme, fonts, files that arrived). pub fn logic(&mut self, ctx: &egui::Context) { + annotation::sync(self); #[cfg(not(target_arch = "wasm32"))] self.session.statistics_need_read_ahead(self.reads_off_frame); let _diagnostics = self.session.diagnostics.enter(); diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 71dd52703b..742d2e8ad6 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -134,6 +134,7 @@ fn right_title(pane: RightPane) -> (&'static str, &'static str) { RightPane::Symbology => ("Symbology", "ui.pane.symbology"), RightPane::History => ("History", "ui.pane.history"), RightPane::Editing => ("Editing", "ui.pane.editing"), + RightPane::Annotation => ("Annotate", "ui.pane.annotation"), RightPane::Design => ("Data Design", "ui.pane.design"), RightPane::Workflows => ("Workflows", "ui.pane.workflows"), RightPane::Script => ("Script", "ui.pane.script"), @@ -200,6 +201,7 @@ fn right_pane(app: &mut RustGisApp, ui: &mut Ui, right: RightPane, t: &Tokens) { RightPane::Symbology => symbology(app, ui, t), RightPane::History => history(app, ui, t), RightPane::Editing => crate::editpanes::editing(app, ui, t), + RightPane::Annotation => crate::annotation::panel(app, ui), RightPane::Design => crate::editpanes::design(app, ui, t), RightPane::Workflows => crate::analysispane::workflows(app, ui, t), RightPane::Tasks => crate::workspacepanes::tasks(app, ui, t), diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index 7588fa150e..bf19e2fc8f 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -36,6 +36,14 @@ impl Reads { && a.relay.is_none() && a.params.get("request").and_then(Value::as_u64) == Some(request) }; + self.cancel_where(belongs); + } + pub(crate) fn cancel_annotation_grid(&mut self, request: u64) { + self.cancel_where(|a| { + a.command == "raster.imageGrid" && a.relay.is_none() && matches!(a.then, Some(Completion::ImageGrid(id)) if id == request) + }); + } + fn cancel_where(&mut self, belongs: impl Fn(&Asked) -> bool) { if let Some(job) = &mut self.job && belongs(&job.asked) { @@ -76,12 +84,14 @@ pub type Then = fn(&mut RustGisApp, Landed); enum Completion { Command(Then), ShapeConversion(Option), + ImageGrid(u64), } impl Completion { fn deliver(self, app: &mut RustGisApp, result: Landed) { match self { Self::Command(then) => then(app, result), Self::ShapeConversion(request) => crate::shapeconvert::landed(app, request, result), + Self::ImageGrid(request) => crate::annotation::landed(app, request, result), } } } @@ -123,6 +133,11 @@ pub(crate) fn run_shape_conversion(app: &mut RustGisApp, p: Value) -> Result Result { + run_presented(app, "raster.imageGrid", p, Some(Completion::ImageGrid(request)), None, None) +} + /// Keeps a service layer's title and connection display name outside recorded parameters /// until its admitted discovery lands. The ordinary read queue owns progress and Cancel. #[cfg(not(target_arch = "wasm32"))] @@ -160,6 +175,7 @@ fn run_presented( | "labels.convert" | "source.syncState" | "table.statistics" + | "raster.imageGrid" | "edit.previewShapeConversion" ) .then(|| app.session.layer_id(&p).ok().and_then(|id| app.session.map().layer(id)).map(|l| (l.instance_id(), l.name.clone()))) @@ -477,7 +493,13 @@ fn start(app: &mut RustGisApp, mut asked: Asked, p: Value) -> Result Date: Tue, 17 Nov 2026 08:58:45 +0000 Subject: [PATCH 02/14] Make annotation details optional and suggest image-based export names --- crates/ui-egui/src/annotation.rs | 167 ++++++++++++++++++++++++++++++- crates/ui-egui/src/lib.rs | 11 +- docs/user-guide/editing.md | 9 +- 3 files changed, 178 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index 6b8c3afdf1..a3e4257695 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -8,6 +8,7 @@ pub(crate) struct State { context: Option<(u64, u64)>, image: Option, image_chosen: bool, + image_filename: bool, source: Option, pixels: bool, frame: Option, @@ -127,6 +128,38 @@ fn polygon_layer(app: &RustGisApp, id: u32) -> bool { && matches!(l.geometry_type(), rustgis_engine::data::GeometryType::Polygon | rustgis_engine::data::GeometryType::None) }) } +pub(crate) fn filename_option(app: &mut RustGisApp, on: bool) { + sync(app); + app.annotation.image_filename = on; +} +fn image_export_name(app: &RustGisApp) -> Option { + if app.annotation.context != Some((app.session.opened, app.session.map().instance_id())) + || !app.annotation.source.as_ref()?.current(&app.session) + { + return None; + } + let raster = app.session.map().layer(app.annotation.image?)?.raster.as_ref()?; + let file = raster.file_name.as_deref().or_else(|| raster.path.as_deref().filter(|p| rustgis_engine::map::paths::is_local(p)))?; + // Use only the basename, including for Windows paths opened on another platform. + let leaf = file.rsplit(['/', '\\']).next()?; + let stem = std::path::Path::new(leaf).file_stem()?.to_str()?; + if stem.is_empty() || matches!(stem, "." | "..") { + return None; + } + Some(format!("{stem}.geojson")) +} +/// An annotation-only filename suggestion. The ordinary export action keeps its layer name. +/// Returns a basename, never a destination path; the existing save dialog chooses the location. +pub(crate) fn export_name(app: &RustGisApp, layer: u32, ext: &str, annotation: bool) -> Result { + if annotation && ext != "geojson" { + return Err(tl!("Annotation export uses GeoJSON.").into()); + } + if annotation && app.annotation.image_filename { + return image_export_name(app) + .ok_or_else(|| tl!("Select an image with an available filename, or uncheck Use image filename.").into()); + } + Ok(app.session.map().layer(layer).map(|l| format!("{}.{ext}", l.name)).unwrap_or_default()) +} pub(crate) fn select_layer(app: &mut RustGisApp, p: &Value) -> Result<(), String> { let id = app.session.layer_id(p)?; if !polygon_layer(app, id) { @@ -226,12 +259,32 @@ pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) { ui.small(tl!("Drawing requires editing enabled, a loaded polygon layer and a flat Earth map with zero pitch.")); } ui.small(tl!("Space or middle-drag pans; releasing Space returns to the active drawing tool. Undo/Redo use the existing commands.")); - crate::boxedit::panel(app, ui); - crate::circleedit::panel(app, ui); + if matches!(app.session.tool, Tool::Circle | Tool::RotatedBox) { + egui::CollapsingHeader::new(tl!("Shape details")).id_salt("annotation_shape_details").default_open(false).show(ui, |ui| { + crate::boxedit::panel(app, ui); + crate::circleedit::panel(app, ui); + }); + } ui.separator(); - ui.add_enabled_ui(layer.is_some(), |ui| { + let mut image_filename = app.annotation.image_filename; + if ui + .checkbox(&mut image_filename, tl!("Use image filename")) + .on_hover_text(tl!("For img1.tiff, suggest img1.geojson. Choose the location in the save dialog.")) + .changed() + { + let _ = app.run("ui.annotationFilename", json!({"on":image_filename})); + } + let name = if image_filename { image_export_name(app) } else { None }; + if image_filename { + if let Some(name) = &name { + ui.small(crate::i18n::fmt("Suggested filename: {name}", &[("name", name)])); + } else { + ui.small(tl!("Select an image with an available filename, or uncheck Use image filename.")); + } + } + ui.add_enabled_ui(layer.is_some() && (!image_filename || name.is_some()), |ui| { if ui.button(tl!("Export GeoJSON…")).clicked() { - let _ = app.run("ui.exportLayer", json!({"format":"geojson"})); + let _ = app.run("ui.annotationExport", json!({"format":"geojson"})); } }); ui.small(tl!( @@ -477,4 +530,110 @@ mod tests { h.run(); assert_eq!(h.state().session.tool, Tool::Circle); } + #[test] + fn annotation_shape_details_start_hidden_and_toggle_without_changing_the_object() { + use egui_kittest::kittest::Queryable; + for tool in [Tool::Circle, Tool::RotatedBox] { + let (mut app, image, layer, frame) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("tool.set", json!({"tool":tool.code()})).unwrap(); + drag(&mut app, &frame, [20., 20.], [80., 60.], true); + let feature = app.session.map().layer(layer).unwrap().dataset.features[0].clone(); + let depth = app.session.undo_depth(); + let mut h = egui_kittest::Harness::builder() + .with_size(egui::vec2(600., 1400.)) + .with_max_steps(1000) + .build_ui_state(|ui, app| panel(app, ui), app); + let dimension = if tool == Tool::Circle { "Radius" } else { "Width" }; + assert!(h.query_by_label(dimension).is_none()); + h.get_by_label("Shape details").click(); + h.run(); + assert!(h.query_by_label(dimension).is_some()); + h.get_by_label("Shape details").click(); + h.run(); + assert!(h.query_by_label(dimension).is_none()); + assert!(h.query_by_label("Export GeoJSON…").is_some()); + assert_eq!(h.state().session.tool, tool); + assert_eq!(h.state().session.undo_depth(), depth); + assert_eq!(h.state().session.map().layer(layer).unwrap().dataset.features[0], feature); + assert!(if tool == Tool::Circle { + h.state().session.circle_spec(layer, 0).is_some() + } else { + h.state().session.box_spec(layer, 0).is_some() + }); + } + } + + #[test] + fn annotation_filename_uses_source_basename_instead_of_layer_title() { + for (file, expected, local) in [ + ("img1.tiff", "img1.geojson", false), + ("folder/scene.v2.TIFF", "scene.v2.geojson", false), + ("C:\\images\\été 1.tif", "été 1.geojson", false), + ("/images/flight.v2.tiff", "flight.v2.geojson", true), + ] { + let (mut app, image, layer, _) = app(); + let raster = app.session.map_mut().layer_mut(image).unwrap().raster.as_mut().unwrap(); + raster.file_name = (!local).then(|| file.into()); + raster.path = local.then(|| file.into()); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("layer.rename", json!({"layer":image,"name":"Renamed image title"})).unwrap(); + assert_eq!(export_name(&app, layer, "geojson", true).unwrap(), "Objects.geojson"); + app.run("ui.annotationFilename", json!({"on":true})).unwrap(); + assert_eq!(export_name(&app, layer, "geojson", true).unwrap(), expected); + assert_eq!(export_name(&app, layer, "geojson", false).unwrap(), "Objects.geojson"); + } + } + #[test] + fn annotation_filename_checkbox_controls_real_geojson_download_without_affecting_other_exports() { + use egui_kittest::kittest::Queryable; + let (mut app, image, _, frame) = app(); + app.session.map_mut().layer_mut(image).unwrap().raster.as_mut().unwrap().file_name = Some("img1.tiff".into()); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("tool.set", json!({"tool":"circle"})).unwrap(); + drag(&mut app, &frame, [20., 20.], [80., 60.], true); + let downloads = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); + let output = downloads.clone(); + app.services.download = Some(Box::new(move |name, bytes| { + output.lock().unwrap().push((name.to_string(), bytes)); + Ok(()) + })); + let mut h = egui_kittest::Harness::builder() + .with_size(egui::vec2(600., 1400.)) + .with_max_steps(1000) + .build_ui_state(|ui, app| panel(app, ui), app); + h.get_by_label("Use image filename").click(); + h.run(); + assert!(h.state().annotation.image_filename); + assert!(h.query_by_label("Suggested filename: img1.geojson").is_some()); + h.get_by_label("Export GeoJSON…").click(); + h.run(); + let mut app = h.into_state(); + app.run("ui.exportLayer", json!({"format":"geojson"})).unwrap(); + app.run("ui.annotationFilename", json!({"on":false})).unwrap(); + app.run("ui.annotationExport", Value::Null).unwrap(); + let output = downloads.lock().unwrap(); + assert_eq!( + output.iter().map(|(name, _)| name.as_str()).collect::>(), + ["img1.geojson", "Objects.geojson", "Objects.geojson"] + ); + let geojson: Value = serde_json::from_slice(&output[0].1).unwrap(); + assert_eq!(geojson["type"], "FeatureCollection"); + assert_eq!(geojson["features"].as_array().unwrap().len(), 1); + } + #[test] + fn annotation_filename_missing_image_does_not_silently_export_under_another_name() { + let (mut app, image, _, _) = app(); + app.run("ui.annotationFilename", json!({"on":true})).unwrap(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("ui.annotationImage", json!({"image":null})).unwrap(); + let downloads = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let output = downloads.clone(); + app.services.download = Some(Box::new(move |_, _| { + output.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + Ok(()) + })); + assert!(app.run("ui.annotationExport", Value::Null).unwrap_err().contains("filename")); + assert_eq!(downloads.load(std::sync::atomic::Ordering::SeqCst), 0); + } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index b39779ccdb..587a5bbc15 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3331,6 +3331,7 @@ impl RustGisApp { } } "ui.annotationImage" => annotation::select_image(self, p)?, + "ui.annotationFilename" => annotation::filename_option(self, p.get("on").and_then(Value::as_bool).ok_or("on is required")?), "ui.annotationGrid" => annotation::select_grid(self, p.get("pixels").and_then(Value::as_bool).ok_or("pixels is required")?)?, "ui.annotationCancel" => annotation::cancel_read(self), "ui.annotationLayer" => annotation::select_layer(self, p)?, @@ -4778,13 +4779,15 @@ impl RustGisApp { "ui.exportLayer" if p.get("format").and_then(Value::as_str).is_none() => { self.dialogs.open("exportLayer", &self.session)?; } - "ui.exportLayer" if self.services.download.is_some() => { + "ui.exportLayer" | "ui.annotationExport" if self.services.download.is_some() => { + let annotation_export = id == "ui.annotationExport"; let id = self.export_target(p)?; let construction_notes = self.session.layer_construction_output_notes(id)?; let l = self.session.map().layer(id).ok_or_else(|| tl!("select a layer in the Layers pane first").to_string())?; // Any format the dialog offers here (`dialogs::export_formats`): not GeoPackage or // FlatGeobuf, which the data crate refuses in the browser. let ext = p.get("format").and_then(Value::as_str).unwrap_or("geojson").to_ascii_lowercase(); + let name = annotation::export_name(self, id, if ext == "shp" { "zip" } else { &ext }, annotation_export)?; rustgis_engine::cmd::share::gpkg_table(p, &ext)?; // A streamed layer exports its whole source, not the features read for the view. let (whole, lost) = if l.is_streamed() { @@ -4806,7 +4809,6 @@ impl RustGisApp { } }; let ext = if ext == "shp" { "zip".to_string() } else { ext }; - let name = format!("{}.{ext}", l.name); // The features written, read whole, give their own extent (not a raster's, or the // one a streamed layer's source declares). let mut notes = rustgis_engine::data::export_notes([(ds, ds.extent())], &name, crs); @@ -4923,7 +4925,8 @@ impl RustGisApp { return self.run("share.exportMap", args); } } - "ui.exportLayer" => { + "ui.exportLayer" | "ui.annotationExport" => { + let annotation_export = id == "ui.annotationExport"; let ext = p.get("format").and_then(Value::as_str).unwrap_or("geojson").to_ascii_lowercase(); let label = dialogs::EXPORT_FORMATS .iter() @@ -4934,6 +4937,7 @@ impl RustGisApp { // Checked as the command checks them, before a file is asked for; then passed on. let gpkg = rustgis_engine::cmd::share::gpkg_table(p, &ext)?; let id = self.export_target(p)?; + let name = annotation::export_name(self, id, &ext, annotation_export)?; let with_table = |mut args: Value| { for key in ["mode", "tableName"] { if let Some(v) = p.get(key) { @@ -4952,7 +4956,6 @@ impl RustGisApp { } return Ok(Value::Null); } - let name = self.session.map().layer(id).map(|l| format!("{}.{ext}", l.name)).unwrap_or_default(); if let Some(path) = dialogs::pick_save(&name, &[ext.as_str()], label) { // The format chosen decides: its extension is added unless it was typed (a dot // in the name, as in "U.S. Cities", isn't taken for one). diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index e72b817ef9..ffaf7672e4 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -185,8 +185,15 @@ Removing/replacing an image invalidates its cached grid. Local and embedded GeoT first. In the browser, upload the original GeoTIFF. Space or middle-drag pans; releasing Space keeps the drawing tool. Use the selected object's -existing handles and numeric controls, Undo/Redo, and **Export GeoJSON…**. Exports contain +existing handles, Undo/Redo, and **Export GeoJSON…**. **Shape details** is collapsed by default; +expand it for numeric dimensions and additional editing controls. Map handles remain available +while the section is hidden. Exports contain ordinary geographic polygons and feature properties; circles are polygon approximations. +Check **Use image filename** to suggest `img1.geojson` for an image named `img1.tiff`. +The preview shows the suggested basename; the save dialog still chooses the folder and lets +you change the name. The option starts unchecked and applies only to this pane’s GeoJSON +export; ordinary layer exports keep their layer-based names. + Image selection, grid choices and exact constructions are session-only. Reopened polygons can be explicitly converted again. No image-binding metadata or palette sidecars are added. From 80b2d2720e7b8351ee98af77c05d04c29a2e1f71 Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 09:59:06 +0000 Subject: [PATCH 03/14] Fix annotation selection, cancellation and command search --- crates/ui-egui/src/annotation.rs | 69 +++++++++++++++++++++++++++++--- crates/ui-egui/src/chrome.rs | 1 - crates/ui-egui/src/readjob.rs | 23 +++++++++-- 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index a3e4257695..68d3df0ce1 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -39,12 +39,15 @@ pub(crate) fn sync(app: &mut RustGisApp) { } } pub(crate) fn open(app: &mut RustGisApp) -> Result<(), String> { + sync(app); app.ui.open_right(RightPane::Annotation); if !app.annotation.image_chosen && app.annotation.image.is_none() && let Some(id) = app.session.active_layer.filter(|id| app.session.map().layer(*id).is_some_and(|l| l.raster.is_some())) + && let Err(error) = select_image(app, &json!({"image":id})) { - select_image(app, &json!({"image":id}))?; + app.annotation.image_chosen = true; + app.annotation.error = Some(error); } Ok(()) } @@ -52,7 +55,7 @@ pub(crate) fn select_image(app: &mut RustGisApp, p: &Value) -> Result<(), String sync(app); let image = p.get("image").filter(|v| !v.is_null()).map(|v| app.session.layer_id(&json!({"layer":v}))).transpose()?; let source = image.map(|id| Source::capture(&app.session, id)).transpose()?; - let request = app.annotation.next.checked_add(1).ok_or("image-grid request limit reached")?; + let request = app.annotation.next.checked_add(1).ok_or_else(|| tl!("Image-grid request limit reached").to_string())?; cancel(app); app.annotation.next = request; app.annotation.image = image; @@ -171,10 +174,7 @@ pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) { sync(app); let mut image = app.annotation.image; let mut picked = false; - let name = image - .and_then(|id| app.session.map().layer(id)) - .filter(|_| app.annotation.source.as_ref().is_some_and(|s| s.current(&app.session))) - .map_or_else(|| tl!("No image").to_string(), |l| l.name.clone()); + let name = image.and_then(|id| app.session.map().layer(id)).map_or_else(|| tl!("No image").to_string(), |l| l.name.clone()); egui::ComboBox::from_label(tl!("Image")).selected_text(name).show_ui(ui, |ui| { picked |= ui.selectable_value(&mut image, None, tl!("No image")).clicked(); for l in app.session.map().layers.iter().filter(|l| l.raster.is_some()) { @@ -503,6 +503,63 @@ mod tests { assert!(app.annotation.image.is_none()); assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); } + #[test] + fn annotation_grid_open_keeps_automatic_selection_errors_inline() { + use egui_kittest::kittest::Queryable; + let (mut app, image, _, _) = app(); + sync(&mut app); + app.annotation.next = u64::MAX; + app.run("layer.select", json!({"layer":image})).unwrap(); + app.run("ui.pane.annotation", Value::Null).unwrap(); + assert_eq!(app.ui.right, Some(RightPane::Annotation)); + assert_eq!(app.annotation.error.as_deref(), Some("Image-grid request limit reached")); + assert!(app.annotation.image_chosen); + let mut h = egui_kittest::Harness::builder().build_ui_state(|ui, app: &mut RustGisApp| panel(app, ui), app); + assert!(h.query_by_label("Image-grid request limit reached").is_some()); + h.state_mut().run("ui.pane.annotation", Value::Null).unwrap(); + h.state_mut().annotation.error = None; + h.state_mut().run("ui.pane.annotation", Value::Null).unwrap(); + assert!(h.state().annotation.error.is_none(), "reopening must not retry a failed automatic choice"); + assert!(h.state_mut().run("ui.annotationImage", json!({"image":image})).is_err(), "explicit selection still reports errors"); + } + + #[test] + fn annotation_grid_stale_or_cancelled_image_keeps_its_selected_name() { + use egui_kittest::kittest::Queryable; + for replace in [false, true] { + let (mut app, image, _, _) = app(); + app.reads_off_frame = !replace; + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + if replace { + app.session.map_mut().layer_mut(image).unwrap().raster.as_mut().unwrap().embedded = Some(std::sync::Arc::new(vec![0; 16])); + } else { + app.run("ui.annotationCancel", Value::Null).unwrap(); + } + let h = egui_kittest::Harness::builder().build_ui_state(|ui, app: &mut RustGisApp| grid_controls(app, ui), app); + assert!(h.query_all_by_role(egui::accesskit::Role::ComboBox).any(|n| n.value().as_deref() == Some("Test TIFF"))); + assert_eq!(h.state().annotation.image, Some(image)); + assert!(h.state().annotation.error.is_some()); + assert!(drawing_frame(h.state()).is_err()); + assert!(h.query_by_label("Reload Grid").is_some()); + } + } + + #[test] + fn annotation_grid_search_queries_the_grid_without_toggling_the_pane() { + let (mut app, image, _, _) = app(); + app.run("layer.select", json!({"layer":image})).unwrap(); + open(&mut app).unwrap(); + app.ui.command_search = Some("raster.imageGrid".into()); + let mut h = egui_kittest::Harness::builder() + .with_size(egui::vec2(1366., 900.)) + .build_ui_state(|ui, app: &mut RustGisApp| crate::chrome::command_search(app, ui.ctx()), app); + h.key_press(egui::Key::Enter); + h.run(); + assert!(h.state().ui.command_search.is_none()); + assert_eq!(h.state().ui.right, Some(RightPane::Annotation)); + assert!(h.state().session.notices_since(0).iter().all(|n| !n.error)); + } + #[test] fn annotation_grid_pane_entry_defaults_and_layer_selection_share_commands() { use egui_kittest::kittest::Queryable; diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index 6b302bcc0a..f13b3119fe 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -920,7 +920,6 @@ fn search_action(id: &str) -> Option { Some(match id { "project.open" => Command("ui.open"), "project.save" => Command("ui.save"), - "raster.imageGrid" => Command("ui.pane.annotation"), "edit.previewShapeConversion" => Command("ui.convertShape"), "project.saveAs" => Command("ui.saveAs"), "project.addFolder" => Command("ui.addFolder"), diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index bf19e2fc8f..2932073ebc 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -47,7 +47,10 @@ impl Reads { if let Some(job) = &mut self.job && belongs(&job.asked) { - job.cancel_waiting = false; + // A request-specific cancel must not downgrade an earlier status-bar Cancel. + if !job.control.is_cancelled() { + job.cancel_waiting = false; + } job.control.cancel(); } self.waiting.retain(|a| !belongs(a)); @@ -739,6 +742,15 @@ mod tests { #[test] fn annotation_grid_cancel_preserves_unrelated_waiting_reads() { + annotation_grid_cancel_case(false); + } + + #[test] + fn annotation_grid_cancel_preserves_an_earlier_queue_wide_cancel() { + annotation_grid_cancel_case(true); + } + + fn annotation_grid_cancel_case(queue_wide: bool) { use rustgis_engine::{ Project, Session, data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}, @@ -765,12 +777,17 @@ mod tests { let file = folder.path().join("queued.csv"); std::fs::write(&file, "name,lon,lat\nQueued,1,2\n").unwrap(); run(&mut app, "layer.addData", json!({"path":file,"name":"Queued after grid"})).unwrap(); + if queue_wide { + app.reads.cancel_for_tests(); + } + app.run("ui.annotationCancel", Value::Null).unwrap(); app.run("ui.annotationCancel", Value::Null).unwrap(); let job = app.reads.job.as_ref().unwrap(); - assert!(job.control.is_cancelled() && !job.cancel_waiting); + assert!(job.control.is_cancelled()); + assert_eq!(job.cancel_waiting, queue_wide); assert_eq!(app.reads.waiting.len(), 1); landed(&mut app, 60); - assert!(app.session.map().layer_by_name("Queued after grid").is_some()); + assert_eq!(app.session.map().layer_by_name("Queued after grid").is_some(), !queue_wide); assert!(crate::annotation::drawing_frame(&app).is_err()); } From 7433506d31062c70008d160b3eba1cc89dd9f2dd Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 11:23:09 +0000 Subject: [PATCH 04/14] Verify image-grid source revisions and show search results --- crates/data/src/raster/file_revision.rs | 76 ++++++++++++++++++++++ crates/data/src/raster/geotiff.rs | 19 +++++- crates/data/src/raster/mod.rs | 7 +++ crates/engine/src/image_grid.rs | 58 ++++++++++++++++- crates/ui-egui/src/annotation.rs | 83 +++++++++++++++++++++---- crates/ui-egui/src/lib.rs | 9 +++ docs/user-guide/editing.md | 6 ++ 7 files changed, 242 insertions(+), 16 deletions(-) create mode 100644 crates/data/src/raster/file_revision.rs diff --git a/crates/data/src/raster/file_revision.rs b/crates/data/src/raster/file_revision.rs new file mode 100644 index 0000000000..5699d54c03 --- /dev/null +++ b/crates/data/src/raster/file_revision.rs @@ -0,0 +1,76 @@ +//! Runtime identity of the local GeoTIFF used to decode a displayed raster. +use std::{ + fs::Metadata, + path::{Path, PathBuf}, +}; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) struct FileRevision { + path: PathBuf, + len: u64, + modified: std::time::SystemTime, + #[cfg(unix)] + identity: (u64, u64, i64, i64), + #[cfg(windows)] + created: u64, +} + +impl FileRevision { + pub(super) fn capture(path: &Path, metadata: &Metadata) -> std::io::Result { + #[cfg(unix)] + use std::os::unix::fs::MetadataExt; + #[cfg(windows)] + use std::os::windows::fs::MetadataExt; + Ok(Self { + path: std::path::absolute(path)?, + len: metadata.len(), + modified: metadata.modified()?, + #[cfg(unix)] + identity: (metadata.dev(), metadata.ino(), metadata.ctime(), metadata.ctime_nsec()), + #[cfg(windows)] + created: metadata.creation_time(), + }) + } +} + +impl super::Raster { + /// Metadata only, called on the grid reader's worker, never by per-frame UI checks. + /// Missing provenance is refused rather than pairing an arbitrary file with displayed pixels. + pub fn check_original_file(&self, path: &Path, metadata: &Metadata) -> std::result::Result<(), String> { + let current = FileRevision::capture(path, metadata).map_err(|e| e.to_string())?; + match &self.source_file { + Some(original) if *original == current => Ok(()), + Some(_) => Err("The image file changed since its pixels were loaded. Use Reload Data, then Reload Grid.".into()), + None => Err("The displayed image has no verified original file revision. Use Reload Data, then Reload Grid.".into()), + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn annotation_grid_revision_detects_replacement_with_the_same_length_and_modification_time() { + let folder = tempfile::tempdir().unwrap(); + let path = folder.path().join("image.tif"); + let bytes = super::super::geotiff::write(&super::super::tests::small(), 0).unwrap(); + std::fs::write(&path, &bytes).unwrap(); + let raster = super::super::geotiff::read_file(&path, "image").unwrap(); + let original = std::fs::File::open(&path).unwrap(); + let metadata = original.metadata().unwrap(); + raster.check_original_file(&path, &metadata).unwrap(); + let replacement = folder.path().join("replacement.tif"); + std::fs::write(&replacement, &bytes).unwrap(); + let file = std::fs::File::options().write(true).open(&replacement).unwrap(); + file.set_times(std::fs::FileTimes::new().set_modified(metadata.modified().unwrap())).unwrap(); + drop(file); + std::fs::remove_file(&path).unwrap(); + std::fs::rename(&replacement, &path).unwrap(); + let current = std::fs::metadata(&path).unwrap(); + assert_eq!(metadata.len(), current.len()); + assert_eq!(metadata.modified().unwrap(), current.modified().unwrap()); + // Metadata captured before replacement still describes the old file, not the live path. + raster.check_original_file(&path, &metadata).unwrap(); + assert!(raster.check_original_file(&path, ¤t).unwrap_err().contains("Reload Data")); + assert!(raster.clone().check_original_file(&path, ¤t).is_err()); + } +} diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 2e9965122f..f59f3c9afb 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1396,9 +1396,22 @@ pub fn read_file(path: &std::path::Path, name: &str) -> Result { /// [`read_file`], with the no-data value a sidecar gives (see [`read_limited_with_nodata`]). #[cfg(not(target_arch = "wasm32"))] pub(super) fn read_file_with_nodata(path: &std::path::Path, name: &str, sidecar_nodata: Option) -> Result { - let path = path.to_path_buf(); - let open = move || -> std::io::Result> { reader(std::io::BufReader::new(crate::provenance::open(&path)?)) }; - read_limited_with_nodata(open().map_err(err)?, name, max_samples(), Some(&open), sidecar_nodata) + let file = crate::provenance::open(path)?; + let revision = super::file_revision::FileRevision::capture(path, &file.metadata()?).ok(); + let open = || -> std::io::Result> { + let file = crate::provenance::open(path)?; + if revision.is_some() && super::file_revision::FileRevision::capture(path, &file.metadata()?).ok() != revision { + return Err(std::io::Error::other("the image file changed while its pixels were read; reload it")); + } + reader(std::io::BufReader::new(file)) + }; + let mut raster = + read_limited_with_nodata(reader(std::io::BufReader::new(file)).map_err(err)?, name, max_samples(), Some(&open), sidecar_nodata)?; + raster.source_file = revision; + if raster.source_file.is_some() { + raster.check_original_file(path, &std::fs::metadata(path)?).map_err(err)?; + } + Ok(raster) } /// [`read`], holding at most `max` samples (the tests' small limit exercises the overviews), with diff --git a/crates/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index 124158239e..8434f6825a 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -15,6 +15,8 @@ mod epsg_gap; mod esri; mod esri_datums; #[cfg(not(target_arch = "wasm32"))] +mod file_revision; +#[cfg(not(target_arch = "wasm32"))] pub mod gdal; pub mod geotiff; mod image_grid; @@ -621,6 +623,9 @@ pub struct Raster { pub data_type: String, /// Where it was read from, if anywhere. pub source: Option, + /// The original local GeoTIFF's revision, retained with these pixels and never serialized. + #[cfg(not(target_arch = "wasm32"))] + source_file: Option, /// Whether the pixels come from a reduced-resolution overview. pub overview: Option<(u32, u32)>, /// With `overview`, whether the pixels are every `step`-th of the file's, from its top-left @@ -820,6 +825,8 @@ impl Raster { color, data_type, source: None, + #[cfg(not(target_arch = "wasm32"))] + source_file: None, overview: None, step: 1, georeferenced: true, diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs index f23f83bf44..935fac7605 100644 --- a/crates/engine/src/image_grid.rs +++ b/crates/engine/src/image_grid.rs @@ -57,7 +57,15 @@ pub(crate) fn read(r: &RasterLayer) -> Result { #[cfg(not(target_arch = "wasm32"))] { let input = std::fs::File::open(path).map_err(|e| e.to_string())?; - rustgis_data::raster::geotiff::read_image_grid(input) + let displayed = r.data.as_ref().ok_or("reload the image layer before reading its original grid")?; + let path = std::path::Path::new(path); + displayed.check_original_file(path, &input.metadata().map_err(|e| e.to_string())?)?; + let grid = rustgis_data::raster::geotiff::read_image_grid(&input); + // Both the handle and path must still name the revision that supplied the display. + // A replacement during parsing must not pair new coordinates with old pixels. + displayed.check_original_file(path, &input.metadata().map_err(|e| e.to_string())?)?; + displayed.check_original_file(path, &std::fs::metadata(path).map_err(|e| e.to_string())?)?; + grid } #[cfg(target_arch = "wasm32")] { @@ -136,6 +144,54 @@ mod tests { let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); assert_eq!(serde_json::from_value::(crate::cmd::reads::read_land(&mut s, done).unwrap()).unwrap(), expected); } + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn annotation_grid_file_changes_before_selection_or_queued_read_require_a_pixel_reload() { + for queued in [false, true] { + let (mut s, id, original) = session(); + let folder = std::env::temp_dir().join(format!("rustgis-annotation-grid-file-{}-{queued}", std::process::id())); + std::fs::create_dir_all(&folder).unwrap(); + let path = folder.join("image.tif"); + let bytes = s.map().layer(id).unwrap().raster.as_ref().unwrap().embedded.clone().unwrap(); + std::fs::write(&path, bytes.as_slice()).unwrap(); + let pixels = rustgis_data::raster::read_path(&path).unwrap(); + s.map_mut().layer_mut(id).unwrap().raster = Some(RasterLayer::from_path(pixels, path.display().to_string())); + let displayed = s.map().layer(id).unwrap().raster.as_ref().unwrap().data.clone().unwrap(); + assert_eq!(serde_json::from_value::(s.run("raster.imageGrid", &json!({"layer":id})).unwrap()).unwrap(), original); + let ahead = queued.then(|| crate::cmd::reads::read_ahead(&mut s, "raster.imageGrid", &json!({"layer":id})).unwrap()); + let replacement = Raster::new( + "new", + 7, + 3, + vec![vec![2.; 21]], + GeoTransform { origin_x: 5000., origin_y: 9000., pixel_width: 10., pixel_height: -10. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .unwrap(); + let replacement = geotiff::write(&replacement, 0).unwrap(); + std::fs::write(&path, &replacement).unwrap(); + let depth = s.undo_depth(); + let result = if let Some(ahead) = ahead { + let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); + crate::cmd::reads::read_land(&mut s, done) + } else { + s.run("raster.imageGrid", &json!({"layer":id})) + }; + assert!(result.unwrap_err().contains("Reload Data")); + assert_eq!(s.undo_depth(), depth); + assert!(Arc::ptr_eq(&displayed, s.map().layer(id).unwrap().raster.as_ref().unwrap().data.as_ref().unwrap())); + s.run("layer.reloadData", &json!({"layer":id})).unwrap(); + let expected = Frame::from_grid(&geotiff::read_image_grid(std::io::Cursor::new(&replacement)).unwrap()).unwrap(); + let frame: Frame = serde_json::from_value(s.run("raster.imageGrid", &json!({"layer":id})).unwrap()).unwrap(); + assert_eq!(frame, expected); + assert_ne!(frame, original); + std::fs::remove_dir_all(folder).unwrap(); + } + } + #[test] fn annotation_grid_background_read_cancels_and_rejects_replaced_sources() { let (mut s, id, _) = session(); diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index 68d3df0ce1..15ef11af52 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -110,6 +110,20 @@ pub(crate) fn landed(app: &mut RustGisApp, request: u64, result: Landed) { Landed::Ran(Err(e)) | Landed::NotRun(e) => app.annotation.error = Some(e), } } +/// Command Search's read-only grid query says its answer after background or inline delivery. +pub(crate) fn grid_said(app: &mut RustGisApp, result: Landed) { + let Landed::Ran(Ok(value)) = result else { return }; + match serde_json::from_value::(value) { + Ok(frame) => app.session.notify( + crate::i18n::fmt( + "Original image grid: {width} × {height} pixels, EPSG:{epsg}", + &[("width", &frame.width.to_string()), ("height", &frame.height.to_string()), ("epsg", &frame.epsg.to_string())], + ), + false, + ), + Err(_) => app.session.notify(tl!("The original image grid result could not be displayed."), true), + } +} /// Only new objects use this choice. Existing objects keep their own frozen frame. pub(crate) fn drawing_frame(app: &RustGisApp) -> Result { if app.annotation.context != Some((app.session.opened, app.session.map().instance_id())) || !app.annotation.pixels { @@ -451,6 +465,41 @@ mod tests { app.run("ui.annotationGrid", json!({"pixels":false})).unwrap(); assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); } + #[test] + fn annotation_grid_changed_linked_files_block_drawing_until_the_display_is_reloaded() { + let (mut app, image, layer, _) = app(); + let folder = tempfile::tempdir().unwrap(); + let path = folder.path().join("image.tif"); + let bytes = app.session.map().layer(image).unwrap().raster.as_ref().unwrap().embedded.clone().unwrap(); + std::fs::write(&path, bytes.as_slice()).unwrap(); + let pixels = rustgis_engine::data::raster::read_path(&path).unwrap(); + app.session.map_mut().layer_mut(image).unwrap().raster = Some(RasterLayer::from_path(pixels, path.display().to_string())); + let replacement = Frame { width: 128, height: 128, affine: [1000., 2., 0.4, 4000., 0.2, -3.], ..Frame::default() }; + std::fs::write(&path, tiff(&replacement)).unwrap(); + let depth = app.session.undo_depth(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + assert!(app.annotation.error.as_deref().unwrap().contains("Reload Data")); + assert!(drawing_frame(&app).is_err()); + for tool in ["circle", "rotatedBox"] { + app.run("tool.set", json!({"tool":tool})).unwrap(); + let started = if tool == "circle" { + crate::circleedit::drag_start(&mut app, rect(), egui::pos2(400., 350.), true) + } else { + crate::boxedit::drag_start(&mut app, rect(), egui::pos2(400., 350.), true) + }; + assert!(!started); + } + assert!(app.session.map().layer(layer).unwrap().dataset.features.is_empty()); + assert_eq!(app.session.undo_depth(), depth); + app.run("layer.reloadData", json!({"layer":image})).unwrap(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), replacement); + app.session.map_mut().camera.center = replacement.to_lonlat([64., 64.]).unwrap(); + app.run("tool.set", json!({"tool":"circle"})).unwrap(); + drag(&mut app, &replacement, [20., 20.], [25., 20.], true); + assert_eq!(app.session.map().layer(layer).unwrap().dataset.features.len(), 1); + } + #[test] fn annotation_grid_source_changes_during_a_drag_cannot_commit_in_the_old_grid() { for tool in [Tool::Circle, Tool::RotatedBox] { @@ -546,18 +595,28 @@ mod tests { #[test] fn annotation_grid_search_queries_the_grid_without_toggling_the_pane() { - let (mut app, image, _, _) = app(); - app.run("layer.select", json!({"layer":image})).unwrap(); - open(&mut app).unwrap(); - app.ui.command_search = Some("raster.imageGrid".into()); - let mut h = egui_kittest::Harness::builder() - .with_size(egui::vec2(1366., 900.)) - .build_ui_state(|ui, app: &mut RustGisApp| crate::chrome::command_search(app, ui.ctx()), app); - h.key_press(egui::Key::Enter); - h.run(); - assert!(h.state().ui.command_search.is_none()); - assert_eq!(h.state().ui.right, Some(RightPane::Annotation)); - assert!(h.state().session.notices_since(0).iter().all(|n| !n.error)); + for queued in [false, true] { + let (mut app, image, _, _) = app(); + app.run("layer.select", json!({"layer":image})).unwrap(); + open(&mut app).unwrap(); + app.reads_off_frame = queued; + app.ui.command_search = Some("raster.imageGrid".into()); + let mut h = egui_kittest::Harness::builder() + .with_size(egui::vec2(1366., 900.)) + .build_ui_state(|ui, app: &mut RustGisApp| crate::chrome::command_search(app, ui.ctx()), app); + h.key_press(egui::Key::Enter); + h.run(); + let end = std::time::Instant::now() + std::time::Duration::from_secs(30); + while crate::readjob::land(h.state_mut()) { + assert!(std::time::Instant::now() < end, "the grid query did not land"); + std::thread::sleep(std::time::Duration::from_millis(5)); // test-wait: await the background grid result before checking its user-facing summary + } + assert!(h.state().ui.command_search.is_none()); + assert_eq!(h.state().ui.right, Some(RightPane::Annotation)); + let notices = h.state().session.notices_since(0); + assert!(notices.iter().all(|n| !n.error)); + assert_eq!(notices.iter().filter(|n| n.text == "Original image grid: 128 × 128 pixels, EPSG:3857").count(), 1); + } } #[test] diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 587a5bbc15..f91e44fb79 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2664,6 +2664,15 @@ impl RustGisApp { if id.starts_with(rustgis_engine::plugins::PREFIX) { return self.ask_plugin(id, params).map(|()| Value::Null); } + if id == "raster.imageGrid" { + #[cfg(not(target_arch = "wasm32"))] + if self.reads_off_frame { + return readjob::run_then(self, "raster.imageGrid", params, Some(annotation::grid_said)); + } + let result = self.run(id, params); + annotation::grid_said(self, Landed::Ran(result.clone())); + return result; + } self.run(id, params) } diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index ffaf7672e4..91172fd16e 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -181,8 +181,14 @@ switching to Web Mercator or using **Cancel Grid Read** stops only that lookup a unrelated queued work. The status-bar Cancel retains its queue-wide behaviour. Pixel-based drawing waits for a valid grid; a failed or cancelled read never silently substitutes map metres. After changing a source file externally, reload the image layer first, then use **Reload Grid**. +New grid reads from a linked GeoTIFF are refused if its file revision differs from the one +that supplied the displayed pixels, or that revision cannot be verified. This prevents fitting +new annotations with a changed file's grid over the previously loaded image. An already-read +grid stays aligned with the pixels retained in the session. Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; download remote originals first. In the browser, upload the original GeoTIFF. +**Original Image Grid** in Command Search reports the active image's original dimensions and +EPSG coordinate system in a notice, while keeping the Annotate pane open. Space or middle-drag pans; releasing Space keeps the drawing tool. Use the selected object's existing handles, Undo/Redo, and **Export GeoJSON…**. **Shape details** is collapsed by default; From aa5512b7e3f7483d57b55ceaeffb166ae90328ca Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 12:31:37 +0000 Subject: [PATCH 05/14] Cover linked-image conversions and clarify revision recovery --- crates/data/src/raster/file_revision.rs | 6 +- crates/engine/src/image_grid.rs | 2 +- crates/engine/src/shape_conversion.rs | 125 ++++++++++++++++++++++++ docs/user-guide/editing.md | 15 ++- 4 files changed, 142 insertions(+), 6 deletions(-) diff --git a/crates/data/src/raster/file_revision.rs b/crates/data/src/raster/file_revision.rs index 5699d54c03..07841eac20 100644 --- a/crates/data/src/raster/file_revision.rs +++ b/crates/data/src/raster/file_revision.rs @@ -1,4 +1,6 @@ //! Runtime identity of the local GeoTIFF used to decode a displayed raster. +//! Metadata comparison, not a content hash: rewrites preserving every compared field are not detected. +//! Windows compares length, modification and creation times; Unix also compares device, inode and ctime. use std::{ fs::Metadata, path::{Path, PathBuf}, @@ -40,8 +42,8 @@ impl super::Raster { let current = FileRevision::capture(path, metadata).map_err(|e| e.to_string())?; match &self.source_file { Some(original) if *original == current => Ok(()), - Some(_) => Err("The image file changed since its pixels were loaded. Use Reload Data, then Reload Grid.".into()), - None => Err("The displayed image has no verified original file revision. Use Reload Data, then Reload Grid.".into()), + Some(_) => Err("The image file changed since its pixels were loaded. Use Reload Data, then retry the operation.".into()), + None => Err("The displayed image has no verified original file revision. Use Reload Data, then retry the operation.".into()), } } } diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs index 935fac7605..4ae4d42879 100644 --- a/crates/engine/src/image_grid.rs +++ b/crates/engine/src/image_grid.rs @@ -57,7 +57,7 @@ pub(crate) fn read(r: &RasterLayer) -> Result { #[cfg(not(target_arch = "wasm32"))] { let input = std::fs::File::open(path).map_err(|e| e.to_string())?; - let displayed = r.data.as_ref().ok_or("reload the image layer before reading its original grid")?; + let displayed = r.data.as_ref().ok_or("The image pixels are not loaded. Use Reload Data, then retry the operation.")?; let path = std::path::Path::new(path); displayed.check_original_file(path, &input.metadata().map_err(|e| e.to_string())?)?; let grid = rustgis_data::raster::geotiff::read_image_grid(&input); diff --git a/crates/engine/src/shape_conversion.rs b/crates/engine/src/shape_conversion.rs index ec64a2ab35..d5767b734d 100644 --- a/crates/engine/src/shape_conversion.rs +++ b/crates/engine/src/shape_conversion.rs @@ -600,6 +600,131 @@ mod tests { assert!((fit.radius - 1.).abs() < 1e-5); } + #[cfg(not(target_arch = "wasm32"))] + mod linked_images { + use super::*; + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + + struct Dir(std::path::PathBuf); + impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + fn pixels() -> Raster { + Raster::new( + "image", + 64, + 64, + vec![vec![1.; 4096]], + GeoTransform { origin_x: 100., origin_y: 2000., pixel_width: 2., pixel_height: -3. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .unwrap() + } + fn setup(case: &str, kind: &str, queued: bool) -> (Dir, Session, u32, u32, Frame) { + let dir = Dir(std::env::temp_dir().join(format!("rustgis-conversion-linked-{}-{case}-{kind}-{queued}", std::process::id()))); + std::fs::create_dir_all(&dir.0).unwrap(); + let path = dir.0.join("image.tif"); + let bytes = geotiff::write(&pixels(), 0).unwrap(); + std::fs::write(&path, &bytes).unwrap(); + let frame = Frame::from_grid(&geotiff::read_image_grid(std::io::Cursor::new(&bytes)).unwrap()).unwrap(); + let geometry = if kind == "circle" { + CircleSpec { frame: frame.clone(), center: [32., 32.], radius: 5., tolerance: 0.01 }.geometry().unwrap() + } else { + BoxSpec { frame: frame.clone(), center: [32., 32.], width: 12., height: 8., angle: 27. }.geometry().unwrap() + }; + let (mut s, layer) = ordinary(geometry); + let image = s.map_mut().add_raster( + "Image", + rustgis_map::RasterLayer::from_path(rustgis_data::raster::read_path(&path).unwrap(), path.display().to_string()), + ); + (dir, s, layer, image, frame) + } + fn preview(s: &mut Session, params: &Value, queued: bool) -> Result { + if queued { + let read = crate::cmd::reads::read_ahead(s, "edit.previewShapeConversion", params)?; + let done = read.run(&rustgis_analysis::control::Control::new())?; + crate::cmd::reads::read_land(s, done) + } else { + s.run("edit.previewShapeConversion", params) + } + } + fn apply(s: &mut Session, layer: u32, params: &Value, queued: bool, expected: &Frame) { + let before = s.map().layer(layer).unwrap().dataset.features[0].clone(); + let depth = s.undo_depth(); + let value = preview(s, params, queued).unwrap(); + let proposal: Proposal = serde_json::from_value(value.clone()).unwrap(); + let frame = match proposal.shape { + Shape::Circle(c) => c.frame, + Shape::RotatedBox(b) => b.frame, + }; + assert_eq!(&frame, expected); + assert_eq!(s.undo_depth(), depth); + assert_eq!(s.map().layer(layer).unwrap().dataset.features[0], before); + s.run("edit.applyShapeConversion", &json!({"preview":value,"confirm":true})).unwrap(); + assert_eq!(s.undo_depth(), depth + 1); + let after = &s.map().layer(layer).unwrap().dataset.features[0]; + assert_eq!(after.id, before.id); + assert_eq!(after.attrs, before.attrs); + assert!(s.circle_spec(layer, 0).is_some() || s.box_spec(layer, 0).is_some()); + } + + #[test] + fn shape_conversion_verified_local_images_preview_and_apply_in_both_read_paths() { + for queued in [false, true] { + for kind in ["circle", "rotatedBox"] { + let (_dir, mut s, layer, image, frame) = setup("verified", kind, queued); + apply(&mut s, layer, &json!({"layer":layer,"row":0,"kind":kind,"image":image}), queued, &frame); + } + } + } + + #[test] + fn shape_conversion_local_image_failures_preserve_features_and_recover_after_reload() { + for queued in [false, true] { + for kind in ["circle", "rotatedBox"] { + for case in ["missing", "unverified", "changed"] { + let (dir, mut s, layer, image, frame) = setup(case, kind, queued); + let raster = s.map_mut().layer_mut(image).unwrap().raster.as_mut().unwrap(); + let expected = match case { + "missing" => { + raster.data = None; + "pixels are not loaded" + } + "unverified" => { + raster.data = Some(std::sync::Arc::new(pixels())); + "no verified original file revision" + } + _ => { + let path = dir.0.join("image.tif"); + let mut bytes = std::fs::read(&path).unwrap(); + bytes.push(0); // Valid TIFF, deterministically different length even on coarse-timestamp filesystems. + std::fs::write(path, bytes).unwrap(); + "file changed" + } + }; + let before = s.map().layer(layer).unwrap().dataset.features[0].clone(); + let depth = s.undo_depth(); + let params = json!({"layer":layer,"row":0,"kind":kind,"image":image}); + let error = preview(&mut s, ¶ms, queued).unwrap_err(); + assert!(error.contains(expected), "{error}"); + assert!(error.contains("Reload Data") && error.contains("retry"), "{error}"); + assert!(!error.contains("Reload Grid"), "conversion has no Reload Grid action: {error}"); + assert_eq!(s.undo_depth(), depth); + assert_eq!(s.map().layer(layer).unwrap().dataset.features[0], before); + assert!(s.box_spec(layer, 0).is_none() && s.circle_spec(layer, 0).is_none()); + s.run("layer.reloadData", &json!({"layer":image})).unwrap(); + apply(&mut s, layer, ¶ms, queued, &frame); + } + } + } + } + } + #[test] fn shape_conversion_enclosing_fits_are_stable_for_concave_polygons() { let frame = Frame::default(); diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 91172fd16e..4c0892a1b5 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -181,10 +181,15 @@ switching to Web Mercator or using **Cancel Grid Read** stops only that lookup a unrelated queued work. The status-bar Cancel retains its queue-wide behaviour. Pixel-based drawing waits for a valid grid; a failed or cancelled read never silently substitutes map metres. After changing a source file externally, reload the image layer first, then use **Reload Grid**. -New grid reads from a linked GeoTIFF are refused if its file revision differs from the one -that supplied the displayed pixels, or that revision cannot be verified. This prevents fitting -new annotations with a changed file's grid over the previously loaded image. An already-read +New grid reads from a linked GeoTIFF require loaded pixels and a recorded filesystem-metadata +revision. A detected metadata mismatch, or an unverifiable revision, refuses the read rather +than pairing an unchecked header with the displayed image. An already-read grid stays aligned with the pixels retained in the session. +Detection compares filesystem metadata, not file contents: length and modification time, plus +device/inode/change time on Unix or creation time on Windows. A rewrite preserving every +compared field is not detected. In particular, Windows cannot detect an in-place rewrite that +preserves length, modification time and creation time. Reload the image after known external edits +even if no change was reported. Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; download remote originals first. In the browser, upload the original GeoTIFF. **Original Image Grid** in Command Search reports the active image's original dimensions and @@ -483,6 +488,10 @@ keep their current selection when opening the map menu. display overview. Local GeoTIFFs and embedded/browser-uploaded GeoTIFFs are supported; remote original grids must be downloaded and added first. An unreadable or unsupported source gives an error, without falling back to a different grid. + A local image must have loaded pixels and a verified file revision matching those pixels; + conversion does not read an unchecked header when pixels are missing or their revision is + unknown. If asked to reload, use **Reload Data** on the image, then retry **Preview Conversion** + or **Convert Now**. The same metadata-detection limits described above apply here. 2. For a quick conversion, click **Convert Now** to fit and apply using the chosen shape and grid, without a preview or another confirmation. Alternatively, click **Preview Conversion** to show an orange outline without changing the feature or undo history, review or adjust the From 8e38c3d7f23b9e07b163a54ac92068e0848d6810 Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 13:42:36 +0000 Subject: [PATCH 06/14] Fix converted-image grid errors and unavailable image recovery --- crates/data/src/raster/mod.rs | 19 +++++++ crates/engine/src/image_grid.rs | 49 +++++++++++++++++ crates/ui-egui/src/annotation.rs | 91 ++++++++++++++++++++++++++++---- docs/user-guide/editing.md | 5 ++ 4 files changed, 155 insertions(+), 9 deletions(-) diff --git a/crates/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index 26aed34a67..daa12b9ff3 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -1346,6 +1346,8 @@ pub fn read_converted(path: &Path, c: &gdal::Converted) -> Result { let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("raster"); crate::provenance::path(path); let mut r = crate::provenance::without(|| geotiff::read_file(&c.path, stem))?; + // This revision belongs to GDAL's cache copy, not the original source named below. + r.source_file = None; if let Some(s) = &c.subdataset { r.name = format!("{stem} {}", gdal::short_name(s)); } @@ -2439,6 +2441,23 @@ pub(crate) mod tests { assert!(r.lost.is_empty(), "{:?}", r.lost); let _ = std::fs::remove_dir_all(&dir); } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn annotation_grid_converted_pixels_do_not_claim_the_cache_revision() { + let dir = tempfile::tempdir().unwrap(); + let cache = dir.path().join("converted.tif"); + std::fs::write(&cache, geotiff::write(&small(), 0).unwrap()).unwrap(); + let metadata = std::fs::metadata(&cache).unwrap(); + geotiff::read_file(&cache, "cache").unwrap().check_original_file(&cache, &metadata).unwrap(); + let converted = gdal::Converted { path: cache.clone(), subdataset: None, others: Vec::new(), assumed_lonlat: false }; + let source = dir.path().join("original.nc"); + let pixels = read_converted(&source, &converted).unwrap(); + assert_eq!(pixels.source.as_deref(), source.to_str()); + let error = pixels.check_original_file(&cache, &metadata).unwrap_err(); + assert!(error.contains("no verified original file revision"), "{error}"); + assert!(!error.contains("file changed"), "{error}"); + } } #[cfg(test)] diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs index 4ae4d42879..0cf2595b8b 100644 --- a/crates/engine/src/image_grid.rs +++ b/crates/engine/src/image_grid.rs @@ -56,6 +56,9 @@ pub(crate) fn read(r: &RasterLayer) -> Result { .ok_or("choose a local or embedded GeoTIFF; remote original grids must be downloaded and added first")?; #[cfg(not(target_arch = "wasm32"))] { + if rustgis_data::raster::gdal::is_gdal_raster_name(path) { + return Err("Original image grids require a GeoTIFF source. Export this image as a GeoTIFF and add that file.".into()); + } let input = std::fs::File::open(path).map_err(|e| e.to_string())?; let displayed = r.data.as_ref().ok_or("The image pixels are not loaded. Use Reload Data, then retry the operation.")?; let path = std::path::Path::new(path); @@ -192,6 +195,52 @@ mod tests { } } + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn annotation_grid_converted_sources_offer_geotiff_recovery_without_a_reload_loop() { + struct Dir(std::path::PathBuf); + impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let dir = Dir(std::env::temp_dir().join(format!("rustgis-grid-converted-{}", std::process::id()))); + std::fs::create_dir_all(&dir.0).unwrap(); + let (mut s, id, expected) = session(); + let cache = dir.0.join("converted.tif"); + std::fs::write(&cache, s.map().layer(id).unwrap().raster.as_ref().unwrap().embedded.as_ref().unwrap().as_slice()).unwrap(); + let converted = + rustgis_data::raster::gdal::Converted { path: cache.clone(), subdataset: None, others: Vec::new(), assumed_lonlat: false }; + for extension in ["nc", "jp2"] { + let source = dir.0.join(format!("original.{extension}")); + std::fs::write(&source, b"original source represented by the conversion result").unwrap(); + for queued in [false, true] { + // Model the result of GDAL conversion, without needing GDAL installed for this test. + for _ in 0..2 { + let pixels = rustgis_data::raster::read_converted(&source, &converted).unwrap(); + s.map_mut().layer_mut(id).unwrap().raster = Some(RasterLayer::from_path(pixels, source.display().to_string())); + let depth = s.undo_depth(); + let params = json!({"layer":id}); + let result = if queued { + let read = crate::cmd::reads::read_ahead(&mut s, "raster.imageGrid", ¶ms).unwrap(); + let done = read.run(&rustgis_analysis::control::Control::new()).unwrap(); + crate::cmd::reads::read_land(&mut s, done) + } else { + s.run("raster.imageGrid", ¶ms) + }; + let error = result.unwrap_err(); + assert!(error.contains("GeoTIFF") && error.contains("Export") && error.contains("add"), "{error}"); + assert!(!error.contains("changed") && !error.contains("Reload Data"), "{error}"); + assert_eq!(s.undo_depth(), depth); + } + } + } + // Adding the actual GeoTIFF supplies a verified original grid. + let pixels = rustgis_data::raster::read_path(&cache).unwrap(); + s.map_mut().layer_mut(id).unwrap().raster = Some(RasterLayer::from_path(pixels, cache.display().to_string())); + assert_eq!(serde_json::from_value::(s.run("raster.imageGrid", &json!({"layer":id})).unwrap()).unwrap(), expected); + } + #[test] fn annotation_grid_background_read_cancels_and_rejects_replaced_sources() { let (mut s, id, _) = session(); diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index 15ef11af52..f6775f13c6 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -7,6 +7,7 @@ use serde_json::{Value, json}; pub(crate) struct State { context: Option<(u64, u64)>, image: Option, + image_name: Option, image_chosen: bool, image_filename: bool, source: Option, @@ -31,11 +32,18 @@ pub(crate) fn sync(app: &mut RustGisApp) { if app.annotation.context != Some(context) { cancel(app); app.annotation = State { context: Some(context), next: app.annotation.next, ..State::default() }; - } else if app.annotation.source.as_ref().is_some_and(|s| !s.current(&app.session)) { - cancel(app); - app.annotation.frame = None; - app.annotation.source = None; - app.annotation.error = Some(tl!("The image changed or was removed. Select it again or reload its grid.").into()); + } else { + if let Some(name) = app.annotation.image.and_then(|id| app.session.map().layer(id)).filter(|l| l.raster.is_some()).map(|l| &l.name) + && app.annotation.image_name.as_deref() != Some(name.as_str()) + { + app.annotation.image_name = Some(name.clone()); + } + if app.annotation.source.as_ref().is_some_and(|s| !s.current(&app.session)) { + cancel(app); + app.annotation.frame = None; + app.annotation.source = None; + app.annotation.error = Some(tl!("The image changed or was removed. Select it again or reload its grid.").into()); + } } } pub(crate) fn open(app: &mut RustGisApp) -> Result<(), String> { @@ -59,6 +67,7 @@ pub(crate) fn select_image(app: &mut RustGisApp, p: &Value) -> Result<(), String cancel(app); app.annotation.next = request; app.annotation.image = image; + app.annotation.image_name = image.and_then(|id| app.session.map().layer(id)).map(|l| l.name.clone()); app.annotation.image_chosen = true; app.annotation.source = source; app.annotation.pixels = image.is_some(); @@ -188,7 +197,18 @@ pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) { sync(app); let mut image = app.annotation.image; let mut picked = false; - let name = image.and_then(|id| app.session.map().layer(id)).map_or_else(|| tl!("No image").to_string(), |l| l.name.clone()); + let live = image.and_then(|id| app.session.map().layer(id)).filter(|l| l.raster.is_some()); + let available = live.is_some(); + let name = if let Some(layer) = live { + layer.name.clone() + } else if image.is_some() { + app.annotation + .image_name + .as_deref() + .map_or_else(|| tl!("Unavailable image").to_string(), |name| crate::i18n::fmt("Unavailable image: {name}", &[("name", name)])) + } else { + tl!("No image").to_string() + }; egui::ComboBox::from_label(tl!("Image")).selected_text(name).show_ui(ui, |ui| { picked |= ui.selectable_value(&mut image, None, tl!("No image")).clicked(); for l in app.session.map().layers.iter().filter(|l| l.raster.is_some()) { @@ -202,7 +222,7 @@ pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) { egui::ComboBox::from_label(tl!("New shape grid")) .selected_text(if pixels { tl!("Original image pixels") } else { tl!("Web Mercator grid metres") }) .show_ui(ui, |ui| { - ui.add_enabled_ui(image.is_some(), |ui| ui.selectable_value(&mut pixels, true, tl!("Original image pixels"))); + ui.add_enabled_ui(available, |ui| ui.selectable_value(&mut pixels, true, tl!("Original image pixels"))); ui.selectable_value(&mut pixels, false, tl!("Web Mercator grid metres")); }); if pixels != app.annotation.pixels { @@ -224,13 +244,15 @@ pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) { if ui.button(tl!("Cancel Grid Read")).clicked() { let _ = app.run("ui.annotationCancel", Value::Null); } - } else if ui.button(tl!("Reload Grid")).clicked() { + } else if ui.add_enabled(available, egui::Button::new(tl!("Reload Grid"))).clicked() { let _ = app.run("ui.annotationImage", json!({"image":image})); } } else { ui.small(tl!("Web Mercator grid metres, not ground metres. Image pixels are the default when an image is selected.")); } - if let Some(error) = &app.annotation.error { + if image.is_some() && !available { + ui.colored_label(egui::Color32::RED, tl!("This image is unavailable. Choose another image or No image.")); + } else if let Some(error) = &app.annotation.error { ui.colored_label(egui::Color32::RED, error); } } @@ -593,6 +615,57 @@ mod tests { } } + #[test] + fn annotation_grid_removed_image_keeps_its_name_blocks_reload_and_has_explicit_recovery() { + use egui_kittest::kittest::{NodeT, Queryable}; + for recovery in ["undo", "another", "none", "metres"] { + let (mut app, image, _, frame) = app(); + let raster = app.session.map().layer(image).unwrap().raster.clone().unwrap(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("layer.rename", json!({"layer":image,"name":"Survey image"})).unwrap(); + app.run("layer.remove", json!({"layer":image})).unwrap(); + let mut h = egui_kittest::Harness::builder().build_ui_state(|ui, app: &mut RustGisApp| grid_controls(app, ui), app); + assert!( + h.query_all_by_role(egui::accesskit::Role::ComboBox) + .any(|n| n.value().as_deref() == Some("Unavailable image: Survey image")) + ); + assert!(h.get_by_label("Reload Grid").accesskit_node().is_disabled()); + assert!(h.query_by_label("This image is unavailable. Choose another image or No image.").is_some()); + assert!(h.state().annotation.pixels); + assert!(drawing_frame(h.state()).is_err()); + match recovery { + "undo" => { + h.state_mut().run("edit.undo", json!({})).unwrap(); + h.run(); + assert!(!h.get_by_label("Reload Grid").accesskit_node().is_disabled()); + h.get_by_label("Reload Grid").click(); + h.run(); + assert_eq!(drawing_frame(h.state()).unwrap(), frame); + } + "another" => { + let next = h.state_mut().session.map_mut().add_raster("Other image", raster); + h.state_mut().run("ui.annotationImage", json!({"image":next})).unwrap(); + h.run(); + assert_eq!(h.state().annotation.image, Some(next)); + assert_eq!(drawing_frame(h.state()).unwrap(), frame); + } + "none" => { + h.state_mut().run("ui.annotationImage", json!({"image":null})).unwrap(); + h.run(); + assert!(h.state().annotation.image_name.is_none()); + assert!(h.query_all_by_role(egui::accesskit::Role::ComboBox).any(|n| n.value().as_deref() == Some("No image"))); + assert_eq!(drawing_frame(h.state()).unwrap(), Frame::default()); + } + _ => { + h.state_mut().run("ui.annotationGrid", json!({"pixels":false})).unwrap(); + h.run(); + assert!(!h.state().annotation.pixels); + assert_eq!(drawing_frame(h.state()).unwrap(), Frame::default()); + } + } + } + } + #[test] fn annotation_grid_search_queries_the_grid_without_toggling_the_pane() { for queued in [false, true] { diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 4c0892a1b5..5f2bb922a7 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -192,6 +192,11 @@ preserves length, modification time and creation time. Reload the image after kn even if no change was reported. Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; download remote originals first. In the browser, upload the original GeoTIFF. +An image removed from the map keeps its last name as **Unavailable image**. Pixel drawing stays +blocked and **Reload Grid** is disabled until the image is restored or another image is chosen. +Choose **No image** to return explicitly to Web Mercator grid metres. Images loaded through GDAL +(such as NetCDF or JPEG 2000) cannot supply an original GeoTIFF grid: export the image as a GeoTIFF +and add that file instead of repeatedly reloading the converted source. **Original Image Grid** in Command Search reports the active image's original dimensions and EPSG coordinate system in a notice, while keeping the Annotate pane open. From c9f088982acaa4c18aa44c61c1a6019fc47d17a0 Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 14:11:31 +0000 Subject: [PATCH 07/14] Harden image revision checks and stale pixel contexts --- crates/data/src/raster/file_revision.rs | 61 ++++++++++++++++++++----- crates/data/src/raster/geotiff.rs | 58 ++++++++++++++++++++--- crates/data/src/raster/mod.rs | 9 ++-- crates/engine/src/image_grid.rs | 6 +-- crates/ui-egui/src/annotation.rs | 26 ++++++++++- docs/user-guide/editing.md | 5 ++ 6 files changed, 139 insertions(+), 26 deletions(-) diff --git a/crates/data/src/raster/file_revision.rs b/crates/data/src/raster/file_revision.rs index 07841eac20..78269da980 100644 --- a/crates/data/src/raster/file_revision.rs +++ b/crates/data/src/raster/file_revision.rs @@ -33,19 +33,42 @@ impl FileRevision { created: metadata.creation_time(), }) } + + fn check_metadata(&self, path: &Path, metadata: &Metadata) -> std::result::Result<(), String> { + let current = Self::capture(path, metadata).map_err(|e| format!("Could not verify the current image file metadata: {e}"))?; + if *self == current { + Ok(()) + } else { + Err("The image file changed since its pixels were loaded. Use Reload Data, then retry the operation.".into()) + } + } } impl super::Raster { - /// Metadata only, called on the grid reader's worker, never by per-frame UI checks. - /// Missing provenance is refused rather than pairing an arbitrary file with displayed pixels. - pub fn check_original_file(&self, path: &Path, metadata: &Metadata) -> std::result::Result<(), String> { - let current = FileRevision::capture(path, metadata).map_err(|e| e.to_string())?; + fn original_revision(&self) -> std::result::Result<&FileRevision, String> { match &self.source_file { - Some(original) if *original == current => Ok(()), - Some(_) => Err("The image file changed since its pixels were loaded. Use Reload Data, then retry the operation.".into()), + Some(Ok(original)) => Ok(original), + Some(Err(error)) => Err(format!( + "The original image file revision could not be captured ({error}). Check filesystem metadata support/access, or export the image as a GeoTIFF to another location and add it." + )), None => Err("The displayed image has no verified original file revision. Use Reload Data, then retry the operation.".into()), } } + + /// Checks fresh metadata at the live path on the reader's worker, never per frame. + pub fn check_original_file(&self, path: &Path) -> std::result::Result<(), String> { + let original = self.original_revision()?; + let metadata = std::fs::metadata(path).map_err(|e| format!("Could not read the current image file metadata: {e}"))?; + original.check_metadata(path, &metadata) + } + + /// Checks fresh metadata from this open handle against the original source at `path`. + /// This does not verify that the live path still names the handle; check_original_file does. + pub fn check_original_handle(&self, path: &Path, file: &std::fs::File) -> std::result::Result<(), String> { + let original = self.original_revision()?; + let metadata = file.metadata().map_err(|e| format!("Could not read the open image file metadata: {e}"))?; + original.check_metadata(path, &metadata) + } } #[cfg(test)] @@ -59,7 +82,8 @@ mod tests { let raster = super::super::geotiff::read_file(&path, "image").unwrap(); let original = std::fs::File::open(&path).unwrap(); let metadata = original.metadata().unwrap(); - raster.check_original_file(&path, &metadata).unwrap(); + raster.check_original_file(&path).unwrap(); + raster.check_original_handle(&path, &original).unwrap(); let replacement = folder.path().join("replacement.tif"); std::fs::write(&replacement, &bytes).unwrap(); let file = std::fs::File::options().write(true).open(&replacement).unwrap(); @@ -70,9 +94,24 @@ mod tests { let current = std::fs::metadata(&path).unwrap(); assert_eq!(metadata.len(), current.len()); assert_eq!(metadata.modified().unwrap(), current.modified().unwrap()); - // Metadata captured before replacement still describes the old file, not the live path. - raster.check_original_file(&path, &metadata).unwrap(); - assert!(raster.check_original_file(&path, ¤t).unwrap_err().contains("Reload Data")); - assert!(raster.clone().check_original_file(&path, ¤t).is_err()); + // Retaining old metadata cannot make the live-path API accept the replacement. + assert!(raster.check_original_file(&path).unwrap_err().contains("Reload Data")); + let replacement_handle = std::fs::File::open(&path).unwrap(); + assert!(raster.check_original_handle(&path, &replacement_handle).is_err()); + } + + #[test] + fn annotation_grid_handle_validation_rejects_a_different_file() { + let folder = tempfile::tempdir().unwrap(); + let path = folder.path().join("image.tif"); + let other = folder.path().join("other.tif"); + let bytes = super::super::geotiff::write(&super::super::tests::small(), 0).unwrap(); + std::fs::write(&path, &bytes).unwrap(); + let mut other_bytes = bytes.clone(); + other_bytes.push(0); // Distinct metadata even on filesystems with coarse timestamps. + std::fs::write(&other, &other_bytes).unwrap(); + let raster = super::super::geotiff::read_file(&path, "image").unwrap(); + assert!(raster.check_original_handle(&path, &std::fs::File::open(&other).unwrap()).is_err()); + raster.check_original_file(&path).unwrap(); } } diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 36bd1748ae..0bb53b65cc 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1437,23 +1437,69 @@ pub fn read_file(path: &std::path::Path, name: &str) -> Result { #[cfg(not(target_arch = "wasm32"))] pub(super) fn read_file_with_nodata(path: &std::path::Path, name: &str, sidecar_nodata: Option) -> Result { let file = crate::provenance::open(path)?; - let revision = super::file_revision::FileRevision::capture(path, &file.metadata()?).ok(); + let revision = file.metadata().and_then(|metadata| super::file_revision::FileRevision::capture(path, &metadata)); + read_file_with_revision(path, name, sidecar_nodata, file, revision) +} + +#[cfg(not(target_arch = "wasm32"))] +fn read_file_with_revision( + path: &std::path::Path, + name: &str, + sidecar_nodata: Option, + file: std::fs::File, + revision: std::io::Result, +) -> Result { + let revision = revision.map_err(|e| e.to_string()); let open = || -> std::io::Result> { let file = crate::provenance::open(path)?; - if revision.is_some() && super::file_revision::FileRevision::capture(path, &file.metadata()?).ok() != revision { - return Err(std::io::Error::other("the image file changed while its pixels were read; reload it")); + if let Ok(original) = &revision { + let current = super::file_revision::FileRevision::capture(path, &file.metadata()?)?; + if *original != current { + return Err(std::io::Error::other("the image file changed while its pixels were read; reload it")); + } } reader(std::io::BufReader::new(file)) }; let mut raster = read_limited_with_nodata(reader(std::io::BufReader::new(file)).map_err(err)?, name, max_samples(), Some(&open), sidecar_nodata)?; - raster.source_file = revision; - if raster.source_file.is_some() { - raster.check_original_file(path, &std::fs::metadata(path)?).map_err(err)?; + raster.source_file = Some(revision); + if raster.source_file.as_ref().is_some_and(|revision| revision.is_ok()) { + raster.check_original_file(path).map_err(err)?; } Ok(raster) } +#[cfg(all(test, not(target_arch = "wasm32")))] +#[test] +fn annotation_grid_capture_failure_preserves_pixels_and_reports_the_original_cause() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("image.tif"); + let pixels = super::tests::small(); + std::fs::write(&path, write(&pixels, 0).unwrap()).unwrap(); + let file = std::fs::File::open(&path).unwrap(); + let raster = read_file_with_revision( + &path, + "image", + None, + file, + Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "modification timestamps unavailable")), + ) + .unwrap(); + assert_eq!((raster.width, raster.height), (pixels.width, pixels.height)); + assert_eq!(raster.bands[0].data, pixels.bands[0].data); + let error = raster.check_original_file(&path).unwrap_err(); + assert!(error.contains("modification timestamps unavailable"), "{error}"); + assert!(error.contains("metadata support/access") && error.contains("another location"), "{error}"); + assert!(!error.contains("Reload Data"), "{error}"); + assert!( + raster + .clone() + .check_original_handle(&path, &std::fs::File::open(&path).unwrap()) + .unwrap_err() + .contains("modification timestamps unavailable") + ); +} + /// [`read`], holding at most `max` samples (the tests' small limit exercises the overviews), with /// `open` making more readers of the same file for decoding it on several threads. pub(super) fn read_limited(r: Reader<'_>, name: &str, max: usize, open: Option>) -> Result { diff --git a/crates/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index daa12b9ff3..2e9d19ddc0 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -622,9 +622,9 @@ pub struct Raster { pub data_type: String, /// Where it was read from, if anywhere. pub source: Option, - /// The original local GeoTIFF's revision, retained with these pixels and never serialized. + /// The original local GeoTIFF's revision or capture failure, retained with these pixels and never serialized. #[cfg(not(target_arch = "wasm32"))] - source_file: Option, + source_file: Option>, /// Whether the pixels come from a reduced-resolution overview. pub overview: Option<(u32, u32)>, /// With `overview`, whether the pixels are every `step`-th of the file's, from its top-left @@ -2448,13 +2448,12 @@ pub(crate) mod tests { let dir = tempfile::tempdir().unwrap(); let cache = dir.path().join("converted.tif"); std::fs::write(&cache, geotiff::write(&small(), 0).unwrap()).unwrap(); - let metadata = std::fs::metadata(&cache).unwrap(); - geotiff::read_file(&cache, "cache").unwrap().check_original_file(&cache, &metadata).unwrap(); + geotiff::read_file(&cache, "cache").unwrap().check_original_file(&cache).unwrap(); let converted = gdal::Converted { path: cache.clone(), subdataset: None, others: Vec::new(), assumed_lonlat: false }; let source = dir.path().join("original.nc"); let pixels = read_converted(&source, &converted).unwrap(); assert_eq!(pixels.source.as_deref(), source.to_str()); - let error = pixels.check_original_file(&cache, &metadata).unwrap_err(); + let error = pixels.check_original_file(&cache).unwrap_err(); assert!(error.contains("no verified original file revision"), "{error}"); assert!(!error.contains("file changed"), "{error}"); } diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs index 0cf2595b8b..210b10da8c 100644 --- a/crates/engine/src/image_grid.rs +++ b/crates/engine/src/image_grid.rs @@ -62,12 +62,12 @@ pub(crate) fn read(r: &RasterLayer) -> Result { let input = std::fs::File::open(path).map_err(|e| e.to_string())?; let displayed = r.data.as_ref().ok_or("The image pixels are not loaded. Use Reload Data, then retry the operation.")?; let path = std::path::Path::new(path); - displayed.check_original_file(path, &input.metadata().map_err(|e| e.to_string())?)?; + displayed.check_original_handle(path, &input)?; let grid = rustgis_data::raster::geotiff::read_image_grid(&input); // Both the handle and path must still name the revision that supplied the display. // A replacement during parsing must not pair new coordinates with old pixels. - displayed.check_original_file(path, &input.metadata().map_err(|e| e.to_string())?)?; - displayed.check_original_file(path, &std::fs::metadata(path).map_err(|e| e.to_string())?)?; + displayed.check_original_handle(path, &input)?; + displayed.check_original_file(path)?; grid } #[cfg(target_arch = "wasm32")] diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index f6775f13c6..381ab50e9b 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -135,9 +135,12 @@ pub(crate) fn grid_said(app: &mut RustGisApp, result: Landed) { } /// Only new objects use this choice. Existing objects keep their own frozen frame. pub(crate) fn drawing_frame(app: &RustGisApp) -> Result { - if app.annotation.context != Some((app.session.opened, app.session.map().instance_id())) || !app.annotation.pixels { + if !app.annotation.pixels { return Ok(Frame::default()); } + if app.annotation.context != Some((app.session.opened, app.session.map().instance_id())) { + return Err(tl!("The image context changed. Select the image again before drawing.").into()); + } if !app.annotation.source.as_ref().is_some_and(|s| s.current(&app.session)) { return Err(tl!("The image changed. Reload its grid before drawing.").into()); } @@ -574,6 +577,27 @@ mod tests { assert!(app.annotation.image.is_none()); assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); } + #[test] + fn annotation_grid_stale_pixel_context_never_substitutes_map_metres() { + for project_changed in [false, true] { + let (mut app, image, _, frame) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + let context = app.annotation.context.unwrap(); + // Model a stale binding before the next UI synchronization, while the source is live. + app.annotation.context = + Some(if project_changed { (context.0.wrapping_add(1), context.1) } else { (context.0, context.1.wrapping_add(1)) }); + assert!(app.annotation.source.as_ref().unwrap().current(&app.session)); + assert!(drawing_frame(&app).unwrap_err().contains("context changed")); + assert!(app.annotation.pixels); + assert_eq!(app.annotation.frame.as_ref(), Some(&frame)); + app.annotation.pixels = false; + assert_eq!(drawing_frame(&app).unwrap(), Frame::default(), "explicit map units remain valid"); + app.annotation.pixels = true; + app.annotation.context = Some(context); + assert_eq!(drawing_frame(&app).unwrap(), frame); + } + } + #[test] fn annotation_grid_open_keeps_automatic_selection_errors_inline() { use egui_kittest::kittest::Queryable; diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 5f2bb922a7..f9c5f4fce9 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -190,6 +190,11 @@ device/inode/change time on Unix or creation time on Windows. A rewrite preservi compared field is not detected. In particular, Windows cannot detect an in-place rewrite that preserves length, modification time and creation time. Reload the image after known external edits even if no change was reported. +If revision capture fails, the image can still load for viewing, but grid reads report the +original metadata error. Check filesystem metadata support/access, or export the image as a +GeoTIFF to another location and add it. Repeatedly reloading the same unsupported filesystem +does not repair its metadata support. Stale project/map bindings in pixel mode refuse drawing +until the image is selected again; only an explicit map-grid choice returns map metres. Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; download remote originals first. In the browser, upload the original GeoTIFF. An image removed from the map keeps its last name as **Unavailable image**. Pixel drawing stays From a0d6b23cff46f53a0deedbf435a70e2bbf90790c Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 14:34:08 +0000 Subject: [PATCH 08/14] Clarify original-grid recovery for non-TIFF images --- crates/data/src/raster/geotiff.rs | 62 +++++++++++++++---------------- crates/engine/src/image_grid.rs | 60 ++++++++++++++++++++++++++++-- crates/ui-egui/src/annotation.rs | 4 +- crates/ui-egui/src/lib.rs | 4 +- docs/user-guide/editing.md | 17 ++++----- 5 files changed, 100 insertions(+), 47 deletions(-) diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 0bb53b65cc..058b416ccf 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1469,37 +1469,6 @@ fn read_file_with_revision( Ok(raster) } -#[cfg(all(test, not(target_arch = "wasm32")))] -#[test] -fn annotation_grid_capture_failure_preserves_pixels_and_reports_the_original_cause() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join("image.tif"); - let pixels = super::tests::small(); - std::fs::write(&path, write(&pixels, 0).unwrap()).unwrap(); - let file = std::fs::File::open(&path).unwrap(); - let raster = read_file_with_revision( - &path, - "image", - None, - file, - Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "modification timestamps unavailable")), - ) - .unwrap(); - assert_eq!((raster.width, raster.height), (pixels.width, pixels.height)); - assert_eq!(raster.bands[0].data, pixels.bands[0].data); - let error = raster.check_original_file(&path).unwrap_err(); - assert!(error.contains("modification timestamps unavailable"), "{error}"); - assert!(error.contains("metadata support/access") && error.contains("another location"), "{error}"); - assert!(!error.contains("Reload Data"), "{error}"); - assert!( - raster - .clone() - .check_original_handle(&path, &std::fs::File::open(&path).unwrap()) - .unwrap_err() - .contains("modification timestamps unavailable") - ); -} - /// [`read`], holding at most `max` samples (the tests' small limit exercises the overviews), with /// `open` making more readers of the same file for decoding it on several threads. pub(super) fn read_limited(r: Reader<'_>, name: &str, max: usize, open: Option>) -> Result { @@ -2191,6 +2160,37 @@ mod tests { use super::*; use crate::raster::tests::small; + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn annotation_grid_capture_failure_preserves_pixels_and_reports_the_original_cause() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("image.tif"); + let pixels = small(); + std::fs::write(&path, write(&pixels, 0).unwrap()).unwrap(); + let file = std::fs::File::open(&path).unwrap(); + let raster = read_file_with_revision( + &path, + "image", + None, + file, + Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "modification timestamps unavailable")), + ) + .unwrap(); + assert_eq!((raster.width, raster.height), (pixels.width, pixels.height)); + assert_eq!(raster.bands[0].data, pixels.bands[0].data); + let error = raster.check_original_file(&path).unwrap_err(); + assert!(error.contains("modification timestamps unavailable"), "{error}"); + assert!(error.contains("metadata support/access") && error.contains("another location"), "{error}"); + assert!(!error.contains("Reload Data"), "{error}"); + assert!( + raster + .clone() + .check_original_handle(&path, &std::fs::File::open(&path).unwrap()) + .unwrap_err() + .contains("modification timestamps unavailable") + ); + } + #[cfg(not(target_arch = "wasm32"))] #[test] fn chunked_reads_count_progress_and_stop_before_or_during_serial_and_parallel_work() { diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs index 210b10da8c..817bc4e7f6 100644 --- a/crates/engine/src/image_grid.rs +++ b/crates/engine/src/image_grid.rs @@ -56,7 +56,11 @@ pub(crate) fn read(r: &RasterLayer) -> Result { .ok_or("choose a local or embedded GeoTIFF; remote original grids must be downloaded and added first")?; #[cfg(not(target_arch = "wasm32"))] { - if rustgis_data::raster::gdal::is_gdal_raster_name(path) { + let tiff = std::path::Path::new(path) + .extension() + .and_then(|e| e.to_str()) + .is_some_and(|e| e.eq_ignore_ascii_case("tif") || e.eq_ignore_ascii_case("tiff")); + if !tiff { return Err("Original image grids require a GeoTIFF source. Export this image as a GeoTIFF and add that file.".into()); } let input = std::fs::File::open(path).map_err(|e| e.to_string())?; @@ -91,8 +95,8 @@ pub(crate) fn ahead(s: &Session, p: &Value) -> Option CmdResult { let source = Source::capture(s, s.layer_id(p)?)?; let ready = crate::cmd::reads::take::(s, "imageGrid"); @@ -241,6 +245,56 @@ mod tests { assert_eq!(serde_json::from_value::(s.run("raster.imageGrid", &json!({"layer":id})).unwrap()).unwrap(), expected); } + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn annotation_grid_other_local_formats_offer_geotiff_recovery_without_a_reload_loop() { + struct Dir(std::path::PathBuf); + impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let dir = Dir(std::env::temp_dir().join(format!("rustgis-grid-local-formats-{}", std::process::id()))); + std::fs::create_dir_all(&dir.0).unwrap(); + let (mut s, id, expected) = session(); + for extension in ["asc", "png", "jpg", "jpeg"] { + let path = dir.0.join(format!("image.{extension}")); + if extension == "asc" { + std::fs::write(&path, "ncols 2\nnrows 2\nxllcorner 0\nyllcorner 0\ncellsize 1\n1 2\n3 4\n").unwrap(); + } else { + image::RgbImage::from_pixel(2, 2, image::Rgb([64, 128, 192])).save(&path).unwrap(); + } + let pixels = rustgis_data::raster::read_path(&path).unwrap(); + s.map_mut().layer_mut(id).unwrap().raster = Some(RasterLayer::from_path(pixels, path.display().to_string())); + for queued in [false, true] { + for _ in 0..2 { + let depth = s.undo_depth(); + let params = json!({"layer":id}); + let result = if queued { + let read = crate::cmd::reads::read_ahead(&mut s, "raster.imageGrid", ¶ms).unwrap(); + let done = read.run(&rustgis_analysis::control::Control::new()).unwrap(); + crate::cmd::reads::read_land(&mut s, done) + } else { + s.run("raster.imageGrid", ¶ms) + }; + let error = result.unwrap_err(); + assert!(error.contains("GeoTIFF") && error.contains("Export") && error.contains("add"), "{extension}: {error}"); + assert!(!error.contains("Reload Data") && !error.contains("file changed"), "{extension}: {error}"); + assert_eq!(s.undo_depth(), depth); + s.run("layer.reloadData", ¶ms).unwrap(); + } + } + } + // A real TIFF, including an uppercase extension, still supplies its original grid. + let path = dir.0.join("original.TIFF"); + let (source, source_id, _) = session(); + std::fs::write(&path, source.map().layer(source_id).unwrap().raster.as_ref().unwrap().embedded.as_ref().unwrap().as_slice()) + .unwrap(); + let pixels = rustgis_data::raster::read_path(&path).unwrap(); + s.map_mut().layer_mut(id).unwrap().raster = Some(RasterLayer::from_path(pixels, path.display().to_string())); + assert_eq!(serde_json::from_value::(s.run("raster.imageGrid", &json!({"layer":id})).unwrap()).unwrap(), expected); + } + #[test] fn annotation_grid_background_read_cancels_and_rejects_replaced_sources() { let (mut s, id, _) = session(); diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index 381ab50e9b..27963f5c40 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -120,7 +120,7 @@ pub(crate) fn landed(app: &mut RustGisApp, request: u64, result: Landed) { } } /// Command Search's read-only grid query says its answer after background or inline delivery. -pub(crate) fn grid_said(app: &mut RustGisApp, result: Landed) { +pub(crate) fn show_grid_result(app: &mut RustGisApp, result: Landed) { let Landed::Ran(Ok(value)) = result else { return }; match serde_json::from_value::(value) { Ok(frame) => app.session.notify( @@ -222,7 +222,7 @@ pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) { let _ = app.run("ui.annotationImage", json!({"image":image})); } let mut pixels = app.annotation.pixels; - egui::ComboBox::from_label(tl!("New shape grid")) + egui::ComboBox::new("annotation-construction-grid", tl!("Grid")) .selected_text(if pixels { tl!("Original image pixels") } else { tl!("Web Mercator grid metres") }) .show_ui(ui, |ui| { ui.add_enabled_ui(available, |ui| ui.selectable_value(&mut pixels, true, tl!("Original image pixels"))); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 9875a08e77..c13116c761 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2715,10 +2715,10 @@ impl RustGisApp { if id == "raster.imageGrid" { #[cfg(not(target_arch = "wasm32"))] if self.reads_off_frame { - return readjob::run_then(self, "raster.imageGrid", params, Some(annotation::grid_said)); + return readjob::run_then(self, "raster.imageGrid", params, Some(annotation::show_grid_result)); } let result = self.run(id, params); - annotation::grid_said(self, Landed::Ran(result.clone())); + annotation::show_grid_result(self, Landed::Ran(result.clone())); return result; } self.run(id, params) diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index f9c5f4fce9..8cab9c99a5 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -186,22 +186,21 @@ revision. A detected metadata mismatch, or an unverifiable revision, refuses the than pairing an unchecked header with the displayed image. An already-read grid stays aligned with the pixels retained in the session. Detection compares filesystem metadata, not file contents: length and modification time, plus -device/inode/change time on Unix or creation time on Windows. A rewrite preserving every -compared field is not detected. In particular, Windows cannot detect an in-place rewrite that -preserves length, modification time and creation time. Reload the image after known external edits -even if no change was reported. +device/inode/change time on Unix or creation time on Windows. Rewrites preserving every compared +field go undetected; on Windows this includes in-place rewrites preserving length, modification +time and creation time. Reload after known external edits even without a reported change. If revision capture fails, the image can still load for viewing, but grid reads report the original metadata error. Check filesystem metadata support/access, or export the image as a GeoTIFF to another location and add it. Repeatedly reloading the same unsupported filesystem does not repair its metadata support. Stale project/map bindings in pixel mode refuse drawing until the image is selected again; only an explicit map-grid choice returns map metres. -Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; download remote originals -first. In the browser, upload the original GeoTIFF. +Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; +download remote originals first. In the browser, upload the original GeoTIFF. Other local formats +(including PNG, JPEG, ASCII grids and GDAL-converted images) are refused with guidance to export +the image as a GeoTIFF and add that file; reloading the unsupported source cannot supply its grid. An image removed from the map keeps its last name as **Unavailable image**. Pixel drawing stays blocked and **Reload Grid** is disabled until the image is restored or another image is chosen. -Choose **No image** to return explicitly to Web Mercator grid metres. Images loaded through GDAL -(such as NetCDF or JPEG 2000) cannot supply an original GeoTIFF grid: export the image as a GeoTIFF -and add that file instead of repeatedly reloading the converted source. +Choose **No image** to return explicitly to Web Mercator grid metres. **Original Image Grid** in Command Search reports the active image's original dimensions and EPSG coordinate system in a notice, while keeping the Annotate pane open. From 7ba682b0f8c1fb2f86ece76f8bea8cf0e44e30f6 Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 15:01:24 +0000 Subject: [PATCH 09/14] Preserve pixel mode across context changes and prepare scripted grid queries --- crates/data/src/raster/geotiff.rs | 10 ++- crates/engine/src/cmd/gp.rs | 9 ++- crates/engine/src/cmd/workflows.rs | 31 +++++---- crates/engine/src/image_grid.rs | 104 ++++++++++++++++++++++++++++- crates/engine/src/lib.rs | 5 ++ crates/ui-egui/src/analysispane.rs | 38 +++++++++++ crates/ui-egui/src/annotation.rs | 57 +++++++++++++++- crates/ui-egui/src/lib.rs | 5 +- crates/ui-egui/src/readjob.rs | 49 +++++++++++++- docs/user-guide/editing.md | 11 ++- 10 files changed, 290 insertions(+), 29 deletions(-) diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 058b416ccf..3ec7e886ab 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1436,7 +1436,7 @@ pub fn read_file(path: &std::path::Path, name: &str) -> Result { /// [`read_file`], with the no-data value a sidecar gives (see [`read_limited_with_nodata`]). #[cfg(not(target_arch = "wasm32"))] pub(super) fn read_file_with_nodata(path: &std::path::Path, name: &str, sidecar_nodata: Option) -> Result { - let file = crate::provenance::open(path)?; + let file = crate::provenance::open(path).map_err(err)?; let revision = file.metadata().and_then(|metadata| super::file_revision::FileRevision::capture(path, &metadata)); read_file_with_revision(path, name, sidecar_nodata, file, revision) } @@ -2160,6 +2160,14 @@ mod tests { use super::*; use crate::raster::tests::small; + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn annotation_grid_geotiff_open_failure_retains_format_context() { + let dir = tempfile::tempdir().unwrap(); + let error = read_file(&dir.path().join("missing.tif"), "missing").unwrap_err().to_string(); + assert!(error.contains("GeoTIFF:"), "{error}"); + } + #[cfg(not(target_arch = "wasm32"))] #[test] fn annotation_grid_capture_failure_preserves_pixels_and_reports_the_original_cause() { diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 58c77c297d..8de48aff8c 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -862,6 +862,7 @@ pub(crate) struct RanAheads { tools: Vec, files: Vec, zones: Vec, + grids: Vec, } #[cfg(not(target_arch = "wasm32"))] @@ -872,11 +873,13 @@ pub(crate) struct RanAheads { pub(crate) fn taking(s: &mut Session, ran: RanAheads, run: impl FnOnce(&mut Session) -> T) -> T { s.tools_ahead = Some(Ahead::Taking(ran.tools.into())); s.files_ahead = Some(FilesAhead::Taking(ran.files.into())); + s.grids_ahead = Some(crate::image_grid::Ahead::Taking(ran.grids.into())); s.zones_ahead = Some(super::imagery::ZonesAhead::Taking(ran.zones.into())); let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(s))); s.tools_ahead = None; s.files_ahead = None; s.zones_ahead = None; + s.grids_ahead = None; r.unwrap_or_else(|p| std::panic::resume_unwind(p)) } @@ -895,7 +898,11 @@ pub(crate) fn ran_ahead(s: &mut Session) -> RanAheads { Some(super::imagery::ZonesAhead::Recording(read)) => read, _ => Vec::new(), }; - RanAheads { tools, files, zones } + let grids = match s.grids_ahead.take() { + Some(crate::image_grid::Ahead::Recording(read)) => read, + _ => Vec::new(), + }; + RanAheads { tools, files, zones, grids } } #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 7439fe089d..1f87a15266 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -826,6 +826,7 @@ pub(crate) fn ahead_on_copy(s: &Session, command: &'static str, p: &Value) -> su copy.sync_policy_generation(); copy.tools_ahead = Some(super::gp::Ahead::Recording(Vec::new())); copy.files_ahead = Some(super::gp::FilesAhead::Recording(Vec::new())); + copy.grids_ahead = Some(crate::image_grid::Ahead::Recording(Vec::new())); copy.zones_ahead = Some(super::imagery::ZonesAhead::Recording(Vec::new())); let _ = copy.run(command, &p); super::gp::ran_ahead(&mut copy) @@ -833,34 +834,33 @@ pub(crate) fn ahead_on_copy(s: &Session, command: &'static str, p: &Value) -> su } #[cfg(not(target_arch = "wasm32"))] -/// Prepares a native script's replayable prefix. Every table write must fit within that prefix; -/// otherwise refuse before any line runs, so a desktop table edit never falls back to the frame. -/// Restricted callers keep their existing authority and must dispatch table writes individually. +/// Prepares a native script's replayable prefix. Grid queries and table writes must fit within it; +/// otherwise refuse before any line runs, so neither header reads nor table edits reach the UI thread. +/// Restricted callers keep their existing authority and dispatch those operations individually. pub(crate) fn script_ahead(s: &Session, p: &Value, native: bool) -> Result, String> { let Some(script) = ps(p, "script") else { return Ok(None) }; let Ok(lines) = script_commands(script) else { return Ok(None) }; let table = |id: &str, p: &Value| super::edit::table_edit_in_background(id).is_some() && p.get("table").is_some_and(|t| !t.is_null()); - if lines.iter().any(|(_, id, p)| table(id, p)) { + if lines.iter().any(|(_, id, p)| table(id, p) || id == "raster.imageGrid") { if !native { return Err( - "this script cannot prepare table writes under its current permissions: run the table edit commands directly".into() + "this script cannot prepare queries or table writes under its current permissions: run those commands directly".into() ); } - check_table_prefix(s, &lines)?; + check_prepared_prefix(s, &lines)?; } let Some((_, id, params)) = lines.first() else { return Ok(None) }; Ok((native && runs_on_a_copy(s, id, params)).then(|| ahead_on_copy(s, SCRIPT_AHEAD, p))) } #[cfg(not(target_arch = "wasm32"))] -/// Refuses an unrecordable prefix before any live line can run ahead of a table write. Inspect +/// Refuses an unrecordable prefix before live lines can precede a grid query or table write. Inspect /// model tool metadata too: their command id alone does not reveal file-producing or plugin steps. /// Recheck at landing, since a named model may have changed while the script worker ran. -fn check_table_prefix(s: &Session, lines: &[(usize, String, Value)]) -> Result<(), String> { - let Some(last) = lines - .iter() - .rposition(|(_, id, p)| super::edit::table_edit_in_background(id).is_some() && p.get("table").is_some_and(|t| !t.is_null())) - else { +fn check_prepared_prefix(s: &Session, lines: &[(usize, String, Value)]) -> Result<(), String> { + let Some(last) = lines.iter().rposition(|(_, id, p)| { + id == "raster.imageGrid" || (super::edit::table_edit_in_background(id).is_some() && p.get("table").is_some_and(|t| !t.is_null())) + }) else { return Ok(()); }; let recordable_tool = |id: &str| { @@ -881,7 +881,7 @@ fn check_table_prefix(s: &Session, lines: &[(usize, String, Value)]) -> Result<( }; if !nested || !runs_on_a_copy(s, id, p) { return Err(format!( - "line {n} ({id}) cannot be prepared before the table writes: run the setup and table writes as separate scripts" + "line {n} ({id}) cannot be prepared before the queries or table writes: run the setup and prepared commands as separate scripts" )); } } @@ -931,6 +931,9 @@ pub(crate) const ON_A_COPY: &[&str] = &[ /// other line stops the copy there, Add Data and Load All among them (they would read their /// files and servers twice). fn runs_on_a_copy(s: &Session, id: &str, params: &Value) -> bool { + if id == "raster.imageGrid" { + return true; // Header results are recorded on the worker and consumed once at landing. + } if super::edit::table_edit_in_background(id).is_some() { return params.get("table").is_some_and(|t| !t.is_null()) && params.get("layer").is_none_or(Value::is_null); } @@ -1096,7 +1099,7 @@ fn run_script_here(s: &mut Session, p: &Value, prepared: bool) -> CmdResult { let lines = script_commands(script)?; #[cfg(not(target_arch = "wasm32"))] if prepared { - check_table_prefix(s, &lines)?; + check_prepared_prefix(s, &lines)?; } let mut out = Vec::new(); for (n, id, params) in lines { diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs index 817bc4e7f6..ccaa551526 100644 --- a/crates/engine/src/image_grid.rs +++ b/crates/engine/src/image_grid.rs @@ -43,6 +43,7 @@ impl Source { /// Cancel is checked before/after the bounded header parse; a filesystem read/seek itself /// cannot be interrupted until the OS returns. No raster pixel decoding takes place. +/// This counts the header phase; conversion fitting starts its own progress phase afterwards. pub(crate) fn read(r: &RasterLayer) -> Result { progress::begin(2); progress::step(1).map_err(|e| e.to_string())?; @@ -63,7 +64,7 @@ pub(crate) fn read(r: &RasterLayer) -> Result { if !tiff { return Err("Original image grids require a GeoTIFF source. Export this image as a GeoTIFF and add that file.".into()); } - let input = std::fs::File::open(path).map_err(|e| e.to_string())?; + let input = rustgis_data::provenance::open(path).map_err(|e| e.to_string())?; let displayed = r.data.as_ref().ok_or("The image pixels are not loaded. Use Reload Data, then retry the operation.")?; let path = std::path::Path::new(path); displayed.check_original_handle(path, &input)?; @@ -84,10 +85,16 @@ pub(crate) fn read(r: &RasterLayer) -> Result { progress::step(1).map_err(|e| e.to_string())?; Frame::from_grid(&grid) } -struct Prepared { +pub(crate) struct Prepared { source: Source, result: Result, } +/// Script workers record read-only grids; landing consumes them without repeating file I/O. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) enum Ahead { + Recording(Vec), + Taking(std::collections::VecDeque), +} pub(crate) fn ahead(s: &Session, p: &Value) -> Option { let source = Source::capture(s, s.layer_id(p).ok()?).ok()?; Some(crate::cmd::reads::ahead("Read Original Image Grid", "imageGrid".into(), move || { @@ -101,13 +108,26 @@ pub(crate) fn query(s: &mut Session, p: &Value) -> CmdResult { let source = Source::capture(s, s.layer_id(p)?)?; let ready = crate::cmd::reads::take::(s, "imageGrid"); #[cfg(not(target_arch = "wasm32"))] + let ready = if let Some(Ahead::Taking(grids)) = &mut s.grids_ahead { + Some(grids.pop_front().ok_or("the script's image grid was not prepared; run the query separately")?) + } else { + ready + }; + #[cfg(not(target_arch = "wasm32"))] if ready.is_none() && s.statistics_window_thread == Some(std::thread::current().id()) { return Err("original image grids need the desktop background read queue".into()); } let frame = match ready { Some(done) if done.source.current(s) && done.source.layer == source.layer => done.result?, Some(_) => return Err("image source changed while reading its grid; select the image again".into()), - None => read(&source.raster)?, + None => { + let result = read(&source.raster); + #[cfg(not(target_arch = "wasm32"))] + if let Some(Ahead::Recording(grids)) = &mut s.grids_ahead { + grids.push(Prepared { source, result: result.clone() }); + } + result? + } }; serde_json::to_value(frame).map_err(|e| e.to_string()) } @@ -295,6 +315,84 @@ mod tests { assert_eq!(serde_json::from_value::(s.run("raster.imageGrid", &json!({"layer":id})).unwrap()).unwrap(), expected); } + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn annotation_grid_script_worker_returns_frames_without_reopening_on_the_window_thread() { + for replace in [false, true] { + let (mut s, id, expected) = session(); + let dir = std::env::temp_dir().join(format!("rustgis-grid-script-{}-{replace}", std::process::id())); + struct Dir(std::path::PathBuf); + impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let dir = Dir(dir); + std::fs::create_dir_all(&dir.0).unwrap(); + let path = dir.0.join("image.tif"); + let bytes = s.map().layer(id).unwrap().raster.as_ref().unwrap().embedded.clone().unwrap(); + std::fs::write(&path, bytes.as_slice()).unwrap(); + s.map_mut().layer_mut(id).unwrap().raster = + Some(RasterLayer::from_path(rustgis_data::raster::read_path(&path).unwrap(), path.display().to_string())); + let params = json!({"script":format!("raster.imageGrid {}\nraster.imageGrid {}", json!({"layer":id}), json!({"layer":id}))}); + let direct = s.run("script.run", ¶ms).unwrap(); + assert_eq!(serde_json::from_value::(direct["ran"][0]["result"].clone()).unwrap(), expected); + s.statistics_need_read_ahead(true); + let ahead = crate::cmd::reads::read_ahead(&mut s, "script.run", ¶ms).unwrap(); + let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); + if replace { + let raster = s.map().layer(id).unwrap().raster.clone().unwrap(); + s.map_mut().layer_mut(id).unwrap().raster = Some(RasterLayer::from_bytes( + raster.data.as_ref().unwrap().as_ref().clone(), + "replacement.tif", + bytes.as_ref().clone(), + )); + } + // No file can be reopened at landing. Frames remain bound to the loaded pixels. + std::fs::remove_file(&path).unwrap(); + let result = crate::cmd::reads::read_land(&mut s, done); + if replace { + assert!(result.unwrap_err().contains("source changed")); + } else { + let answer = result.unwrap(); + for index in [0, 1] { + assert_eq!(serde_json::from_value::(answer["ran"][index]["result"].clone()).unwrap(), expected); + assert!(answer["ran"][index]["result"].get("started").is_none()); + } + } + assert!(s.grids_ahead.is_none(), "prepared queries must not leak into later scripts"); + let unsafe_prefix = json!({"script":format!("project.new {{}}\nraster.imageGrid {}", json!({"layer":id}))}); + let before = s.opened; + assert!(crate::cmd::reads::read_ahead(&mut s, "script.run", &unsafe_prefix).err().unwrap().contains("separate scripts")); + assert_eq!(s.opened, before); + } + } + + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn annotation_grid_header_read_records_provenance_without_a_content_hash() { + let (s, id, expected) = session(); + let dir = std::env::temp_dir().join(format!("rustgis-grid-provenance-{}", std::process::id())); + struct Dir(std::path::PathBuf); + impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let dir = Dir(dir); + std::fs::create_dir_all(&dir.0).unwrap(); + let path = dir.0.join("image.tif"); + std::fs::write(&path, s.map().layer(id).unwrap().raster.as_ref().unwrap().embedded.as_ref().unwrap().as_slice()).unwrap(); + let layer = RasterLayer::from_path(rustgis_data::raster::read_path(&path).unwrap(), path.display().to_string()); + let capture = rustgis_data::provenance::Capture::start(); + assert_eq!(read(&layer).unwrap(), expected); + let inputs = capture.inputs(); + assert_eq!(inputs.len(), 1); + assert_eq!(inputs[0].path, path.display().to_string()); + assert_eq!(inputs[0].size, Some(std::fs::metadata(&path).unwrap().len())); + assert!(inputs[0].sha256.is_none()); + } + #[test] fn annotation_grid_background_read_cancels_and_rejects_replaced_sources() { let (mut s, id, _) = session(); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index c303d0ca59..e1c8f0bc63 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1007,6 +1007,9 @@ pub struct Session { /// ([`cmd::gp::FilesAhead`]). #[cfg(not(target_arch = "wasm32"))] pub(crate) files_ahead: Option, + /// Original grids read by a script worker and consumed at landing without file I/O. + #[cfg(not(target_arch = "wasm32"))] + pub(crate) grids_ahead: Option, /// The zones' spectral means a script read on that copy, which its run here takes /// ([`cmd::imagery::ZonesAhead`]). #[cfg(not(target_arch = "wasm32"))] @@ -1344,6 +1347,8 @@ impl Session { #[cfg(not(target_arch = "wasm32"))] files_ahead: None, #[cfg(not(target_arch = "wasm32"))] + grids_ahead: None, + #[cfg(not(target_arch = "wasm32"))] zones_ahead: None, #[cfg(not(target_arch = "wasm32"))] requests_into: None, diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 4f4445b019..4437040651 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -2311,6 +2311,44 @@ mod tests { } } + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn annotation_grid_script_pane_receives_the_frame_after_worker_delivery() { + use rustgis_engine::data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + for off_frame in [false, true] { + let raster = Raster::new( + "Image", + 2, + 3, + vec![vec![1.; 6]], + GeoTransform { origin_x: 100., origin_y: 500., pixel_width: 2., pixel_height: -3. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .unwrap(); + let bytes = geotiff::write(&raster, 0).unwrap(); + let expected = + rustgis_engine::box_edit::Frame::from_grid(&geotiff::read_image_grid(std::io::Cursor::new(&bytes)).unwrap()).unwrap(); + let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); + let image = app.session.map_mut().add_raster("Image", rustgis_engine::map::RasterLayer::from_bytes(raster, "image.tif", bytes)); + app.reads_off_frame = off_frame; + start_script(&mut app, format!("raster.imageGrid {}", json!({"layer":image}))); + let end = std::time::Instant::now() + std::time::Duration::from_secs(30); + while crate::readjob::land(&mut app) { + assert!(std::time::Instant::now() < end, "grid script never landed"); + std::thread::sleep(std::time::Duration::from_millis(5)); // test-wait: await the script worker's real frame delivery + } + assert!(app.workflow.script_started.is_empty()); + let run = app.workflow.script_last.as_ref().unwrap(); + assert!(run.error.is_none(), "{:?}", app.workflow.script_out); + assert_eq!(run.ran.len(), 1); + assert_eq!(serde_json::from_value::(run.ran[0].result.clone()).unwrap(), expected); + assert!(app.workflow.script_out.iter().all(|(_, error)| !error)); + } + } + /// A Script pane run works its tools out on a thread of its own: the results say Running… /// until it lands, runs land in the order asked, and one cancelled or left says so. #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index 27963f5c40..b4ccf0cd12 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -31,7 +31,15 @@ pub(crate) fn sync(app: &mut RustGisApp) { let context = (app.session.opened, app.session.map().instance_id()); if app.annotation.context != Some(context) { cancel(app); - app.annotation = State { context: Some(context), next: app.annotation.next, ..State::default() }; + let pixels = app.annotation.pixels; + let error = pixels.then(|| { + tl!("The project or map changed. Choose an image again or select Web Mercator grid metres before drawing.").to_string() + }); + app.annotation = + State { context: Some(context), next: app.annotation.next, pixels, image_chosen: pixels, error, ..State::default() }; + if let Some(error) = &app.annotation.error { + app.session.notify(error.clone(), true); + } } else { if let Some(name) = app.annotation.image.and_then(|id| app.session.map().layer(id)).filter(|l| l.raster.is_some()).map(|l| &l.name) && app.annotation.image_name.as_deref() != Some(name.as_str()) @@ -141,6 +149,9 @@ pub(crate) fn drawing_frame(app: &RustGisApp) -> Result { if app.annotation.context != Some((app.session.opened, app.session.map().instance_id())) { return Err(tl!("The image context changed. Select the image again before drawing.").into()); } + if app.annotation.image.is_none() { + return Err(tl!("Choose an image or Web Mercator grid metres before drawing.").into()); + } if !app.annotation.source.as_ref().is_some_and(|s| s.current(&app.session)) { return Err(tl!("The image changed. Reload its grid before drawing.").into()); } @@ -575,7 +586,7 @@ mod tests { assert!(drawing_frame(&app).is_err()); app.run("project.new", json!({})).unwrap(); assert!(app.annotation.image.is_none()); - assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); + assert!(drawing_frame(&app).is_err()); } #[test] fn annotation_grid_stale_pixel_context_never_substitutes_map_metres() { @@ -598,6 +609,48 @@ mod tests { } } + #[test] + fn annotation_grid_real_context_switch_preserves_pixels_and_requires_explicit_recovery() { + for command in ["project.new", "map.new"] { + for pixels in [false, true] { + let (mut app, image, _, _) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("ui.annotationGrid", json!({"pixels":pixels})).unwrap(); + let old = app.annotation.context; + let next = app.annotation.next; + let old_request = next; + let mark = app.session.notice_mark(); + app.run(command, json!({"name":"Other"})).unwrap(); + app.logic(&egui::Context::default()); + assert_ne!(app.annotation.context, old); + assert_eq!(app.annotation.pixels, pixels); + assert!(app.annotation.image.is_none() && app.annotation.source.is_none() && app.annotation.frame.is_none()); + assert!(app.annotation.pending.is_none()); + assert_eq!(app.annotation.next, next); + if pixels { + assert!(drawing_frame(&app).unwrap_err().contains("Choose an image")); + assert!(app.annotation.error.as_ref().unwrap().contains("project or map changed")); + assert_eq!(app.session.notices_since(mark).iter().filter(|n| n.text.contains("project or map changed")).count(), 1); + landed(&mut app, old_request, Landed::Ran(Ok(serde_json::to_value(Frame::default()).unwrap()))); + assert!(drawing_frame(&app).is_err(), "late completions must not repair another context"); + open(&mut app).unwrap(); + assert!(drawing_frame(&app).is_err(), "opening the pane must not clear the refusal"); + app.run("ui.annotationGrid", json!({"pixels":false})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); + let original = self::app(); + let raster = original.0.session.map().layer(original.1).unwrap().raster.clone().unwrap(); + let image = app.session.map_mut().add_raster("New image", raster); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + assert_eq!(drawing_frame(&app).unwrap(), original.3); + } else { + assert_eq!(drawing_frame(&app).unwrap(), Frame::default()); + assert!(app.annotation.error.is_none()); + assert!(!app.session.notices_since(mark).iter().any(|n| n.text.contains("project or map changed"))); + } + } + } + } + #[test] fn annotation_grid_open_keeps_automatic_selection_errors_inline() { use egui_kittest::kittest::Queryable; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index c13116c761..60412c9efb 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2805,14 +2805,15 @@ impl RustGisApp { if id == "source.syncState" { return readjob::run_then(self, "source.syncState", params, Some(sync_state_said)); } - // Counting a column goes over every row of the layer: on a thread of its own, and the - // answer shown once it lands (the table's Statistics window). + // Direct desktop callers receive a queue acknowledgement; callbacks receive the frame. + // Script lines use engine dispatch and consume their prepared worker results instead. if id == "raster.imageGrid" { return readjob::run(self, "raster.imageGrid", params); } if id == "edit.previewShapeConversion" { return readjob::run_shape_conversion(self, params); } + // Counting a column goes over every row: the Statistics window shows the landed answer. if id == "table.statistics" { return readjob::run_then(self, "table.statistics", params, Some(table::statistics_landed)); } diff --git a/crates/ui-egui/src/readjob.rs b/crates/ui-egui/src/readjob.rs index 948e503e8d..57955661b2 100644 --- a/crates/ui-egui/src/readjob.rs +++ b/crates/ui-egui/src/readjob.rs @@ -24,6 +24,9 @@ pub struct Reads { job: Option, /// The commands waiting for the read before, in order. waiting: VecDeque, + /// Scoped cancellations deliver their callbacks on the next landing pass, after the UI + /// has replaced the old request, rather than dropping waiting completions silently. + cancelled: VecDeque, #[cfg(test)] fail_spawn: bool, } @@ -53,7 +56,14 @@ impl Reads { } job.control.cancel(); } - self.waiting.retain(|a| !belongs(a)); + let waiting = std::mem::take(&mut self.waiting); + for asked in waiting { + if belongs(&asked) { + self.cancelled.push_back(asked); + } else { + self.waiting.push_back(asked); + } + } } } @@ -404,7 +414,7 @@ fn file_name(path: &str) -> &str { /// Whether a read is under way or waiting. fn busy(app: &RustGisApp) -> bool { - app.reads.job.is_some() || !app.reads.waiting.is_empty() + app.reads.job.is_some() || !app.reads.waiting.is_empty() || !app.reads.cancelled.is_empty() } /// Why a command asked for can't run now: what was open when it was asked isn't any more. @@ -584,6 +594,12 @@ fn complete_here(app: &mut RustGisApp, asked: Asked, p: Value, done: Result bool { + for asked in std::mem::take(&mut app.reads.cancelled) { + let what = called(asked.command, &asked.what); + let why = crate::i18n::fmt("{what} was cancelled before starting", &[("what", what)]); + app.session.notify(why.clone(), false); + not_run_asked(app, &asked, why); + } if !busy(app) { return false; } @@ -678,7 +694,7 @@ pub fn land(app: &mut RustGisApp) -> bool { pub fn stop(app: &mut RustGisApp, wait: std::time::Duration) { let why = tl!("RustGIS is closing").to_string(); let job = app.reads.job.take(); - let waiting: Vec = app.reads.waiting.drain(..).collect(); + let waiting: Vec = app.reads.cancelled.drain(..).chain(app.reads.waiting.drain(..)).collect(); if let Some(job) = job { job.control.cancel(); let end = std::time::Instant::now() + wait; @@ -807,6 +823,33 @@ mod tests { assert!(crate::annotation::drawing_frame(&app).is_err()); } + #[test] + fn annotation_grid_scoped_cancel_delivers_waiting_callbacks_once_and_keeps_other_reads() { + let mut app = RustGisApp::new(rustgis_engine::Session::new(rustgis_engine::Project::default())); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("points.csv"); + std::fs::write(&path, "name,lon,lat\nA,1,2\n").unwrap(); + app.reads_off_frame = true; + fn cancelled(app: &mut RustGisApp, result: Landed) { + let Landed::NotRun(why) = result else { panic!("cancelled waiting read must not run") }; + assert!(why.contains("cancelled before starting")); + app.session.notify("Waiting callback delivered", false); + } + run(&mut app, "layer.addData", json!({"path":path,"name":"First"})).unwrap(); + run_then(&mut app, "layer.addData", json!({"path":path,"name":"Cancel me"}), Some(cancelled)).unwrap(); + run(&mut app, "layer.addData", json!({"path":path,"name":"Keep me"})).unwrap(); + app.reads.cancel_where(|a| a.params["name"] == "Cancel me"); + app.reads.cancel_where(|a| a.params["name"] == "Cancel me"); + assert_eq!(app.reads.waiting.len(), 1); + landed(&mut app, 60); + assert!(app.session.map().layer_by_name("First").is_some()); + assert!(app.session.map().layer_by_name("Keep me").is_some()); + assert!(app.session.map().layer_by_name("Cancel me").is_none()); + assert_eq!(app.session.notices.iter().filter(|n| n.text == "Waiting callback delivered").count(), 1); + assert!(app.reads.cancelled.is_empty()); + assert!(!land(&mut app)); + } + #[test] fn annotation_grid_worker_failure_never_reads_headers_on_the_frame() { use rustgis_engine::{ diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 8cab9c99a5..e74efdc749 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -192,8 +192,9 @@ time and creation time. Reload after known external edits even without a reporte If revision capture fails, the image can still load for viewing, but grid reads report the original metadata error. Check filesystem metadata support/access, or export the image as a GeoTIFF to another location and add it. Repeatedly reloading the same unsupported filesystem -does not repair its metadata support. Stale project/map bindings in pixel mode refuse drawing -until the image is selected again; only an explicit map-grid choice returns map metres. +does not repair its metadata support. Changing projects or maps clears the old image binding, +keeps pixel mode selected and reports a warning. Drawing stays blocked until you choose an image +again or explicitly choose Web Mercator grid metres; opening the pane does not clear the warning. Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; download remote originals first. In the browser, upload the original GeoTIFF. Other local formats (including PNG, JPEG, ASCII grids and GDAL-converted images) are refused with guidance to export @@ -221,7 +222,11 @@ For CLI, Python, Assistant/MCP and permitted plugins, `raster.imageGrid={"layer" returns the original `epsg`, `width`, `height` and six affine coefficients in the same format accepted by `edit.addBox.box.frame` and `edit.addCircle.circle.frame`. The query is read-only, requires Files permission, and uses the shared read worker on desktop/MCP. Headless synchronous -callers must run file reads off their own UI thread. +callers must run file reads off their own UI thread. The desktop Script pane prepares grid queries +on its worker and shows the actual frame in each line's result, rather than a queue acknowledgement. +Queries may follow replayable selection/edit steps, but file-opening or project-changing setup +must run in a separate script first. A changed image invalidates the prepared result instead of +reopening its file on the UI thread. Scoped callers should use the individually approved query. ### Rotated boxes From d2ebb4a2344103cf7d0e8f0da1031af728267fef Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 15:25:40 +0000 Subject: [PATCH 10/14] Clarify GeoTIFF load recovery and surface refused shape gestures --- crates/data/src/raster/geotiff.rs | 57 ++++++++++++++++++++++++++++--- crates/ui-egui/src/annotation.rs | 48 ++++++++++++++++++++++++++ crates/ui-egui/src/boxedit.rs | 3 +- crates/ui-egui/src/circleedit.rs | 3 +- crates/ui-egui/src/lib.rs | 14 +++++--- docs/user-guide/editing.md | 45 +++++++++++++----------- 6 files changed, 140 insertions(+), 30 deletions(-) diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 3ec7e886ab..3024509c18 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1441,6 +1441,13 @@ pub(super) fn read_file_with_nodata(path: &std::path::Path, name: &str, sidecar_ read_file_with_revision(path, name, sidecar_nodata, file, revision) } +#[cfg(not(target_arch = "wasm32"))] +fn load_revision_error(reason: impl std::fmt::Display) -> String { + format!( + "{reason}. Wait until the file is stable, or copy it to a stable local location. Retry Add Data; for an existing layer, retry Reload Data." + ) +} + #[cfg(not(target_arch = "wasm32"))] fn read_file_with_revision( path: &std::path::Path, @@ -1453,19 +1460,27 @@ fn read_file_with_revision( let open = || -> std::io::Result> { let file = crate::provenance::open(path)?; if let Ok(original) = &revision { - let current = super::file_revision::FileRevision::capture(path, &file.metadata()?)?; + let current = file + .metadata() + .and_then(|metadata| super::file_revision::FileRevision::capture(path, &metadata)) + .map_err(|e| std::io::Error::other(load_revision_error(format!("Could not verify the image file while reading: {e}"))))?; if *original != current { - return Err(std::io::Error::other("the image file changed while its pixels were read; reload it")); + return Err(std::io::Error::other(load_revision_error("The image file changed while its pixels were read"))); } } reader(std::io::BufReader::new(file)) }; let mut raster = read_limited_with_nodata(reader(std::io::BufReader::new(file)).map_err(err)?, name, max_samples(), Some(&open), sidecar_nodata)?; - raster.source_file = Some(revision); - if raster.source_file.as_ref().is_some_and(|revision| revision.is_ok()) { - raster.check_original_file(path).map_err(err)?; + if let Ok(original) = &revision { + let current = std::fs::metadata(path) + .and_then(|metadata| super::file_revision::FileRevision::capture(path, &metadata)) + .map_err(|e| err(load_revision_error(format!("Could not verify the image file after reading: {e}"))))?; + if *original != current { + return Err(err(load_revision_error("The image file changed while its pixels were read"))); + } } + raster.source_file = Some(revision); Ok(raster) } @@ -2168,6 +2183,38 @@ mod tests { assert!(error.contains("GeoTIFF:"), "{error}"); } + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn annotation_grid_load_change_and_missing_path_refuse_with_add_data_recovery() { + use std::io::Write; + for removed in [false, true] { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("image.tif"); + let pixels = small(); + let bytes = write(&pixels, 0).unwrap(); + std::fs::write(&path, &bytes).unwrap(); + let file = std::fs::File::open(&path).unwrap(); + let revision = super::super::file_revision::FileRevision::capture(&path, &file.metadata().unwrap()).unwrap(); + // Inject a change between the initial source snapshot and the final load check, + // without timers or filesystem races. The already-open TIFF remains decodable. + if removed { + std::fs::remove_file(&path).unwrap(); + } else { + std::fs::OpenOptions::new().append(true).open(&path).unwrap().write_all(&[0]).unwrap(); + } + let error = read_file_with_revision(&path, "image", None, file, Ok(revision)).unwrap_err().to_string(); + assert!(error.contains("GeoTIFF:") && error.contains("Retry Add Data"), "{error}"); + assert!(error.contains("stable local location") && error.contains("for an existing layer, retry Reload Data"), "{error}"); + assert!(!error.contains("Use Reload Data, then retry the operation"), "{error}"); + assert!(error.contains(if removed { "Could not verify" } else { "file changed" }), "{error}"); + // Once stable, an ordinary Add Data read succeeds and has a verified grid revision. + std::fs::write(&path, &bytes).unwrap(); + let loaded = read_file(&path, "image").unwrap(); + assert_eq!(loaded.bands[0].data, pixels.bands[0].data); + loaded.check_original_file(&path).unwrap(); + } + } + #[cfg(not(target_arch = "wasm32"))] #[test] fn annotation_grid_capture_failure_preserves_pixels_and_reports_the_original_cause() { diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index b4ccf0cd12..3cb05c7fc8 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -796,6 +796,54 @@ mod tests { h.run(); assert_eq!(h.state().session.tool, Tool::Circle); } + #[test] + fn annotation_grid_refused_gestures_notify_with_shape_details_collapsed() { + use egui_kittest::kittest::Queryable; + for tool in [Tool::Circle, Tool::RotatedBox] { + let (mut app, image, layer, frame) = app(); + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("ui.pane.annotation", Value::Null).unwrap(); + app.run("tool.set", json!({"tool":tool.code()})).unwrap(); + // A pending lookup has no frame yet; its failure must be visible outside details. + app.annotation.frame = None; + app.annotation.error = None; + app.annotation.pending = Some(app.annotation.next); + let depth = app.session.undo_depth(); + let mark = app.session.notice_mark(); + let origin = screen(&app, &frame, [32., 32.]); + let accepted = if tool == Tool::Circle { + crate::circleedit::drag_start(&mut app, rect(), origin, true) + } else { + crate::boxedit::drag_start(&mut app, rect(), origin, true) + }; + assert!(!accepted); + let error = "Wait for the original image grid before drawing."; + assert_eq!(app.session.notices_since(mark).iter().filter(|n| n.error && n.text == error).count(), 1); + assert_eq!(app.session.undo_depth(), depth); + assert!(app.session.map().layer(layer).unwrap().dataset.features.is_empty()); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1366., 900.)).with_max_steps(1000).build_ui_state( + |ui, app: &mut RustGisApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + app, + ); + // Pending reads keep the spinner repainting; inspect a frame rather than await idle. + h.run_steps(1); + assert!(h.query_by_label("Shape details").is_some()); + assert!(h.query_by_label("Radius").is_none() && h.query_by_label("Width").is_none()); + assert!(h.query_all_by_label(error).next().is_some(), "refusal is visible without expanding details"); + let app = h.state_mut(); + app.annotation.pending = None; + app.annotation.frame = Some(frame.clone()); + let mark = app.session.notice_mark(); + drag(app, &frame, [32., 32.], [64., 64.], true); + assert_eq!(app.session.map().layer(layer).unwrap().dataset.features.len(), 1); + assert!(!app.session.notices_since(mark).iter().any(|n| n.error), "valid recovery must not produce a refusal"); + } + } + #[test] fn annotation_shape_details_start_hidden_and_toggle_without_changing_the_object() { use egui_kittest::kittest::Queryable; diff --git a/crates/ui-egui/src/boxedit.rs b/crates/ui-egui/src/boxedit.rs index 43f9a64bf0..9bd39916d7 100644 --- a/crates/ui-egui/src/boxedit.rs +++ b/crates/ui-egui/src/boxedit.rs @@ -147,7 +147,8 @@ pub(crate) fn drag_start(app: &mut RustGisApp, rect: Rect, origin: Pos2, force_n let frame = match crate::annotation::drawing_frame(app) { Ok(frame) => frame, Err(error) => { - app.boxes.error = Some(error); + app.boxes.error = Some(error.clone()); + app.session.notify(error, true); return false; } }; diff --git a/crates/ui-egui/src/circleedit.rs b/crates/ui-egui/src/circleedit.rs index 736fec713b..681243eacd 100644 --- a/crates/ui-egui/src/circleedit.rs +++ b/crates/ui-egui/src/circleedit.rs @@ -115,7 +115,8 @@ pub(crate) fn drag_start(app: &mut RustGisApp, rect: Rect, origin: Pos2, force_n let frame = match crate::annotation::drawing_frame(app) { Ok(frame) => frame, Err(error) => { - app.circles.error = Some(error); + app.circles.error = Some(error.clone()); + app.session.notify(error, true); return false; } }; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 60412c9efb..16df9049cc 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3410,8 +3410,13 @@ impl RustGisApp { } } "ui.annotationImage" => annotation::select_image(self, p)?, - "ui.annotationFilename" => annotation::filename_option(self, p.get("on").and_then(Value::as_bool).ok_or("on is required")?), - "ui.annotationGrid" => annotation::select_grid(self, p.get("pixels").and_then(Value::as_bool).ok_or("pixels is required")?)?, + "ui.annotationFilename" => { + annotation::filename_option(self, p.get("on").and_then(Value::as_bool).ok_or_else(|| tl!("on is required").to_string())?) + } + "ui.annotationGrid" => annotation::select_grid( + self, + p.get("pixels").and_then(Value::as_bool).ok_or_else(|| tl!("pixels is required").to_string())?, + )?, "ui.annotationCancel" => annotation::cancel_read(self), "ui.annotationLayer" => annotation::select_layer(self, p)?, "ui.pane.editing" => toggle_right(&mut self.ui, RightPane::Editing), @@ -4873,7 +4878,8 @@ impl RustGisApp { // Any format the dialog offers here (`dialogs::export_formats`): not GeoPackage or // FlatGeobuf, which the data crate refuses in the browser. let ext = p.get("format").and_then(Value::as_str).unwrap_or("geojson").to_ascii_lowercase(); - let name = annotation::export_name(self, id, if ext == "shp" { "zip" } else { &ext }, annotation_export)?; + let download_ext = if ext == "shp" { "zip".to_string() } else { ext.clone() }; + let name = annotation::export_name(self, id, &download_ext, annotation_export)?; rustgis_engine::cmd::share::gpkg_table(p, &ext)?; // A streamed layer exports its whole source, not the features read for the view. let (whole, lost) = if l.is_streamed() { @@ -4894,7 +4900,7 @@ impl RustGisApp { return Err(e); } }; - let ext = if ext == "shp" { "zip".to_string() } else { ext }; + let ext = download_ext; // The features written, read whole, give their own extent (not a raster's, or the // one a streamed layer's source declares). let mut notes = rustgis_engine::data::export_notes([(ds, ds.extent())], &name, crs); diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index e74efdc749..9e960eae81 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -181,24 +181,14 @@ switching to Web Mercator or using **Cancel Grid Read** stops only that lookup a unrelated queued work. The status-bar Cancel retains its queue-wide behaviour. Pixel-based drawing waits for a valid grid; a failed or cancelled read never silently substitutes map metres. After changing a source file externally, reload the image layer first, then use **Reload Grid**. -New grid reads from a linked GeoTIFF require loaded pixels and a recorded filesystem-metadata -revision. A detected metadata mismatch, or an unverifiable revision, refuses the read rather -than pairing an unchecked header with the displayed image. An already-read -grid stays aligned with the pixels retained in the session. -Detection compares filesystem metadata, not file contents: length and modification time, plus -device/inode/change time on Unix or creation time on Windows. Rewrites preserving every compared -field go undetected; on Windows this includes in-place rewrites preserving length, modification -time and creation time. Reload after known external edits even without a reported change. -If revision capture fails, the image can still load for viewing, but grid reads report the -original metadata error. Check filesystem metadata support/access, or export the image as a -GeoTIFF to another location and add it. Repeatedly reloading the same unsupported filesystem -does not repair its metadata support. Changing projects or maps clears the old image binding, -keeps pixel mode selected and reports a warning. Drawing stays blocked until you choose an image -again or explicitly choose Web Mercator grid metres; opening the pane does not clear the warning. +Original-pixel drawing and conversion share the same [source checks](#image-source-checks). Removing/replacing an image invalidates its cached grid. Local and embedded GeoTIFFs are supported; download remote originals first. In the browser, upload the original GeoTIFF. Other local formats (including PNG, JPEG, ASCII grids and GDAL-converted images) are refused with guidance to export the image as a GeoTIFF and add that file; reloading the unsupported source cannot supply its grid. +Changing projects or maps clears the old image binding, keeps pixel mode selected and reports a +warning. Drawing stays blocked until you choose an image again or explicitly choose Web Mercator +grid metres; opening the pane does not clear the warning. An image removed from the map keeps its last name as **Unavailable image**. Pixel drawing stays blocked and **Reload Grid** is disabled until the image is restored or another image is chosen. Choose **No image** to return explicitly to Web Mercator grid metres. @@ -208,7 +198,8 @@ EPSG coordinate system in a notice, while keeping the Annotate pane open. Space or middle-drag pans; releasing Space keeps the drawing tool. Use the selected object's existing handles, Undo/Redo, and **Export GeoJSON…**. **Shape details** is collapsed by default; expand it for numeric dimensions and additional editing controls. Map handles remain available -while the section is hidden. Exports contain +while the section is hidden. Refused drawing gestures show an error notification even when +**Shape details** is collapsed. Exports contain ordinary geographic polygons and feature properties; circles are polygon approximations. Check **Use image filename** to suggest `img1.geojson` for an image named `img1.tiff`. The preview shows the suggested basename; the save dialog still chooses the folder and lets @@ -228,6 +219,24 @@ Queries may follow replayable selection/edit steps, but file-opening or project- must run in a separate script first. A changed image invalidates the prepared result instead of reopening its file on the UI thread. Scoped callers should use the individually approved query. +### Image source checks + +Local GeoTIFF loading records the file revision with its decoded pixels. If a verified file changes +or becomes unverifiable during decoding, **Add Data** and **Reload Data** refuse that load rather +than accepting potentially inconsistent pixels. Wait for writes/cloud synchronization to finish, +or copy the image to a stable local location; then retry **Add Data**, or **Reload Data** for an +existing layer. This load-time check also applies outside annotation workflows. + +Grid reads require loaded pixels and a matching verified revision. After known external edits, +reload the image layer and retry the operation. Detection compares metadata, not content hashes: +length and modification time, plus device/inode/change time on Unix or creation time on Windows. +Rewrites preserving every compared field go undetected, so reload after known edits even without +a warning. A cached grid remains aligned with the pixels retained in the session. + +If the initial revision capture fails, viewing can still load, but grid reads report the original +metadata error. Check filesystem metadata support/access, or export the image as a GeoTIFF to +another location and add it; repeated reloads cannot repair unsupported metadata. + ### Rotated boxes Choose a loaded, visible polygon layer, turn **Edit › Edit** on, and select **Edit › Features › @@ -502,10 +511,8 @@ keep their current selection when opening the map menu. display overview. Local GeoTIFFs and embedded/browser-uploaded GeoTIFFs are supported; remote original grids must be downloaded and added first. An unreadable or unsupported source gives an error, without falling back to a different grid. - A local image must have loaded pixels and a verified file revision matching those pixels; - conversion does not read an unchecked header when pixels are missing or their revision is - unknown. If asked to reload, use **Reload Data** on the image, then retry **Preview Conversion** - or **Convert Now**. The same metadata-detection limits described above apply here. + The same [image source checks](#image-source-checks) apply. If asked to reload, use + **Reload Data** on the image, then retry **Preview Conversion** or **Convert Now**. 2. For a quick conversion, click **Convert Now** to fit and apply using the chosen shape and grid, without a preview or another confirmation. Alternatively, click **Preview Conversion** to show an orange outline without changing the feature or undo history, review or adjust the From 8d155ca415405c320e58484ea4df86a5f567ea9c Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 15:49:56 +0000 Subject: [PATCH 11/14] Bind scripted grid queries to accepted raster tool outputs --- crates/engine/src/cmd/gp.rs | 65 ++++++++-- crates/engine/src/image_grid.rs | 213 +++++++++++++++++++++++++++++++- docs/user-guide/editing.md | 10 +- 3 files changed, 268 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 8de48aff8c..819f917836 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -801,6 +801,8 @@ pub(crate) struct ToolAhead { result: Result, full_ids: FullIds, millis: u64, + /// Runtime identities of successfully installed outputs on the worker, in return order. + grid_outputs: Vec, } #[cfg(not(target_arch = "wasm32"))] @@ -873,7 +875,7 @@ pub(crate) struct RanAheads { pub(crate) fn taking(s: &mut Session, ran: RanAheads, run: impl FnOnce(&mut Session) -> T) -> T { s.tools_ahead = Some(Ahead::Taking(ran.tools.into())); s.files_ahead = Some(FilesAhead::Taking(ran.files.into())); - s.grids_ahead = Some(crate::image_grid::Ahead::Taking(ran.grids.into())); + s.grids_ahead = Some(crate::image_grid::Ahead::taking(ran.grids)); s.zones_ahead = Some(super::imagery::ZonesAhead::Taking(ran.zones.into())); let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(s))); s.tools_ahead = None; @@ -929,15 +931,19 @@ fn read_batch_file(s: &mut Session, path: &std::path::Path) -> Result<(Dataset, #[cfg(not(target_arch = "wasm32"))] /// The next tool run ahead, if it is this one on the same inputs: its result, the streamed rows /// it read whole and how long it took. Any other drops the rest, which came after it. -fn take_ahead( - s: &mut Session, - pin: &crate::policy::ToolPin, - params: &Value, -) -> Option<(Result, FullIds, u64)> { - let Some(Ahead::Taking(ran)) = &mut s.tools_ahead else { return None }; - let next = ran.pop_front()?; +fn take_ahead(s: &mut Session, pin: &crate::policy::ToolPin, params: &Value) -> Option> { + let Some(Ahead::Taking(ran)) = &mut s.tools_ahead else { + crate::image_grid::invalidate_script_grids(s); + return None; + }; + let Some(next) = ran.pop_front() else { + s.tools_ahead = None; + crate::image_grid::invalidate_script_grids(s); + return None; + }; let RanAhead::Tool(next) = next else { s.tools_ahead = None; + crate::image_grid::invalidate_script_grids(s); return None; }; let now = Snapshot::of(s); @@ -947,9 +953,10 @@ fn take_ahead( { s.tools_taken += 1; } - return Some((next.result, next.full_ids, next.millis)); + return Some(next); } s.tools_ahead = None; + crate::image_grid::invalidate_script_grids(s); None } @@ -2540,7 +2547,20 @@ pub(crate) fn execute( } let continuation = s.tool_continuation(&op)?; #[cfg(not(target_arch = "wasm32"))] - let ran = take_ahead(s, pin, params); + let output_slot = match &s.tools_ahead { + Some(Ahead::Recording(recorded)) => Some(recorded.len()), + _ => None, + }; + #[cfg(not(target_arch = "wasm32"))] + let mut ran = take_ahead(s, pin, params); + #[cfg(not(target_arch = "wasm32"))] + if crate::image_grid::script_grids_invalidated(s) { + return Err("the script inputs changed; run the script again".into()); + } + #[cfg(not(target_arch = "wasm32"))] + let replayed_outputs = ran.as_mut().map(|tool| std::mem::take(&mut tool.grid_outputs)); + #[cfg(not(target_arch = "wasm32"))] + let ran = ran.map(|tool| (tool.result, tool.full_ids, tool.millis)); #[cfg(target_arch = "wasm32")] let ran = None; let (mut res, ids, millis) = match ran { @@ -2590,6 +2610,7 @@ pub(crate) fn execute( result: res.clone(), full_ids: ids.clone(), millis, + grid_outputs: Vec::new(), }))); } (res, ids, millis) @@ -2598,7 +2619,29 @@ pub(crate) fn execute( pin.current()?; continuation.check()?; fit_to_windows(s, &mut res, &ids); - apply_with(s, run_of(t0), kept(), res, millis, opts) + let applied = apply_with(s, run_of(t0), kept(), res, millis, opts); + #[cfg(not(target_arch = "wasm32"))] + if let Ok((out, _)) = &applied + && (recording || replayed_outputs.is_some()) + { + let outputs = output_layers(s, out); + if let Some(recorded) = replayed_outputs { + crate::image_grid::replay_tool_outputs(s, &recorded, &outputs)?; + } else if let Some(index) = output_slot + && let Some(Ahead::Recording(recorded)) = &mut s.tools_ahead + && let Some(RanAhead::Tool(tool)) = recorded.get_mut(index) + { + tool.grid_outputs = outputs; + } + } + applied +} + +/// The exact layers just installed by a tool, rather than similarly named layers in the map. +#[cfg(not(target_arch = "wasm32"))] +fn output_layers(s: &Session, out: &Value) -> Vec { + let ids = out.get("layer").into_iter().chain(out.get("layers").and_then(Value::as_array).into_iter().flatten()); + ids.filter_map(Value::as_u64).filter_map(|id| u32::try_from(id).ok()).filter_map(|id| s.map().layer(id).cloned()).collect() } /// How [`execute`] applies a run: by default as `gp.run` does. A batch item keeps the diff --git a/crates/engine/src/image_grid.rs b/crates/engine/src/image_grid.rs index ccaa551526..4667b662a2 100644 --- a/crates/engine/src/image_grid.rs +++ b/crates/engine/src/image_grid.rs @@ -25,9 +25,22 @@ fn same_arc(a: &Option>, b: &Option>) -> bool { } impl Source { pub fn capture(s: &Session, layer: u32) -> Result { - let l = s.map().layer(layer).ok_or("no image layer")?; - let raster = l.raster.clone().ok_or("choose a GeoTIFF image layer")?; - Ok(Self { map: s.map().instance_id(), layer, instance: l.instance_id(), replacement: l.replacement_revision(), raster }) + Self::from_layer(s.map().instance_id(), s.map().layer(layer).ok_or("no image layer")?) + } + fn from_layer(map: u64, layer: &rustgis_map::Layer) -> Result { + let raster = layer.raster.clone().ok_or("choose a GeoTIFF image layer")?; + Ok(Self { map, layer: layer.id, instance: layer.instance_id(), replacement: layer.replacement_revision(), raster }) + } + #[cfg(not(target_arch = "wasm32"))] + fn key(&self) -> (u64, u32, u64, u64) { + (self.map, self.layer, self.instance, self.replacement) + } + #[cfg(not(target_arch = "wasm32"))] + fn same(&self, other: &Self) -> bool { + self.key() == other.key() + && self.raster.path == other.raster.path + && same_arc(&self.raster.embedded, &other.raster.embedded) + && same_arc(&self.raster.data, &other.raster.data) } pub fn current(&self, s: &Session) -> bool { s.map().instance_id() == self.map @@ -93,7 +106,71 @@ pub(crate) struct Prepared { #[cfg(not(target_arch = "wasm32"))] pub(crate) enum Ahead { Recording(Vec), - Taking(std::collections::VecDeque), + Taking(Replay), +} +#[cfg(not(target_arch = "wasm32"))] +pub(crate) struct Replay { + grids: std::collections::VecDeque, + // Only exact outputs of accepted tool results may bridge worker/live layer instances. + outputs: std::collections::HashMap<(u64, u32, u64, u64), (Source, Source)>, + invalidated: bool, +} +#[cfg(not(target_arch = "wasm32"))] +impl Ahead { + pub(crate) fn taking(grids: Vec) -> Self { + Self::Taking(Replay { grids: grids.into(), outputs: Default::default(), invalidated: false }) + } +} +/// A rejected tool invalidates all dependent queries, including queries of existing images. +/// Keep an empty, invalid replay rather than letting a later query read on the UI thread. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn invalidate_script_grids(s: &mut Session) { + if let Some(Ahead::Taking(replay)) = &mut s.grids_ahead { + replay.invalidated |= !replay.grids.is_empty(); + replay.grids.clear(); + replay.outputs.clear(); + } +} +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn script_grids_invalidated(s: &Session) -> bool { + matches!(&s.grids_ahead, Some(Ahead::Taking(replay)) if replay.invalidated) +} +/// Called only after an admitted, snapshot-validated tool result has been applied successfully. +/// Its newly created raster layers keep live IDs/style, but share the accepted worker pixels, +/// embedded bytes and placeholder rows. Following tool snapshots can then consume that data too. +/// Bind queries by output identity, never by a name or a layer ID alone; no headers are reread. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn replay_tool_outputs(s: &mut Session, recorded: &[rustgis_map::Layer], landed: &[rustgis_map::Layer]) -> Result<(), String> { + if !matches!(&s.grids_ahead, Some(Ahead::Taking(replay)) if !replay.grids.is_empty()) { + return Ok(()); + } + if recorded.len() != landed.len() + || recorded.iter().zip(landed).any(|(old, new)| { + old.name != new.name + || match (&old.raster, &new.raster) { + (None, None) => false, + (Some(a), Some(b)) => a.path != b.path || a.file_name != b.file_name || a.placement != b.placement, + _ => true, + } + }) + { + invalidate_script_grids(s); + return Err("the script's tool outputs changed; run the script again".into()); + } + let map = s.map().instance_id(); + for (old, new) in recorded.iter().zip(landed).filter(|(old, _)| old.raster.is_some()) { + let from = Source::from_layer(map, old)?; + let layer = s.map_mut().layer_mut(new.id).ok_or("the script's output layer disappeared")?; + let raster = layer.raster.as_mut().ok_or("the script's raster output disappeared")?; + raster.data.clone_from(&from.raster.data); + raster.embedded.clone_from(&from.raster.embedded); + layer.dataset.features.clone_from(&old.dataset.features); + let to = Source::capture(s, new.id)?; + if let Some(Ahead::Taking(replay)) = &mut s.grids_ahead { + replay.outputs.insert(from.key(), (from, to)); + } + } + Ok(()) } pub(crate) fn ahead(s: &Session, p: &Value) -> Option { let source = Source::capture(s, s.layer_id(p).ok()?).ok()?; @@ -108,8 +185,18 @@ pub(crate) fn query(s: &mut Session, p: &Value) -> CmdResult { let source = Source::capture(s, s.layer_id(p)?)?; let ready = crate::cmd::reads::take::(s, "imageGrid"); #[cfg(not(target_arch = "wasm32"))] - let ready = if let Some(Ahead::Taking(grids)) = &mut s.grids_ahead { - Some(grids.pop_front().ok_or("the script's image grid was not prepared; run the query separately")?) + let ready = if let Some(Ahead::Taking(replay)) = &mut s.grids_ahead { + if replay.invalidated { + return Err("the script inputs changed; run the script again".into()); + } + let mut prepared = replay.grids.pop_front().ok_or("the script's image grid was not prepared; run the query separately")?; + if let Some((from, to)) = replay.outputs.get(&prepared.source.key()) { + if !prepared.source.same(from) { + return Err("the script's image source changed; run the script again".into()); + } + prepared.source = to.clone(); + } + Some(prepared) } else { ready }; @@ -393,6 +480,120 @@ mod tests { assert!(inputs[0].sha256.is_none()); } + #[cfg(not(target_arch = "wasm32"))] + fn interpolation_session() -> (Session, u32) { + use rustgis_data::{Dataset, Field, FieldType, Geometry, GeometryType, Value as Cell}; + let (mut s, _, _) = session(); + let mut samples = Dataset::new("Samples", GeometryType::Point, vec![Field::new("v", FieldType::Double)]); + samples.crs = Some(Crs::LonLat); + for (x, y, value) in [(0., 0., 1.), (2., 0., 2.), (0., 2., 3.), (2., 2., 4.)] { + samples.push(Some(Geometry::Point(rustgis_geom::Coord::new(x, y))), vec![Cell::Float(value)]); + } + let id = s.map_mut().add_dataset(samples); + (s, id) + } + #[cfg(not(target_arch = "wasm32"))] + fn interpolation_line(tool: &str, cell: f64) -> String { + format!( + "gp.run {}", + json!({"tool":tool,"params":{"input":"Samples","field":"v","extent":"0,0,2,2","pixelSize":cell,"output":"Surface"}}) + ) + } + #[cfg(not(target_arch = "wasm32"))] + fn grid_line(layer: &str) -> String { + format!("raster.imageGrid {}", json!({"layer":layer})) + } + + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn annotation_grid_script_replays_generated_rasters_and_every_queued_query() { + use rustgis_map::raster::Resampling; + for multi_output in [false, true] { + let (lines, indices, taken) = if multi_output { + ( + vec![ + interpolation_line("kriging", 0.5), + grid_line("Surface"), + grid_line("Surface_variance"), + grid_line("Surface"), + grid_line("Surface_variance"), + ], + vec![1, 2, 3, 4], + 1, + ) + } else { + ( + vec![ + interpolation_line("idwInterpolation", 0.5), + grid_line("Surface"), + grid_line("Image"), + grid_line("Surface"), + interpolation_line("idwInterpolation", 1.), + grid_line("Surface (2)"), + grid_line("Surface"), + grid_line("Image"), + ], + vec![1, 2, 3, 5, 6, 7], + 2, + ) + }; + let params = json!({"script":lines.join("\n")}); + let (mut direct, _) = interpolation_session(); + let expected = direct.run("script.run", ¶ms).unwrap(); + let (mut queued, _) = interpolation_session(); + queued.statistics_need_read_ahead(true); + let ahead = crate::cmd::reads::read_ahead(&mut queued, "script.run", ¶ms).unwrap(); + let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); + // Worker data identity must not replace the user's current display defaults. + queued.raster_defaults.resampling = Resampling::Bilinear; + let result = crate::cmd::reads::read_land(&mut queued, done).unwrap(); + assert_eq!(queued.tools_taken, taken, "later tools must take their worker result too"); + for index in indices { + let actual: Frame = serde_json::from_value(result["ran"][index]["result"].clone()).unwrap(); + let expected: Frame = serde_json::from_value(expected["ran"][index]["result"].clone()).unwrap(); + assert_eq!(actual, expected, "grid at script line {}", index + 1); + } + for name in if multi_output { ["Surface", "Surface_variance"] } else { ["Surface", "Surface (2)"] } { + let output = queued.map().layer_by_name(name).unwrap(); + assert_eq!(output.raster.as_ref().unwrap().style.resampling, Resampling::Bilinear); + } + assert!(queued.grids_ahead.is_none() && queued.tools_ahead.is_none()); + } + } + + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn annotation_grid_rejected_tool_discards_all_script_grids_and_allows_a_fresh_retry() { + let (mut s, samples) = interpolation_session(); + let script = + [interpolation_line("idwInterpolation", 0.5), grid_line("Surface"), grid_line("Image"), grid_line("Surface")].join("\n"); + let params = json!({"script":script}); + s.statistics_need_read_ahead(true); + let ahead = crate::cmd::reads::read_ahead(&mut s, "script.run", ¶ms).unwrap(); + let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); + s.run("edit.enable", &json!({"on":true})).unwrap(); + s.run("edit.setValue", &json!({"layer":samples,"row":0,"field":"v","value":9})).unwrap(); + let before = (s.undo_depth(), s.map().layers.len()); + let error = crate::cmd::reads::read_land(&mut s, done).unwrap_err(); + assert!(error.contains("script inputs changed"), "{error}"); + assert_eq!(s.tools_taken, 0); + assert_eq!((s.undo_depth(), s.map().layers.len()), before); + assert!(s.map().layer_by_name("Surface").is_none()); + assert!(s.grids_ahead.is_none() && s.tools_ahead.is_none()); + // A new read must prepare every result again, including the existing image's grid. + let ahead = crate::cmd::reads::read_ahead(&mut s, "script.run", ¶ms).unwrap(); + let done = ahead.run(&rustgis_analysis::control::Control::new()).unwrap(); + let answer = crate::cmd::reads::read_land(&mut s, done).unwrap(); + assert_eq!(s.tools_taken, 1); + for (index, name) in [(1, "Surface"), (2, "Image"), (3, "Surface")] { + let layer = s.map().layer_by_name(name).unwrap(); + let expected = read(layer.raster.as_ref().unwrap()).unwrap(); + let frame: Frame = serde_json::from_value(answer["ran"][index]["result"].clone()).unwrap(); + assert_eq!(frame, expected); + } + assert!(s.grids_ahead.is_none() && s.tools_ahead.is_none()); + } + #[test] fn annotation_grid_background_read_cancels_and_rejects_replaced_sources() { let (mut s, id, _) = session(); diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 9e960eae81..07897981a5 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -215,9 +215,13 @@ accepted by `edit.addBox.box.frame` and `edit.addCircle.circle.frame`. The query requires Files permission, and uses the shared read worker on desktop/MCP. Headless synchronous callers must run file reads off their own UI thread. The desktop Script pane prepares grid queries on its worker and shows the actual frame in each line's result, rather than a queue acknowledgement. -Queries may follow replayable selection/edit steps, but file-opening or project-changing setup -must run in a separate script first. A changed image invalidates the prepared result instead of -reopening its file on the UI thread. Scoped callers should use the individually approved query. +Queries may follow replayable selection/edit steps or query embedded rasters produced by +`gp.run` earlier in the same script. Accepted tool outputs retain their prepared raster data; +repeated queries and tools with multiple raster outputs use the matching output's grid without +rereading headers on the UI thread. Live output IDs and display styles are preserved. If a tool's +inputs change while the script is being prepared, all dependent grid results are discarded and +the script must be run again. File-opening or project-changing setup must run in a separate +script first. Scoped callers should use the individually approved query. ### Image source checks From 62c02a1804d146e8f22c09073d51980b69a25f3c Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 16:37:13 +0000 Subject: [PATCH 12/14] Test atomic script rollback and explain cancelled shape previews --- crates/engine/src/cmd/gp.rs | 62 +++++++++++++++++ crates/ui-egui/src/annotation.rs | 115 +++++++++++++++++++++++++------ crates/ui-egui/src/boxedit.rs | 28 +++++++- crates/ui-egui/src/circleedit.rs | 28 +++++++- docs/user-guide/editing.md | 5 +- 5 files changed, 211 insertions(+), 27 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 819f917836..d75a052671 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -6695,3 +6695,65 @@ pub(crate) fn project_input<'a>( pub(crate) fn vector_file_input(spec: &rustgis_analysis::ToolSpec, map: &rustgis_map::Map, name: &str) -> bool { rustgis_analysis::TOOLS.iter().any(|t| std::ptr::eq(t, spec)) && map.layer_by_name(name).is_none() } + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod replay_rollback_tests { + use super::*; + use rustgis_data::{Field, FieldType, Geometry, GeometryType, Value as Cell}; + fn prepare(s: &Session, params: &Value) -> RanAheads { + let mut worker = Session::new(s.project.clone()); + worker.active_layer = s.active_layer; + worker.opened = s.opened; + worker.view_size = s.view_size; + worker.set_extensions(s.extensions().clone()); + worker.tools_ahead = Some(Ahead::Recording(Vec::new())); + worker.grids_ahead = Some(crate::image_grid::Ahead::Recording(Vec::new())); + worker.run("script.run", params).unwrap(); + ran_ahead(&mut worker) + } + #[test] + fn annotation_grid_post_apply_replay_error_rolls_back_outputs_history_and_undo() { + let mut s = Session::new(crate::Project::default()); + let mut samples = Dataset::new("Samples", GeometryType::Point, vec![Field::new("v", FieldType::Double)]); + for (x, y, v) in [(0., 0., 1.), (2., 0., 2.), (0., 2., 3.), (2., 2., 4.)] { + samples.push(Some(Geometry::Point(rustgis_geom::Coord::new(x, y))), vec![Cell::Float(v)]); + } + s.active_layer = Some(s.map_mut().add_dataset(samples)); + s.run("project.rename", &json!({"name":"Before"})).unwrap(); + s.run("project.rename", &json!({"name":"Future"})).unwrap(); + s.run("edit.undo", &json!({})).unwrap(); + s.mark_saved(); + let params = json!({"script":format!("gp.run {}\nraster.imageGrid {}", json!({"tool":"idwInterpolation","params":{"input":"Samples","field":"v","extent":"0,0,2,2","pixelSize":0.5,"output":"Surface"}}), json!({"layer":"Surface"}))}); + let before = serde_json::to_value(&s.project).unwrap(); + let history = (s.undo_depth(), s.undo_label(), s.redo_label(), s.dirty()); + let mut ran = prepare(&s, ¶ms); + // Corrupt only the recorded output identity: the tool's input snapshot and actual + // output still match, so replay accepts and applies it before binding fails. + let Some(RanAhead::Tool(tool)) = ran.tools.first_mut() else { panic!("expected prepared tool") }; + tool.grid_outputs[0].name = "Injected output identity mismatch".into(); + s.statistics_need_read_ahead(true); + let error = super::super::reads::taking(&mut s, "script.run", ran, |s| s.run("script.run", ¶ms)).unwrap_err(); + assert!(error.contains("tool outputs changed"), "{error}"); + assert_eq!(s.tools_taken, 1, "the error must occur after the prepared tool was taken and applied"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "outputs and successful GP history must roll back"); + assert_eq!((s.undo_depth(), s.undo_label(), s.redo_label(), s.dirty()), history); + assert!(s.map().layer_by_name("Surface").is_none()); + assert!(s.grids_ahead.is_none() && s.tools_ahead.is_none()); + s.run("edit.redo", &json!({})).unwrap(); + assert_eq!(s.project.name, "Future", "a failed replay must preserve the prior Redo entry"); + s.run("edit.undo", &json!({})).unwrap(); + let ran = prepare(&s, ¶ms); + let answer = super::super::reads::taking(&mut s, "script.run", ran, |s| s.run("script.run", ¶ms)).unwrap(); + assert_eq!(answer["ran"][1]["result"]["width"], 4); + assert_eq!(s.map().layers.iter().filter(|layer| layer.name == "Surface").count(), 1); + assert!(s.map().layer_by_name("Surface (2)").is_none()); + assert_eq!(s.undo_depth(), history.0 + 1); + s.run("edit.undo", &json!({})).unwrap(); + // Undo deliberately bumps geometry revisions to invalidate rendering caches. + assert!(s.map().layer_by_name("Surface").is_none()); + assert_eq!(s.map().layers.len(), 1); + assert_eq!(serde_json::to_value(&s.map().layers[0].dataset).unwrap(), before["maps"][0]["layers"][0]["dataset"]); + assert!(s.project.gp_history.is_empty()); + assert_eq!(s.undo_depth(), history.0); + } +} diff --git a/crates/ui-egui/src/annotation.rs b/crates/ui-egui/src/annotation.rs index 3cb05c7fc8..17e6729cbd 100644 --- a/crates/ui-egui/src/annotation.rs +++ b/crates/ui-egui/src/annotation.rs @@ -24,9 +24,18 @@ fn cancel(app: &mut RustGisApp) { #[cfg(target_arch = "wasm32")] let _ = request; } + if crate::boxedit::new_drawing(app) || crate::circleedit::new_drawing(app) { + drawing_cancelled(app); + } crate::boxedit::cancel(app); crate::circleedit::cancel(app); } +pub(crate) fn drawing_cancelled(app: &mut RustGisApp) { + app.session.notify( + tl!("Drawing cancelled because the map, image or construction grid changed. Check the grid, then start a new shape."), + false, + ); +} pub(crate) fn sync(app: &mut RustGisApp) { let context = (app.session.opened, app.session.map().instance_id()); if app.annotation.context != Some(context) { @@ -126,6 +135,10 @@ pub(crate) fn landed(app: &mut RustGisApp, request: u64, result: Landed) { }, Landed::Ran(Err(e)) | Landed::NotRun(e) => app.annotation.error = Some(e), } + // The pending reason is obsolete on success or failure; grid_controls shows any failure. + // Validation errors from an active gesture belong to that gesture and remain untouched. + crate::boxedit::clear_idle_error(app); + crate::circleedit::clear_idle_error(app); } /// Command Search's read-only grid query says its answer after background or inline delivery. pub(crate) fn show_grid_result(app: &mut RustGisApp, result: Landed) { @@ -310,10 +323,16 @@ pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) { } ui.small(tl!("Space or middle-drag pans; releasing Space returns to the active drawing tool. Undo/Redo use the existing commands.")); if matches!(app.session.tool, Tool::Circle | Tool::RotatedBox) { - egui::CollapsingHeader::new(tl!("Shape details")).id_salt("annotation_shape_details").default_open(false).show(ui, |ui| { - crate::boxedit::panel(app, ui); - crate::circleedit::panel(app, ui); - }); + let details = + egui::CollapsingHeader::new(tl!("Shape details")).id_salt("annotation_shape_details").default_open(false).show(ui, |ui| { + crate::boxedit::panel(app, ui); + crate::circleedit::panel(app, ui); + }); + if details.body_response.is_none() + && let Some(error) = if app.session.tool == Tool::Circle { crate::circleedit::error(app) } else { crate::boxedit::error(app) } + { + ui.colored_label(egui::Color32::RED, error); + } } ui.separator(); let mut image_filename = app.annotation.image_filename; @@ -552,6 +571,7 @@ mod tests { assert!(crate::boxedit::drag_start(&mut app, rect(), start, true)); crate::boxedit::drag_to(&mut app, rect(), end, false); } + let mark = app.session.notice_mark(); // Also protects a source replacement received directly through MCP before UI sync. app.session.map_mut().layer_mut(image).unwrap().raster.as_mut().unwrap().embedded = Some(std::sync::Arc::new(vec![0; 32])); if tool == Tool::Circle { @@ -559,10 +579,64 @@ mod tests { } else { crate::boxedit::drag_stop(&mut app); } + sync(&mut app); + crate::boxedit::cancel_stale(&mut app); + crate::circleedit::cancel_stale(&mut app); + assert_eq!(app.session.notices_since(mark).iter().filter(|n| n.text.starts_with("Drawing cancelled")).count(), 1); assert!(app.session.map().layer(layer).unwrap().dataset.features.is_empty()); assert_eq!(app.session.undo_depth(), depth); } } + #[test] + fn annotation_grid_changes_report_cancelled_new_drags_once_without_committing() { + for tool in [Tool::Circle, Tool::RotatedBox] { + for change in ["grid", "image", "reload", "map", "project"] { + let (mut app, image, _, frame) = app(); + let folder = tempfile::tempdir().unwrap(); + if change == "reload" { + // Reload Data applies to linked local files; embedded snapshots are re-added. + let path = folder.path().join("image.tif"); + std::fs::write(&path, tiff(&frame)).unwrap(); + let pixels = rustgis_engine::data::raster::read_path(&path).unwrap(); + app.session.map_mut().layer_mut(image).unwrap().raster = + Some(RasterLayer::from_path(pixels, path.display().to_string())); + } + app.run("ui.annotationImage", json!({"image":image})).unwrap(); + app.run("tool.set", json!({"tool":tool.code()})).unwrap(); + let start = screen(&app, &frame, [20., 20.]); + let end = screen(&app, &frame, [60., 60.]); + if tool == Tool::Circle { + assert!(crate::circleedit::drag_start(&mut app, rect(), start, true)); + crate::circleedit::drag_to(&mut app, rect(), end); + } else { + assert!(crate::boxedit::drag_start(&mut app, rect(), start, true)); + crate::boxedit::drag_to(&mut app, rect(), end, false); + } + let mark = app.session.notice_mark(); + match change { + "grid" => app.run("ui.annotationGrid", json!({"pixels":false})), + "image" => app.run("ui.annotationImage", json!({"image":image})), + "reload" => app.run("layer.reloadData", json!({"layer":image})), + "map" => app.run("map.new", json!({"name":"Other"})), + _ => app.run("project.new", json!({})), + } + .unwrap(); + assert!(!crate::boxedit::drawing(&app) && !crate::circleedit::drawing(&app)); + let depth = app.session.undo_depth(); + crate::boxedit::drag_stop(&mut app); + crate::circleedit::drag_stop(&mut app); + sync(&mut app); + assert_eq!( + app.session.notices_since(mark).iter().filter(|n| n.text.starts_with("Drawing cancelled")).count(), + 1, + "{tool:?}: {change}" + ); + assert_eq!(app.session.undo_depth(), depth, "release must not commit the abandoned preview"); + assert!(app.session.project.maps.iter().flat_map(|map| &map.layers).all(|layer| layer.dataset.features.is_empty())); + } + } + } + #[test] fn annotation_grid_replaced_requests_ignore_old_failures_and_cancellation() { let (mut app, image, _, frame) = app(); @@ -811,32 +885,33 @@ mod tests { let depth = app.session.undo_depth(); let mark = app.session.notice_mark(); let origin = screen(&app, &frame, [32., 32.]); - let accepted = if tool == Tool::Circle { - crate::circleedit::drag_start(&mut app, rect(), origin, true) - } else { - crate::boxedit::drag_start(&mut app, rect(), origin, true) - }; - assert!(!accepted); + for _ in 0..3 { + let accepted = if tool == Tool::Circle { + crate::circleedit::drag_start(&mut app, rect(), origin, true) + } else { + crate::boxedit::drag_start(&mut app, rect(), origin, true) + }; + assert!(!accepted); + } let error = "Wait for the original image grid before drawing."; assert_eq!(app.session.notices_since(mark).iter().filter(|n| n.error && n.text == error).count(), 1); assert_eq!(app.session.undo_depth(), depth); assert!(app.session.map().layer(layer).unwrap().dataset.features.is_empty()); - let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1366., 900.)).with_max_steps(1000).build_ui_state( - |ui, app: &mut RustGisApp| { - let ctx = ui.ctx().clone(); - app.logic(&ctx); - app.ui(ui); - }, - app, - ); + // Render the pane without the toast/status bar to prove the inline reason persists. + let mut h = egui_kittest::Harness::builder() + .with_size(egui::vec2(600., 1000.)) + .with_max_steps(1000) + .build_ui_state(|ui, app| panel(app, ui), app); // Pending reads keep the spinner repainting; inspect a frame rather than await idle. h.run_steps(1); assert!(h.query_by_label("Shape details").is_some()); assert!(h.query_by_label("Radius").is_none() && h.query_by_label("Width").is_none()); assert!(h.query_all_by_label(error).next().is_some(), "refusal is visible without expanding details"); + let request = h.state().annotation.pending.unwrap(); + landed(h.state_mut(), request, Landed::Ran(Ok(serde_json::to_value(&frame).unwrap()))); + h.run_steps(1); + assert!(h.query_all_by_label(error).next().is_none(), "a completed grid clears the stale waiting reason"); let app = h.state_mut(); - app.annotation.pending = None; - app.annotation.frame = Some(frame.clone()); let mark = app.session.notice_mark(); drag(app, &frame, [32., 32.], [64., 64.], true); assert_eq!(app.session.map().layer(layer).unwrap().dataset.features.len(), 1); diff --git a/crates/ui-egui/src/boxedit.rs b/crates/ui-egui/src/boxedit.rs index 9bd39916d7..649da3517b 100644 --- a/crates/ui-egui/src/boxedit.rs +++ b/crates/ui-egui/src/boxedit.rs @@ -50,6 +50,17 @@ fn selected(app: &RustGisApp) -> Option<(u32, usize, BoxSpec)> { pub(crate) fn drawing(app: &RustGisApp) -> bool { app.boxes.gesture.is_some() } +pub(crate) fn new_drawing(app: &RustGisApp) -> bool { + app.boxes.gesture.as_ref().is_some_and(|g| g.row.is_none()) +} +pub(crate) fn error(app: &RustGisApp) -> Option<&str> { + app.boxes.error.as_deref() +} +pub(crate) fn clear_idle_error(app: &mut RustGisApp) { + if app.boxes.gesture.is_none() { + app.boxes.error = None; + } +} pub(crate) fn cancel(app: &mut RustGisApp) { app.boxes = State::default(); app.map.cancel_rotated_box_drag(); @@ -70,7 +81,16 @@ fn valid(app: &RustGisApp, g: &Gesture) -> bool { }) } pub(crate) fn cancel_stale(app: &mut RustGisApp) { - if app.boxes.gesture.as_ref().is_some_and(|g| !valid(app, g)) { + if let Some(grid_changed) = app.boxes.gesture.as_ref().filter(|g| !valid(app, g)).map(|g| { + g.row.is_none() + && (g.project != app.session.opened + || g.map != app.session.project.active_map + || g.map_name != app.session.map().name + || !crate::annotation::drawing_frame(app).is_ok_and(|frame| frame == g.original.frame)) + }) { + if grid_changed { + crate::annotation::drawing_cancelled(app); + } cancel(app); } if !app.forms.template.is_empty() @@ -147,8 +167,10 @@ pub(crate) fn drag_start(app: &mut RustGisApp, rect: Rect, origin: Pos2, force_n let frame = match crate::annotation::drawing_frame(app) { Ok(frame) => frame, Err(error) => { - app.boxes.error = Some(error.clone()); - app.session.notify(error, true); + if app.boxes.error.as_deref() != Some(error.as_str()) { + app.session.notify(error.clone(), true); + } + app.boxes.error = Some(error); return false; } }; diff --git a/crates/ui-egui/src/circleedit.rs b/crates/ui-egui/src/circleedit.rs index 681243eacd..8c0fa2e273 100644 --- a/crates/ui-egui/src/circleedit.rs +++ b/crates/ui-egui/src/circleedit.rs @@ -50,6 +50,17 @@ fn selected(app: &RustGisApp) -> Option<(u32, usize, CircleSpec)> { pub(crate) fn drawing(app: &RustGisApp) -> bool { app.circles.gesture.is_some() } +pub(crate) fn new_drawing(app: &RustGisApp) -> bool { + app.circles.gesture.as_ref().is_some_and(|g| g.row.is_none()) +} +pub(crate) fn error(app: &RustGisApp) -> Option<&str> { + app.circles.error.as_deref() +} +pub(crate) fn clear_idle_error(app: &mut RustGisApp) { + if app.circles.gesture.is_none() { + app.circles.error = None; + } +} pub(crate) fn cancel(app: &mut RustGisApp) { app.circles = State::default(); app.map.cancel_circle_drag(); @@ -70,7 +81,16 @@ fn valid(app: &RustGisApp, g: &Gesture) -> bool { }) } pub(crate) fn cancel_stale(app: &mut RustGisApp) { - if app.circles.gesture.as_ref().is_some_and(|g| !valid(app, g)) { + if let Some(grid_changed) = app.circles.gesture.as_ref().filter(|g| !valid(app, g)).map(|g| { + g.row.is_none() + && (g.project != app.session.opened + || g.map != app.session.project.active_map + || g.map_name != app.session.map().name + || !crate::annotation::drawing_frame(app).is_ok_and(|frame| frame == g.original.frame)) + }) { + if grid_changed { + crate::annotation::drawing_cancelled(app); + } cancel(app); } if !app.forms.template.is_empty() @@ -115,8 +135,10 @@ pub(crate) fn drag_start(app: &mut RustGisApp, rect: Rect, origin: Pos2, force_n let frame = match crate::annotation::drawing_frame(app) { Ok(frame) => frame, Err(error) => { - app.circles.error = Some(error.clone()); - app.session.notify(error, true); + if app.circles.error.as_deref() != Some(error.as_str()) { + app.session.notify(error.clone(), true); + } + app.circles.error = Some(error); return false; } }; diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 07897981a5..2a5e639c46 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -199,7 +199,10 @@ Space or middle-drag pans; releasing Space keeps the drawing tool. Use the selec existing handles, Undo/Redo, and **Export GeoJSON…**. **Shape details** is collapsed by default; expand it for numeric dimensions and additional editing controls. Map handles remain available while the section is hidden. Refused drawing gestures show an error notification even when -**Shape details** is collapsed. Exports contain +**Shape details** is collapsed. The reason stays visible in the pane, and repeated refusals with +the same message do not add duplicate notices. Changing the image/grid or invalidating its source +cancels an unfinished new shape with an explanation; releasing afterwards does not save that +preview or create an Undo step. Exports contain ordinary geographic polygons and feature properties; circles are polygon approximations. Check **Use image filename** to suggest `img1.geojson` for an image named `img1.tiff`. The preview shows the suggested basename; the save dialog still chooses the folder and lets From 0eb681eef3c2a4c2e6fe9b350aec810b1318fbab Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 17:20:58 +0000 Subject: [PATCH 13/14] Propagate rollback test preparation errors in helper --- crates/engine/src/cmd/gp.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index d75a052671..652735bdd3 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -6700,7 +6700,7 @@ pub(crate) fn vector_file_input(spec: &rustgis_analysis::ToolSpec, map: &rustgis mod replay_rollback_tests { use super::*; use rustgis_data::{Field, FieldType, Geometry, GeometryType, Value as Cell}; - fn prepare(s: &Session, params: &Value) -> RanAheads { + fn prepare(s: &Session, params: &Value) -> Result { let mut worker = Session::new(s.project.clone()); worker.active_layer = s.active_layer; worker.opened = s.opened; @@ -6708,8 +6708,8 @@ mod replay_rollback_tests { worker.set_extensions(s.extensions().clone()); worker.tools_ahead = Some(Ahead::Recording(Vec::new())); worker.grids_ahead = Some(crate::image_grid::Ahead::Recording(Vec::new())); - worker.run("script.run", params).unwrap(); - ran_ahead(&mut worker) + worker.run("script.run", params)?; + Ok(ran_ahead(&mut worker)) } #[test] fn annotation_grid_post_apply_replay_error_rolls_back_outputs_history_and_undo() { @@ -6726,7 +6726,7 @@ mod replay_rollback_tests { let params = json!({"script":format!("gp.run {}\nraster.imageGrid {}", json!({"tool":"idwInterpolation","params":{"input":"Samples","field":"v","extent":"0,0,2,2","pixelSize":0.5,"output":"Surface"}}), json!({"layer":"Surface"}))}); let before = serde_json::to_value(&s.project).unwrap(); let history = (s.undo_depth(), s.undo_label(), s.redo_label(), s.dirty()); - let mut ran = prepare(&s, ¶ms); + let mut ran = prepare(&s, ¶ms).unwrap(); // Corrupt only the recorded output identity: the tool's input snapshot and actual // output still match, so replay accepts and applies it before binding fails. let Some(RanAhead::Tool(tool)) = ran.tools.first_mut() else { panic!("expected prepared tool") }; @@ -6742,7 +6742,7 @@ mod replay_rollback_tests { s.run("edit.redo", &json!({})).unwrap(); assert_eq!(s.project.name, "Future", "a failed replay must preserve the prior Redo entry"); s.run("edit.undo", &json!({})).unwrap(); - let ran = prepare(&s, ¶ms); + let ran = prepare(&s, ¶ms).unwrap(); let answer = super::super::reads::taking(&mut s, "script.run", ran, |s| s.run("script.run", ¶ms)).unwrap(); assert_eq!(answer["ran"][1]["result"]["width"], 4); assert_eq!(s.map().layers.iter().filter(|layer| layer.name == "Surface").count(), 1); From 8712d19859e924136b47edaaeff6c98c5a6019ae Mon Sep 17 00:00:00 2001 From: "robin.cole@earthdaily.com" Date: Tue, 17 Nov 2026 17:48:48 +0000 Subject: [PATCH 14/14] Validate construction frame CRS before accepting image grids --- crates/engine/src/box_edit.rs | 26 +++++++++++++++++++++++++- docs/user-guide/editing.md | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/box_edit.rs b/crates/engine/src/box_edit.rs index 62665bf6d1..07a5aebd57 100644 --- a/crates/engine/src/box_edit.rs +++ b/crates/engine/src/box_edit.rs @@ -36,7 +36,9 @@ impl Frame { if epsg == 0 { return Err("construction grids currently require a recognised EPSG coordinate system".into()); } - Ok(Self { epsg, width: grid.width(), height: grid.height(), affine: grid.affine() }) + let frame = Self { epsg, width: grid.width(), height: grid.height(), affine: grid.affine() }; + frame.grid()?; + Ok(frame) } fn grid(&self) -> Result { let crs = Crs::from_epsg(self.epsg) @@ -291,6 +293,28 @@ mod tests { fn spec() -> BoxSpec { BoxSpec { frame: Frame::default(), center: [1000., 1000.], width: 200., height: 100., angle: 30. } } + #[test] + fn annotation_grid_frame_refuses_unsupported_crs_before_drawing() { + let affine = [500_000., 0.8, 0.2, 1000., 0.1, -0.8]; + assert!(Crs::from_epsg(65535).is_none()); + let grid = ImageGrid::new(400, 300, Crs::Epsg(65535), affine).unwrap(); + assert_eq!(Frame::from_grid(&grid).unwrap_err(), "construction grid needs a supported EPSG coordinate system"); + } + #[test] + fn annotation_grid_frame_rejects_custom_datum_and_retains_supported_pixel_round_trips() { + let affine = [500_000., 0.8, 0.2, 1000., 0.1, -0.8]; + let custom = Crs::parse("+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-100 +ellps=clrk66 +units=m +no_defs").unwrap(); + assert!(custom.lacks_datum_shift()); + let grid = ImageGrid::new(400, 300, custom, affine).unwrap(); + assert_eq!(Frame::from_grid(&grid).unwrap_err(), "construction grids currently require a recognised EPSG coordinate system"); + for code in [3857, 32633] { + let grid = ImageGrid::new(400, 300, Crs::from_epsg(code).unwrap(), affine).unwrap(); + let frame = Frame::from_grid(&grid).unwrap(); + let point = [200., 150.]; + let actual = frame.to_local(frame.to_lonlat(point).unwrap()).unwrap(); + assert!((actual[0] - point[0]).abs() < 1e-6 && (actual[1] - point[1]).abs() < 1e-6); + } + } fn add(s: &mut Session, b: &BoxSpec) -> usize { s.run("edit.addBox", &json!({"box":b,"attributes":{"class":"vehicle","_rustgis_box":"user-owned"}})).unwrap()["row"] .as_u64() diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index 2a5e639c46..ea24dcc5b3 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -186,6 +186,8 @@ Removing/replacing an image invalidates its cached grid. Local and embedded GeoT download remote originals first. In the browser, upload the original GeoTIFF. Other local formats (including PNG, JPEG, ASCII grids and GDAL-converted images) are refused with guidance to export the image as a GeoTIFF and add that file; reloading the unsupported source cannot supply its grid. +Construction grids require a supported EPSG coordinate system. Unsupported or custom systems +are refused during the grid read; choose a supported image or **Web Mercator grid metres**. Changing projects or maps clears the old image binding, keeps pixel mode selected and reports a warning. Drawing stays blocked until you choose an image again or explicitly choose Web Mercator grid metres; opening the pane does not clear the warning.