Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e0a61c2
feat(annotation): add image-aware pane and original-pixel drawing
robmarkcole Nov 16, 2026
4f88e4c
Make annotation details optional and suggest image-based export names
robmarkcole Nov 17, 2026
80b2d27
Fix annotation selection, cancellation and command search
robmarkcole Nov 17, 2026
7433506
Verify image-grid source revisions and show search results
robmarkcole Nov 17, 2026
4245edd
Merge upstream main into annotation image pane
robmarkcole Nov 17, 2026
aa5512b
Cover linked-image conversions and clarify revision recovery
robmarkcole Nov 17, 2026
8e38c3d
Fix converted-image grid errors and unavailable image recovery
robmarkcole Nov 17, 2026
c9f0889
Harden image revision checks and stale pixel contexts
robmarkcole Nov 17, 2026
a0d6b23
Clarify original-grid recovery for non-TIFF images
robmarkcole Nov 17, 2026
7ba682b
Preserve pixel mode across context changes and prepare scripted grid …
robmarkcole Nov 17, 2026
d2ebb4a
Clarify GeoTIFF load recovery and surface refused shape gestures
robmarkcole Nov 17, 2026
8d155ca
Bind scripted grid queries to accepted raster tool outputs
robmarkcole Nov 17, 2026
62c02a1
Test atomic script rollback and explain cancelled shape previews
robmarkcole Nov 17, 2026
0eb681e
Propagate rollback test preparation errors in helper
robmarkcole Nov 17, 2026
8712d19
Validate construction frame CRS before accepting image grids
robmarkcole Nov 17, 2026
58b4279
Merge upstream main into annotation image pane branch
robmarkcole Nov 18, 2026
84500c8
Merge branch 'main' into feat/annotation-image-pane
robmarkcole Nov 18, 2026
5eaf0b1
Merge remote-tracking branch 'origin/main' into pr-2290
giswqs Nov 21, 2026
7e32ecb
Address review feedback
giswqs Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/assistant/src/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions crates/assistant/src/relay/archive_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
114 changes: 114 additions & 0 deletions crates/data/src/raster/file_revision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//! 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};

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct FileRevision {
len: u64,
modified: std::time::SystemTime,
#[cfg(unix)]
identity: (u64, u64, i64, i64),
#[cfg(windows)]
created: u64,
}

impl FileRevision {
/// The path a revision was read from is not part of it: the callers check the path they hold,
/// so a changed working directory never looks like a changed file.
pub(super) fn capture(metadata: &Metadata) -> std::io::Result<Self> {
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::fs::MetadataExt;
Ok(Self {
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, metadata: &Metadata) -> std::result::Result<(), String> {
let current = Self::capture(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(&metadata)
}

/// Checks fresh metadata from this open handle against the original source.
/// This does not verify that the live path still names the handle; check_original_file does.
pub fn check_original_handle(&self, 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(&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(&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(&replacement_handle).is_err());
}

#[test]
fn annotation_grid_handle_validation_rejects_a_different_file() {
Comment thread
giswqs marked this conversation as resolved.
Resolved
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(&std::fs::File::open(&other).unwrap()).is_err());
raster.check_original_file(&path).unwrap();
}
}
129 changes: 126 additions & 3 deletions crates/data/src/raster/geotiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,9 +1328,58 @@ pub fn read_file(path: &std::path::Path, name: &str) -> Result<Raster> {
/// [`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<f64>) -> Result<Raster> {
let path = path.to_path_buf();
let open = move || -> std::io::Result<Reader<'static>> { 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(&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, then Retry Add Data (for an existing layer, Reload Data). Or copy it to a stable local location and add the copy (for an existing layer, point it at the copy with Repair Data Source)."
)
}

#[cfg(not(target_arch = "wasm32"))]
fn read_file_with_revision(
path: &std::path::Path,
name: &str,
sidecar_nodata: Option<f64>,
file: std::fs::File,
revision: std::io::Result<super::file_revision::FileRevision>,
) -> Result<Raster> {
let revision = revision.map_err(|e| e.to_string());
let open = || -> std::io::Result<Reader<'static>> {
let file = crate::provenance::open(path)?;
if let Ok(original) = &revision {
let current = file
.metadata()
.and_then(|metadata| super::file_revision::FileRevision::capture(&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 decoded = reader(std::io::BufReader::new(file))
.map_err(err)
.and_then(|r| read_limited_with_nodata(r, name, max_samples(), Some(&open), sidecar_nodata));
// A file cut short while it is read fails to decode: the revision says whether that was a
// change to wait out (Cancel is said as itself).
if let Ok(original) = &revision
&& !matches!(decoded, Err(crate::DataError::Cancelled))
{
let current = std::fs::metadata(path)
.and_then(|metadata| super::file_revision::FileRevision::capture(&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")));
}
}
let mut raster = decoded?;
raster.source_file = Some(revision);
Ok(raster)
}

/// [`read`], holding at most `max` samples (the tests' small limit exercises the overviews), with
Expand Down Expand Up @@ -2029,6 +2078,80 @@ 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(&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("Reload Data") && error.contains("Repair Data Source"),
"{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(&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() {
Expand Down
30 changes: 30 additions & 0 deletions crates/data/src/raster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub mod crs;
pub mod crs_search;
mod decoded;
#[cfg(not(target_arch = "wasm32"))]
mod file_revision;
#[cfg(not(target_arch = "wasm32"))]
pub mod gdal;
pub mod geotiff;
mod image_grid;
Expand Down Expand Up @@ -648,6 +650,9 @@ pub struct Raster {
pub data_type: String,
/// Where it was read from, if anywhere.
pub source: Option<String>,
/// The original local GeoTIFF's revision or capture failure, retained with these pixels and never serialized.
#[cfg(not(target_arch = "wasm32"))]
source_file: Option<std::result::Result<file_revision::FileRevision, String>>,
/// 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
Expand Down Expand Up @@ -847,6 +852,8 @@ impl Raster {
color,
data_type,
source: None,
#[cfg(not(target_arch = "wasm32"))]
source_file: None,
overview: None,
step: 1,
georeferenced: true,
Expand Down Expand Up @@ -984,6 +991,11 @@ impl Raster {
/// This raster as a scan: its pixels with no coordinates, as `"as": "scan"` opens a picture.
pub fn into_scan(mut self) -> Result<Raster> {
if self.georeferenced {
// Its pixels no longer follow the file's own grid, so no grid read may pair with them.
#[cfg(not(target_arch = "wasm32"))]
{
self.source_file = None;
}
self.crs = Crs::LonLat;
self.transformer = self.crs.transformer().map_err(DataError::Format)?;
self.transform = unreferenced(self.width, self.height);
Expand Down Expand Up @@ -1380,6 +1392,8 @@ pub fn read_converted(path: &Path, c: &gdal::Converted) -> Result<Raster> {
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));
}
Expand Down Expand Up @@ -2500,6 +2514,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)]
Expand Down
Loading
Loading