diff --git a/crates/assistant/src/mcp.rs b/crates/assistant/src/mcp.rs index b47dc0159..c77905ffa 100644 --- a/crates/assistant/src/mcp.rs +++ b/crates/assistant/src/mcp.rs @@ -164,7 +164,14 @@ 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" | "layer.sizeBy") + Some( + "map.reloadTiles" + | "map.repairTiles" + | "table.statistics" + | "edit.previewShapeConversion" + | "raster.imageGrid" + | "layer.sizeBy" + ) ) { return None; diff --git a/crates/assistant/src/relay/archive_tests.rs b/crates/assistant/src/relay/archive_tests.rs index 48a3c2a57..2f447f4c0 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/data/src/raster/file_revision.rs b/crates/data/src/raster/file_revision.rs new file mode 100644 index 000000000..78269da98 --- /dev/null +++ b/crates/data/src/raster/file_revision.rs @@ -0,0 +1,117 @@ +//! 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}, +}; + +#[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(), + }) + } + + 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 { + fn original_revision(&self) -> std::result::Result<&FileRevision, String> { + match &self.source_file { + 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)] +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).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(); + 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()); + // 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 413b2d9c9..5190032be 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1328,9 +1328,52 @@ 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).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) +} + +#[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, + 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 let Ok(original) = &revision { + 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(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)?; + 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) } /// [`read`], holding at most `max` samples (the tests' small limit exercises the overviews), with @@ -2029,6 +2072,77 @@ 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_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() { + 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/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index 95596b3aa..cd39c5831 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -14,6 +14,8 @@ pub(crate) mod epsg_axes; mod epsg_gap; mod esri; #[cfg(not(target_arch = "wasm32"))] +mod file_revision; +#[cfg(not(target_arch = "wasm32"))] pub mod gdal; pub mod geotiff; mod image_grid; @@ -646,6 +648,9 @@ pub struct Raster { pub data_type: String, /// Where it was read from, if anywhere. pub source: Option, + /// The original local GeoTIFF's revision or capture failure, 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 @@ -845,6 +850,8 @@ impl Raster { color, data_type, source: None, + #[cfg(not(target_arch = "wasm32"))] + source_file: None, overview: None, step: 1, georeferenced: true, @@ -1378,6 +1385,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)); } @@ -2497,6 +2506,22 @@ 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(); + 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).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/box_edit.rs b/crates/engine/src/box_edit.rs index 62665bf6d..07a5aebd5 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/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 58c77c297..652735bdd 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"))] @@ -862,6 +864,7 @@ pub(crate) struct RanAheads { tools: Vec, files: Vec, zones: Vec, + grids: Vec, } #[cfg(not(target_arch = "wasm32"))] @@ -872,11 +875,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)); 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 +900,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"))] @@ -922,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); @@ -940,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 } @@ -2533,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 { @@ -2583,6 +2610,7 @@ pub(crate) fn execute( result: res.clone(), full_ids: ids.clone(), millis, + grid_outputs: Vec::new(), }))); } (res, ids, millis) @@ -2591,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 @@ -6645,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) -> Result { + 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)?; + Ok(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).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") }; + 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).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); + 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/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 0b094adf5..856c4965d 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|.pdf (JPEG 2000: desktop, through GDAL; PDF: desktop, command line and Python)", "data": base64, "zoom"?: bool, "layer"?: "one layer of a zip, KML or GPX by name, or a PDF's page number (the first when left out or blank)", "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 77179a5af..0fdca7440 100644 --- a/crates/engine/src/cmd/reads.rs +++ b/crates/engine/src/cmd/reads.rs @@ -222,6 +222,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), "layer.sizeBy" => ("Size by Field", super::layer::size_by_ahead(s, params), None), diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 073f05df5..617b0a68a 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 new file mode 100644 index 000000000..4667b662a --- /dev/null +++ b/crates/engine/src/image_grid.rs @@ -0,0 +1,643 @@ +//! 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 { + 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 + && 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. +/// 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())?; + 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 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 = 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)?; + 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_handle(path, &input)?; + displayed.check_original_file(path)?; + grid + } + #[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) +} +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(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()?; + 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 calls read synchronously on +/// the caller's thread; the browser reads uploaded bytes inline. Reads only headers, never 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"))] + 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 + }; + #[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 => { + 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()) +} + +#[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] + #[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] + #[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] + #[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] + #[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()); + } + + #[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(); + 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 0478517e2..a4cf6b999 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -14,6 +14,7 @@ pub mod durable; pub mod extensions; pub mod geometry_calc; pub mod grids; +pub mod image_grid; pub mod model; mod notice_text; #[cfg(not(target_arch = "wasm32"))] @@ -1041,6 +1042,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"))] @@ -1388,6 +1392,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/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs index 41653e5a4..aa1dbaa3f 100644 --- a/crates/engine/src/policy/dispatch.rs +++ b/crates/engine/src/policy/dispatch.rs @@ -123,15 +123,20 @@ impl Session { } let CallKind::Command { id, params } = &call.kind else { return Err( - "only standalone archive, column-statistics, shape-conversion or size-by-field commands can prepare a scoped read".into() + "only standalone archive, column-statistics, shape-conversion, image-grid or size-by-field commands can prepare a scoped read".into() ); }; if !matches!( id.as_str(), - "map.reloadTiles" | "map.repairTiles" | "table.statistics" | "edit.previewShapeConversion" | "layer.sizeBy" + "map.reloadTiles" + | "map.repairTiles" + | "table.statistics" + | "edit.previewShapeConversion" + | "raster.imageGrid" + | "layer.sizeBy" ) { return Err( - "only standalone archive, column-statistics, shape-conversion or size-by-field commands can prepare a scoped read".into() + "only standalone archive, column-statistics, shape-conversion, image-grid or size-by-field commands can prepare a scoped read".into() ); } let capture = |s: &mut Session| s.capture_read(id, params); @@ -163,6 +168,7 @@ impl Session { | "service.add" | "source.syncState" | "table.statistics" + | "raster.imageGrid" | "edit.previewShapeConversion" | "layer.sizeBy" ) && crate::cmd::edit::table_edit_in_background(command).is_none() @@ -180,9 +186,10 @@ impl Session { | "labels.convert" | "source.syncState" | "table.statistics" + | "raster.imageGrid" | "edit.previewShapeConversion" | "layer.sizeBy" - ) && (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 d5c89fb9f..d5767b734 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 { @@ -620,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/crates/engine/src/tests/cancellation.rs b/crates/engine/src/tests/cancellation.rs index 136012457..0e5ac6a3b 100644 --- a/crates/engine/src/tests/cancellation.rs +++ b/crates/engine/src/tests/cancellation.rs @@ -178,6 +178,8 @@ pub(super) fn long_commands(boundaries: bool) { assert!(control.looked() > 0, "edit.previewShapeConversion never looks at Cancel"); assert!(contents(&shapes.project) == before, "edit.previewShapeConversion changed the project after Cancel"); } + // Original TIFF headers share the read queue and both cancellation sweeps. + exercise(&s, "raster.imageGrid", &json!({"layer": dem}), Kind::Read, &dir.0, boundaries); // A PDF's page is read on the same queue: the file, then its drawing, Cancel looked at between // (#2336). s.run("layout.new", &json!({"page": "a4", "template": "map"})).unwrap(); diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 4f4445b01..443704065 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 new file mode 100644 index 000000000..17e6729cb --- /dev/null +++ b/crates/ui-egui/src/annotation.rs @@ -0,0 +1,1028 @@ +//! 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_name: Option, + image_chosen: bool, + image_filename: 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; + } + 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) { + cancel(app); + 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()) + { + 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> { + 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})) + { + app.annotation.image_chosen = true; + app.annotation.error = Some(error); + } + 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_else(|| tl!("Image-grid request limit reached").to_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(); + 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), + } + // 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) { + 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.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.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()); + } + 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 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) { + 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 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()) { + 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::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"))); + 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.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 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); + } +} +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.")); + if matches!(app.session.tool, Tool::Circle | Tool::RotatedBox) { + 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; + 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.annotationExport", 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_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] { + 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); + } + 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 { + crate::circleedit::drag_stop(&mut app); + } 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(); + 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!(drawing_frame(&app).is_err()); + } + #[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_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; + 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_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] { + 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] + 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); + } + #[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.]); + 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()); + // 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(); + 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; + 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/boxedit.rs b/crates/ui-egui/src/boxedit.rs index 4a5fd6348..649da3517 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(); @@ -61,6 +72,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 @@ -69,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() @@ -142,7 +163,19 @@ 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) => { + if app.boxes.error.as_deref() != Some(error.as_str()) { + app.session.notify(error.clone(), true); + } + 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 f8cc29087..ef0d319ca 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -880,6 +880,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"), diff --git a/crates/ui-egui/src/circleedit.rs b/crates/ui-egui/src/circleedit.rs index 6425fd52c..8c0fa2e27 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(); @@ -61,6 +72,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 @@ -69,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() @@ -110,7 +131,19 @@ 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) => { + if app.circles.error.as_deref() != Some(error.as_str()) { + app.session.notify(error.clone(), true); + } + 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 d597959b4..300db4222 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 07a2b8dbb..930b2a970 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -90,6 +90,7 @@ pub enum Landed { Ran(Result), NotRun(String), } +mod annotation; mod boxedit; mod circleedit; #[cfg(not(target_arch = "wasm32"))] @@ -660,6 +661,7 @@ pub enum RightPane { Symbology, History, Editing, + Annotation, Design, Workflows, Script, @@ -1191,6 +1193,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, @@ -1506,6 +1509,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(), @@ -2735,6 +2739,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::show_grid_result)); + } + let result = self.run(id, params); + annotation::show_grid_result(self, Landed::Ran(result.clone())); + return result; + } self.run(id, params) } @@ -2819,11 +2832,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)); } @@ -2955,6 +2972,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 { @@ -3416,6 +3434,23 @@ 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.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), "ui.pane.design" => toggle_right(&mut self.ui, RightPane::Design), "ui.pane.script" => toggle_right(&mut self.ui, RightPane::Script), @@ -4887,13 +4922,16 @@ 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 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() { @@ -4914,8 +4952,7 @@ impl RustGisApp { return Err(e); } }; - let ext = if ext == "shp" { "zip".to_string() } else { ext }; - let name = format!("{}.{ext}", l.name); + 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); @@ -5032,7 +5069,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() @@ -5043,6 +5081,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) { @@ -5061,7 +5100,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). @@ -6224,6 +6262,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 fecbe87fc..eae66c143 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -135,6 +135,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"), @@ -201,6 +202,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 4209c5dff..618515584 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, } @@ -36,13 +39,31 @@ 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) { - 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)); + 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); + } + } } } @@ -76,12 +97,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 +146,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"))] @@ -173,6 +201,7 @@ fn run_presented( | "labels.convert" | "source.syncState" | "table.statistics" + | "raster.imageGrid" | "edit.previewShapeConversion" | "layer.sizeBy" ) @@ -385,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. @@ -499,6 +528,7 @@ fn start(app: &mut RustGisApp, mut asked: Asked, p: Value) -> 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; } @@ -659,7 +695,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; @@ -737,6 +773,113 @@ 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}, + }; + let raster = Raster::new( + "image", + 1, + 1, + vec![vec![1.]], + GeoTransform { origin_x: 0., origin_y: 1., pixel_width: 1., pixel_height: -1. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .unwrap(); + let bytes = geotiff::write(&raster, 0).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 mut app = RustGisApp::new(s); + app.reads_off_frame = true; + app.run("ui.annotationImage", json!({"image":id})).unwrap(); + let folder = tempfile::tempdir().unwrap(); + 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()); + assert_eq!(job.cancel_waiting, queue_wide); + assert_eq!(app.reads.waiting.len(), 1); + landed(&mut app, 60); + assert_eq!(app.session.map().layer_by_name("Queued after grid").is_some(), !queue_wide); + 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::{ + Project, Session, + data::raster::{ColorHint, Crs, GeoTransform, Raster}, + }; + let raster = Raster::new( + "image", + 1, + 1, + vec![vec![1.]], + GeoTransform { origin_x: 0., origin_y: 1., pixel_width: 1., pixel_height: -1. }, + Crs::from_epsg(3857).unwrap(), + None, + ColorHint::Gray, + "UInt8", + ) + .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", vec![0; 32])); + let mut app = RustGisApp::new(s); + app.reads_off_frame = true; + app.reads.fail_spawn = true; + app.run("ui.annotationImage", json!({"image":id})).unwrap(); + assert!(!showing(&app)); + assert!(crate::annotation::drawing_frame(&app).unwrap_err().contains("worker could not start")); + assert!(app.session.notices.iter().any(|n| n.error && n.text.contains("worker could not start"))); + } + #[test] fn shape_conversion_worker_failure_does_not_fit_or_apply_on_the_frame() { use egui_kittest::kittest::Queryable; diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 8d60c3131..3d623b650 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -2183,6 +2183,18 @@ fn view_tab(app: &mut RustGisApp, ui: &mut Ui) { }); }); group(ui, "Windows", |ui| { + col(ui, |ui| { + small( + ui, + app, + "edit", + "Annotate", + "Show or hide the Annotate pane.", + "ui.pane.annotation", + Value::Null, + app.ui.right == Some(RightPane::Annotation), + ); + }); big( ui, app, @@ -2556,6 +2568,19 @@ fn layer_hint(ui: &mut Ui, text: &str) { /// The Analyze tab: geoprocessing, workflows and notebooks. Its imagery groups are on the Imagery extension's tab (#1752). fn analyze_tab(app: &mut RustGisApp, ui: &mut Ui) { + group(ui, "Annotate", |ui| { + big( + ui, + app, + "edit", + "Annotate", + "Choose an image and target layer, then draw in the original image grid.", + "ui.pane.annotation", + Value::Null, + app.ui.right == Some(RightPane::Annotation), + false, + ); + }); analysis_groups(app, ui); notebook_group(app, ui); } diff --git a/docs/user-guide/editing.md b/docs/user-guide/editing.md index db84d1f5c..ea24dcc5b 100644 --- a/docs/user-guide/editing.md +++ b/docs/user-guide/editing.md @@ -157,6 +157,95 @@ you edit the value or choose units; click **Apply elevation** to apply the new i [Geoprocessing tools](analysis.md#geoprocessing-tools), **Near the poles**). - **Status**: features added, changed and deleted per layer since the last save. +### Annotating an image + +**Analyze → Annotate** opens the annotation pane. It is also available from **View → Windows** +and Command Search. Add a GeoTIFF and a polygon feature layer first, then choose the **Image** +and **Target layer** in the pane, enable editing, and choose Circle or Rotated Box. +The ordinary tools remain available under Edit and share these settings. + +Selecting an image defaults the **New shape grid** to **Original image pixels**. RustGIS reads +its original TIFF header, including rotation and shear, independently of any displayed overview. +New box widths/heights and circle radii are then expressed in source-image pixels. A circle in +image coordinates may appear elliptical on the map; a rectangle may appear skewed. The image is +not resampled by this choice, and shapes are not clipped to its boundary. + +Choose **Web Mercator grid metres** explicitly to draw in map coordinates instead; those are not +ground-distance metres. Choosing **No image** restores that grid. Under Edit, the same choices +are in Editing → New shape grid when a circle or rotated box tool is active. Grid changes affect +new shapes; existing shapes retain their own construction grid when moved, resized or rotated. +Undo/Redo retain those constructions within the session. + +On desktop, header reads run in the background with progress and Cancel. Changing images, +switching to Web Mercator or using **Cancel Grid Read** stops only that lookup and preserves +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**. +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. +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. +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. +**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; +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. 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 +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. + +For CLI, Python, Assistant/MCP and permitted plugins, `raster.imageGrid={"layer":"Image"}` +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. 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 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 + +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 › @@ -431,6 +520,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. + 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