From 51cbe7a29e9cb62c306f1b967b2b863f1cb623ea Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 01:55:56 -0500 Subject: [PATCH 1/5] Raster: report export losses for downloads and tools Share the GeoTIFF note builder across file exports, browser downloads, mosaics and warp outputs. Preserve source classes and credits so the writer can retain them or report their loss. Cover clean and lossy outputs, decode the written metadata, and report differing mosaic credits. Fixes #1638 --- crates/analysis/src/warp.rs | 6 +- crates/engine/src/cmd/imagery.rs | 24 ++++--- crates/engine/src/cmd/raster.rs | 63 +++++++++++------- crates/engine/src/tests.rs | 108 +++++++++++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 2 +- 5 files changed, 170 insertions(+), 33 deletions(-) diff --git a/crates/analysis/src/warp.rs b/crates/analysis/src/warp.rs index 6fea0f594d..04abfc22f3 100644 --- a/crates/analysis/src/warp.rs +++ b/crates/analysis/src/warp.rs @@ -635,7 +635,7 @@ impl Canvas { } } - /// The output raster on `g`, its bands named as the source's. + /// The output raster on `g`, keeping the source's band labels, class names and credits. fn finish(&self, r: &Raster, g: &Grid, bands: Vec>, name: String) -> Result { let mut out = Raster::new( name, @@ -651,7 +651,11 @@ impl Canvas { .map_err(|e| e.to_string())?; for (i, band) in out.bands.iter_mut().enumerate() { band.name = r.bands.get(i).map_or_else(|| "Alpha".to_string(), |b| b.name.clone()); + if let Some(source) = r.bands.get(i) { + band.classes = source.classes.clone(); + } } + out.copyright = r.copyright.clone(); Ok(out) } } diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 50fed65538..b144c70ef3 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -340,10 +340,9 @@ fn warp_to_fit(s: &mut Session, p: &Value, layer: &str, r: &Raster) -> CmdResult // A file written is the layer's source; without one the pixels are kept in the project. let rl = match &path { Some(path) => { - let cog = - rustgis_data::raster::cogwrite::write_file(&out, 0, std::path::Path::new(path)).map_err(|e| format!("{path}: {e}"))?; + let wrote = super::raster::write_and_report(s, &out, 0, true, path)?; info["path"] = json!(path); - info["cog"] = json!({"overviews": cog.levels.len().saturating_sub(1), "bigTiff": cog.big, "bytes": cog.bytes}); + info["cog"] = wrote.get("cog").cloned().unwrap_or(Value::Null); let e = out.extent(); info["extent"] = json!([e.min_x, e.min_y, e.max_x, e.max_y]); RasterLayer::from_path(out, path.clone()) @@ -607,10 +606,9 @@ fn add_output(s: &mut Session, out: Raster, path: Option, classes: bool, // A file written is the layer's source; without one the pixels are kept in the project. let mut rl = match &path { Some(path) => { - let cog = - rustgis_data::raster::cogwrite::write_file(&out, 0, std::path::Path::new(path)).map_err(|e| format!("{path}: {e}"))?; + let wrote = super::raster::write_and_report(s, &out, 0, true, path)?; info["path"] = json!(path); - info["cog"] = json!({"overviews": cog.levels.len().saturating_sub(1), "bigTiff": cog.big, "bytes": cog.bytes}); + info["cog"] = wrote.get("cog").cloned().unwrap_or(Value::Null); RasterLayer::from_path(out, path.clone()) } None => layer_of(out)?, @@ -1287,6 +1285,17 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult { o.name = i.name.clone(); o.classes = i.classes.clone(); }); + out.copyright = first.copyright.clone(); + // The first input's metadata accompanies its pixels; report other credits rather than + // silently dropping them or attributing every input to the first one's owner. + super::raster::say_losses( + s, + rs.iter() + .skip(1) + .filter(|r| r.copyright.is_some() && r.copyright != first.copyright) + .map(|r| (r.name.clone(), "its copyright differs from the first input's and is not kept in the mosaic".to_string())) + .collect(), + ); // Cells rounded when an input was read may be among those the mosaic keeps. out.keep_rounded_marks(rs.iter().map(|r| &**r)); let other: Vec<&str> = rs @@ -1300,8 +1309,7 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult { } let path = ps(p, "path").map(str::trim).filter(|p| !p.is_empty()).map(str::to_string); if let Some(path) = &path { - let bytes = rustgis_data::raster::geotiff::write(&out, 0).map_err(|e| e.to_string())?; - rustgis_data::io::write_atomic(std::path::Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?; + super::raster::write_and_report(s, &out, 0, false, path)?; } let mut v = add(s, out, false)?; if let (Some(o), Some(path)) = (v.as_object_mut(), path) { diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index bf24217ef3..7bfdc4513a 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -954,11 +954,16 @@ fn info(s: &mut Session, p: &Value) -> CmdResult { /// GeoTIFF bytes of a raster layer (band is 1-based; RGB rasters keep their colour bands; `cog` /// makes it a cloud-optimised one). -pub fn geotiff_bytes(s: &Session, p: &Value) -> Result<(String, Vec), String> { +pub fn geotiff_bytes(s: &mut Session, p: &Value) -> Result<(String, Vec), String> { let (_, r) = get(s, p)?; let bi = band(p, "band", &r)?.unwrap_or(0); - let bytes = if b(p, "cog") == Some(true) { raster::cogwrite::write(&r, bi) } else { raster::geotiff::write(&r, bi) }; - Ok((r.name.clone(), bytes.map_err(|e| e.to_string())?)) + let cog = b(p, "cog") == Some(true); + let bytes = if cog { raster::cogwrite::write(&r, bi) } else { raster::geotiff::write(&r, bi) }.map_err(|e| e.to_string())?; + let note = geotiff_note(&r, bi, cog)?; + if !note.is_empty() { + s.notify(format!("Exported {}.tif{note}", r.name), false); + } + Ok((r.name.clone(), bytes)) } /// `share.exportRaster`'s inputs, and the work that writes the raster as a GeoTIFF (a @@ -994,17 +999,43 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res rustgis_data::io::write_atomic(Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?; None }; + let note = geotiff_note(r, bi, cog.is_some())?; + let mut v = match r.overview { + Some((fw, fh)) => json!({"path": path, "width": r.width, "height": r.height, "overviewOf": [fw, fh]}), + None => json!({"path": path}), + }; + if let (Some(o), Some(c)) = (v.as_object_mut(), cog) { + o.insert("cog".into(), json!({"overviews": c.levels.len().saturating_sub(1), "bigTiff": c.big, "bytes": c.bytes})); + } + Ok(super::exports::Wrote { + answer: v, + notes: vec![format!("Exported {path}{note}")], + written_path: Some(path.to_string()), + then: None, + #[cfg(not(target_arch = "wasm32"))] + owner: None, + }) +} + +/// Writes a tool's raster file and reports the same export notes as `share.exportRaster`. +pub(super) fn write_and_report(s: &mut Session, r: &Raster, bi: usize, cog: bool, path: &str) -> Result { + let wrote = write_geotiff(r, bi, cog, path)?; + for note in wrote.notes { + s.notify(note, false); + } + Ok(wrote.answer) +} + +/// What the selected GeoTIFF writer keeps, changes or leaves out, shared by files and downloads. +fn geotiff_note(r: &Raster, bi: usize, cog: bool) -> Result { let mut note = match r.overview { Some((fw, fh)) => format!(" (the {} × {} overview of the {fw} × {fh} raster)", r.width, r.height), None => String::new(), }; // Values changed when the file was read are written as they are held: those of the bands the // writer writes, as it chooses them. - let bands = if cog.is_some() { - raster::cogwrite::written_bands(r, bi).map_err(|e| e.to_string())? - } else { - raster::geotiff::written_bands(r, bi) - }; + let bands = + if cog { raster::cogwrite::written_bands(r, bi).map_err(|e| e.to_string())? } else { raster::geotiff::written_bands(r, bi) }; let written: u64 = bands.iter().filter_map(|b| r.rounded.get(*b)).sum(); if written > 0 { let (values, verb) = values_were(written); @@ -1035,21 +1066,7 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res if spaced > 0 { note.push_str(&format!("; {spaced} band or class name(s) held control characters, written as spaces")); } - let mut v = match r.overview { - Some((fw, fh)) => json!({"path": path, "width": r.width, "height": r.height, "overviewOf": [fw, fh]}), - None => json!({"path": path}), - }; - if let (Some(o), Some(c)) = (v.as_object_mut(), cog) { - o.insert("cog".into(), json!({"overviews": c.levels.len().saturating_sub(1), "bigTiff": c.big, "bytes": c.bytes})); - } - Ok(super::exports::Wrote { - answer: v, - notes: vec![format!("Exported {path}{note}")], - written_path: Some(path.to_string()), - then: None, - #[cfg(not(target_arch = "wasm32"))] - owner: None, - }) + Ok(note) } /// Pixel values of the topmost visible raster under a longitude/latitude point. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 3af618bad5..94e2fbe636 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -16002,6 +16002,114 @@ fn rounded_raster_values_are_said_on_import_and_export() { std::fs::remove_dir_all(dir).unwrap(); } +/// Downloads and files made by mosaic/warp report the rounded values they actually write. +#[test] +fn raster_download_mosaic_and_warp_report_export_losses() { + let dir = std::env::temp_dir().join(format!("rustgis-raster-export-notes-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + for (name, values, rounded) in [("rounded", "16777217 100000001\n1234567.891 7", true), ("clean", "1 2\n3 4", false)] { + let mut s = Session::new(Project::default()); + let path = dir.join(format!("{name}.asc")); + std::fs::write(&path, format!("ncols 2\nnrows 2\nxllcorner 10\nyllcorner 40\ncellsize 1\n{values}")).unwrap(); + let id = s.run("layer.addData", &json!({"path": path, "name": name})).unwrap()["layer"].as_u64().unwrap(); + for cog in [false, true] { + let mark = s.notice_mark(); + let (_, bytes) = crate::cmd::raster::geotiff_bytes(&mut s, &json!({"layer": id, "cog": cog})).unwrap(); + assert_eq!(rustgis_data::raster::read_bytes("download.tif", &bytes).unwrap().width, 2); + let notes = s.notices_since(mark); + assert_eq!( + notes.iter().filter(|n| n.text.contains("3 values in it were rounded to 32-bit floats")).count(), + usize::from(rounded), + "download, cog={cog}: {notes:?}" + ); + } + let points = [[0, 0, 10, 42], [2, 0, 12, 42], [0, 2, 10, 40]]; + for (command, mut params) in [ + ("raster.mosaic", json!({"inputs": [name, name], "name": "mosaic"})), + ("raster.georeference", json!({"layer": name, "transform": "affine", "points": points, "name": "warped"})), + ] { + let output = dir.join(format!("{name}-{command}.tif")); + params["path"] = json!(output); + let mark = s.notice_mark(); + s.run(command, ¶ms).unwrap(); + assert!(rustgis_data::raster::read_path(&output).is_ok()); + let notes = s.notices_since(mark); + assert_eq!( + notes.iter().filter(|n| n.text.contains("some values in it may have been rounded to 32-bit floats")).count(), + usize::from(rounded), + "{command}: {notes:?}" + ); + assert!(!notes.iter().any(|n| n.text.contains("3 values in it")), "{command}: {notes:?}"); + } + } + std::fs::remove_dir_all(dir).unwrap(); +} + +/// Metadata limits are reported from downloads, mosaics and warped files, with the files checked. +#[test] +fn raster_export_paths_report_band_class_and_credit_changes() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + let dir = std::env::temp_dir().join(format!("rustgis-raster-export-metadata-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let mut s = Session::new(Project::default()); + let gt = GeoTransform { origin_x: 10.0, origin_y: 42.0, pixel_width: 1.0, pixel_height: -1.0 }; + let mut r = + Raster::new("Metadata", 2, 2, vec![vec![1.0; 4]; 5], gt, Crs::LonLat, None, ColorHint::Rgb { alpha: false }, "Float32").unwrap(); + r.bands[0].name = "red\u{1}name".into(); + r.bands[0].classes = vec!["zero".into(), "one\u{2}name".into()]; + r.bands[1].classes = vec!["zero".into(), "green class".into()]; + r.copyright = Some("© Owner".into()); + let mut other = r.clone(); + other.name = "Other".into(); + other.copyright = Some("Another owner".into()); + let bytes = geotiff::write(&r, 0).unwrap(); + crate::cmd::raster::add_raster(&mut s, "Metadata", rustgis_map::RasterLayer::from_bytes(r, "metadata.tif", bytes)).unwrap(); + let bytes = geotiff::write(&other, 0).unwrap(); + crate::cmd::raster::add_raster(&mut s, "Other", rustgis_map::RasterLayer::from_bytes(other, "other.tif", bytes)).unwrap(); + let check = |s: &Session, mark, bytes: &[u8]| { + let notes = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect::>().join("\n"); + for text in [ + "3 of its 5 bands written", + "class names of band(s) 2 aren't written", + "Copyright tag holds ASCII", + "2 band or class name(s) held control characters", + ] { + assert!(notes.contains(text), "missing {text}: {notes}"); + } + let written = rustgis_data::raster::read_bytes("written.tif", bytes).unwrap(); + assert_eq!(written.band_count(), 3); + assert_eq!(written.bands[0].name, "red name"); + assert_eq!(written.bands[0].classes, ["zero", "one name"]); + assert_eq!(written.copyright.as_deref(), Some("(c) Owner")); + }; + for cog in [false, true] { + let mark = s.notice_mark(); + let (_, bytes) = crate::cmd::raster::geotiff_bytes(&mut s, &json!({"layer": "Metadata", "cog": cog})).unwrap(); + check(&s, mark, &bytes); + } + let points = [[0, 0, 10, 42], [2, 0, 12, 42], [0, 2, 10, 40]]; + for (cmd, mut params) in [ + ("raster.mosaic", json!({"inputs": ["Metadata", "Metadata"], "name": "mosaic"})), + ( + "raster.georeference", + json!({"layer": "Metadata", "transform": "affine", "points": points, "resampling": "nearest", "name": "warped"}), + ), + ] { + let path = dir.join(format!("{cmd}.tif")); + params["path"] = json!(path); + let mark = s.notice_mark(); + s.run(cmd, ¶ms).unwrap(); + check(&s, mark, &std::fs::read(path).unwrap()); + } + for (second, changed) in [("Metadata", false), ("Other", true)] { + let mark = s.notice_mark(); + let out = s.run("raster.mosaic", &json!({"inputs": ["Metadata", second], "name": "rights"})).unwrap(); + assert_eq!(out_raster(&s, &out).copyright.as_deref(), Some("© Owner")); + assert_eq!(s.notices_since(mark).iter().any(|n| n.text.contains("copyright differs")), changed, "{:?}", s.notices_since(mark)); + } + std::fs::remove_dir_all(dir).unwrap(); +} + /// A raster clipped, resampled, mosaicked, warped or pixel-edited from one with values rounded when it /// was read keeps a mark /// of them, with no count (it holds an unknown share), and its export says so; one made from a diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 0fdcf54a97..2eec2cc271 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -3953,7 +3953,7 @@ impl RustGisApp { let tiles = p.get("tiles").cloned().unwrap_or(Value::Null); return self.run("share.exportRaster", json!({"path": "tiles.tif", "tiles": tiles})); } - match rustgis_engine::cmd::raster::geotiff_bytes(&self.session, &Value::Null) { + match rustgis_engine::cmd::raster::geotiff_bytes(&mut self.session, &Value::Null) { Ok((name, bytes)) => { self.download(&format!("{name}.tif"), bytes); } From 78917398cc02fcd303b2b85ea5f4e1fa5d9d9b56 Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 03:28:36 -0500 Subject: [PATCH 2/5] Raster: keep class labels only for preserved values Report removed class names once with affected band numbers when resampling mixes values. Cover default bilinear, cubic, average, nearest and mode through memory and file output, with palette and clean controls. --- crates/analysis/src/warp.rs | 16 +++--- crates/engine/src/cmd/imagery.rs | 33 +++++++++++- crates/engine/src/tests.rs | 92 ++++++++++++++++++++++++++++++++ docs/user-guide/analysis.md | 7 ++- 4 files changed, 137 insertions(+), 11 deletions(-) diff --git a/crates/analysis/src/warp.rs b/crates/analysis/src/warp.rs index 04abfc22f3..48a27aecb5 100644 --- a/crates/analysis/src/warp.rs +++ b/crates/analysis/src/warp.rs @@ -635,8 +635,8 @@ impl Canvas { } } - /// The output raster on `g`, keeping the source's band labels, class names and credits. - fn finish(&self, r: &Raster, g: &Grid, bands: Vec>, name: String) -> Result { + /// The output on `g`, keeping band labels and credits, and class names when values are preserved. + fn finish(&self, r: &Raster, g: &Grid, bands: Vec>, method: Method, name: String) -> Result { let mut out = Raster::new( name, g.width as u32, @@ -651,7 +651,9 @@ impl Canvas { .map_err(|e| e.to_string())?; for (i, band) in out.bands.iter_mut().enumerate() { band.name = r.bands.get(i).map_or_else(|| "Alpha".to_string(), |b| b.name.clone()); - if let Some(source) = r.bands.get(i) { + if matches!(method, Method::Sample(Resampling::Nearest) | Method::Mode) + && let Some(source) = r.bands.get(i) + { band.classes = source.classes.clone(); } } @@ -760,7 +762,7 @@ fn sampled( let sampler = Sampler { r, method, alpha: canvas.alpha_src, round: !r.data_type.starts_with("Float") }; let mut bands = canvas.bands(ow, oh)?; if ow == 0 { - return canvas.finish(r, g, bands, name); + return canvas.finish(r, g, bands, Method::Sample(method), name); } let x_at = |ox: usize| g.min_x + (ox as f64 + 0.5) * g.cell_x; // A cell's work in `MIN_ROW_WORK`'s units (some 30 ns): its pixels read per band, and its @@ -791,7 +793,7 @@ fn sampled( } }, )?; - canvas.finish(r, g, bands, name) + canvas.finish(r, g, bands, Method::Sample(method), name) } /// `r` on the grid `g`, each cell the mean (or, with `mode`, the most frequent value, with GDAL's @@ -815,7 +817,7 @@ fn aggregated( let nearest = Sampler { r, method: Resampling::Nearest, alpha: None, round }; let mut bands = canvas.bands(ow, oh)?; if ow == 0 { - return canvas.finish(r, g, bands, name); + return canvas.finish(r, g, bands, if mode { Method::Mode } else { Method::Average }, name); } let x_at = |i: usize| g.min_x + i as f64 * g.cell_x; let each = Aggregate { r, canvas: &canvas, nearest, mode, round }; @@ -851,7 +853,7 @@ fn aggregated( each.row(s, &mut line); }, )?; - canvas.finish(r, g, bands, name) + canvas.finish(r, g, bands, if mode { Method::Mode } else { Method::Average }, name) } /// A thread's scratch in [`aggregated`]. diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index b144c70ef3..8d46d82b27 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -323,6 +323,8 @@ fn warp_to_fit(s: &mut Session, p: &Value, layer: &str, r: &Raster) -> CmdResult let new_layer = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).map(|n| n.chars().take(200).collect::()); let out_name = new_layer.clone().unwrap_or_else(|| r.name.clone()); let mut out = warp::warp(r, &fit, &warp::Options { crs, points_crs, resampling, cell, name: out_name })?; + let used = warp::Method::Sample(resampling).used_on(r); + report_resampled_classes(s, r, &out, used.id()); // Resampled from values rounded when the file was read, some or all of them. out.keep_rounded_mark(r); if let Some((fw, fh)) = r.overview { @@ -331,7 +333,7 @@ fn warp_to_fit(s: &mut Session, p: &Value, layer: &str, r: &Raster) -> CmdResult s.notify(note, false); } let mut info = json!({ - "transform": kind.id(), "crs": crs.label(), "pointsCrs": points_crs.label(), "resampling": resampling.id(), + "transform": kind.id(), "crs": crs.label(), "pointsCrs": points_crs.label(), "resampling": used.id(), "width": out.width, "height": out.height, "bands": out.band_count(), "cellSize": out.transform.pixel_width, }); if let Some((fw, fh)) = r.overview { @@ -382,8 +384,10 @@ fn project_raster(s: &mut Session, p: &Value) -> CmdResult { let path = tif_path(p)?; let out_name = name(p, format!("{} projected", r.name)); let out = warp::project(&r, &grid, method, out_name.clone())?; + let used = method.used_on(&r); + report_resampled_classes(s, &r, &out, used.id()); let mut info = json!({ - "name": out_name, "crs": grid.crs.label(), "resampling": method.used_on(&r).id(), "width": out.width, "height": out.height, "bands": out.band_count(), + "name": out_name, "crs": grid.crs.label(), "resampling": used.id(), "width": out.width, "height": out.height, "bands": out.band_count(), "cellSize": [grid.cell_x, grid.cell_y], "corner": [grid.min_x, grid.max_y], }); if let Some((fw, fh)) = r.overview { @@ -395,6 +399,31 @@ fn project_raster(s: &mut Session, p: &Value) -> CmdResult { add_output(s, out, path, false, info) } +/// Report categorical labels removed by value-mixing resampling, with bounded band numbers. +fn report_resampled_classes(s: &mut Session, source: &Raster, out: &Raster, method: &str) { + let mut count = 0usize; + let mut bands = Vec::new(); + for (i, band) in source.bands.iter().enumerate() { + if band.classes.iter().any(|name| !name.is_empty()) && out.bands.get(i).is_some_and(|b| b.classes.is_empty()) { + count += 1; + if bands.len() < 16 { + bands.push((i + 1).to_string()); + } + } + } + if count > 0 { + let more = if count > bands.len() { format!(", … ({} more)", count - bands.len()) } else { String::new() }; + s.notify( + format!( + "{}: class names of {count} band(s) ({}{more}) not kept under {method}: resampling can mix their values", + source.name, + bands.join(", ") + ), + false, + ); + } +} + /// The largest whole number a raster's 32-bit float pixels hold exactly. const EXACT_WHOLE: f64 = 16_777_216.0; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 94e2fbe636..e446285bc3 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -16110,6 +16110,98 @@ fn raster_export_paths_report_band_class_and_credit_changes() { std::fs::remove_dir_all(dir).unwrap(); } +/// Class labels describe source values; interpolating or averaging those values drops the labels +/// with a notice, while nearest, mode and palette-safe sampling retain them. +#[test] +fn raster_resampling_class_names_follow_the_values_and_report_losses() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + let dir = std::env::temp_dir().join(format!("rustgis-resampled-classes-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let mut s = Session::new(Project::default()); + let labels: Vec = ["unknown", "one", "two", "water", "snow", "forest"].map(str::to_owned).to_vec(); + let gt = GeoTransform { origin_x: 10.0, origin_y: 42.0, pixel_width: 1.0, pixel_height: -1.0 }; + for (name, color, labelled, data_type) in [ + ("Classes", ColorHint::Gray, true, "Float32"), + ("Palette", ColorHint::Palette((0..6).map(|i| [i * 30, 0, 0, 255]).collect()), true, "UInt8"), + ("Clean", ColorHint::Gray, false, "Float32"), + ] { + let mut r = Raster::new(name, 2, 2, vec![vec![3.0, 5.0, 5.0, 3.0]], gt, Crs::LonLat, None, color, data_type).unwrap(); + if labelled { + r.bands[0].classes = labels.clone(); + } + let bytes = geotiff::write(&r, 0).unwrap(); + crate::cmd::raster::add_raster(&mut s, name, rustgis_map::RasterLayer::from_bytes(r, format!("{name}.tif"), bytes)).unwrap(); + } + let points = [[0, 0, 10, 42], [2, 0, 12, 42], [0, 2, 10, 40]]; + let params = |cmd: &str, layer: &str| { + if cmd == "raster.georeference" { + json!({"layer":layer, "transform":"affine", "points":points, "cellSize":0.5}) + } else { + json!({"layer":layer, "crs":4326, "extent":[10,40,12,42], "cellSize":2}) + } + }; + let lost = |n: &&crate::Notice| n.text.contains("class names of") && n.text.contains("not kept under"); + for (cmd, requested, method, keep) in [ + ("raster.georeference", None, "bilinear", false), + ("raster.georeference", Some("nearest"), "nearest", true), + ("raster.georeference", Some("cubic"), "cubic", false), + ("raster.project", Some("nearest"), "nearest", true), + ("raster.project", Some("bilinear"), "bilinear", false), + ("raster.project", Some("cubic"), "cubic", false), + ("raster.project", Some("average"), "average", false), + ("raster.project", Some("mode"), "mode", true), + ] { + for file in [false, true] { + let mut p = params(cmd, "Classes"); + p["name"] = json!(format!("{cmd}-{method}-{file}")); + if let Some(method) = requested { + p["resampling"] = json!(method); + } + let path = dir.join(format!("{cmd}-{method}.tif")); + if file { + p["path"] = json!(path); + } + let mark = s.notice_mark(); + let answer = s.run(cmd, &p).unwrap(); + let out = out_raster(&s, &answer); + if keep { + assert_eq!(out.bands[0].classes, labels, "{cmd}/{method}"); + } else { + assert!(out.bands[0].classes.is_empty(), "{cmd}/{method}: labels no longer describe the values"); + assert!(out.bands[0].data.iter().any(|v| v.is_finite() && *v != 3.0 && *v != 5.0), "{cmd}/{method}: values really mixed"); + } + let notes: Vec<_> = s.notices_since(mark).iter().filter(lost).collect(); + assert_eq!(notes.len(), usize::from(!keep), "{cmd}/{method}: {:?}", s.notices_since(mark)); + if !keep { + assert!(notes[0].text.contains("1 band(s) (1)") && notes[0].text.contains(method), "{:?}", notes[0]); + } + if file { + let written = rustgis_data::raster::read_bytes("written.tif", &std::fs::read(path).unwrap()).unwrap(); + assert_eq!(written.bands[0].classes, out.bands[0].classes); + } + } + } + // Palette requests use value-preserving methods, and report the effective method. + for (cmd, requested, effective) in + [("raster.georeference", "bilinear", "nearest"), ("raster.project", "bilinear", "nearest"), ("raster.project", "average", "mode")] + { + let mut p = params(cmd, "Palette"); + p["name"] = json!(format!("palette-{cmd}-{requested}")); + p["resampling"] = json!(requested); + let mark = s.notice_mark(); + let answer = s.run(cmd, &p).unwrap(); + assert_eq!(answer["resampling"], effective); + assert_eq!(out_raster(&s, &answer).bands[0].classes, labels); + assert!(!s.notices_since(mark).iter().any(|n| lost(&n))); + } + for cmd in ["raster.georeference", "raster.project"] { + let mark = s.notice_mark(); + s.run(cmd, ¶ms(cmd, "Clean")).unwrap(); + assert!(!s.notices_since(mark).iter().any(|n| lost(&n))); + } + std::fs::remove_dir_all(dir).unwrap(); +} + /// A raster clipped, resampled, mosaicked, warped or pixel-edited from one with values rounded when it /// was read keeps a mark /// of them, with no count (it holds an unknown share), and its export says so; one made from a diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 7496825335..3b920eddb7 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1397,7 +1397,8 @@ The Analyze tab's imagery groups open their tools in the Workflows pane: `cellSize` (output units) says otherwise. The result replaces the layer's raster, or becomes a new layer with `name`; `path` also writes it as a cloud-optimised GeoTIFF, which then is the layer's file. A raster opened from an overview is warped at that reduced resolution, and the result says - so. + so. Class names are kept with `nearest`; under `bilinear` or `cubic` they are removed with a + notice naming the affected bands, because the values can be mixed. Palette images use `nearest`. - **Control Point Fit** (`raster.gcpFit`) fits a similarity, affine, projective, second- or third-order polynomial, or thin plate spline transform to control points and reports how well it holds: each point's residual in pixels and map units, the RMSE, and each point's error when @@ -1442,7 +1443,9 @@ The Analyze tab's imagery groups open their tools in the Workflows pane: centre. Classes take `nearest` or `mode`, continuous values `bilinear` or `cubic`, or `average` going coarser. A whole-number pixel type keeps its type, so its mean is rounded. On a colour image the colours count as much as their pixels are opaque, and `mode` is taken band by band - (each channel's most frequent value among the pixels that show). + (each channel's most frequent value among the pixels that show). Class names are kept with + `nearest` and `mode`; other methods remove them with a notice naming the affected bands. + Palette images use `nearest` for sampling and `mode` for aggregation, keeping their classes. - Cells off the raster are its no-data value. A raster without one keeps its bands as they are, with NaN there in a floating-point type and 0 in a whole-number one, as GDAL leaves them; a colour image gains an alpha band instead, so they are see-through. From af2c6a93b2b40520626f272fb6147af333027641 Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 05:21:15 -0500 Subject: [PATCH 3/5] Raster: keep precision notices through reprojection --- crates/analysis/src/warp.rs | 1 + crates/engine/src/tests.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/analysis/src/warp.rs b/crates/analysis/src/warp.rs index 48a27aecb5..d0a56a6da8 100644 --- a/crates/analysis/src/warp.rs +++ b/crates/analysis/src/warp.rs @@ -658,6 +658,7 @@ impl Canvas { } } out.copyright = r.copyright.clone(); + out.keep_rounded_mark(r); Ok(out) } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index e446285bc3..64db562ca9 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -16232,12 +16232,30 @@ fn rasters_made_from_rounded_ones_keep_the_rounded_note() { let points = [[0, 0, 10, 42], [2, 0, 12, 42], [0, 2, 10, 40]]; let fit = json!({"layer": name, "transform": "affine", "crs": 4326, "pointsCrs": 4326, "points": points, "name": warped}); s.run("raster.georeference", &fit).unwrap(); + let projected = format!("{name}_projected"); + let projected_file = format!("{name}_projected_file"); + for (output, file) in [(&projected, false), (&projected_file, true)] { + let mut p = json!({"layer":name, "crs":4326, "extent":[10,40,12,42], "cellSize":0.5, "name":output}); + if file { + p["path"] = json!(dir.join(format!("{output}.tif"))); + } + let mark = s.notice_mark(); + s.run("raster.project", &p).unwrap(); + assert_eq!( + s.notices_since(mark).iter().any(|n| n.text.contains(SOME)), + file && rounded, + "{output}: {:?}", + s.notices_since(mark) + ); + } s.run( "raster.editPixels", &json!({"layer": name, "polygon": [[10.1, 40.1], [10.9, 40.1], [10.9, 40.9], [10.1, 40.9]], "value": 1}), ) .unwrap(); - for layer in [clip.as_str(), half.as_str(), joined.as_str(), warped.as_str(), name.as_str()] { + for layer in + [clip.as_str(), half.as_str(), joined.as_str(), warped.as_str(), projected.as_str(), projected_file.as_str(), name.as_str()] + { let mark = s.notice_mark(); s.run("share.exportRaster", &json!({"layer": layer, "path": dir.join(format!("{layer}.tif"))})).unwrap(); let notes = s.notices_since(mark); From 6aeb8a97a1170afe57dceacfd9993c58589a193c Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 05:51:58 -0500 Subject: [PATCH 4/5] Raster: distinguish outside cells from named class zero --- crates/analysis/src/warp.rs | 27 ++++++++++++++++++++++++--- crates/engine/src/cmd/raster.rs | 6 +++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/analysis/src/warp.rs b/crates/analysis/src/warp.rs index d0a56a6da8..d278d9b868 100644 --- a/crates/analysis/src/warp.rs +++ b/crates/analysis/src/warp.rs @@ -589,8 +589,8 @@ impl Canvas { /// How `r` is held: as a `picture` (a scan being georeferenced), a grey byte image gains /// alpha as a colour one does; as data, it keeps its one band, and a cell with no value is - /// its no-data, else NaN in a floating-point type and 0 in a whole-number one, as GDAL leaves - /// it. + /// its no-data, else NaN for floating-point or named-class data and 0 in other whole-number + /// data. Named class zero must remain distinct from cells outside the coverage. fn of(r: &Raster, picture: bool) -> Canvas { let nb = r.bands.len(); let alpha_src = match r.color { @@ -606,7 +606,8 @@ impl Canvas { let add_alpha = ((rgb && nb == 3) || gray) && alpha_src.is_none() && r.nodata.is_none(); // Cells with no value: no-data where the raster has some. Colours with alpha are zero there. let clear = alpha_src.is_some() || add_alpha; - let none = if picture || rgbi || r.data_type.starts_with("Float") { f32::NAN } else { 0.0 }; + let labelled = r.bands.iter().any(|b| !b.classes.is_empty()); + let none = if picture || rgbi || labelled || r.data_type.starts_with("Float") { f32::NAN } else { 0.0 }; let outside = if clear { 0.0 } else { r.nodata.map_or(none, |d| d as f32) }; let color = match (add_alpha, rgb) { (true, true) => ColorHint::Rgb { alpha: true }, @@ -1442,6 +1443,26 @@ mod tests { assert_eq!((out.band_count(), out.value(0, 0, 0), out.value(0, 3, 1)), (1, None, Some(3.5))); } + #[test] + fn projected_classes_keep_zero_distinct_from_outside_cells() { + let gt = GeoTransform { origin_x: 0.0, origin_y: 2.0, pixel_width: 1.0, pixel_height: -1.0 }; + let mut r = Raster::new("c", 2, 2, vec![vec![0.0, 1.0, 1.0, 0.0]], gt, Crs::LonLat, None, ColorHint::Gray, "UInt8").unwrap(); + r.bands[0].classes = vec!["water".into(), "land".into()]; + let g = Grid::snapped(r.crs, [-1.0, 0.0, 2.0, 2.0], (1.0, 1.0), (0.0, 0.0)).unwrap(); + for method in [Method::Sample(Resampling::Nearest), Method::Mode] { + let out = project(&r, &g, method, "p".into()).unwrap(); + assert_eq!(out.value(0, 0, 0), None, "outside must not become class zero"); + assert_eq!(out.value(0, 1, 0), Some(0.0)); + assert_eq!(out.bands[0].class_name(0.0), Some("water")); + assert_eq!(out.bands[0].classes, r.bands[0].classes); + let bytes = rustgis_data::raster::geotiff::write(&out, 0).unwrap(); + let written = rustgis_data::raster::read_bytes("p.tif", &bytes).unwrap(); + assert_eq!(written.value(0, 0, 0), None); + assert_eq!(written.value(0, 1, 0), Some(0.0)); + assert_eq!(written.bands[0].classes, r.bands[0].classes); + } + } + /// Grids with the same cell and origin line up wherever their areas are; a grid too large to /// hold is refused before anything is made. #[test] diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 7bfdc4513a..94d5fb9953 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -1021,7 +1021,11 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res pub(super) fn write_and_report(s: &mut Session, r: &Raster, bi: usize, cog: bool, path: &str) -> Result { let wrote = write_geotiff(r, bi, cog, path)?; for note in wrote.notes { - s.notify(note, false); + // Tools write their default bands; selecting another is an export command parameter. + s.notify( + note.replace("give \"band\" for another", "export the resulting layer with share.exportRaster and give \"band\" for another"), + false, + ); } Ok(wrote.answer) } From f1056520ca4290c9b71d3f61881f4c0f295b08a9 Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 06:38:55 -0500 Subject: [PATCH 5/5] Raster: preserve categorical COG codes and export paths --- crates/analysis/src/warp.rs | 11 ++++----- crates/data/src/raster/cogwrite.rs | 20 ++++++++++++++++- crates/engine/src/cmd/raster.rs | 22 +++++++++--------- crates/engine/src/tests.rs | 36 ++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/crates/analysis/src/warp.rs b/crates/analysis/src/warp.rs index d278d9b868..6d54fe150a 100644 --- a/crates/analysis/src/warp.rs +++ b/crates/analysis/src/warp.rs @@ -1455,11 +1455,12 @@ mod tests { assert_eq!(out.value(0, 1, 0), Some(0.0)); assert_eq!(out.bands[0].class_name(0.0), Some("water")); assert_eq!(out.bands[0].classes, r.bands[0].classes); - let bytes = rustgis_data::raster::geotiff::write(&out, 0).unwrap(); - let written = rustgis_data::raster::read_bytes("p.tif", &bytes).unwrap(); - assert_eq!(written.value(0, 0, 0), None); - assert_eq!(written.value(0, 1, 0), Some(0.0)); - assert_eq!(written.bands[0].classes, r.bands[0].classes); + for bytes in [rustgis_data::raster::geotiff::write(&out, 0).unwrap(), rustgis_data::raster::cogwrite::write(&out, 0).unwrap()] { + let written = rustgis_data::raster::read_bytes("p.tif", &bytes).unwrap(); + assert_eq!(written.value(0, 0, 0), None); + assert_eq!(written.value(0, 1, 0), Some(0.0)); + assert_eq!(written.bands[0].classes, r.bands[0].classes); + } } } diff --git a/crates/data/src/raster/cogwrite.rs b/crates/data/src/raster/cogwrite.rs index 2bd3b8cdfc..17c2c7d67f 100644 --- a/crates/data/src/raster/cogwrite.rs +++ b/crates/data/src/raster/cogwrite.rs @@ -190,7 +190,7 @@ fn plan(r: &Raster, band: usize) -> Result { }; let max = if sample == Sample::Byte { 255.0 } else { 65_535.0 }; // A few distinct whole numbers are classes; photographs and rasters with a range are not. - let categorical = sample != Sample::Float && b.stats.categories.as_ref().is_some_and(|c| c.len() <= 64); + let categorical = !b.classes.is_empty() || (sample != Sample::Float && b.stats.categories.as_ref().is_some_and(|c| c.len() <= 64)); Ok(Plan { bands: vec![band], sample, @@ -750,6 +750,24 @@ mod tests { assert_eq!(at(0, 3, 1), 14); } + #[test] + fn raster_export_review_named_classes_keep_codes_in_float_overviews() { + let (w, h) = (700u32, 4u32); + let data = (0..w * h).map(|i| if i % w < 2 { f32::NAN } else { (i % 2) as f32 }).collect(); + let mut r = utm(w, h, vec![data], ColorHint::Gray, "UInt8", None); + r.bands[0].classes = vec!["Water".into(), "Land".into()]; + let bytes = write(&r, 0).unwrap(); + let ov = geotiff::read_limited(geotiff::reader(Cursor::new(bytes.as_slice())).unwrap(), "o", 1500, None).unwrap(); + assert_eq!((ov.width, ov.height), (350, 2)); + assert_eq!(ov.value(0, 0, 0), None); + assert!(ov.bands[0].data.iter().all(|v| v.is_nan() || *v == 0.0 || *v == 1.0), "no averaged class codes"); + assert_eq!(ov.bands[0].classes, r.bands[0].classes); + r.bands[0].classes.clear(); + let bytes = write(&r, 0).unwrap(); + let ov = geotiff::read_limited(geotiff::reader(Cursor::new(bytes.as_slice())).unwrap(), "o", 1500, None).unwrap(); + assert_eq!(ov.value(0, 2, 0), Some(0.5), "continuous float overviews still average"); + } + #[test] fn edges_of_odd_sizes_average_what_is_there() { // 1025 × 3 floats: the second overview's last column averages one pixel column, and the diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 94d5fb9953..868574843b 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -959,7 +959,7 @@ pub fn geotiff_bytes(s: &mut Session, p: &Value) -> Result<(String, Vec), St let bi = band(p, "band", &r)?.unwrap_or(0); let cog = b(p, "cog") == Some(true); let bytes = if cog { raster::cogwrite::write(&r, bi) } else { raster::geotiff::write(&r, bi) }.map_err(|e| e.to_string())?; - let note = geotiff_note(&r, bi, cog)?; + let note = geotiff_note(&r, bi, cog, "give \"band\" for another")?; if !note.is_empty() { s.notify(format!("Exported {}.tif{note}", r.name), false); } @@ -991,6 +991,11 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result Result { + write_geotiff_with_guidance(r, bi, cog, path, "give \"band\" for another") +} + +/// Builds the band instruction before combining the note with any caller-supplied path or name. +fn write_geotiff_with_guidance(r: &Raster, bi: usize, cog: bool, path: &str, band_hint: &str) -> Result { let cog = if cog { // Tiles go to the file as they are made: the whole file is never held in memory. Some(raster::cogwrite::write_file(r, bi, Path::new(path)).map_err(|e| format!("{path}: {e}"))?) @@ -999,7 +1004,7 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res rustgis_data::io::write_atomic(Path::new(path), &bytes).map_err(|e| format!("{path}: {e}"))?; None }; - let note = geotiff_note(r, bi, cog.is_some())?; + let note = geotiff_note(r, bi, cog.is_some(), band_hint)?; let mut v = match r.overview { Some((fw, fh)) => json!({"path": path, "width": r.width, "height": r.height, "overviewOf": [fw, fh]}), None => json!({"path": path}), @@ -1019,19 +1024,16 @@ pub(super) fn write_geotiff(r: &Raster, bi: usize, cog: bool, path: &str) -> Res /// Writes a tool's raster file and reports the same export notes as `share.exportRaster`. pub(super) fn write_and_report(s: &mut Session, r: &Raster, bi: usize, cog: bool, path: &str) -> Result { - let wrote = write_geotiff(r, bi, cog, path)?; + let wrote = + write_geotiff_with_guidance(r, bi, cog, path, "export the resulting layer with share.exportRaster and give \"band\" for another")?; for note in wrote.notes { - // Tools write their default bands; selecting another is an export command parameter. - s.notify( - note.replace("give \"band\" for another", "export the resulting layer with share.exportRaster and give \"band\" for another"), - false, - ); + s.notify(note, false); } Ok(wrote.answer) } /// What the selected GeoTIFF writer keeps, changes or leaves out, shared by files and downloads. -fn geotiff_note(r: &Raster, bi: usize, cog: bool) -> Result { +fn geotiff_note(r: &Raster, bi: usize, cog: bool, band_hint: &str) -> Result { let mut note = match r.overview { Some((fw, fh)) => format!(" (the {} × {} overview of the {fw} × {fh} raster)", r.width, r.height), None => String::new(), @@ -1053,7 +1055,7 @@ fn geotiff_note(r: &Raster, bi: usize, cog: bool) -> Result { // The bands the writer leaves out (it writes `band`, or a colour raster's colour bands, which // `band` doesn't change) are said, names and all. if bands.len() < r.bands.len() { - let which = if bands == [bi] { "give \"band\" for another" } else { "its colour bands" }; + let which = if bands == [bi] { band_hint } else { "its colour bands" }; note.push_str(&format!("; {} of its {} bands written ({which})", bands.len(), r.bands.len())); } // The file keeps the first written band's class names only (as dataset `CLASS_` items). diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 64db562ca9..44c3d475be 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -16202,6 +16202,42 @@ fn raster_resampling_class_names_follow_the_values_and_report_losses() { std::fs::remove_dir_all(dir).unwrap(); } +#[test] +fn raster_export_review_tool_notice_preserves_path_and_band_guidance() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + let dir = std::env::temp_dir().join(format!("rustgis-mosaic-band-guidance-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let filename = if cfg!(target_os = "windows") { "mosaic.tif" } else { "give \"band\" for another.tif" }; + let path = dir.join(filename); + let gt = GeoTransform { origin_x: 0.0, origin_y: 2.0, pixel_width: 1.0, pixel_height: -1.0 }; + let r = Raster::new( + "Multi", + 2, + 2, + vec![vec![1.0, 2.0, 3.0, 4.0], vec![5.0, 6.0, 7.0, 8.0]], + gt, + Crs::LonLat, + None, + ColorHint::Gray, + "Float32", + ) + .unwrap(); + let bytes = geotiff::write(&r, 0).unwrap(); + let mut s = Session::new(Project::default()); + crate::cmd::raster::add_raster(&mut s, "Multi", rustgis_map::RasterLayer::from_bytes(r, "multi.tif", bytes)).unwrap(); + let mark = s.notice_mark(); + s.run("raster.mosaic", &json!({"inputs":["Multi","Multi"],"name":"Mosaic","path":path})).unwrap(); + assert!(path.is_file()); + let notes = s.notices_since(mark); + let note = ¬es.iter().find(|n| n.text.starts_with("Exported ")).unwrap().text; + assert!(note.starts_with(&format!("Exported {}", path.display())), "{note}"); + assert!(note.contains("1 of its 2 bands written"), "{note}"); + let instruction = "export the resulting layer with share.exportRaster and give \"band\" for another"; + assert_eq!(note.matches(instruction).count(), 1, "{note}"); + std::fs::remove_dir_all(dir).unwrap(); +} + /// A raster clipped, resampled, mosaicked, warped or pixel-edited from one with values rounded when it /// was read keeps a mark /// of them, with no count (it holds an unknown share), and its export says so; one made from a