From 1520b883463d52f5c1b39849a8e17737939fe71a Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 11:21:10 -0300 Subject: [PATCH 01/25] feat(imagery): spectral profile: chart and compare a multiband raster's bands at points The Workflows pane's Spectral Analysis section becomes a spectral profile: pick a raster of two bands or more, turn on Pick Points and click. Each click adds a point until it is turned off. Up to six points are drawn as lines, each in the colour of a numbered marker on the map. The x axis is wavelength (nm) when the file gives one for every band, in wavelength order, and band number otherwise. Hovering shows a band's name, wavelength and each point's value; a point can be taken off; Clear and Export CSV... (desktop, a row a band, a column a point; names guarded against spreadsheet formulas). Identify's pop-up on a pixel of two bands or more gets a Spectral Profile button that adds that pixel and opens the section. After GeoLibre's spectral profile (opengeos/geolibre#1818). Bands gain `wavelength` (nm), read from GDAL's metadata tag and the .aux.xml sidecar: CENTRAL_WAVELENGTH_UM (GDAL's IMAGERY domain), or `wavelength` / `central_wavelength` in the unit `wavelength_units` gives (with none, a value under 100 is micrometres). Items that aren't a length are said; read ones are no longer counted as band items not read. The GeoTIFF writers write them back, and the commands that copy band names (georeference, mosaic, clip, pixel editor, project) copy them too. `raster.spectral` takes `points` ([[lon, lat], ...], up to 1000) for a profile each; a point off the raster keeps its place with null values and is said. Every answer carries `wavelengths` when the file gives any. A zone is read before points, and points before lon/lat: what isn't read is said. Closes #1622. Co-Authored-By: Claude Opus 5.5 --- crates/analysis/src/warp.rs | 1 + crates/data/src/raster/mod.rs | 18 +- crates/data/src/raster/pam.rs | 210 +++++++++++- crates/engine/src/cmd/imagery.rs | 83 ++++- crates/engine/src/tests.rs | 66 ++++ crates/ui-egui/src/analysispane.rs | 14 +- crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/mapview.rs | 18 ++ crates/ui-egui/src/panes.rs | 4 + crates/ui-egui/src/ribbon.rs | 11 +- crates/ui-egui/src/spectralui.rs | 496 +++++++++++++++++++++++++++++ docs/user-guide/analysis.md | 18 +- docs/user-guide/data.md | 10 +- 13 files changed, 921 insertions(+), 29 deletions(-) create mode 100644 crates/ui-egui/src/spectralui.rs diff --git a/crates/analysis/src/warp.rs b/crates/analysis/src/warp.rs index 6fea0f594d..9866cc1398 100644 --- a/crates/analysis/src/warp.rs +++ b/crates/analysis/src/warp.rs @@ -651,6 +651,7 @@ 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()); + band.wavelength = r.bands.get(i).and_then(|b| b.wavelength); } Ok(out) } diff --git a/crates/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index 3ca2d63e19..4363b8cbee 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -506,6 +506,9 @@ pub struct Band { /// The names of the band's classes, by value, from GDAL's `.aux.xml` sidecar (see /// [`Band::class_name`]). Empty when the band names no class. pub classes: Vec, + /// The band's central wavelength in nanometres, from GDAL's metadata (the file's tag or its + /// sidecar, [`pam::wavelength_nm`]); `None` when the file gives none. + pub wavelength: Option, } impl Band { @@ -613,11 +616,13 @@ pub(crate) fn band_name_of(r: &Raster, band: usize) -> Option<&str> { } /// GDAL's metadata tag (42112) for the bands `bands` of `r`, written as samples in that order: -/// their names, and the first one's class names. Both GeoTIFF writers take it from here. +/// their names and wavelengths, and the first one's class names. Both GeoTIFF writers take it +/// from here. pub(crate) fn gdal_metadata_of(r: &Raster, bands: &[usize]) -> Option { let names: Vec> = bands.iter().map(|b| band_name_of(r, *b)).collect(); + let wavelengths: Vec> = bands.iter().map(|b| r.bands.get(*b).and_then(|b| b.wavelength)).collect(); let first = bands.first().and_then(|b| r.bands.get(*b)).map_or(&[][..], |b| &b.classes[..]); - pam::gdal_metadata(&names, first) + pam::gdal_metadata(&names, first, &wavelengths) } /// Text for a TIFF ASCII tag, which holds 7-bit ASCII only: control characters (a NUL would end @@ -744,6 +749,7 @@ impl Raster { stats: if measure { BandStats::compute(&data, valid) } else { BandStats::default() }, data, classes: Vec::new(), + wavelength: None, }) .collect(); let mut r = Raster { @@ -784,7 +790,10 @@ impl Raster { let beyond = pam .bands .iter() - .filter(|b| (b.band == 0 || b.band > self.bands.len()) && (b.description.is_some() || !b.categories.is_empty())) + .filter(|b| { + (b.band == 0 || b.band > self.bands.len()) + && (b.description.is_some() || !b.categories.is_empty() || b.wavelength.is_some()) + }) .count(); if beyond > 0 { self.lost.push(format!("{from}: names for {beyond} band(s) it doesn't have (it has {}) left out", self.bands.len())); @@ -840,6 +849,9 @@ impl Raster { if let Some(p) = p.filter(|p| !p.categories.is_empty()) { band.classes = p.categories.clone(); } + if let Some(w) = p.and_then(|p| p.wavelength) { + band.wavelength = Some(w); + } let stored = p.and_then(|p| p.stats).filter(|_| overview); if recompute || stored.is_some() { band.stats = BandStats::compute_with(&band.data, valid, stored); diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 064089fe6f..e9fc418f35 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -52,6 +52,8 @@ pub struct PamBand { /// The names of the band's classes, by value (GDAL's category names): `categories[v]` names /// value `v`, and is empty when that value has none. Empty when the band names no class. pub categories: Vec, + /// The band's central wavelength in nanometres, when the file gives one ([`wavelength_nm`]). + pub wavelength: Option, } /// What a sidecar says about a raster's bands. @@ -94,6 +96,53 @@ impl Pam { } } +/// GDAL's item for a band's central wavelength, in micrometres (its `IMAGERY` metadata domain, +/// GDAL 3.10 and later). +pub const CENTRAL_WAVELENGTH_UM: &str = "CENTRAL_WAVELENGTH_UM"; + +/// The band items that give a wavelength, in the order one is taken: GDAL's own, then the +/// `wavelength` and `central_wavelength` items rasterio, STAC exports and ENVI files write. +const WAVELENGTH_ITEMS: [&str; 3] = [CENTRAL_WAVELENGTH_UM, "wavelength", "central_wavelength"]; + +/// The item some files give the unit of their `wavelength` items in (ENVI's `wavelength units`). +const WAVELENGTH_UNITS: &str = "wavelength_units"; + +/// Whether a band metadata item named `name` is read as (or for) a wavelength. +fn is_wavelength_item(name: &str) -> bool { + name.eq_ignore_ascii_case(WAVELENGTH_UNITS) || WAVELENGTH_ITEMS.iter().any(|w| name.eq_ignore_ascii_case(w)) +} + +/// A wavelength item's value in nanometres: `None` when it isn't a positive number, or its unit +/// isn't a length this knows. `CENTRAL_WAVELENGTH_UM` is in micrometres by its name; another item +/// is in the unit `units` gives (nanometres or micrometres, as ENVI and others spell them). With no +/// unit (or ENVI's `Unknown`), a value under 100 is in micrometres and any other in nanometres: +/// imaging sensors see from about 0.3 to 15 µm (300 to 15,000 nm), so the two never meet. +pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option { + let v = text.trim().parse::().ok().filter(|v| v.is_finite() && *v > 0.0)?; + // Micrometres in nanometres, to a millionth of one: 0.665 µm is 665 nm, not 665.0000000000001. + let nm = |um: f64| (um * 1e9).round() / 1e6; + if item.eq_ignore_ascii_case(CENTRAL_WAVELENGTH_UM) { + return Some(nm(v)); + } + let unit = units.map(|u| u.trim().to_lowercase()).unwrap_or_default(); + let micro = match unit.as_str() { + "" | "unknown" => v < 100.0, + "nm" | "nanometer" | "nanometers" | "nanometre" | "nanometres" => false, + "um" | "\u{b5}m" | "\u{3bc}m" | "micrometer" | "micrometers" | "micrometre" | "micrometres" | "micron" | "microns" => true, + _ => return None, + }; + Some(if micro { nm(v) } else { v }) +} + +/// A sidecar node's metadata item `key` (any domain, any case): its text. +fn mdi_text<'a>(node: roxmltree::Node<'a, '_>, key: &str) -> Option<&'a str> { + node.children() + .filter(|n| n.has_tag_name("Metadata")) + .flat_map(|m| m.children()) + .find(|n| n.has_tag_name("MDI") && n.attribute("key").is_some_and(|k| k.eq_ignore_ascii_case(key))) + .and_then(|n| n.text()) +} + /// The sidecars GDAL would read for `path`: `x.tif.aux.xml`, then `x.aux.xml`. pub fn sidecar_paths(path: &Path) -> Vec { let mut long = path.as_os_str().to_owned(); @@ -155,8 +204,10 @@ fn parse_sidecar(text: &str) -> Result { // from 1 to `MAX_BANDS`, a band's records after its first, and the scale, offset, units and // other band metadata items and colour tables that aren't read, counted. let (mut cut, mut beyond, mut odd_bands, mut unscaled, mut units) = (0usize, 0usize, 0usize, 0usize, 0usize); - let (mut items, mut tables, mut odd_nodata) = (0usize, 0usize, 0usize); + let (mut items, mut tables, mut odd_nodata, mut odd_wavelengths) = (0usize, 0usize, 0usize, 0usize); let (mut seen, mut twice) = (std::collections::HashSet::new(), 0usize); + // The unit the dataset gives its bands' wavelengths in, for a band that gives none. + let dataset_units = mdi_text(root, WAVELENGTH_UNITS); let mut kept = |t: &str| { cut += usize::from(t.chars().nth(MAX_NAME).is_some()); t.chars().take(MAX_NAME).collect::() @@ -183,8 +234,16 @@ fn parse_sidecar(text: &str) -> Result { .children() .filter(|n| n.has_tag_name("Metadata")) .flat_map(|m| m.children()) - .filter(|n| n.has_tag_name("MDI") && n.attribute("key").is_none_or(|k| !k.starts_with("STATISTICS_"))) + .filter(|n| n.has_tag_name("MDI") && n.attribute("key").is_none_or(|k| !k.starts_with("STATISTICS_") && !is_wavelength_item(k))) .count(); + // Its wavelength: the first item there is of those that give one, in any domain (GDAL's in + // `IMAGERY`, ENVI's in the default one). + let units = mdi_text(b, WAVELENGTH_UNITS).or(dataset_units); + let wavelength = WAVELENGTH_ITEMS.iter().find_map(|w| mdi_text(b, w).map(|t| (w, t))).and_then(|(w, t)| { + let nm = wavelength_nm(w, t, units); + odd_wavelengths += usize::from(nm.is_none()); + nm + }); // A colour table here isn't read (only the file's own is): one with entries is counted. tables += usize::from(b.children().any(|n| n.has_tag_name("ColorTable") && n.children().any(|e| e.has_tag_name("Entry")))); let description = text_of("Description").filter(|d| !d.is_empty()).map(&mut kept); @@ -222,7 +281,7 @@ fn parse_sidecar(text: &str) -> Result { while categories.last().is_some_and(String::is_empty) { categories.pop(); } - pam.bands.push(PamBand { band, description, nodata, stats, categories }); + pam.bands.push(PamBand { band, description, nodata, stats, categories, wavelength }); } if odd_bands > 0 { pam.lost.push(format!("{odd_bands} band record(s) for no band from 1 to {MAX_BANDS} left out")); @@ -245,6 +304,9 @@ fn parse_sidecar(text: &str) -> Result { if odd_nodata > 0 { pam.lost.push(format!("{odd_nodata} no-data value(s) that aren't numbers left out: those pixels are read as values")); } + if odd_wavelengths > 0 { + pam.lost.push(format!("{odd_wavelengths} wavelength(s) that aren't a positive length in nanometres or micrometres left out")); + } if beyond > 0 { pam.lost.push(format!("{beyond} class name(s) past the first {MAX_CATEGORIES} of a band left out")); } @@ -312,6 +374,7 @@ pub fn parse_gdal_metadata(text: &str) -> Option { twice += 1; } } + let odd_wavelengths = read_wavelengths(root, &mut pam); // A band's scale and offset aren't applied: the values stay as stored. One that changes // nothing (a scale of 1, an offset of 0) isn't counted. let unscaled = root @@ -345,6 +408,8 @@ pub fn parse_gdal_metadata(text: &str) -> Option { .filter(|n| n.has_tag_name("Item") && (n.attribute("sample").is_some() || n.attribute("role").is_some())) .filter(|n| !matches!(n.attribute("role"), Some("description" | "scale" | "offset"))) .filter(|n| n.attribute("name").is_none_or(|name| !name.starts_with("CLASS_") && !name.starts_with("STATISTICS_"))) + // Wavelengths, a band's, are read (one that isn't a length is counted with them). + .filter(|n| n.attribute("sample").is_none() || n.attribute("name").is_none_or(|name| !is_wavelength_item(name))) .count(); if unread > 0 { pam.lost.push(format!("{unread} band item(s) not read: left out")); @@ -376,6 +441,11 @@ pub fn parse_gdal_metadata(text: &str) -> Option { if odd_classes > 0 { pam.lost.push(format!("{odd_classes} CLASS_ item(s) whose value isn't a whole number below {MAX_CATEGORIES} left out")); } + if odd_wavelengths > 0 { + pam.lost.push(format!( + "{odd_wavelengths} wavelength item(s) that aren't a positive length in nanometres or micrometres, or for no band from 1 to {MAX_BANDS}, left out" + )); + } if twice > 0 { pam.lost.push(format!("{twice} band or class name(s) given again left out (the first is kept)")); } @@ -391,6 +461,54 @@ pub fn parse_gdal_metadata(text: &str) -> Option { (!pam.bands.is_empty() || !pam.lost.is_empty()).then_some(pam) } +/// Reads the bands' wavelengths of GDAL's metadata tag into `pam`: a band's (`sample`) first item +/// of [`WAVELENGTH_ITEMS`], in that order, in the unit of its band's `wavelength_units` item, else +/// the dataset's. A band's other wavelength items are other names for the same, and aren't +/// counted. Returns how many items were left out: not a length, or for no band. +fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> usize { + let named = |n: &roxmltree::Node<'_, '_>, name: &str| { + n.has_tag_name("Item") && n.attribute("name").is_some_and(|a| a.eq_ignore_ascii_case(name)) + }; + let band_of = |n: &roxmltree::Node<'_, '_>| { + n.attribute("sample").and_then(|v| v.trim().parse::().ok()).and_then(|s| s.checked_add(1)).filter(|n| *n <= MAX_BANDS) + }; + // The units given, by band (0: the dataset's). + let mut units: std::collections::HashMap = std::collections::HashMap::new(); + for item in root.children().filter(|n| named(n, WAVELENGTH_UNITS)) { + let at = if item.attribute("sample").is_none() { Some(0) } else { band_of(&item) }; + if let (Some(at), Some(t)) = (at, item.text()) { + units.entry(at).or_insert(t); + } + } + // Where each band already described is in `pam.bands`. + let mut at: std::collections::HashMap = pam.bands.iter().enumerate().map(|(i, b)| (b.band, i)).collect(); + let mut odd = 0usize; + for name in WAVELENGTH_ITEMS { + // A dataset item (no `sample`) belongs to no band, as GeoLibre also reads it: not counted. + for item in root.children().filter(|n| named(n, name) && n.attribute("sample").is_some()) { + let Some(band) = band_of(&item) else { + odd += 1; + continue; + }; + let unit = units.get(&band).or_else(|| units.get(&0)).copied(); + let Some(nm) = wavelength_nm(name, item.text().unwrap_or_default(), unit) else { + odd += 1; + continue; + }; + match at.get(&band).and_then(|i| pam.bands.get_mut(*i)) { + Some(b) => { + b.wavelength.get_or_insert(nm); + } + None => { + at.insert(band, pam.bands.len()); + pam.bands.push(PamBand { band, wavelength: Some(nm), ..PamBand::default() }); + } + } + } + } + odd +} + /// Whether XML can't hold `c` (a control character other than a tab or a line break, or a /// noncharacter): [`gdal_metadata`] writes it as a space. pub fn xml_cannot_hold(c: char) -> bool { @@ -403,7 +521,10 @@ pub fn xml_cannot_hold(c: char) -> bool { /// are written whole, in plain ASCII (other characters as XML references), as the tag's type is. /// A character XML can't hold (a control character other than a tab or a line break) is written /// as a space: names read from GDAL's metadata never hold one, since XML can't. -pub fn gdal_metadata(descriptions: &[Option<&str>], classes: &[String]) -> Option { +/// +/// Each band's wavelength in `wavelengths` (nanometres, in the order of `descriptions`) is written +/// as GDAL's `CENTRAL_WAVELENGTH_UM`, in micrometres, in its `IMAGERY` domain. +pub fn gdal_metadata(descriptions: &[Option<&str>], classes: &[String], wavelengths: &[Option]) -> Option { let mut items = String::new(); let mut put = |open: String, name: &str| { items.push_str(&open); @@ -429,6 +550,12 @@ pub fn gdal_metadata(descriptions: &[Option<&str>], classes: &[String]) -> Optio put(format!(""), name.trim()); } } + for (sample, nm) in wavelengths.iter().enumerate() { + if let Some(nm) = nm.filter(|v| v.is_finite() && *v > 0.0) { + items + .push_str(&format!("{}", nm / 1000.0)); + } + } (!items.is_empty()).then(|| format!("{items}")) } @@ -439,16 +566,16 @@ mod tests { #[test] fn band_and_class_names_written_are_read_back() { let classes = vec![String::new(), "Floresta & savana".into(), String::new(), "Formação ".into()]; - let xml = gdal_metadata(&[Some("classe_mapbiomas"), None], &classes).unwrap(); + let xml = gdal_metadata(&[Some("classe_mapbiomas"), None], &classes, &[]).unwrap(); assert!(xml.is_ascii(), "{xml}"); let pam = parse_gdal_metadata(&xml).unwrap(); assert_eq!(pam.band(1).unwrap().categories, classes); assert_eq!(pam.band(1).unwrap().description.as_deref(), Some("classe_mapbiomas")); assert!(pam.band(2).is_none() && pam.lost.is_empty(), "{pam:?}"); // Nothing named, no document. - assert_eq!(gdal_metadata(&[None, Some(" ")], &[String::new(), " ".into()]), None); + assert_eq!(gdal_metadata(&[None, Some(" ")], &[String::new(), " ".into()], &[None, Some(f64::NAN)]), None); // A character XML can't hold is written as a space, so the document still reads. - let xml = gdal_metadata(&[Some("a\u{1}b")], &[]).unwrap(); + let xml = gdal_metadata(&[Some("a\u{1}b")], &[], &[]).unwrap(); assert_eq!(parse_gdal_metadata(&xml).unwrap().band(1).unwrap().description.as_deref(), Some("a b")); } @@ -722,6 +849,75 @@ mod tests { } } + /// Wavelengths in GDAL's metadata tag: GDAL's own in micrometres first, then `wavelength` and + /// `central_wavelength` in the unit given (the band's, else the dataset's) or, with none, by + /// their size; those that aren't a length, or are for no band, are said. They aren't counted + /// as band items not read. + #[test] + fn reads_wavelengths_from_gdal_metadata() { + let xml = r#" + 0.4825 + 999 + 0.5613 + 654.6 + 865 + Nanometers + 1.6 + far + 2.2 + furlongs + 1 + 500 +"#; + let pam = parse_gdal_metadata(xml).unwrap(); + let w = |b: usize| pam.band(b).and_then(|b| b.wavelength); + assert_eq!( + [w(1), w(2), w(3), w(4), w(5), w(6), w(7)], + [Some(482.5), Some(561.3), Some(654.6), Some(865.0), Some(1600.0), None, None] + ); + assert_eq!(pam.lost.len(), 1, "{:?}", pam.lost); + assert!(pam.lost[0].starts_with("3 wavelength item(s)"), "{:?}", pam.lost); + // A unit the dataset gives is a band's when it gives none; then a large value may be in µm. + let xml = + r#"micrometers120"#; + let pam = parse_gdal_metadata(xml).unwrap(); + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(120_000.0)); + assert!(pam.lost.is_empty(), "{:?}", pam.lost); + } + + /// Wavelengths written (as GDAL's `CENTRAL_WAVELENGTH_UM`) are read back as they were. + #[test] + fn wavelengths_written_are_read_back() { + let xml = gdal_metadata(&[None, None, Some("SWIR 2")], &[], &[Some(482.5), None, Some(2200.0)]).unwrap(); + assert!(xml.contains(r#"name="CENTRAL_WAVELENGTH_UM" sample="0" domain="IMAGERY">0.4825<"#), "{xml}"); + let pam = parse_gdal_metadata(&xml).unwrap(); + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(482.5)); + assert!(pam.band(2).is_none()); + assert_eq!(pam.band(3).and_then(|b| b.wavelength), Some(2200.0)); + assert_eq!(pam.band(3).and_then(|b| b.description.as_deref()), Some("SWIR 2")); + assert!(pam.lost.is_empty(), "{:?}", pam.lost); + } + + /// A sidecar's wavelengths, in GDAL's `IMAGERY` domain or ENVI's default one; one that isn't a + /// length is said, and other band items still are. + #[test] + fn reads_wavelengths_from_a_sidecar() { + let pam = parse( + r#" + Nanometers + 0.6650.03 + 842 + far +"#, + ) + .unwrap(); + let w = |b: usize| pam.band(b).and_then(|b| b.wavelength); + assert_eq!([w(1), w(2), w(3)], [Some(665.0), Some(842.0), None]); + assert_eq!(pam.lost.len(), 2, "{:?}", pam.lost); + assert!(pam.lost.iter().any(|l| l.starts_with("1 band metadata item(s) not read")), "{:?}", pam.lost); + assert!(pam.lost.iter().any(|l| l.starts_with("1 wavelength(s)")), "{:?}", pam.lost); + } + #[test] fn sidecar_names() { let p = sidecar_paths(Path::new("/d/scene.tif")); diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 50fed65538..4eb22235fa 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -55,7 +55,7 @@ pub fn commands() -> Vec { .params(r#"{"before": raster, "after": raster, "band"?: 1-based, "method"?: "difference"|"ratio"|"percent", "name"?, "ran"?: (internal: a raster the app has worked out)}"#), C::new("raster.spectral", "Spectral Analysis", "Analyze › Image Analysis", spectral).inputs(spectral_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() - .params(r#"{"layer": raster, "lon"?, "lat"?, "zone"?: polygon layer (the selected or first polygon)}"#), + .params(r#"{"layer": raster, "lon"?, "lat"?, "points"?: [[lon, lat], …] (up to 1000: a profile each, in "profiles"; one off the raster has a null pixel and values, and is said), "zone"?: polygon layer (the mean of its selected or first polygon's pixels)}"#), C::new("raster.classify", "Classification Wizard", "Analyze › Image Classification", |s, p| { super::workflows::made_or_ran(s, p, "raster.classify") }).inputs(raster_inputs).roles(&[]).effects(crate::policy::Effects::NONE) @@ -216,6 +216,7 @@ fn rebuild(r: &Raster, transform: GeoTransform, crs: Crs, bands: Vec>) .map_err(|e| e.to_string())?; for (o, i) in out.bands.iter_mut().zip(&r.bands) { o.name = i.name.clone(); + o.wavelength = i.wavelength; } Ok(out) } @@ -847,10 +848,73 @@ pub(crate) fn change_work(s: &Session, p: &Value) -> Result CmdResult { let r = raster(s, p)?; let all: Vec = (0..r.band_count()).collect(); let names: Vec = r.bands.iter().map(|b| b.name.clone()).collect(); + // The bands' wavelengths, when the file gives any (`null` for a band without one). + let wavelengths: Vec> = r.bands.iter().map(|b| b.wavelength).collect(); + let answer = |mut v: Value| { + if wavelengths.iter().any(Option::is_some) { + v["wavelengths"] = json!(wavelengths); + v["wavelengthUnit"] = json!("nm"); + } + Ok(v) + }; + // A zone is read before points, and points before one point: what isn't read is said. + let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null()); + let left_out = if spectral_zone(p).is_some() && given("points") { + Some("points") + } else if given("points") && (given("lon") || given("lat")) { + Some("lon and lat") + } else { + None + }; + if let Some(what) = left_out { + let read = if spectral_zone(p).is_some() { "the zone's mean is read" } else { "points are read" }; + s.notify(format!("{}: {what} left out: {read}", r.name), false); + } + if let Some(points) = p.get("points").filter(|v| !v.is_null() && spectral_zone(p).is_none()) { + let points = points.as_array().ok_or("points is a list of [lon, lat]")?; + if points.is_empty() { + return Err("points is empty: give at least one [lon, lat]".into()); + } + if points.len() > MAX_SPECTRAL_POINTS { + return Err(format!("{} points given: at most {MAX_SPECTRAL_POINTS} are read in one run", points.len())); + } + let mut profiles = Vec::with_capacity(points.len()); + let mut outside = 0usize; + for (i, pt) in points.iter().enumerate() { + let xy = pt + .as_array() + .filter(|a| a.len() == 2) + .and_then(|a| Some((a.first()?.as_f64()?, a.get(1)?.as_f64()?))) + .filter(|(x, y)| x.is_finite() && y.is_finite()) + .ok_or_else(|| format!("point {} isn't [lon, lat]", i + 1))?; + profiles.push(match r.pixel_at(Coord::new(xy.0, xy.1)) { + Some((col, row)) => { + let vals: Vec> = all.iter().map(|b| r.value(*b, col, row)).collect(); + json!({"lon": xy.0, "lat": xy.1, "pixel": [col, row], "values": vals}) + } + None => { + outside += 1; + json!({"lon": xy.0, "lat": xy.1, "pixel": null, "values": vec![Value::Null; all.len()]}) + } + }); + } + if outside == points.len() { + return Err(if outside == 1 { "the point is outside the raster".into() } else { "every point is outside the raster".into() }); + } + if outside > 0 { + s.notify(format!("{}: {outside} of {} points fall outside the raster: their values are null", r.name, points.len()), false); + } + return answer(json!({"bands": names, "profiles": profiles})); + } if let Some(z) = spectral_zone(p) { let l = s .map() @@ -863,12 +927,12 @@ fn spectral(s: &mut Session, p: &Value) -> CmdResult { }; let rings = polys.first().ok_or("the zone has no polygon")?; let (means, n) = rasterops::zonal_means(&r, &all, rings).ok_or("no pixels fall in the zone")?; - return Ok(json!({"bands": names, "values": means, "pixels": n})); + return answer(json!({"bands": names, "values": means, "pixels": n})); } - let c = Coord::new(f(p, "lon").ok_or("give lon and lat, or a zone")?, f(p, "lat").ok_or("lat is required")?); + let c = Coord::new(f(p, "lon").ok_or("give lon and lat, points or a zone")?, f(p, "lat").ok_or("lat is required")?); let (col, row) = r.pixel_at(c).ok_or("the point is outside the raster")?; let vals: Vec> = all.iter().map(|b| r.value(*b, col, row)).collect(); - Ok(json!({"bands": names, "values": vals, "pixel": [col, row]})) + answer(json!({"bands": names, "values": vals, "pixel": [col, row]})) } fn supervised(s: &mut Session, p: &Value) -> CmdResult { @@ -1286,6 +1350,7 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult { out.bands.iter_mut().zip(&first.bands).for_each(|(o, i)| { o.name = i.name.clone(); o.classes = i.classes.clone(); + o.wavelength = i.wavelength; }); // Cells rounded when an input was read may be among those the mosaic keeps. out.keep_rounded_marks(rs.iter().map(|r| &**r)); @@ -1386,7 +1451,10 @@ fn clip_window( let o = r.transform.to_world(f64::from(cols.start), f64::from(rows.start)); let gt = GeoTransform { origin_x: o.x, origin_y: o.y, ..r.transform }; let mut out = Raster::new(name, w, h, bands, gt, r.crs, nodata, r.color.clone(), r.data_type.clone()).map_err(|e| e.to_string())?; - out.bands.iter_mut().zip(&r.bands).for_each(|(o, i)| o.name = i.name.clone()); + out.bands.iter_mut().zip(&r.bands).for_each(|(o, i)| { + o.name = i.name.clone(); + o.wavelength = i.wavelength; + }); // Some of the values rounded when the file was read may be in the window: how many isn't known. out.keep_rounded_mark(r); Ok(out) @@ -1478,7 +1546,10 @@ fn edit_pixels(s: &mut Session, p: &Value) -> CmdResult { let nodata = if f(p, "value").is_none() && r.nodata.is_none() { Some(NODATA as f64) } else { r.nodata }; let mut out = Raster::new(r.name.clone(), r.width, r.height, bands, r.transform, r.crs, nodata, r.color.clone(), r.data_type.clone()) .map_err(|e| e.to_string())?; - out.bands.iter_mut().zip(&r.bands).for_each(|(o, i)| o.name = i.name.clone()); + out.bands.iter_mut().zip(&r.bands).for_each(|(o, i)| { + o.name = i.name.clone(); + o.wavelength = i.wavelength; + }); // The edit may have overwritten some of the values rounded when the file was read. out.keep_rounded_mark(&r); replace(s, &layer, out)?; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f0c80f39ae..e9f4fe9cd2 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -34342,3 +34342,69 @@ fn bundled_ca_roots_are_said_once_a_session() { other.say_bundled_roots(true); assert_eq!(said(&other), 1); } + +/// `raster.spectral` reads several points (one off the raster said, its values null), and every +/// answer gives the bands' wavelengths in nanometres, read back from the GeoTIFF's GDAL metadata; +/// a raster whose file gives none has none. +#[test] +fn spectral_profiles_at_points_with_wavelengths() { + use base64::Engine; + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + let tiff = |wavelengths: bool| { + let gt = GeoTransform { origin_x: 10.0, origin_y: 45.0, pixel_width: 0.01, pixel_height: -0.01 }; + // Red, green, blue: 30 + i, 20 + i, 10 + i at pixel i. + let bands = (0..3).map(|b| (0..16).map(|i| (30 - b * 10 + i) as f32).collect()).collect(); + let mut r = Raster::new("scene", 4, 4, bands, gt, Crs::LonLat, None, ColorHint::Rgb { alpha: false }, "UInt8").unwrap(); + if wavelengths { + for (band, nm) in r.bands.iter_mut().zip([665.0, 560.0, 490.0]) { + band.wavelength = Some(nm); + } + } + base64::engine::general_purpose::STANDARD.encode(geotiff::write(&r, 0).unwrap()) + }; + let mut s = Session::new(Project::default()); + s.run("layer.addDataBytes", &json!({"fileName": "scene.tif", "data": tiff(true)})).unwrap(); + s.run("layer.addDataBytes", &json!({"fileName": "plain.tif", "data": tiff(false)})).unwrap(); + let mark = s.notice_mark(); + let v = s.run("raster.spectral", &json!({"layer": "scene", "points": [[10.005, 44.995], [10.015, 44.985], [20.0, 10.0]]})).unwrap(); + assert_eq!(v["wavelengths"], json!([665.0, 560.0, 490.0]), "{v}"); + assert_eq!(v["wavelengthUnit"], "nm"); + assert_eq!(v["bands"].as_array().map(Vec::len), Some(3)); + assert_eq!(v["profiles"][0]["pixel"], json!([0, 0])); + assert_eq!(v["profiles"][0]["values"], json!([30.0, 20.0, 10.0])); + assert_eq!(v["profiles"][1]["pixel"], json!([1, 1])); + assert_eq!(v["profiles"][1]["values"], json!([35.0, 25.0, 15.0])); + assert_eq!(v["profiles"][2]["pixel"], Value::Null); + assert_eq!(v["profiles"][2]["values"], json!([null, null, null])); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert_eq!(said, ["scene: 1 of 3 points fall outside the raster: their values are null"]); + // All inside: nothing said. + let mark = s.notice_mark(); + s.run("raster.spectral", &json!({"layer": "scene", "points": [[10.005, 44.995]]})).unwrap(); + assert!(s.notices_since(mark).is_empty()); + // One point and a zone give the wavelengths too; a file without them gives none. + let one = s.run("raster.spectral", &json!({"layer": "scene", "lon": 10.005, "lat": 44.995})).unwrap(); + assert_eq!(one["wavelengths"], json!([665.0, 560.0, 490.0])); + assert_eq!(one["values"], json!([30.0, 20.0, 10.0])); + let plain = s.run("raster.spectral", &json!({"layer": "plain", "points": [[10.005, 44.995]]})).unwrap(); + assert!(plain.get("wavelengths").is_none(), "{plain}"); + // Points read before one point: the point left out is said. + let mark = s.notice_mark(); + let both = s.run("raster.spectral", &json!({"layer": "scene", "lon": 10.015, "lat": 44.985, "points": [[10.005, 44.995]]})).unwrap(); + assert_eq!(both["profiles"][0]["pixel"], json!([0, 0])); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert_eq!(said, ["scene: lon and lat left out: points are read"]); + // Points that can't be read are errors. + for (points, error) in [ + (json!([[20.0, 10.0], [30.0, 10.0]]), "every point is outside the raster"), + (json!([[20.0, 10.0]]), "the point is outside the raster"), + (json!([]), "points is empty"), + (json!("10,45"), "points is a list"), + (json!([[10.0, 45.0], [10.0]]), "point 2 isn't [lon, lat]"), + (json!([[10.0, "45"]]), "point 1 isn't [lon, lat]"), + (json!(vec![[10.005, 44.995]; 1001]), "at most 1000"), + ] { + let e = s.run("raster.spectral", &json!({"layer": "scene", "points": points})).unwrap_err(); + assert!(e.contains(error), "{e}"); + } +} diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 0d3add75d3..70e58c1f94 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -47,6 +47,8 @@ pub struct Workflow { /// ([`rustgis_engine::Session::opened`]); `None` until the pane first shows. script_project: Option, pub trend: TrendForm, + /// The Spectral Analysis section's profile: the points read and charted. + pub spectral: crate::spectralui::Spectral, pub selected_only: Option, pub zonal_stats: ZonalStatsForm, /// A workflow running on a thread of its own: its progress and Cancel show, and what it made @@ -868,14 +870,7 @@ pub fn workflows(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } "trend" => trend(app, ui, &rasters), - "spectral" => { - heading(ui, "Band values at a point"); - combo(ui, "wf_sp", "Layer", &mut app.workflow.layer, &rasters); - pick_button(app, ui, "spectral", 1, "Click a pixel on the map"); - if let Some(p) = picked(app, "spectral").and_then(|v| v.first().copied()) { - run(app, "raster.spectral", json!({"layer": app.workflow.layer, "lon": p.x, "lat": p.y})); - } - } + "spectral" => crate::spectralui::section(app, ui, t), "classify" => { heading(ui, "Unsupervised classes (k-means)"); combo(ui, "wf_k", "Layer", &mut app.workflow.layer, &rasters); @@ -1173,7 +1168,8 @@ pub fn workflows(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } } - if let Some(p) = &app.workflow.pick { + // The spectral profile picks on until turned off: its button shows it, with no count. + if let Some(p) = app.workflow.pick.as_ref().filter(|p| p.purpose != crate::spectralui::PURPOSE) { ui.label( egui::RichText::new(crate::i18n::fmt( "Click the map: {n} of {total}", diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 0fdcf54a97..afa52baa73 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -93,6 +93,7 @@ mod shortcutfile; mod shortcutui; mod showme; mod sourcespane; +mod spectralui; mod sqlbox; mod sqlpane; mod stacui; diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 7f144f16f1..3db94cfce9 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -3051,6 +3051,8 @@ fn overlays(app: &mut RustGisApp, ui: &mut Ui, p: &egui::Painter, rect: Rect, t: } // The NLDI's point, while its pane is open. crate::nldiui::paint(app, p, rect, t, |c| lonlat_to_screen(app, rect, c)); + // The spectral profile's points, while its section shows. + crate::spectralui::paint(app, p, t, |c| lonlat_to_screen(app, rect, c)); // Points picked for a workflow. if let Some(pick) = &app.workflow.pick { let pts = pick.points.clone(); @@ -3515,6 +3517,7 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { follow_popup(app); let Some(pp) = &app.map.popup else { return }; let t = Tokens::get(ui.ctx()); + let at = pp.at; let Some(anchor) = lonlat_to_screen(app, rect, pp.at) else { return }; if !rect.contains(anchor) { return; @@ -3532,6 +3535,9 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { }; let mut step: Option = None; let mut zoom = false; + // A pixel of a raster of two bands or more: its layer, for the spectral profile. + let spectral = crate::spectralui::offered(app, &info); + let mut profile = false; // The feature shown, as its layer id and feature id (a raster pixel has neither). let shown = info["layerId"].as_u64().and_then(|id| u32::try_from(id).ok()).zip(info["fid"].as_u64()); // Only WMS layers being asked so far, or still waited for: their names. @@ -3595,6 +3601,15 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { if ui.add(egui::Button::new(tl!("×")).frame(false)).clicked() { close = true; } + // A pixel of several bands: add it to the spectral profile. + if spectral.is_some() { + let (r, resp) = ui.allocate_exact_size(vec2(18.0, 18.0), Sense::click()); + let ink = if resp.hovered() { t.text } else { t.icon }; + crate::icons::paint(ui.painter(), r, "chart", ink, t.accent); + if resp.on_hover_text(tl!("Spectral Profile: chart every band here, and compare points")).clicked() { + profile = true; + } + } // A feature (not a raster pixel): zoom to it. if shown.is_some() { let (r, resp) = ui.allocate_exact_size(vec2(18.0, 18.0), Sense::click()); @@ -3705,6 +3720,9 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { app.session.zoom_to(&e); } } + if let (true, Some(id)) = (profile, spectral) { + crate::spectralui::from_identify(app, id, at); + } if close { app.map.popup = None; } else if let (Some(i), Some(pp)) = (step, &mut app.map.popup) { diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 00c0740fae..022e32a17e 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -58,6 +58,10 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui) { if app.ui.right != Some(RightPane::TimeSeries) && app.workflow.pick.as_ref().is_some_and(|p| p.purpose == "ts_point") { app.workflow.pick = None; } + // So for the spectral profile's points, which it goes on picking until turned off. + if app.ui.right != Some(RightPane::Workflows) && app.workflow.pick.as_ref().is_some_and(|p| p.purpose == crate::spectralui::PURPOSE) { + app.workflow.pick = None; + } // A failed renderer switch waits for a field only while the Symbology pane shows its layer. if app.ui.right == Some(RightPane::Symbology) { let _ = pending_symbology(app, ui.ctx()); diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 7c919bac6d..05ac1e1b34 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -4042,7 +4042,16 @@ fn imagery_groups(app: &mut RustGisApp, ui: &mut Ui) { wf("change"), false, ); - small(ui, app, "graduated", "Spectral Analysis", "Every band's value at a pixel.", "ui.pane.workflows", wf("spectral"), false); + small( + ui, + app, + "graduated", + "Spectral Analysis", + "Chart every band at points on the map, and compare them.", + "ui.pane.workflows", + wf("spectral"), + false, + ); }); }); group(ui, "Image Classification", |ui| { diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs new file mode 100644 index 0000000000..0e93023d4d --- /dev/null +++ b/crates/ui-egui/src/spectralui.rs @@ -0,0 +1,496 @@ +//! The spectral profile: a multiband raster's bands charted at points clicked on the map, up to +//! six compared, against wavelength when the file gives one for every band and band number +//! otherwise. It is the Workflows pane's Spectral Analysis section, and Identify's pop-up on a +//! pixel of two or more bands adds that pixel to it. After GeoLibre's spectral profile +//! (opengeos/geolibre#1818, MIT). + +use egui::{Color32, Pos2, Sense, Stroke, Ui, pos2, vec2}; +use serde_json::{Value, json}; + +use crate::RustGisApp; +use crate::theme::{Tokens, regular, semibold}; + +/// The most points compared, a line each (as the Time Series pane keeps). +pub const MAX_POINTS: usize = 6; + +/// The Workflows pane's section the profile is. +pub const SECTION: &str = "spectral"; + +/// What map clicks are for while the section picks points: each adds one. +pub const PURPOSE: &str = "spectral"; + +/// A point read: where, its pixel, and every band's value there (`None` where it has no data). +#[derive(Clone, Debug, PartialEq)] +pub struct Sample { + /// Its number, from 1: it keeps its colour and marker when an older point goes. + pub n: u32, + pub lon: f64, + pub lat: f64, + pub pixel: (i64, i64), + pub values: Vec>, +} + +/// The profile: the raster read, its bands' names and wavelengths, and the points read on it. +#[derive(Default)] +pub struct Spectral { + pub layer: String, + pub bands: Vec, + /// Each band's wavelength in nanometres; empty when the file gives none. + pub wavelengths: Vec>, + /// Oldest first. + pub samples: Vec, + /// The last point's number. + last: u32, + /// What the last action said (an error when true). + pub message: Option<(String, bool)>, +} + +impl Spectral { + /// Empties it. + pub fn clear(&mut self) { + self.samples.clear(); + self.message = None; + } + + /// The bands' places along the chart's x axis: their wavelengths when every band has one, + /// else their numbers; and whether they are wavelengths. + pub fn axis(&self) -> (Vec, bool) { + let all: Option> = self.wavelengths.iter().copied().collect(); + match all.filter(|w| !w.is_empty() && w.len() == self.bands.len()) { + Some(w) => (w, true), + None => ((1..=self.bands.len()).map(|b| b as f64).collect(), false), + } + } +} + +/// A point's colour, by its number: the same on the chart, its legend and its marker. +pub fn colour(t: &Tokens, n: u32) -> Color32 { + let colours = [ + t.accent, + Color32::from_rgb(214, 96, 77), + Color32::from_rgb(77, 146, 33), + Color32::from_rgb(118, 42, 131), + Color32::from_rgb(230, 171, 2), + Color32::from_rgb(27, 158, 119), + ]; + colours.get((n.max(1) as usize - 1) % colours.len()).copied().unwrap_or(t.accent) +} + +/// The rasters a profile can be read on: those of two bands or more. +pub fn multiband(app: &RustGisApp) -> Vec { + app.session + .map() + .layers + .iter() + .filter(|l| l.raster.as_ref().and_then(|r| r.data.as_ref()).is_some_and(|r| r.band_count() >= 2)) + .map(|l| l.name.clone()) + .collect() +} + +/// Whether Identify's result `info` is a pixel of a raster of two bands or more: its layer's id. +pub fn offered(app: &RustGisApp, info: &Value) -> Option { + if info["raster"].as_bool() != Some(true) { + return None; + } + let id = info["layerId"].as_u64().and_then(|id| u32::try_from(id).ok())?; + let layer = app.session.map().layer(id)?; + layer.raster.as_ref().and_then(|r| r.data.as_ref()).filter(|r| r.band_count() >= 2).map(|_| id) +} + +/// Identify's Spectral Profile button: opens the section on the layer `id` and adds the point. +pub fn from_identify(app: &mut RustGisApp, id: u32, at: rustgis_engine::geom::Coord) { + let Some(name) = app.session.map().layer(id).map(|l| l.name.clone()) else { return }; + if let Err(e) = app.run("ui.pane.workflows", json!({"section": SECTION})) { + app.session.notify(e, true); + return; + } + app.workflow.layer = name.clone(); + add_point(app, &name, at); +} + +/// Reads every band of `layer` at `at` and adds it to the profile, a raster other than the one +/// read so far starting it again; the oldest point goes past [`MAX_POINTS`]. What goes wrong is +/// said in the section. +pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Coord) { + let r = app.run("raster.spectral", json!({"layer": layer, "lon": at.x, "lat": at.y})); + let sp = &mut app.workflow.spectral; + let v = match r { + Ok(v) => v, + Err(e) => { + sp.message = Some((e, true)); + return; + } + }; + let bands: Vec = v["bands"].as_array().into_iter().flatten().map(|b| b.as_str().unwrap_or_default().to_string()).collect(); + let wavelengths: Vec> = v["wavelengths"].as_array().into_iter().flatten().map(Value::as_f64).collect(); + // Another raster, or the same one's bands changed: its points aren't comparable. + if sp.layer != layer || sp.bands != bands || sp.wavelengths != wavelengths { + sp.samples.clear(); + sp.layer = layer.to_string(); + sp.bands = bands; + sp.wavelengths = wavelengths; + } + let pixel = (v["pixel"][0].as_i64().unwrap_or(0), v["pixel"][1].as_i64().unwrap_or(0)); + let values: Vec> = v["values"].as_array().into_iter().flatten().map(Value::as_f64).collect(); + sp.last += 1; + sp.samples.push(Sample { n: sp.last, lon: at.x, lat: at.y, pixel, values }); + let excess = sp.samples.len().saturating_sub(MAX_POINTS); + sp.samples.drain(..excess); + sp.message = (excess > 0).then(|| { + let n = MAX_POINTS.to_string(); + (crate::i18n::fmt("Up to {n} points are compared: the oldest one was taken off.", &[("n", &n)]), false) + }); +} + +/// The section: the raster, Pick Points, the chart and its points. +pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { + ui.add_space(6.0); + ui.label(egui::RichText::new(tl!("Spectral profile")).font(semibold(12.5))); + ui.add_space(2.0); + let rasters = multiband(app); + if rasters.is_empty() { + ui.label( + egui::RichText::new(tl!("Add a raster of two or more bands first, such as a Landsat or Sentinel-2 scene.")).weak().small(), + ); + return; + } + if !rasters.contains(&app.workflow.layer) { + app.workflow.layer = if rasters.contains(&app.workflow.spectral.layer) { + app.workflow.spectral.layer.clone() + } else { + rasters.first().cloned().unwrap_or_default() + }; + } + egui::ComboBox::from_id_salt("wf_sp") + .selected_text(app.workflow.layer.as_str()) + .width(180.0) + .show_ui(ui, |ui| { + for o in &rasters { + ui.selectable_value(&mut app.workflow.layer, o.clone(), o.as_str()); + } + }) + .response + .on_hover_text(tl!("Layer")); + // Points on another raster aren't comparable with this one's. + if app.workflow.spectral.layer != app.workflow.layer && !app.workflow.spectral.samples.is_empty() { + app.workflow.spectral.clear(); + } + // Points clicked on the map while picking: each is read, and picking goes on. + let clicked = match &mut app.workflow.pick { + Some(pick) if pick.purpose == PURPOSE => std::mem::take(&mut pick.points), + _ => Vec::new(), + }; + let layer = app.workflow.layer.clone(); + for c in clicked { + add_point(app, &layer, c); + } + ui.horizontal_wrapped(|ui| { + let picking = app.workflow.pick.as_ref().is_some_and(|p| p.purpose == PURPOSE); + if ui + .selectable_label(picking, tl!("Pick Points")) + .on_hover_text(tl!("Each click on the map adds a point, until you click this again.")) + .clicked() + { + app.workflow.pick = + if picking { None } else { Some(crate::analysispane::Pick { purpose: PURPOSE, needed: 1, points: Vec::new() }) }; + } + if !app.workflow.spectral.samples.is_empty() { + if ui.button(tl!("Clear")).clicked() { + app.workflow.spectral.clear(); + } + #[cfg(not(target_arch = "wasm32"))] + if ui.button(tl!("Export CSV…")).clicked() + && let Some(path) = crate::dialogs::pick_save("spectral_profile.csv", &["csv"], tl!("Spectral profile")) + { + let sp = &mut app.workflow.spectral; + sp.message = Some(match std::fs::write(&path, csv(sp)) { + Ok(()) => (crate::i18n::fmt("Saved {path}", &[("path", &path)]), false), + Err(e) => (format!("{path}: {e}"), true), + }); + } + } + }); + if let Some((text, error)) = &app.workflow.spectral.message { + let colour = if *error { t.red } else { t.text_dim }; + ui.label(egui::RichText::new(text.as_str()).small().color(colour)); + } + if app.workflow.spectral.samples.is_empty() { + ui.label( + egui::RichText::new(tl!( + "Pick points on the map, or click a pixel with Explore and press Spectral Profile in its pop-up, to chart every band there." + )) + .weak() + .small(), + ); + return; + } + chart(&app.workflow.spectral, ui, t); + // The points, in their lines' colours, each with a button to take it off. + let mut remove = None; + for (i, s) in app.workflow.spectral.samples.iter().enumerate() { + ui.horizontal(|ui| { + let c = colour(t, s.n); + let (n, lon, lat) = (s.n.to_string(), format!("{:.5}", s.lon), format!("{:.5}", s.lat)); + let (col, row) = (s.pixel.0.to_string(), s.pixel.1.to_string()); + let text = crate::i18n::fmt( + "● {n} {lon}°, {lat}° · pixel {col}, {row}", + &[("n", &n), ("lon", &lon), ("lat", &lat), ("col", &col), ("row", &row)], + ); + ui.label(egui::RichText::new(text).small().color(c)); + if ui.add(egui::Button::new(egui::RichText::new("×").small()).frame(false)).on_hover_text(tl!("Take this point off")).clicked() + { + remove = Some(i); + } + }); + } + if let Some(i) = remove { + app.workflow.spectral.samples.remove(i); + } +} + +/// The chart: a line for each point through its bands, in the order of the x axis; hovering +/// shows a band's name, wavelength and the points' values. +fn chart(sp: &Spectral, ui: &mut Ui, t: &Tokens) { + let (xs, by_wavelength) = sp.axis(); + // The bands in the order of the axis: a stack written red, green, blue draws no zigzag. + let mut order: Vec = (0..xs.len()).collect(); + order.sort_by(|a, b| xs.get(*a).copied().unwrap_or(0.0).total_cmp(&xs.get(*b).copied().unwrap_or(0.0))); + let (x0, x1) = xs.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), x| (a.min(*x), b.max(*x))); + let values = sp.samples.iter().flat_map(|s| s.values.iter().flatten()); + let (lo, hi) = values.fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), v| (a.min(*v), b.max(*v))); + if !(x0.is_finite() && lo.is_finite()) { + ui.label(egui::RichText::new(tl!("No data at the points picked.")).weak().small()); + return; + } + let (x1, hi) = (if x1 > x0 { x1 } else { x0 + 1.0 }, if hi > lo { hi } else { lo + 1.0 }); + let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 190.0), Sense::hover()); + let painter = ui.painter_at(rect); + painter.rect_filled(rect, 4.0, t.canvas); + let fmt = |v: f64| rustgis_engine::map::symbology::fmt_num(v); + // A gutter on the left for the values at the top and bottom, clear of the lines, and room for + // the axis' range under it. + let gutter = [hi, lo].iter().map(|v| crate::widgets::text_w(ui, &fmt(*v), regular(10.0))).fold(0.0_f32, f32::max) + 12.0; + let plot = egui::Rect::from_min_max(rect.min + vec2(gutter, 12.0), rect.max - vec2(12.0, 30.0)); + let to = |x: f64, v: f64| { + pos2(plot.min.x + ((x - x0) / (x1 - x0)) as f32 * plot.width(), plot.max.y - ((v - lo) / (hi - lo)) as f32 * plot.height()) + }; + // A tick under each band. + for x in &xs { + let px = to(*x, lo).x; + painter.line_segment([pos2(px, plot.max.y + 2.0), pos2(px, plot.max.y + 6.0)], Stroke::new(1.0, t.text_dim)); + } + painter.line_segment([pos2(plot.min.x, plot.max.y + 2.0), pos2(plot.max.x, plot.max.y + 2.0)], Stroke::new(1.0, t.border_strong)); + for s in &sp.samples { + let c = colour(t, s.n); + // Runs between bands with no data, so a gap shows as one. + let mut runs: Vec> = vec![Vec::new()]; + for b in &order { + match (xs.get(*b), s.values.get(*b).copied().flatten()) { + (Some(x), Some(v)) => runs.last_mut().into_iter().for_each(|r| r.push(to(*x, v))), + _ if runs.last().is_some_and(|r| !r.is_empty()) => runs.push(Vec::new()), + _ => {} + } + } + for run in runs.into_iter().filter(|r| !r.is_empty()) { + for p in &run { + painter.circle_filled(*p, 2.5, c); + } + if run.len() > 1 { + painter.add(egui::Shape::line(run, Stroke::new(1.6, c))); + } + } + } + painter.text(pos2(plot.min.x - 6.0, plot.min.y), egui::Align2::RIGHT_CENTER, fmt(hi), regular(10.0), t.text_dim); + painter.text(pos2(plot.min.x - 6.0, plot.max.y), egui::Align2::RIGHT_CENTER, fmt(lo), regular(10.0), t.text_dim); + let axis = if by_wavelength { + crate::i18n::fmt("Wavelength {from}–{to} nm", &[("from", &fmt(x0)), ("to", &fmt(x1))]) + } else { + let n = sp.bands.len().to_string(); + crate::i18n::fmt("Bands 1–{n}", &[("n", &n)]) + }; + painter.text(rect.center_bottom() + vec2(0.0, -4.0), egui::Align2::CENTER_BOTTOM, axis, regular(10.0), t.text_dim); + // Hovering: the band nearest the pointer, marked, and every point's value there. + let near = resp.hover_pos().and_then(|pos| { + (0..xs.len()).min_by(|a, b| { + let d = |i: &usize| xs.get(*i).map_or(f32::INFINITY, |x| (to(*x, lo).x - pos.x).abs()); + d(a).total_cmp(&d(b)) + }) + }); + if let Some(b) = near { + let px = xs.get(b).map_or(plot.min.x, |x| to(*x, lo).x); + painter.line_segment([pos2(px, plot.min.y), pos2(px, plot.max.y)], Stroke::new(1.0, t.text_dim)); + resp.on_hover_ui_at_pointer(|ui| { + let name = sp.bands.get(b).map_or("", String::as_str); + let number = (b + 1).to_string(); + let title = match sp.wavelengths.get(b).copied().flatten() { + Some(w) => { + crate::i18n::fmt("Band {n}: {name}, {wavelength} nm", &[("n", &number), ("name", name), ("wavelength", &fmt(w))]) + } + None => crate::i18n::fmt("Band {n}: {name}", &[("n", &number), ("name", name)]), + }; + ui.label(egui::RichText::new(title).small().strong()); + for s in &sp.samples { + let v = s.values.get(b).copied().flatten().map_or_else(|| tl!("no data").to_string(), fmt); + ui.label(egui::RichText::new(format!("● {} {v}", s.n)).small().color(colour(t, s.n))); + } + }); + } +} + +/// The profile as CSV: a row a band (its number, name and wavelength in nanometres), a column a +/// point, headed with its number and place. A name a spreadsheet would run as a formula is +/// written with a leading `'`, as the table's export writes it. +#[cfg(not(target_arch = "wasm32"))] +pub fn csv(sp: &Spectral) -> String { + let text = |s: &str| { + let guarded = if s.starts_with(['=', '+', '-', '@', '\t', '\r']) { format!("'{s}") } else { s.to_string() }; + format!("\"{}\"", guarded.replace('"', "\"\"")) + }; + let mut out = String::from("band,name,wavelength_nm"); + for s in &sp.samples { + out.push(','); + out.push_str(&text(&format!("point {} ({}, {})", s.n, s.lon, s.lat))); + } + out.push('\n'); + for (b, name) in sp.bands.iter().enumerate() { + let w = sp.wavelengths.get(b).copied().flatten().map(|w| w.to_string()).unwrap_or_default(); + out.push_str(&format!("{},{},{w}", b + 1, text(name))); + for s in &sp.samples { + out.push(','); + if let Some(v) = s.values.get(b).copied().flatten() { + out.push_str(&v.to_string()); + } + } + out.push('\n'); + } + out +} + +/// The points' numbered markers on the map, in their colours, while the section shows. +pub fn paint(app: &RustGisApp, p: &egui::Painter, t: &Tokens, to_screen: impl Fn(rustgis_engine::geom::Coord) -> Option) { + if app.ui.right != Some(crate::RightPane::Workflows) || app.workflow.section != SECTION { + return; + } + for s in &app.workflow.spectral.samples { + let Some(at) = to_screen(rustgis_engine::geom::Coord::new(s.lon, s.lat)) else { continue }; + let c = colour(t, s.n); + p.circle_filled(at, 7.0, c); + p.circle_stroke(at, 7.0, Stroke::new(1.5, Color32::WHITE)); + p.text(at, egui::Align2::CENTER_CENTER, s.n.to_string(), semibold(10.0), Color32::WHITE); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A colour GeoTIFF of three bands whose wavelengths are given (blue, green, red, written in + /// that order red first), 4 × 4 pixels near 10°E 45°N, as base64. + fn rgb_tiff_b64(wavelengths: bool) -> String { + use base64::Engine; + use rustgis_engine::data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + let gt = GeoTransform { origin_x: 10.0, origin_y: 45.0, pixel_width: 0.01, pixel_height: -0.01 }; + // Red, green, blue: 30 + i, 20 + i, 10 + i at pixel i. + let bands = (0..3).map(|b| (0..16).map(|i| (30 - b * 10 + i) as f32).collect()).collect(); + let mut r = Raster::new("scene", 4, 4, bands, gt, Crs::LonLat, None, ColorHint::Rgb { alpha: false }, "UInt8").unwrap(); + if wavelengths { + for (band, nm) in r.bands.iter_mut().zip([665.0, 560.0, 490.0]) { + band.wavelength = Some(nm); + } + } + base64::engine::general_purpose::STANDARD.encode(geotiff::write(&r, 0).unwrap()) + } + + fn app_with_scene(wavelengths: bool) -> RustGisApp { + let mut app = RustGisApp::with_sample(); + app.run("layer.addDataBytes", json!({"fileName": "scene.tif", "data": rgb_tiff_b64(wavelengths)})).unwrap(); + app + } + + /// Pick Points: two clicks are read and charted, against wavelength when the file gives it; + /// a point is taken off; the CSV has a row a band; Clear empties it. + #[test] + fn the_spectral_profile_charts_points_picked() { + use egui_kittest::kittest::Queryable; + let mut app = app_with_scene(true); + app.run("ui.pane.workflows", json!({"section": SECTION})).unwrap(); + app.workflow.pick = + Some(crate::analysispane::Pick { purpose: PURPOSE, needed: 1, points: vec![rustgis_engine::geom::Coord::new(10.005, 44.995)] }); + let mut h = egui_kittest::Harness::builder().allow_missing_glyphs().with_size(egui::vec2(420.0, 640.0)).build_ui_state( + |ui, app: &mut RustGisApp| { + let t = crate::theme::Tokens::get(ui.ctx()); + section(app, ui, &t); + }, + app, + ); + h.run_steps(2); + assert_eq!(h.state().workflow.layer, "scene"); + assert_eq!(h.state().workflow.spectral.samples.len(), 1); + assert!(h.state().workflow.pick.as_ref().is_some_and(|p| p.purpose == PURPOSE), "picking goes on"); + // A second click, at pixel 5 (column 1, row 1). + if let Some(pick) = &mut h.state_mut().workflow.pick { + pick.points.push(rustgis_engine::geom::Coord::new(10.015, 44.985)); + } + h.run_steps(2); + let sp = &h.state().workflow.spectral; + assert_eq!(sp.samples.len(), 2); + assert_eq!(sp.samples[1].pixel, (1, 1)); + assert_eq!(sp.samples[1].values, vec![Some(35.0), Some(25.0), Some(15.0)]); + assert_eq!(sp.wavelengths, vec![Some(665.0), Some(560.0), Some(490.0)]); + assert_eq!(sp.axis(), (vec![665.0, 560.0, 490.0], true)); + // The CSV: a header and a row a band. + let text = csv(sp); + let lines: Vec<&str> = text.lines().collect(); + assert_eq!(lines.len(), 4, "{text}"); + assert!(lines[0].starts_with("band,name,wavelength_nm,\"point 1 (10.005, 44.995)\""), "{text}"); + assert!(lines[3].starts_with("3,\"Band 3\",490") && lines[3].ends_with(",10,15"), "{text}"); + // The first point taken off: the second keeps its number, so its colour. + h.get_all_by_label("×").next().unwrap().click(); + h.run_steps(2); + assert_eq!(h.state().workflow.spectral.samples.iter().map(|s| s.n).collect::>(), [2]); + h.get_by_label("Clear").click(); + h.run_steps(2); + assert!(h.state().workflow.spectral.samples.is_empty()); + } + + /// Without wavelengths the axis is the band numbers; past six points the oldest goes, and + /// that is said; a point off the raster is said, not added. + #[test] + fn the_profile_keeps_six_points_and_says_what_it_leaves_out() { + let mut app = app_with_scene(false); + for i in 0..7 { + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005 + 0.001 * f64::from(i), 44.995)); + } + let sp = &app.workflow.spectral; + assert_eq!(sp.samples.len(), MAX_POINTS); + assert_eq!(sp.samples.first().map(|s| s.n), Some(2)); + assert!(sp.message.as_ref().is_some_and(|(m, error)| !error && m.contains("oldest")), "{:?}", sp.message); + assert!(sp.wavelengths.is_empty()); + assert_eq!(sp.axis(), (vec![1.0, 2.0, 3.0], false)); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(20.0, 10.0)); + let sp = &app.workflow.spectral; + assert_eq!(sp.samples.len(), MAX_POINTS); + assert!(sp.message.as_ref().is_some_and(|(m, error)| *error && m.contains("outside")), "{:?}", sp.message); + // Another raster starts again. + app.run("layer.addDataBytes", json!({"fileName": "other.tif", "data": rgb_tiff_b64(true)})).unwrap(); + add_point(&mut app, "other", rustgis_engine::geom::Coord::new(10.005, 44.995)); + assert_eq!(app.workflow.spectral.samples.len(), 1); + assert_eq!(app.workflow.spectral.layer, "other"); + } + + /// Identify's pop-up offers the profile on a pixel of two bands or more, and its button opens + /// the section there with the point read. + #[test] + fn identify_offers_the_profile_on_multiband_pixels() { + let mut app = app_with_scene(true); + // The pixel Identify reports where no feature is found. + let info = rustgis_engine::cmd::raster::identify(&app.session, 10.005, 44.995).unwrap(); + let id = offered(&app, &info).expect("a pixel of three bands"); + from_identify(&mut app, id, rustgis_engine::geom::Coord::new(10.005, 44.995)); + assert_eq!(app.ui.right, Some(crate::RightPane::Workflows)); + assert_eq!(app.workflow.section, SECTION); + assert_eq!(app.workflow.spectral.samples.len(), 1); + // A feature isn't offered it. + assert_eq!(offered(&app, &json!({"layer": "Cities", "layerId": id, "attributes": {}})), None); + } +} diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index a1746220ca..c657dcfb70 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1396,8 +1396,22 @@ The Analyze tab's imagery groups open their tools in the Workflows pane: it holds: each point's residual in pixels and map units, the RMSE, and each point's error when it is left out of the fit. With `compare` it fits them all and recommends the simplest whose leave-one-out error is close to the best. -- **Change Detection** (difference, ratio, percent), **Spectral Analysis** (every band at a - pixel), **Indices** (NDVI, NDWI, SAVI, NBR, NDBI, EVI), **Process** (clip to the view, to a +- **Spectral Analysis** (`raster.spectral`) charts a multiband raster's spectral profile: every + band's value at points on the map, to compare the signatures of water, vegetation, soil or roofs + before drawing training samples for a classification. Pick the raster, turn on **Pick Points** and + click: each click adds a point until you turn it off. Identify's pop-up on a pixel of two bands or + more has a **Spectral Profile** button that adds that pixel. Up to six points are compared, a line + each, in the colour of a numbered marker on the map. The x axis is the bands' wavelengths in + nanometres when the file gives one for every band (see [Rasters](data.md#rasters)), drawn in + wavelength order, and their numbers otherwise. Hovering the chart shows a band's name, wavelength + and each point's value; **×** takes a point off, **Clear** all of them, and **Export CSV…** + (desktop) writes a row a band (its number, name and wavelength) and a column a point. From + scripts, `{"layer": "scene", "points": [[-43.95, -19.92], [-43.96, -19.93]]}` gives a profile a + point (up to 1,000; a point off the raster keeps its place with null values, and is said), + `{"lon", "lat"}` one pixel and `{"zone": "polygons"}` the mean in the selected or first polygon; + every answer has the band names and, when the file gives any, `"wavelengths"` in nanometres. + After GeoLibre's spectral profile. +- **Change Detection** (difference, ratio, percent), **Indices** (NDVI, NDWI, SAVI, NBR, NDBI, EVI), **Process** (clip to the view, to a box, or to a polygon layer's selected or all polygons, with the cells outside them no-data; resample) and the **Pixel Editor** (set the pixels inside a polygon). - **Mosaic Rasters** (`raster.mosaic`) merges tiles of one grid, such as the 1 km tiles of a state diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 6af4a9d50b..aa1f6e5c7e 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1120,7 +1120,15 @@ steps where they meet. `raster.stretch` with `{"like": ""}` gives a layer of another layer's stretch as fixed bounds, so the tiles match; `{"type": "custom", "min": …, "max": …}` sets the bounds yourself. -Clicking a raster with the Explore tool shows its pixel values. Projects remember a raster's file +Clicking a raster with the Explore tool shows its pixel values. On a raster of two bands or more, +the pop-up's **Spectral Profile** button charts every band at that pixel and opens the profile, where +more points are compared (see [Spectral Analysis](analysis.md)). A band's wavelength is read from the +GeoTIFF's GDAL metadata or its `.aux.xml` sidecar: GDAL's `CENTRAL_WAVELENGTH_UM` (its `IMAGERY` +domain), or a `wavelength` or `central_wavelength` item (as rasterio, STAC exports and ENVI files +through GDAL write them) in the unit a `wavelength_units` item gives. With no unit, a value under 100 +is read as micrometres and any other as nanometres: imaging sensors see from about 0.3 to 15 µm, so +the two never meet. One that isn't a length is said and left out. The GeoTIFF exports write the +wavelengths back as `CENTRAL_WAVELENGTH_UM`. Projects remember a raster's file path relative to the project and reopen it; rasters added in the browser are stored inside the project. ### Zarr stores From 9889e1b1d1820b2e59bf940a992bae88d7f4afb2 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 19:10:56 -0300 Subject: [PATCH 02/25] fix(imagery): say every wavelength and spectral input left out (review round 1) - A wavelength whose nanometres overflow or round to zero isn't a length: the result is checked, not only the value read. - Another length for a band that has one is said and left out, in the metadata tag and the sidecar; the same length under two names is one wavelength. A sidecar band now takes its first item that is a length, as the tag does, instead of stopping at the first item there is. - raster.spectral says lon and lat left out when a zone is read too. Its points are read in their own function. - The notice for bands a raster doesn't have says "names or wavelengths"; the user guide says an export writes the wavelengths of the bands it writes, and a long line is wrapped. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/mod.rs | 7 +- crates/data/src/raster/pam.rs | 117 +++++++++++++++++++++++++------ crates/engine/src/cmd/imagery.rs | 91 +++++++++++++----------- crates/engine/src/tests.rs | 17 +++++ docs/user-guide/analysis.md | 7 +- docs/user-guide/data.md | 5 +- 6 files changed, 175 insertions(+), 69 deletions(-) diff --git a/crates/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index 4363b8cbee..aa60ff501b 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -786,7 +786,7 @@ impl Raster { /// metadata", "its .aux.xml sidecar"). pub fn apply_pam(&mut self, pam: &pam::Pam, from: &str) { self.lost.extend(pam.lost.iter().map(|l| format!("{from}: {l}"))); - // Names for bands the raster doesn't have have nowhere to go. + // Names and wavelengths for bands the raster doesn't have have nowhere to go. let beyond = pam .bands .iter() @@ -796,7 +796,8 @@ impl Raster { }) .count(); if beyond > 0 { - self.lost.push(format!("{from}: names for {beyond} band(s) it doesn't have (it has {}) left out", self.bands.len())); + self.lost + .push(format!("{from}: names or wavelengths for {beyond} band(s) it doesn't have (it has {}) left out", self.bands.len())); } // So is a no-data value: it isn't the raster's. let outside = pam.bands.iter().filter(|b| (b.band == 0 || b.band > self.bands.len()) && b.nodata.is_some()).count(); @@ -1628,7 +1629,7 @@ pub(crate) mod tests { pam::SIDECAR, ); assert!( - one.lost.iter().any(|l| l == "its .aux.xml sidecar: names for 1 band(s) it doesn't have (it has 1) left out"), + one.lost.iter().any(|l| l == "its .aux.xml sidecar: names or wavelengths for 1 band(s) it doesn't have (it has 1) left out"), "{:?}", one.lost ); diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index e9fc418f35..6f5f891747 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -121,8 +121,10 @@ pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option let v = text.trim().parse::().ok().filter(|v| v.is_finite() && *v > 0.0)?; // Micrometres in nanometres, to a millionth of one: 0.665 µm is 665 nm, not 665.0000000000001. let nm = |um: f64| (um * 1e9).round() / 1e6; + // The result is checked too: a huge micrometre value overflows, a tiny one rounds to 0. + let positive = |w: f64| Some(w).filter(|w| w.is_finite() && *w > 0.0); if item.eq_ignore_ascii_case(CENTRAL_WAVELENGTH_UM) { - return Some(nm(v)); + return positive(nm(v)); } let unit = units.map(|u| u.trim().to_lowercase()).unwrap_or_default(); let micro = match unit.as_str() { @@ -131,7 +133,28 @@ pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option "um" | "\u{b5}m" | "\u{3bc}m" | "micrometer" | "micrometers" | "micrometre" | "micrometres" | "micron" | "microns" => true, _ => return None, }; - Some(if micro { nm(v) } else { v }) + positive(if micro { nm(v) } else { v }) +} + +/// Keeps a band's first wavelength in `slot`: `true` when `nm` is another one, left out (the +/// same length under another item, to a millionth, is the same one). +fn keep_first_wavelength(slot: &mut Option, nm: f64) -> bool { + match *slot { + None => { + *slot = Some(nm); + false + } + Some(w) => (w - nm).abs() > 1e-6 * w.max(nm), + } +} + +/// A sidecar node's metadata items `key` (any domain, any case): their texts. +fn mdi_texts<'a>(node: roxmltree::Node<'a, '_>, key: &'a str) -> impl Iterator { + node.children() + .filter(|n| n.has_tag_name("Metadata")) + .flat_map(|m| m.children()) + .filter(move |n| n.has_tag_name("MDI") && n.attribute("key").is_some_and(|k| k.eq_ignore_ascii_case(key))) + .filter_map(|n| n.text()) } /// A sidecar node's metadata item `key` (any domain, any case): its text. @@ -204,7 +227,7 @@ fn parse_sidecar(text: &str) -> Result { // from 1 to `MAX_BANDS`, a band's records after its first, and the scale, offset, units and // other band metadata items and colour tables that aren't read, counted. let (mut cut, mut beyond, mut odd_bands, mut unscaled, mut units) = (0usize, 0usize, 0usize, 0usize, 0usize); - let (mut items, mut tables, mut odd_nodata, mut odd_wavelengths) = (0usize, 0usize, 0usize, 0usize); + let (mut items, mut tables, mut odd_nodata, mut odd_wavelengths, mut other_wavelengths) = (0usize, 0usize, 0usize, 0usize, 0usize); let (mut seen, mut twice) = (std::collections::HashSet::new(), 0usize); // The unit the dataset gives its bands' wavelengths in, for a band that gives none. let dataset_units = mdi_text(root, WAVELENGTH_UNITS); @@ -236,14 +259,18 @@ fn parse_sidecar(text: &str) -> Result { .flat_map(|m| m.children()) .filter(|n| n.has_tag_name("MDI") && n.attribute("key").is_none_or(|k| !k.starts_with("STATISTICS_") && !is_wavelength_item(k))) .count(); - // Its wavelength: the first item there is of those that give one, in any domain (GDAL's in - // `IMAGERY`, ENVI's in the default one). + // Its wavelength: the first that is a length of the items that give one, in that order and + // in any domain (GDAL's in `IMAGERY`, ENVI's in the default one), as the metadata tag's. let units = mdi_text(b, WAVELENGTH_UNITS).or(dataset_units); - let wavelength = WAVELENGTH_ITEMS.iter().find_map(|w| mdi_text(b, w).map(|t| (w, t))).and_then(|(w, t)| { - let nm = wavelength_nm(w, t, units); - odd_wavelengths += usize::from(nm.is_none()); - nm - }); + let mut wavelength = None; + for w in WAVELENGTH_ITEMS { + for t in mdi_texts(b, w) { + match wavelength_nm(w, t, units) { + Some(nm) => other_wavelengths += usize::from(keep_first_wavelength(&mut wavelength, nm)), + None => odd_wavelengths += 1, + } + } + } // A colour table here isn't read (only the file's own is): one with entries is counted. tables += usize::from(b.children().any(|n| n.has_tag_name("ColorTable") && n.children().any(|e| e.has_tag_name("Entry")))); let description = text_of("Description").filter(|d| !d.is_empty()).map(&mut kept); @@ -307,6 +334,9 @@ fn parse_sidecar(text: &str) -> Result { if odd_wavelengths > 0 { pam.lost.push(format!("{odd_wavelengths} wavelength(s) that aren't a positive length in nanometres or micrometres left out")); } + if other_wavelengths > 0 { + pam.lost.push(format!("{other_wavelengths} other wavelength(s) for a band that has one left out (the first is kept)")); + } if beyond > 0 { pam.lost.push(format!("{beyond} class name(s) past the first {MAX_CATEGORIES} of a band left out")); } @@ -374,7 +404,7 @@ pub fn parse_gdal_metadata(text: &str) -> Option { twice += 1; } } - let odd_wavelengths = read_wavelengths(root, &mut pam); + let (odd_wavelengths, other_wavelengths) = read_wavelengths(root, &mut pam); // A band's scale and offset aren't applied: the values stay as stored. One that changes // nothing (a scale of 1, an offset of 0) isn't counted. let unscaled = root @@ -446,6 +476,9 @@ pub fn parse_gdal_metadata(text: &str) -> Option { "{odd_wavelengths} wavelength item(s) that aren't a positive length in nanometres or micrometres, or for no band from 1 to {MAX_BANDS}, left out" )); } + if other_wavelengths > 0 { + pam.lost.push(format!("{other_wavelengths} other wavelength item(s) for a band that has one left out (the first is kept)")); + } if twice > 0 { pam.lost.push(format!("{twice} band or class name(s) given again left out (the first is kept)")); } @@ -463,9 +496,10 @@ pub fn parse_gdal_metadata(text: &str) -> Option { /// Reads the bands' wavelengths of GDAL's metadata tag into `pam`: a band's (`sample`) first item /// of [`WAVELENGTH_ITEMS`], in that order, in the unit of its band's `wavelength_units` item, else -/// the dataset's. A band's other wavelength items are other names for the same, and aren't -/// counted. Returns how many items were left out: not a length, or for no band. -fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> usize { +/// the dataset's. A band's other wavelength items giving the same length are other names for it, +/// and aren't counted. Returns how many items were left out: not a length or for no band, and +/// another length for a band that has one. +fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usize) { let named = |n: &roxmltree::Node<'_, '_>, name: &str| { n.has_tag_name("Item") && n.attribute("name").is_some_and(|a| a.eq_ignore_ascii_case(name)) }; @@ -482,7 +516,7 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> usize { } // Where each band already described is in `pam.bands`. let mut at: std::collections::HashMap = pam.bands.iter().enumerate().map(|(i, b)| (b.band, i)).collect(); - let mut odd = 0usize; + let (mut odd, mut other) = (0usize, 0usize); for name in WAVELENGTH_ITEMS { // A dataset item (no `sample`) belongs to no band, as GeoLibre also reads it: not counted. for item in root.children().filter(|n| named(n, name) && n.attribute("sample").is_some()) { @@ -496,9 +530,7 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> usize { continue; }; match at.get(&band).and_then(|i| pam.bands.get_mut(*i)) { - Some(b) => { - b.wavelength.get_or_insert(nm); - } + Some(b) => other += usize::from(keep_first_wavelength(&mut b.wavelength, nm)), None => { at.insert(band, pam.bands.len()); pam.bands.push(PamBand { band, wavelength: Some(nm), ..PamBand::default() }); @@ -506,7 +538,7 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> usize { } } } - odd + (odd, other) } /// Whether XML can't hold `c` (a control character other than a tab or a line break, or a @@ -875,8 +907,14 @@ mod tests { [w(1), w(2), w(3), w(4), w(5), w(6), w(7)], [Some(482.5), Some(561.3), Some(654.6), Some(865.0), Some(1600.0), None, None] ); - assert_eq!(pam.lost.len(), 1, "{:?}", pam.lost); + assert_eq!(pam.lost.len(), 2, "{:?}", pam.lost); assert!(pam.lost[0].starts_with("3 wavelength item(s)"), "{:?}", pam.lost); + assert_eq!(pam.lost[1], "1 other wavelength item(s) for a band that has one left out (the first is kept)"); + // The same length under two items is one wavelength: nothing is said. + let xml = r#"0.665665"#; + let pam = parse_gdal_metadata(xml).unwrap(); + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(665.0)); + assert!(pam.lost.is_empty(), "{:?}", pam.lost); // A unit the dataset gives is a band's when it gives none; then a large value may be in µm. let xml = r#"micrometers120"#; @@ -898,6 +936,45 @@ mod tests { assert!(pam.lost.is_empty(), "{:?}", pam.lost); } + /// Wavelengths whose nanometres overflow or round to zero aren't lengths: left out, and the + /// band's next item is read. + #[test] + fn wavelengths_that_overflow_or_round_to_zero_are_left_out() { + for v in ["1e300", "1e-300", "5e-324"] { + for (item, units) in [(CENTRAL_WAVELENGTH_UM, None), ("wavelength", Some("micrometers")), ("wavelength", None)] { + let w = wavelength_nm(item, v, units); + assert!(w.is_none_or(|w| w.is_finite() && w > 0.0), "{item} {v} {units:?}: {w:?}"); + } + assert_eq!(wavelength_nm(CENTRAL_WAVELENGTH_UM, v, None), None, "{v}"); + } + let xml = r#"1e300842"#; + let pam = parse_gdal_metadata(xml).unwrap(); + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(842.0)); + assert!(pam.lost[0].starts_with("1 wavelength item(s)"), "{:?}", pam.lost); + } + + /// A sidecar's band takes the first item that is a length, as the metadata tag's: one that + /// isn't is said and the next is read; another length is said and left out. + #[test] + fn a_sidecar_band_takes_its_first_wavelength_that_is_a_length() { + let pam = parse( + r#" + 1e300842 + 0.665665700 +"#, + ) + .unwrap(); + let w = |b: usize| pam.band(b).and_then(|b| b.wavelength); + assert_eq!([w(1), w(2)], [Some(842.0), Some(665.0)]); + assert_eq!(pam.lost.len(), 2, "{:?}", pam.lost); + assert!(pam.lost.iter().any(|l| l.starts_with("1 wavelength(s) that aren't")), "{:?}", pam.lost); + assert!( + pam.lost.iter().any(|l| l == "1 other wavelength(s) for a band that has one left out (the first is kept)"), + "{:?}", + pam.lost + ); + } + /// A sidecar's wavelengths, in GDAL's `IMAGERY` domain or ENVI's default one; one that isn't a /// length is said, and other band items still are. #[test] diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 4eb22235fa..9c8ea1e613 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -851,6 +851,46 @@ pub(crate) fn change_work(s: &Session, p: &Value) -> Result Result, String> { + let points = points.as_array().ok_or("points is a list of [lon, lat]")?; + if points.is_empty() { + return Err("points is empty: give at least one [lon, lat]".into()); + } + if points.len() > MAX_SPECTRAL_POINTS { + return Err(format!("{} points given: at most {MAX_SPECTRAL_POINTS} are read in one run", points.len())); + } + let bands = r.band_count(); + let mut profiles = Vec::with_capacity(points.len()); + let mut outside = 0usize; + for (i, pt) in points.iter().enumerate() { + let xy = pt + .as_array() + .filter(|a| a.len() == 2) + .and_then(|a| Some((a.first()?.as_f64()?, a.get(1)?.as_f64()?))) + .filter(|(x, y)| x.is_finite() && y.is_finite()) + .ok_or_else(|| format!("point {} isn't [lon, lat]", i + 1))?; + profiles.push(match r.pixel_at(Coord::new(xy.0, xy.1)) { + Some((col, row)) => { + let vals: Vec> = (0..bands).map(|b| r.value(b, col, row)).collect(); + json!({"lon": xy.0, "lat": xy.1, "pixel": [col, row], "values": vals}) + } + None => { + outside += 1; + json!({"lon": xy.0, "lat": xy.1, "pixel": null, "values": vec![Value::Null; bands]}) + } + }); + } + if outside == points.len() { + return Err(if outside == 1 { "the point is outside the raster".into() } else { "every point is outside the raster".into() }); + } + if outside > 0 { + s.notify(format!("{}: {outside} of {} points fall outside the raster: their values are null", r.name, points.len()), false); + } + Ok(profiles) +} + /// Every band's value at a point, at several points (`points`, a profile each), or their mean /// in a polygon (`zone`), with the bands' names and wavelengths. fn spectral(s: &mut Session, p: &Value) -> CmdResult { @@ -868,51 +908,20 @@ fn spectral(s: &mut Session, p: &Value) -> CmdResult { }; // A zone is read before points, and points before one point: what isn't read is said. let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null()); - let left_out = if spectral_zone(p).is_some() && given("points") { - Some("points") - } else if given("points") && (given("lon") || given("lat")) { - Some("lon and lat") - } else { - None + let zone = spectral_zone(p).is_some(); + let point = given("lon") || given("lat"); + let left_out = match (zone && given("points"), (zone || given("points")) && point) { + (true, true) => Some("points, lon and lat"), + (true, false) => Some("points"), + (false, true) => Some("lon and lat"), + (false, false) => None, }; if let Some(what) = left_out { - let read = if spectral_zone(p).is_some() { "the zone's mean is read" } else { "points are read" }; + let read = if zone { "the zone's mean is read" } else { "points are read" }; s.notify(format!("{}: {what} left out: {read}", r.name), false); } - if let Some(points) = p.get("points").filter(|v| !v.is_null() && spectral_zone(p).is_none()) { - let points = points.as_array().ok_or("points is a list of [lon, lat]")?; - if points.is_empty() { - return Err("points is empty: give at least one [lon, lat]".into()); - } - if points.len() > MAX_SPECTRAL_POINTS { - return Err(format!("{} points given: at most {MAX_SPECTRAL_POINTS} are read in one run", points.len())); - } - let mut profiles = Vec::with_capacity(points.len()); - let mut outside = 0usize; - for (i, pt) in points.iter().enumerate() { - let xy = pt - .as_array() - .filter(|a| a.len() == 2) - .and_then(|a| Some((a.first()?.as_f64()?, a.get(1)?.as_f64()?))) - .filter(|(x, y)| x.is_finite() && y.is_finite()) - .ok_or_else(|| format!("point {} isn't [lon, lat]", i + 1))?; - profiles.push(match r.pixel_at(Coord::new(xy.0, xy.1)) { - Some((col, row)) => { - let vals: Vec> = all.iter().map(|b| r.value(*b, col, row)).collect(); - json!({"lon": xy.0, "lat": xy.1, "pixel": [col, row], "values": vals}) - } - None => { - outside += 1; - json!({"lon": xy.0, "lat": xy.1, "pixel": null, "values": vec![Value::Null; all.len()]}) - } - }); - } - if outside == points.len() { - return Err(if outside == 1 { "the point is outside the raster".into() } else { "every point is outside the raster".into() }); - } - if outside > 0 { - s.notify(format!("{}: {outside} of {} points fall outside the raster: their values are null", r.name, points.len()), false); - } + if let Some(points) = p.get("points").filter(|v| !v.is_null() && !zone) { + let profiles = spectral_points(s, &r, points)?; return answer(json!({"bands": names, "profiles": profiles})); } if let Some(z) = spectral_zone(p) { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index e9f4fe9cd2..86f2e495cb 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -34394,6 +34394,23 @@ fn spectral_profiles_at_points_with_wavelengths() { assert_eq!(both["profiles"][0]["pixel"], json!([0, 0])); let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert_eq!(said, ["scene: lon and lat left out: points are read"]); + // A zone is read before both, and says so. + let ring = "[10.001, 44.999], [10.019, 44.999], [10.019, 44.981], [10.001, 44.981], [10.001, 44.999]"; + let zone = format!(r#"{{"type":"Feature","properties":{{}},"geometry":{{"type":"Polygon","coordinates":[[{ring}]]}}}}"#); + s.run("layer.addDataText", &json!({"fileName": "zone.geojson", "text": zone})).unwrap(); + for (extra, left) in [ + (json!({"lon": 10.005, "lat": 44.995}), "lon and lat"), + (json!({"points": [[10.005, 44.995]]}), "points"), + (json!({"points": [[10.005, 44.995]], "lon": 10.005}), "points, lon and lat"), + ] { + let mut params = json!({"layer": "scene", "zone": "zone"}); + params.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone()); + let mark = s.notice_mark(); + let v = s.run("raster.spectral", ¶ms).unwrap(); + assert!(v["pixels"].as_u64().is_some_and(|n| n > 0), "{v}"); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert_eq!(said, [format!("scene: {left} left out: the zone's mean is read")]); + } // Points that can't be read are errors. for (points, error) in [ (json!([[20.0, 10.0], [30.0, 10.0]]), "every point is outside the raster"), diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index c657dcfb70..fd602d9ff0 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1411,9 +1411,10 @@ The Analyze tab's imagery groups open their tools in the Workflows pane: `{"lon", "lat"}` one pixel and `{"zone": "polygons"}` the mean in the selected or first polygon; every answer has the band names and, when the file gives any, `"wavelengths"` in nanometres. After GeoLibre's spectral profile. -- **Change Detection** (difference, ratio, percent), **Indices** (NDVI, NDWI, SAVI, NBR, NDBI, EVI), **Process** (clip to the view, to a - box, or to a polygon layer's selected or all polygons, with the cells outside them no-data; - resample) and the **Pixel Editor** (set the pixels inside a polygon). +- **Change Detection** (difference, ratio, percent), **Indices** (NDVI, NDWI, SAVI, NBR, NDBI, + EVI), **Process** (clip to the view, to a box, or to a polygon layer's selected or all polygons, + with the cells outside them no-data; resample) and the **Pixel Editor** (set the pixels inside a + polygon). - **Mosaic Rasters** (`raster.mosaic`) merges tiles of one grid, such as the 1 km tiles of a state elevation model, into one raster in their own coordinate system and cell size, optionally written to a GeoTIFF: `{"inputs": ["dgm_1.tif", "dgm_2.tif", …], "path": "dem.tif"}`. Inputs diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index aa1f6e5c7e..14813219b6 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1127,8 +1127,9 @@ GeoTIFF's GDAL metadata or its `.aux.xml` sidecar: GDAL's `CENTRAL_WAVELENGTH_UM domain), or a `wavelength` or `central_wavelength` item (as rasterio, STAC exports and ENVI files through GDAL write them) in the unit a `wavelength_units` item gives. With no unit, a value under 100 is read as micrometres and any other as nanometres: imaging sensors see from about 0.3 to 15 µm, so -the two never meet. One that isn't a length is said and left out. The GeoTIFF exports write the -wavelengths back as `CENTRAL_WAVELENGTH_UM`. Projects remember a raster's file +the two never meet. One that isn't a length, or another length for a band that has one, is said and +left out. A GeoTIFF export writes the wavelengths of the bands it writes (one band, or a raster's +RGB) back as `CENTRAL_WAVELENGTH_UM`. Projects remember a raster's file path relative to the project and reopen it; rasters added in the browser are stored inside the project. ### Zarr stores From 0d6320e371eb794c4d77536ba78793d41cef0a3a Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 19:28:22 -0300 Subject: [PATCH 03/25] fix(imagery): say a mosaic's other wavelengths and a dataset's own (review round 2) - raster.mosaic says an input whose bands have other wavelengths than the first's, as it says other band or class names: "band names, classes or wavelengths other than west's, which the mosaic keeps". - A wavelength item of the dataset's own (no band) is for no band: it is counted in the wavelengths left out, in GDAL's metadata tag and in the .aux.xml sidecar, instead of passed over. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 16 ++++++++++------ crates/engine/src/cmd/imagery.rs | 13 +++++++++---- crates/engine/src/tests.rs | 16 ++++++++++++++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 6f5f891747..b626985ce2 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -229,8 +229,10 @@ fn parse_sidecar(text: &str) -> Result { let (mut cut, mut beyond, mut odd_bands, mut unscaled, mut units) = (0usize, 0usize, 0usize, 0usize, 0usize); let (mut items, mut tables, mut odd_nodata, mut odd_wavelengths, mut other_wavelengths) = (0usize, 0usize, 0usize, 0usize, 0usize); let (mut seen, mut twice) = (std::collections::HashSet::new(), 0usize); - // The unit the dataset gives its bands' wavelengths in, for a band that gives none. + // The unit the dataset gives its bands' wavelengths in, for a band that gives none; a + // wavelength of the dataset's own is for no band. let dataset_units = mdi_text(root, WAVELENGTH_UNITS); + odd_wavelengths += WAVELENGTH_ITEMS.iter().map(|w| mdi_texts(root, w).count()).sum::(); let mut kept = |t: &str| { cut += usize::from(t.chars().nth(MAX_NAME).is_some()); t.chars().take(MAX_NAME).collect::() @@ -332,7 +334,9 @@ fn parse_sidecar(text: &str) -> Result { pam.lost.push(format!("{odd_nodata} no-data value(s) that aren't numbers left out: those pixels are read as values")); } if odd_wavelengths > 0 { - pam.lost.push(format!("{odd_wavelengths} wavelength(s) that aren't a positive length in nanometres or micrometres left out")); + pam.lost.push(format!( + "{odd_wavelengths} wavelength(s) that aren't a positive length in nanometres or micrometres, or for no band, left out" + )); } if other_wavelengths > 0 { pam.lost.push(format!("{other_wavelengths} other wavelength(s) for a band that has one left out (the first is kept)")); @@ -498,7 +502,7 @@ pub fn parse_gdal_metadata(text: &str) -> Option { /// of [`WAVELENGTH_ITEMS`], in that order, in the unit of its band's `wavelength_units` item, else /// the dataset's. A band's other wavelength items giving the same length are other names for it, /// and aren't counted. Returns how many items were left out: not a length or for no band, and -/// another length for a band that has one. +/// another length for a band that has one. A dataset's own item (no `sample`) is for no band. fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usize) { let named = |n: &roxmltree::Node<'_, '_>, name: &str| { n.has_tag_name("Item") && n.attribute("name").is_some_and(|a| a.eq_ignore_ascii_case(name)) @@ -518,8 +522,8 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usi let mut at: std::collections::HashMap = pam.bands.iter().enumerate().map(|(i, b)| (b.band, i)).collect(); let (mut odd, mut other) = (0usize, 0usize); for name in WAVELENGTH_ITEMS { - // A dataset item (no `sample`) belongs to no band, as GeoLibre also reads it: not counted. - for item in root.children().filter(|n| named(n, name) && n.attribute("sample").is_some()) { + // A dataset item (no `sample`) is for no band: GeoLibre doesn't read it either; counted. + for item in root.children().filter(|n| named(n, name)) { let Some(band) = band_of(&item) else { odd += 1; continue; @@ -908,7 +912,7 @@ mod tests { [Some(482.5), Some(561.3), Some(654.6), Some(865.0), Some(1600.0), None, None] ); assert_eq!(pam.lost.len(), 2, "{:?}", pam.lost); - assert!(pam.lost[0].starts_with("3 wavelength item(s)"), "{:?}", pam.lost); + assert!(pam.lost[0].starts_with("4 wavelength item(s)"), "{:?}", pam.lost); assert_eq!(pam.lost[1], "1 other wavelength item(s) for a band that has one left out (the first is kept)"); // The same length under two items is one wavelength: nothing is said. let xml = r#"0.665665"#; diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 9c8ea1e613..94b6c21bfe 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -1354,8 +1354,8 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult { first.data_type.clone(), ) .map_err(|e| e.to_string())?; - // Band and class names are the first input's, as its cells are where they overlap; an input - // naming its bands or classes otherwise is said. + // Band and class names and wavelengths are the first input's, as its cells are where they + // overlap; an input giving its bands other ones is said. out.bands.iter_mut().zip(&first.bands).for_each(|(o, i)| { o.name = i.name.clone(); o.classes = i.classes.clone(); @@ -1366,11 +1366,16 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult { let other: Vec<&str> = rs .iter() .skip(1) - .filter(|r| r.bands.iter().zip(&first.bands).any(|(b, f)| b.name != f.name || b.classes != f.classes)) + .filter(|r| { + r.bands.iter().zip(&first.bands).any(|(b, f)| b.name != f.name || b.classes != f.classes || b.wavelength != f.wavelength) + }) .map(|r| r.name.as_str()) .collect(); if !other.is_empty() { - s.notify(format!("{}: band or class names other than {}'s, which the mosaic keeps", other.join(", "), first.name), false); + s.notify( + format!("{}: band names, classes or wavelengths other than {}'s, which the mosaic keeps", other.join(", "), first.name), + false, + ); } let path = ps(p, "path").map(str::trim).filter(|p| !p.is_empty()).map(str::to_string); if let Some(path) = &path { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 86f2e495cb..b651f058ac 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -25917,12 +25917,24 @@ fn dem_tiles_mosaic_clip_by_polygon_share_a_stretch_and_report_overviews() { let mark = s.notice_mark(); let v = s.run("raster.mosaic", &json!({"inputs": ["west", "over"], "name": "renamed"})).unwrap(); let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); - assert!(said.iter().any(|n| n == "over: band or class names other than west's, which the mosaic keeps"), "{said:?}"); + assert!(said.iter().any(|n| n == "over: band names, classes or wavelengths other than west's, which the mosaic keeps"), "{said:?}"); assert_eq!(raster(&s, &v).bands[0].name, west_band); let mark = s.notice_mark(); s.run("raster.mosaic", &json!({"inputs": ["west", "east"], "name": "same"})).unwrap(); - assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("band or class names"))); + assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("band names, classes"))); named(&mut s, &over_band); + // So is one giving its band another wavelength; the mosaic keeps the first's. + let wavelength = |s: &mut Session, nm: Option| { + let data = s.map_mut().layer_mut(over).unwrap().raster.as_mut().unwrap().data.as_mut().unwrap(); + std::sync::Arc::make_mut(data).bands[0].wavelength = nm; + }; + wavelength(&mut s, Some(842.0)); + let mark = s.notice_mark(); + let v = s.run("raster.mosaic", &json!({"inputs": ["west", "over"], "name": "nir"})).unwrap(); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!(said.iter().any(|n| n == "over: band names, classes or wavelengths other than west's, which the mosaic keeps"), "{said:?}"); + assert_eq!(raster(&s, &v).bands[0].wavelength, None); + wavelength(&mut s, None); // A file given by path says what its reading left out (here a sidecar's scale); one read whole // says nothing. let file = dir.join("west.tif"); From 4b987ced16ebfc995ba4809533ebe02d26a50596 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 20:01:50 -0300 Subject: [PATCH 04/25] fix(imagery): an empty wavelength is said, and lengths apart by a millionth differ (review round 3) - A sidecar's empty wavelength item is counted as not a length, as the metadata tag's already was. - Two wavelengths for one band are the same only within a millionth of a nanometre (what micrometres are read to), not a millionth of their length: 665 and 665.0005 nm are two, and the second is said. - raster.spectral says what it left out only once the run has read what it does: a run that fails says only its error. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 22 ++++++++++++++++---- crates/engine/src/cmd/imagery.rs | 35 +++++++++++++++++++------------- crates/engine/src/tests.rs | 5 +++++ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index b626985ce2..8a3239494a 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -137,24 +137,26 @@ pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option } /// Keeps a band's first wavelength in `slot`: `true` when `nm` is another one, left out (the -/// same length under another item, to a millionth, is the same one). +/// same length under another item, to a millionth of a nanometre, is the same one: micrometres +/// are read to that, so only rounding is absorbed). fn keep_first_wavelength(slot: &mut Option, nm: f64) -> bool { match *slot { None => { *slot = Some(nm); false } - Some(w) => (w - nm).abs() > 1e-6 * w.max(nm), + Some(w) => (w - nm).abs() > 1e-6, } } -/// A sidecar node's metadata items `key` (any domain, any case): their texts. +/// A sidecar node's metadata items `key` (any domain, any case): their texts, empty for an item +/// with none (still an item the file gave). fn mdi_texts<'a>(node: roxmltree::Node<'a, '_>, key: &'a str) -> impl Iterator { node.children() .filter(|n| n.has_tag_name("Metadata")) .flat_map(|m| m.children()) .filter(move |n| n.has_tag_name("MDI") && n.attribute("key").is_some_and(|k| k.eq_ignore_ascii_case(key))) - .filter_map(|n| n.text()) + .map(|n| n.text().unwrap_or_default()) } /// A sidecar node's metadata item `key` (any domain, any case): its text. @@ -971,6 +973,18 @@ mod tests { let w = |b: usize| pam.band(b).and_then(|b| b.wavelength); assert_eq!([w(1), w(2)], [Some(842.0), Some(665.0)]); assert_eq!(pam.lost.len(), 2, "{:?}", pam.lost); + // An empty item is one the file gave: said, as the metadata tag's is. + let empty = + parse(r#""#) + .unwrap(); + assert_eq!(empty.band(1).and_then(|b| b.wavelength), None); + assert!(empty.lost.iter().any(|l| l.starts_with("1 wavelength(s) that aren't")), "{:?}", empty.lost); + // Lengths half a thousandth of a nanometre apart are two. + let mut slot = None; + assert!(!keep_first_wavelength(&mut slot, 665.0)); + assert!(!keep_first_wavelength(&mut slot, 0.665e3 + 1e-9)); + assert!(keep_first_wavelength(&mut slot, 665.0005)); + assert_eq!(slot, Some(665.0)); assert!(pam.lost.iter().any(|l| l.starts_with("1 wavelength(s) that aren't")), "{:?}", pam.lost); assert!( pam.lost.iter().any(|l| l == "1 other wavelength(s) for a band that has one left out (the first is kept)"), diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 94b6c21bfe..de9346f243 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -895,18 +895,8 @@ fn spectral_points(s: &mut Session, r: &Raster, points: &Value) -> Result CmdResult { let r = raster(s, p)?; - let all: Vec = (0..r.band_count()).collect(); - let names: Vec = r.bands.iter().map(|b| b.name.clone()).collect(); - // The bands' wavelengths, when the file gives any (`null` for a band without one). - let wavelengths: Vec> = r.bands.iter().map(|b| b.wavelength).collect(); - let answer = |mut v: Value| { - if wavelengths.iter().any(Option::is_some) { - v["wavelengths"] = json!(wavelengths); - v["wavelengthUnit"] = json!("nm"); - } - Ok(v) - }; - // A zone is read before points, and points before one point: what isn't read is said. + // A zone is read before points, and points before one point: what isn't read is said, once + // what is read has been. let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null()); let zone = spectral_zone(p).is_some(); let point = given("lon") || given("lat"); @@ -916,12 +906,29 @@ fn spectral(s: &mut Session, p: &Value) -> CmdResult { (false, true) => Some("lon and lat"), (false, false) => None, }; + let v = spectral_read(s, p, &r, zone)?; if let Some(what) = left_out { let read = if zone { "the zone's mean is read" } else { "points are read" }; s.notify(format!("{}: {what} left out: {read}", r.name), false); } + Ok(v) +} + +/// [`spectral`]'s answer: the zone's mean when `zone`, else the points' profiles, else one point's. +fn spectral_read(s: &mut Session, p: &Value, r: &Raster, zone: bool) -> CmdResult { + let all: Vec = (0..r.band_count()).collect(); + let names: Vec = r.bands.iter().map(|b| b.name.clone()).collect(); + // The bands' wavelengths, when the file gives any (`null` for a band without one). + let wavelengths: Vec> = r.bands.iter().map(|b| b.wavelength).collect(); + let answer = |mut v: Value| { + if wavelengths.iter().any(Option::is_some) { + v["wavelengths"] = json!(wavelengths); + v["wavelengthUnit"] = json!("nm"); + } + Ok(v) + }; if let Some(points) = p.get("points").filter(|v| !v.is_null() && !zone) { - let profiles = spectral_points(s, &r, points)?; + let profiles = spectral_points(s, r, points)?; return answer(json!({"bands": names, "profiles": profiles})); } if let Some(z) = spectral_zone(p) { @@ -935,7 +942,7 @@ fn spectral(s: &mut Session, p: &Value) -> CmdResult { return Err("the zone has no polygon".into()); }; let rings = polys.first().ok_or("the zone has no polygon")?; - let (means, n) = rasterops::zonal_means(&r, &all, rings).ok_or("no pixels fall in the zone")?; + let (means, n) = rasterops::zonal_means(r, &all, rings).ok_or("no pixels fall in the zone")?; return answer(json!({"bands": names, "values": means, "pixels": n})); } let c = Coord::new(f(p, "lon").ok_or("give lon and lat, points or a zone")?, f(p, "lat").ok_or("lat is required")?); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index b651f058ac..6e8aa5d88c 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -34423,6 +34423,11 @@ fn spectral_profiles_at_points_with_wavelengths() { let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert_eq!(said, [format!("scene: {left} left out: the zone's mean is read")]); } + // A run that fails says nothing of what it left out. + let mark = s.notice_mark(); + s.run("raster.spectral", &json!({"layer": "scene", "lon": 10.005, "lat": 44.995, "points": [[20.0, 10.0]]})).unwrap_err(); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert_eq!(said, ["the point is outside the raster"], "only the error is said"); // Points that can't be read are errors. for (points, error) in [ (json!([[20.0, 10.0], [30.0, 10.0]]), "every point is outside the raster"), From 0b8ba0f1ea093ad7b1d26034ff03f8848650a63b Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 20:22:17 -0300 Subject: [PATCH 05/25] fix(imagery): say wavelength units for no band (review round 4) - A wavelength_units item in GDAL's metadata tag for a band past the most a raster has is counted in the wavelength items left out, as a wavelength for no band already is. - raster.spectral reads its zone parameter once; a stale comment in the Identify pop-up is corrected. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 13 +++++++++---- crates/engine/src/cmd/imagery.rs | 15 ++++++++------- crates/ui-egui/src/mapview.rs | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 8a3239494a..f53731acd9 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -512,17 +512,21 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usi let band_of = |n: &roxmltree::Node<'_, '_>| { n.attribute("sample").and_then(|v| v.trim().parse::().ok()).and_then(|s| s.checked_add(1)).filter(|n| *n <= MAX_BANDS) }; - // The units given, by band (0: the dataset's). + // The units given, by band (0: the dataset's); units for no band are counted. + let (mut odd, mut other) = (0usize, 0usize); let mut units: std::collections::HashMap = std::collections::HashMap::new(); for item in root.children().filter(|n| named(n, WAVELENGTH_UNITS)) { let at = if item.attribute("sample").is_none() { Some(0) } else { band_of(&item) }; - if let (Some(at), Some(t)) = (at, item.text()) { + let Some(at) = at else { + odd += 1; + continue; + }; + if let Some(t) = item.text() { units.entry(at).or_insert(t); } } // Where each band already described is in `pam.bands`. let mut at: std::collections::HashMap = pam.bands.iter().enumerate().map(|(i, b)| (b.band, i)).collect(); - let (mut odd, mut other) = (0usize, 0usize); for name in WAVELENGTH_ITEMS { // A dataset item (no `sample`) is for no band: GeoLibre doesn't read it either; counted. for item in root.children().filter(|n| named(n, name)) { @@ -905,6 +909,7 @@ mod tests { 2.2 furlongs 1 + nm 500 "#; let pam = parse_gdal_metadata(xml).unwrap(); @@ -914,7 +919,7 @@ mod tests { [Some(482.5), Some(561.3), Some(654.6), Some(865.0), Some(1600.0), None, None] ); assert_eq!(pam.lost.len(), 2, "{:?}", pam.lost); - assert!(pam.lost[0].starts_with("4 wavelength item(s)"), "{:?}", pam.lost); + assert!(pam.lost[0].starts_with("5 wavelength item(s)"), "{:?}", pam.lost); assert_eq!(pam.lost[1], "1 other wavelength item(s) for a band that has one left out (the first is kept)"); // The same length under two items is one wavelength: nothing is said. let xml = r#"0.665665"#; diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index de9346f243..cdc1472136 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -898,9 +898,9 @@ fn spectral(s: &mut Session, p: &Value) -> CmdResult { // A zone is read before points, and points before one point: what isn't read is said, once // what is read has been. let given = |k: &str| p.get(k).is_some_and(|v| !v.is_null()); - let zone = spectral_zone(p).is_some(); + let zone = spectral_zone(p); let point = given("lon") || given("lat"); - let left_out = match (zone && given("points"), (zone || given("points")) && point) { + let left_out = match (zone.is_some() && given("points"), (zone.is_some() || given("points")) && point) { (true, true) => Some("points, lon and lat"), (true, false) => Some("points"), (false, true) => Some("lon and lat"), @@ -908,14 +908,15 @@ fn spectral(s: &mut Session, p: &Value) -> CmdResult { }; let v = spectral_read(s, p, &r, zone)?; if let Some(what) = left_out { - let read = if zone { "the zone's mean is read" } else { "points are read" }; + let read = if zone.is_some() { "the zone's mean is read" } else { "points are read" }; s.notify(format!("{}: {what} left out: {read}", r.name), false); } Ok(v) } -/// [`spectral`]'s answer: the zone's mean when `zone`, else the points' profiles, else one point's. -fn spectral_read(s: &mut Session, p: &Value, r: &Raster, zone: bool) -> CmdResult { +/// [`spectral`]'s answer: the mean in the polygon layer `zone` when given, else the points' +/// profiles, else one point's. +fn spectral_read(s: &mut Session, p: &Value, r: &Raster, zone: Option<&str>) -> CmdResult { let all: Vec = (0..r.band_count()).collect(); let names: Vec = r.bands.iter().map(|b| b.name.clone()).collect(); // The bands' wavelengths, when the file gives any (`null` for a band without one). @@ -927,11 +928,11 @@ fn spectral_read(s: &mut Session, p: &Value, r: &Raster, zone: bool) -> CmdResul } Ok(v) }; - if let Some(points) = p.get("points").filter(|v| !v.is_null() && !zone) { + if let Some(points) = p.get("points").filter(|v| !v.is_null() && zone.is_none()) { let profiles = spectral_points(s, r, points)?; return answer(json!({"bands": names, "profiles": profiles})); } - if let Some(z) = spectral_zone(p) { + if let Some(z) = zone { let l = s .map() .layer_by_name(z) diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 3db94cfce9..667c5e54ce 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -3538,7 +3538,7 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { // A pixel of a raster of two bands or more: its layer, for the spectral profile. let spectral = crate::spectralui::offered(app, &info); let mut profile = false; - // The feature shown, as its layer id and feature id (a raster pixel has neither). + // The feature shown, as its layer id and feature id (a raster pixel has no feature id). let shown = info["layerId"].as_u64().and_then(|id| u32::try_from(id).ok()).zip(info["fid"].as_u64()); // Only WMS layers being asked so far, or still waited for: their names. let waiting = !is_found(&info); From 14477c8968def1b564284436ff8922f84de1e5f3 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 20:49:10 -0300 Subject: [PATCH 06/25] fix(imagery): say wavelength units given again, and a shadowed raster in Identify (review round 5) - A band's or the dataset's wavelength unit given again otherwise is said and left out (the first is read), in GDAL's metadata tag and in the .aux.xml sidecar; the same unit again is nothing. - Identify's Spectral Profile button on a raster another layer shares its name with (case aside) says so instead of charting the other one's pixels, since commands find a raster by its name. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 49 +++++++++++++++++++++++++++----- crates/ui-egui/src/spectralui.rs | 22 ++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index f53731acd9..6861bd2200 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -149,6 +149,13 @@ fn keep_first_wavelength(slot: &mut Option, nm: f64) -> bool { } } +/// How many of `units` after the first name another unit than it (case and spaces aside): those +/// are left out, the first being the one read. +fn other_units<'a>(mut units: impl Iterator) -> usize { + let Some(first) = units.next() else { return 0 }; + units.filter(|u| !u.trim().eq_ignore_ascii_case(first.trim())).count() +} + /// A sidecar node's metadata items `key` (any domain, any case): their texts, empty for an item /// with none (still an item the file gave). fn mdi_texts<'a>(node: roxmltree::Node<'a, '_>, key: &'a str) -> impl Iterator { @@ -230,6 +237,8 @@ fn parse_sidecar(text: &str) -> Result { // other band metadata items and colour tables that aren't read, counted. let (mut cut, mut beyond, mut odd_bands, mut unscaled, mut units) = (0usize, 0usize, 0usize, 0usize, 0usize); let (mut items, mut tables, mut odd_nodata, mut odd_wavelengths, mut other_wavelengths) = (0usize, 0usize, 0usize, 0usize, 0usize); + // Wavelength units given again otherwise for the dataset or a band (the first is read). + let mut units_again = other_units(mdi_texts(root, WAVELENGTH_UNITS)); let (mut seen, mut twice) = (std::collections::HashSet::new(), 0usize); // The unit the dataset gives its bands' wavelengths in, for a band that gives none; a // wavelength of the dataset's own is for no band. @@ -266,6 +275,7 @@ fn parse_sidecar(text: &str) -> Result { // Its wavelength: the first that is a length of the items that give one, in that order and // in any domain (GDAL's in `IMAGERY`, ENVI's in the default one), as the metadata tag's. let units = mdi_text(b, WAVELENGTH_UNITS).or(dataset_units); + units_again += other_units(mdi_texts(b, WAVELENGTH_UNITS)); let mut wavelength = None; for w in WAVELENGTH_ITEMS { for t in mdi_texts(b, w) { @@ -343,6 +353,9 @@ fn parse_sidecar(text: &str) -> Result { if other_wavelengths > 0 { pam.lost.push(format!("{other_wavelengths} other wavelength(s) for a band that has one left out (the first is kept)")); } + if units_again > 0 { + pam.lost.push(format!("{units_again} other wavelength unit(s) for a band or the dataset left out (the first is kept)")); + } if beyond > 0 { pam.lost.push(format!("{beyond} class name(s) past the first {MAX_CATEGORIES} of a band left out")); } @@ -410,7 +423,7 @@ pub fn parse_gdal_metadata(text: &str) -> Option { twice += 1; } } - let (odd_wavelengths, other_wavelengths) = read_wavelengths(root, &mut pam); + let (odd_wavelengths, other_wavelengths, units_again) = read_wavelengths(root, &mut pam); // A band's scale and offset aren't applied: the values stay as stored. One that changes // nothing (a scale of 1, an offset of 0) isn't counted. let unscaled = root @@ -485,6 +498,9 @@ pub fn parse_gdal_metadata(text: &str) -> Option { if other_wavelengths > 0 { pam.lost.push(format!("{other_wavelengths} other wavelength item(s) for a band that has one left out (the first is kept)")); } + if units_again > 0 { + pam.lost.push(format!("{units_again} other wavelength unit item(s) for a band or the dataset left out (the first is kept)")); + } if twice > 0 { pam.lost.push(format!("{twice} band or class name(s) given again left out (the first is kept)")); } @@ -504,16 +520,18 @@ pub fn parse_gdal_metadata(text: &str) -> Option { /// of [`WAVELENGTH_ITEMS`], in that order, in the unit of its band's `wavelength_units` item, else /// the dataset's. A band's other wavelength items giving the same length are other names for it, /// and aren't counted. Returns how many items were left out: not a length or for no band, and -/// another length for a band that has one. A dataset's own item (no `sample`) is for no band. -fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usize) { +/// another length for a band that has one, and units given again otherwise for a band or the +/// dataset (the first is read). A dataset's own item (no `sample`) is for no band. +fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usize, usize) { let named = |n: &roxmltree::Node<'_, '_>, name: &str| { n.has_tag_name("Item") && n.attribute("name").is_some_and(|a| a.eq_ignore_ascii_case(name)) }; let band_of = |n: &roxmltree::Node<'_, '_>| { n.attribute("sample").and_then(|v| v.trim().parse::().ok()).and_then(|s| s.checked_add(1)).filter(|n| *n <= MAX_BANDS) }; - // The units given, by band (0: the dataset's); units for no band are counted. - let (mut odd, mut other) = (0usize, 0usize); + // The units given, by band (0: the dataset's); units for no band are counted, and so are + // other units given again. + let (mut odd, mut other, mut units_again) = (0usize, 0usize, 0usize); let mut units: std::collections::HashMap = std::collections::HashMap::new(); for item in root.children().filter(|n| named(n, WAVELENGTH_UNITS)) { let at = if item.attribute("sample").is_none() { Some(0) } else { band_of(&item) }; @@ -522,7 +540,14 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usi continue; }; if let Some(t) = item.text() { - units.entry(at).or_insert(t); + match units.entry(at) { + std::collections::hash_map::Entry::Vacant(v) => { + v.insert(t); + } + std::collections::hash_map::Entry::Occupied(o) => { + units_again += usize::from(!o.get().trim().eq_ignore_ascii_case(t.trim())); + } + } } } // Where each band already described is in `pam.bands`. @@ -548,7 +573,7 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usi } } } - (odd, other) + (odd, other, units_again) } /// Whether XML can't hold `c` (a control character other than a tab or a line break, or a @@ -926,6 +951,12 @@ mod tests { let pam = parse_gdal_metadata(xml).unwrap(); assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(665.0)); assert!(pam.lost.is_empty(), "{:?}", pam.lost); + // A band's unit given again otherwise: the first is read, and the other said; the same + // one again is nothing. + let xml = r#"nmum NM 1.6"#; + let pam = parse_gdal_metadata(xml).unwrap(); + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(1.6)); + assert_eq!(pam.lost, ["1 other wavelength unit item(s) for a band or the dataset left out (the first is kept)"]); // A unit the dataset gives is a band's when it gives none; then a large value may be in µm. let xml = r#"micrometers120"#; @@ -984,6 +1015,10 @@ mod tests { .unwrap(); assert_eq!(empty.band(1).and_then(|b| b.wavelength), None); assert!(empty.lost.iter().any(|l| l.starts_with("1 wavelength(s) that aren't")), "{:?}", empty.lost); + // A band's unit given again otherwise: the first is read, and the other said. + let twice = parse(r#"nmmicrometers1.6"#).unwrap(); + assert_eq!(twice.band(1).and_then(|b| b.wavelength), Some(1.6)); + assert_eq!(twice.lost, ["1 other wavelength unit(s) for a band or the dataset left out (the first is kept)"]); // Lengths half a thousandth of a nanometre apart are two. let mut slot = None; assert!(!keep_first_wavelength(&mut slot, 665.0)); diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 0e93023d4d..8473947f1c 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -100,6 +100,15 @@ pub fn offered(app: &RustGisApp, info: &Value) -> Option { /// Identify's Spectral Profile button: opens the section on the layer `id` and adds the point. pub fn from_identify(app: &mut RustGisApp, id: u32, at: rustgis_engine::geom::Coord) { let Some(name) = app.session.map().layer(id).map(|l| l.name.clone()) else { return }; + // Commands find a raster by its name: an earlier layer sharing it would be read instead. + if app.session.map().layer_by_name(&name).map(|l| l.id) != Some(id) { + let text = crate::i18n::fmt( + "{name}: another layer has this name, so its profile would be read from that one: rename one of them", + &[("name", &name)], + ); + app.session.notify(text, true); + return; + } if let Err(e) = app.run("ui.pane.workflows", json!({"section": SECTION})) { app.session.notify(e, true); return; @@ -492,5 +501,18 @@ mod tests { assert_eq!(app.workflow.spectral.samples.len(), 1); // A feature isn't offered it. assert_eq!(offered(&app, &json!({"layer": "Cities", "layerId": id, "attributes": {}})), None); + // A raster named as an earlier one (case aside) isn't read from that one: it is said. + app.run("layer.addDataBytes", json!({"fileName": "copy.tif", "data": rgb_tiff_b64(true)})).unwrap(); + let copy = app.session.map().layer_by_name("copy").map(|l| l.id).expect("the copy"); + app.run("layer.rename", json!({"layer": "copy", "name": "Scene"})).unwrap(); + // The layer the name finds is read; the other one, which commands can't reach by its name. + let found = app.session.map().layer_by_name("Scene").map(|l| l.id).expect("a scene"); + let (shadowed, name) = if found == copy { (id, "scene") } else { (copy, "Scene") }; + let before = app.workflow.spectral.samples.len(); + let mark = app.session.notice_mark(); + from_identify(&mut app, shadowed, rustgis_engine::geom::Coord::new(10.005, 44.995)); + assert_eq!(app.workflow.spectral.samples.len(), before, "nothing read from the other scene"); + let said: Vec = app.session.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert_eq!(said, [format!("{name}: another layer has this name, so its profile would be read from that one: rename one of them")]); } } From 6bc954711af53b67cef5e7b6db037dcfc37d18c0 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 21:03:01 -0300 Subject: [PATCH 07/25] fix(data): an empty wavelength unit names none, in both readers (review round 6) An empty wavelength_units item (or one of spaces) names no unit: the sidecar and the metadata tag now both read the next one, and neither counts the next as another unit given again. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 6861bd2200..87ee3a04b2 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -149,11 +149,12 @@ fn keep_first_wavelength(slot: &mut Option, nm: f64) -> bool { } } -/// How many of `units` after the first name another unit than it (case and spaces aside): those -/// are left out, the first being the one read. -fn other_units<'a>(mut units: impl Iterator) -> usize { +/// How many of `units` after the first that names one name another unit than it (case and +/// spaces aside): those are left out, the first being the one read. An empty item names none. +fn other_units<'a>(units: impl Iterator) -> usize { + let mut units = units.map(str::trim).filter(|u| !u.is_empty()); let Some(first) = units.next() else { return 0 }; - units.filter(|u| !u.trim().eq_ignore_ascii_case(first.trim())).count() + units.filter(|u| !u.eq_ignore_ascii_case(first)).count() } /// A sidecar node's metadata items `key` (any domain, any case): their texts, empty for an item @@ -166,13 +167,15 @@ fn mdi_texts<'a>(node: roxmltree::Node<'a, '_>, key: &'a str) -> impl Iterator(node: roxmltree::Node<'a, '_>, key: &str) -> Option<&'a str> { node.children() .filter(|n| n.has_tag_name("Metadata")) .flat_map(|m| m.children()) - .find(|n| n.has_tag_name("MDI") && n.attribute("key").is_some_and(|k| k.eq_ignore_ascii_case(key))) - .and_then(|n| n.text()) + .filter(|n| n.has_tag_name("MDI") && n.attribute("key").is_some_and(|k| k.eq_ignore_ascii_case(key))) + .filter_map(|n| n.text()) + .find(|t| !t.trim().is_empty()) } /// The sidecars GDAL would read for `path`: `x.tif.aux.xml`, then `x.aux.xml`. @@ -539,7 +542,8 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usi odd += 1; continue; }; - if let Some(t) = item.text() { + // An empty unit names none: the next one is read, as in the sidecar. + if let Some(t) = item.text().filter(|t| !t.trim().is_empty()) { match units.entry(at) { std::collections::hash_map::Entry::Vacant(v) => { v.insert(t); @@ -1019,6 +1023,13 @@ mod tests { let twice = parse(r#"nmmicrometers1.6"#).unwrap(); assert_eq!(twice.band(1).and_then(|b| b.wavelength), Some(1.6)); assert_eq!(twice.lost, ["1 other wavelength unit(s) for a band or the dataset left out (the first is kept)"]); + // An empty unit names none, in the sidecar and the metadata tag alike: the next is read. + let sidecar = parse(r#"nm0.665"#).unwrap(); + let tag = parse_gdal_metadata(r#" nm0.665"#).unwrap(); + for pam in [sidecar, tag] { + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(0.665)); + assert!(pam.lost.is_empty(), "{:?}", pam.lost); + } // Lengths half a thousandth of a nanometre apart are two. let mut slot = None; assert!(!keep_first_wavelength(&mut slot, 665.0)); From 032a6a6e101b517d5b990b82f18f135db141b8ae Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 21:17:02 -0300 Subject: [PATCH 08/25] fix(ui): the profile's list leaves out shadowed rasters, saying so; a pick ends with no raster (review round 7) - The Spectral profile section lists only rasters whose name finds them, since commands read the first layer of a name, and says how many it left out and how to bring them back (rename them). - With no raster of two bands or more, a Pick Points left on ends, so the next map click goes to the active tool. - The CSV export's error goes through i18n::fmt. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/spectralui.rs | 55 +++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 8473947f1c..9566bdd091 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -76,15 +76,20 @@ pub fn colour(t: &Tokens, n: u32) -> Color32 { colours.get((n.max(1) as usize - 1) % colours.len()).copied().unwrap_or(t.accent) } -/// The rasters a profile can be read on: those of two bands or more. -pub fn multiband(app: &RustGisApp) -> Vec { - app.session - .map() - .layers - .iter() - .filter(|l| l.raster.as_ref().and_then(|r| r.data.as_ref()).is_some_and(|r| r.band_count() >= 2)) - .map(|l| l.name.clone()) - .collect() +/// The rasters a profile can be read on: those of two bands or more, by name, and how many +/// more are left out because an earlier layer has their name (commands find a layer by its +/// name, so they would read that one). +pub fn multiband(app: &RustGisApp) -> (Vec, usize) { + let map = app.session.map(); + let (mut names, mut shadowed) = (Vec::new(), 0usize); + for l in map.layers.iter().filter(|l| l.raster.as_ref().and_then(|r| r.data.as_ref()).is_some_and(|r| r.band_count() >= 2)) { + if map.layer_by_name(&l.name).map(|f| f.id) == Some(l.id) { + names.push(l.name.clone()); + } else { + shadowed += 1; + } + } + (names, shadowed) } /// Whether Identify's result `info` is a pixel of a raster of two bands or more: its layer's id. @@ -156,11 +161,20 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { ui.add_space(6.0); ui.label(egui::RichText::new(tl!("Spectral profile")).font(semibold(12.5))); ui.add_space(2.0); - let rasters = multiband(app); + let (rasters, shadowed) = multiband(app); + if shadowed > 0 { + let n = shadowed.to_string(); + let text = crate::i18n::fmt("{n} raster(s) left out: another layer has the same name. Rename them to chart them.", &[("n", &n)]); + ui.label(egui::RichText::new(text).small().color(t.red)); + } if rasters.is_empty() { ui.label( egui::RichText::new(tl!("Add a raster of two or more bands first, such as a Landsat or Sentinel-2 scene.")).weak().small(), ); + // With nothing to read, picking ends: a click would be taken and go nowhere. + if app.workflow.pick.as_ref().is_some_and(|p| p.purpose == PURPOSE) { + app.workflow.pick = None; + } return; } if !rasters.contains(&app.workflow.layer) { @@ -214,7 +228,7 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let sp = &mut app.workflow.spectral; sp.message = Some(match std::fs::write(&path, csv(sp)) { Ok(()) => (crate::i18n::fmt("Saved {path}", &[("path", &path)]), false), - Err(e) => (format!("{path}: {e}"), true), + Err(e) => (crate::i18n::fmt("{path}: {error}", &[("path", &path), ("error", &e.to_string())]), true), }); } } @@ -514,5 +528,24 @@ mod tests { assert_eq!(app.workflow.spectral.samples.len(), before, "nothing read from the other scene"); let said: Vec = app.session.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert_eq!(said, [format!("{name}: another layer has this name, so its profile would be read from that one: rename one of them")]); + // The section's list leaves it out too, and counts it. + let (rasters, shadowed) = multiband(&app); + assert_eq!((rasters.len(), shadowed), (1, 1), "{rasters:?}"); + } + + /// With no raster of two bands or more, the section says so and ends a pick left on. + #[test] + fn the_profile_ends_its_pick_when_no_raster_is_left() { + let mut app = RustGisApp::with_sample(); + app.workflow.pick = Some(crate::analysispane::Pick { purpose: PURPOSE, needed: 1, points: Vec::new() }); + let mut h = egui_kittest::Harness::builder().allow_missing_glyphs().with_size(egui::vec2(420.0, 320.0)).build_ui_state( + |ui, app: &mut RustGisApp| { + let t = crate::theme::Tokens::get(ui.ctx()); + section(app, ui, &t); + }, + app, + ); + h.run_steps(1); + assert!(h.state().workflow.pick.is_none()); } } From f8f0aac01669c666bbe0551898cee8450f76ae75 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 21:34:53 -0300 Subject: [PATCH 09/25] fix(imagery): a resampled raster keeps its wavelengths (review round 8) raster.process's resample (block means) copies each band's wavelength, as the other commands that carry bands over do. The chart's hover lines go through i18n::fmt. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/imagery.rs | 2 ++ crates/engine/src/tests.rs | 4 ++++ crates/ui-egui/src/spectralui.rs | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index cdc1472136..61168cddbb 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -1214,6 +1214,8 @@ pub(crate) fn process_work(s: &Session, p: &Value) -> Result Date: Mon, 9 Nov 2026 00:22:56 -0300 Subject: [PATCH 10/25] spectral profile: say how GeoLibre's code was followed Its TypeScript was read for what it does; this Rust was written anew. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/spectralui.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 05b0ff0fcb..75e1a4114c 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -2,7 +2,8 @@ //! six compared, against wavelength when the file gives one for every band and band number //! otherwise. It is the Workflows pane's Spectral Analysis section, and Identify's pop-up on a //! pixel of two or more bands adds that pixel to it. After GeoLibre's spectral profile -//! (opengeos/geolibre#1818, MIT). +//! (opengeos/geolibre#1818, MIT): its TypeScript (`cog-spectral-profile.ts` and the NetCDF +//! profile chart) was read for what it does, and this Rust was written anew, no code copied. use egui::{Color32, Pos2, Sense, Stroke, Ui, pos2, vec2}; use serde_json::{Value, json}; From f59bb1923d9d6acb1aa71e55a4e5b1830376e19d Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 00:57:49 -0300 Subject: [PATCH 11/25] spectral profile: free colours, a namesake raster starts again, shared wavelengths - A new point takes a colour no point kept has, so taking one off and adding another never gives two points one colour. - The points remember the map and layer they were read on: a raster of the same name added again, on another map or in another project starts the profile again. - Hovering a wavelength several bands share shows each of them. - Identify's Spectral Profile icon is a labelled button for accessibility, and the point list's close glyph goes through tl!. - Tests: a mosaic of inputs sharing a wavelength keeps it; the zone answer gives the wavelengths. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 11 ++++ crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/mapview.rs | 1 + crates/ui-egui/src/spectralui.rs | 96 +++++++++++++++++++++++--------- 4 files changed, 85 insertions(+), 25 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index fb20597fa1..a37574d23f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -25934,6 +25934,16 @@ fn dem_tiles_mosaic_clip_by_polygon_share_a_stretch_and_report_overviews() { let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert!(said.iter().any(|n| n == "over: band names, classes or wavelengths other than west's, which the mosaic keeps"), "{said:?}"); assert_eq!(raster(&s, &v).bands[0].wavelength, None); + // Inputs of one wavelength: the mosaic has it, and nothing is said. + let west = s.map().layer_by_name("west").unwrap().id; + let data = s.map_mut().layer_mut(west).unwrap().raster.as_mut().unwrap().data.as_mut().unwrap(); + std::sync::Arc::make_mut(data).bands[0].wavelength = Some(842.0); + let mark = s.notice_mark(); + let v = s.run("raster.mosaic", &json!({"inputs": ["west", "over"], "name": "nir both"})).unwrap(); + assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("band names, classes")), "{:?}", s.notices_since(mark)); + assert_eq!(raster(&s, &v).bands[0].wavelength, Some(842.0)); + let data = s.map_mut().layer_mut(west).unwrap().raster.as_mut().unwrap().data.as_mut().unwrap(); + std::sync::Arc::make_mut(data).bands[0].wavelength = None; wavelength(&mut s, None); // A file given by path says what its reading left out (here a sidecar's scale); one read whole // says nothing. @@ -34424,6 +34434,7 @@ fn spectral_profiles_at_points_with_wavelengths() { let mark = s.notice_mark(); let v = s.run("raster.spectral", ¶ms).unwrap(); assert!(v["pixels"].as_u64().is_some_and(|n| n > 0), "{v}"); + assert_eq!((&v["wavelengths"], &v["wavelengthUnit"]), (&json!([665.0, 560.0, 490.0]), &json!("nm")), "{v}"); let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert_eq!(said, [format!("scene: {left} left out: the zone's mean is read")]); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index afa52baa73..60a2710979 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2305,6 +2305,8 @@ impl RustGisApp { self.ui.web_file = None; // A deletion asked for in the old project never reaches a namesake in the new one. self.map.deleting = None; + // Nor are a spectral profile's points read on a namesake raster of the new one. + self.workflow.spectral.clear(); // A recording's steps name the old project's layers: it ends here, and says so. if let Some(steps) = self.ws.recording.take() { let n = steps.len().to_string(); diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index 667c5e54ce..0a99f4b956 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -3606,6 +3606,7 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { let (r, resp) = ui.allocate_exact_size(vec2(18.0, 18.0), Sense::click()); let ink = if resp.hovered() { t.text } else { t.icon }; crate::icons::paint(ui.painter(), r, "chart", ink, t.accent); + resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("Spectral Profile"))); if resp.on_hover_text(tl!("Spectral Profile: chart every band here, and compare points")).clicked() { profile = true; } diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 75e1a4114c..8f4f4f80ec 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -25,6 +25,8 @@ pub const PURPOSE: &str = "spectral"; pub struct Sample { /// Its number, from 1: it keeps its colour and marker when an older point goes. pub n: u32, + /// Its colour's place in [`colour`]'s list: the first no other point kept has. + pub slot: usize, pub lon: f64, pub lat: f64, pub pixel: (i64, i64), @@ -38,6 +40,9 @@ pub struct Spectral { pub bands: Vec, /// Each band's wavelength in nanometres; empty when the file gives none. pub wavelengths: Vec>, + /// The map (its place in the project) and layer the points were read on: a raster of the + /// same name on another map, or added again, isn't the same one. + pub source: Option<(usize, u32)>, /// Oldest first. pub samples: Vec, /// The last point's number. @@ -64,8 +69,8 @@ impl Spectral { } } -/// A point's colour, by its number: the same on the chart, its legend and its marker. -pub fn colour(t: &Tokens, n: u32) -> Color32 { +/// A point's colour, by its slot: the same on the chart, its legend and its marker. +pub fn colour(t: &Tokens, slot: usize) -> Color32 { let colours = [ t.accent, Color32::from_rgb(214, 96, 77), @@ -74,7 +79,12 @@ pub fn colour(t: &Tokens, n: u32) -> Color32 { Color32::from_rgb(230, 171, 2), Color32::from_rgb(27, 158, 119), ]; - colours.get((n.max(1) as usize - 1) % colours.len()).copied().unwrap_or(t.accent) + colours.get(slot % colours.len()).copied().unwrap_or(t.accent) +} + +/// The map (its place in the project) and layer the commands read as `layer`. +fn source_of(app: &RustGisApp, layer: &str) -> Option<(usize, u32)> { + app.session.map().layer_by_name(layer).map(|l| (app.session.project.active_map, l.id)) } /// The rasters a profile can be read on: those of two bands or more, by name, and how many @@ -128,6 +138,7 @@ pub fn from_identify(app: &mut RustGisApp, id: u32, at: rustgis_engine::geom::Co /// said in the section. pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Coord) { let r = app.run("raster.spectral", json!({"layer": layer, "lon": at.x, "lat": at.y})); + let source = source_of(app, layer); let sp = &mut app.workflow.spectral; let v = match r { Ok(v) => v, @@ -139,18 +150,21 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co let bands: Vec = v["bands"].as_array().into_iter().flatten().map(|b| b.as_str().unwrap_or_default().to_string()).collect(); let wavelengths: Vec> = v["wavelengths"].as_array().into_iter().flatten().map(Value::as_f64).collect(); // Another raster, or the same one's bands changed: its points aren't comparable. - if sp.layer != layer || sp.bands != bands || sp.wavelengths != wavelengths { + if sp.layer != layer || sp.source != source || sp.bands != bands || sp.wavelengths != wavelengths { sp.samples.clear(); sp.layer = layer.to_string(); + sp.source = source; sp.bands = bands; sp.wavelengths = wavelengths; } let pixel = (v["pixel"][0].as_i64().unwrap_or(0), v["pixel"][1].as_i64().unwrap_or(0)); let values: Vec> = v["values"].as_array().into_iter().flatten().map(Value::as_f64).collect(); sp.last += 1; - sp.samples.push(Sample { n: sp.last, lon: at.x, lat: at.y, pixel, values }); - let excess = sp.samples.len().saturating_sub(MAX_POINTS); - sp.samples.drain(..excess); + // The oldest goes first, so the new point may take its colour. + let excess = (sp.samples.len() + 1).saturating_sub(MAX_POINTS); + sp.samples.drain(..excess.min(sp.samples.len())); + let slot = (0..MAX_POINTS).find(|k| sp.samples.iter().all(|s| s.slot != *k)).unwrap_or(0); + sp.samples.push(Sample { n: sp.last, slot, lon: at.x, lat: at.y, pixel, values }); sp.message = (excess > 0).then(|| { let n = MAX_POINTS.to_string(); (crate::i18n::fmt("Up to {n} points are compared: the oldest one was taken off.", &[("n", &n)]), false) @@ -195,8 +209,10 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { }) .response .on_hover_text(tl!("Layer")); - // Points on another raster aren't comparable with this one's. - if app.workflow.spectral.layer != app.workflow.layer && !app.workflow.spectral.samples.is_empty() { + // Points on another raster aren't comparable with this one's, nor those on a raster of its + // name that has since gone (another map, the layer added again). + let sp = &app.workflow.spectral; + if !sp.samples.is_empty() && (sp.layer != app.workflow.layer || source_of(app, &sp.layer) != sp.source) { app.workflow.spectral.clear(); } // Points clicked on the map while picking: each is read, and picking goes on. @@ -253,7 +269,7 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let mut remove = None; for (i, s) in app.workflow.spectral.samples.iter().enumerate() { ui.horizontal(|ui| { - let c = colour(t, s.n); + let c = colour(t, s.slot); let (n, lon, lat) = (s.n.to_string(), format!("{:.5}", s.lon), format!("{:.5}", s.lat)); let (col, row) = (s.pixel.0.to_string(), s.pixel.1.to_string()); let text = crate::i18n::fmt( @@ -261,7 +277,10 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { &[("n", &n), ("lon", &lon), ("lat", &lat), ("col", &col), ("row", &row)], ); ui.label(egui::RichText::new(text).small().color(c)); - if ui.add(egui::Button::new(egui::RichText::new("×").small()).frame(false)).on_hover_text(tl!("Take this point off")).clicked() + if ui + .add(egui::Button::new(egui::RichText::new(tl!("×")).small()).frame(false)) + .on_hover_text(tl!("Take this point off")) + .clicked() { remove = Some(i); } @@ -305,7 +324,7 @@ fn chart(sp: &Spectral, ui: &mut Ui, t: &Tokens) { } painter.line_segment([pos2(plot.min.x, plot.max.y + 2.0), pos2(plot.max.x, plot.max.y + 2.0)], Stroke::new(1.0, t.border_strong)); for s in &sp.samples { - let c = colour(t, s.n); + let c = colour(t, s.slot); // Runs between bands with no data, so a gap shows as one. let mut runs: Vec> = vec![Vec::new()]; for b in &order { @@ -343,20 +362,24 @@ fn chart(sp: &Spectral, ui: &mut Ui, t: &Tokens) { if let Some(b) = near { let px = xs.get(b).map_or(plot.min.x, |x| to(*x, lo).x); painter.line_segment([pos2(px, plot.min.y), pos2(px, plot.max.y)], Stroke::new(1.0, t.text_dim)); + // Bands of one wavelength sit at one place: each is shown. + let at: Vec = (0..xs.len()).filter(|i| xs.get(*i) == xs.get(b)).collect(); resp.on_hover_ui_at_pointer(|ui| { - let name = sp.bands.get(b).map_or("", String::as_str); - let number = (b + 1).to_string(); - let title = match sp.wavelengths.get(b).copied().flatten() { - Some(w) => { - crate::i18n::fmt("Band {n}: {name}, {wavelength} nm", &[("n", &number), ("name", name), ("wavelength", &fmt(w))]) + for b in at { + let name = sp.bands.get(b).map_or("", String::as_str); + let number = (b + 1).to_string(); + let title = match sp.wavelengths.get(b).copied().flatten() { + Some(w) => { + crate::i18n::fmt("Band {n}: {name}, {wavelength} nm", &[("n", &number), ("name", name), ("wavelength", &fmt(w))]) + } + None => crate::i18n::fmt("Band {n}: {name}", &[("n", &number), ("name", name)]), + }; + ui.label(egui::RichText::new(title).small().strong()); + for s in &sp.samples { + let v = s.values.get(b).copied().flatten().map_or_else(|| tl!("no data").to_string(), fmt); + let n = s.n.to_string(); + ui.label(egui::RichText::new(crate::i18n::fmt("● {n} {v}", &[("n", &n), ("v", &v)])).small().color(colour(t, s.slot))); } - None => crate::i18n::fmt("Band {n}: {name}", &[("n", &number), ("name", name)]), - }; - ui.label(egui::RichText::new(title).small().strong()); - for s in &sp.samples { - let v = s.values.get(b).copied().flatten().map_or_else(|| tl!("no data").to_string(), fmt); - let n = s.n.to_string(); - ui.label(egui::RichText::new(crate::i18n::fmt("● {n} {v}", &[("n", &n), ("v", &v)])).small().color(colour(t, s.n))); } }); } @@ -398,7 +421,7 @@ pub fn paint(app: &RustGisApp, p: &egui::Painter, t: &Tokens, to_screen: impl Fn } for s in &app.workflow.spectral.samples { let Some(at) = to_screen(rustgis_engine::geom::Coord::new(s.lon, s.lat)) else { continue }; - let c = colour(t, s.n); + let c = colour(t, s.slot); p.circle_filled(at, 7.0, c); p.circle_stroke(at, 7.0, Stroke::new(1.5, Color32::WHITE)); p.text(at, egui::Align2::CENTER_CENTER, s.n.to_string(), semibold(10.0), Color32::WHITE); @@ -503,6 +526,29 @@ mod tests { assert_eq!(app.workflow.spectral.layer, "other"); } + /// A point added after one is taken off takes a colour no point kept has; a raster of the + /// same name added again starts the profile again. + #[test] + fn a_new_point_takes_a_free_colour_and_a_new_raster_starts_again() { + let mut app = app_with_scene(false); + for i in 0..MAX_POINTS { + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005 + 0.001 * i as f64, 44.995)); + } + app.workflow.spectral.samples.remove(1); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + let slots: Vec = app.workflow.spectral.samples.iter().map(|s| s.slot).collect(); + assert_eq!(slots, [0, 2, 3, 4, 5, 1], "the new point takes the colour point 2 left"); + // Past six, the oldest goes and the new point takes its colour. + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + let slots: Vec = app.workflow.spectral.samples.iter().map(|s| s.slot).collect(); + assert_eq!(slots, [2, 3, 4, 5, 1, 0]); + // The scene removed and another of its name added: its points aren't the old ones. + app.run("layer.remove", json!({"layer": "scene"})).unwrap(); + app.run("layer.addDataBytes", json!({"fileName": "scene.tif", "data": rgb_tiff_b64(false)})).unwrap(); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + assert_eq!(app.workflow.spectral.samples.len(), 1); + } + /// Identify's pop-up offers the profile on a pixel of two bands or more, and its button opens /// the section there with the point read. #[test] From 3b1779f386dd71bf9f9c21ee427b760ae9004a1c Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 01:39:20 -0300 Subject: [PATCH 12/25] spectral profile: wavelengths from 1 nm to 1 m, the workspace mosaic's, stale points dropped - A wavelength is read only from 1 nm to 1 m: a tiny one would round to 0 when written in micrometres, and a huge one stretches the chart; one outside is said and left out. - Build Mosaic (imagery.mosaic) gives a band the images' wavelength when they all give the same one, and says how many bands they disagree on. - The section drops points read on a raster since gone even when no raster is left, and the map doesn't draw them; opening a peer's project clears them, as opening one does. - Taking a point off clears what the last point added said. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 37 +++++++++++++++++++++----------- crates/engine/src/cmd/imagery.rs | 32 +++++++++++++++++++++------ crates/engine/src/tests.rs | 23 ++++++++++++++++++++ crates/ui-egui/src/lib.rs | 2 ++ crates/ui-egui/src/spectralui.rs | 35 +++++++++++++++++++++++++----- docs/user-guide/data.md | 4 ++-- 6 files changed, 106 insertions(+), 27 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 87ee3a04b2..d2ba6ba81c 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -112,8 +112,8 @@ fn is_wavelength_item(name: &str) -> bool { name.eq_ignore_ascii_case(WAVELENGTH_UNITS) || WAVELENGTH_ITEMS.iter().any(|w| name.eq_ignore_ascii_case(w)) } -/// A wavelength item's value in nanometres: `None` when it isn't a positive number, or its unit -/// isn't a length this knows. `CENTRAL_WAVELENGTH_UM` is in micrometres by its name; another item +/// A wavelength item's value in nanometres: `None` when it isn't a length from 1 nm to 1 m (no +/// sensor's band lies outside), or its unit isn't a length this knows. `CENTRAL_WAVELENGTH_UM` is in micrometres by its name; another item /// is in the unit `units` gives (nanometres or micrometres, as ENVI and others spell them). With no /// unit (or ENVI's `Unknown`), a value under 100 is in micrometres and any other in nanometres: /// imaging sensors see from about 0.3 to 15 µm (300 to 15,000 nm), so the two never meet. @@ -121,10 +121,12 @@ pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option let v = text.trim().parse::().ok().filter(|v| v.is_finite() && *v > 0.0)?; // Micrometres in nanometres, to a millionth of one: 0.665 µm is 665 nm, not 665.0000000000001. let nm = |um: f64| (um * 1e9).round() / 1e6; - // The result is checked too: a huge micrometre value overflows, a tiny one rounds to 0. - let positive = |w: f64| Some(w).filter(|w| w.is_finite() && *w > 0.0); + // The result is checked: a huge micrometre value overflows, a tiny one rounds to 0, and a + // length past those a sensor sees would be lost when written in micrometres or stretch the + // chart's axis. + let in_range = |w: f64| Some(w).filter(|w| (1.0..=1e9).contains(w)); if item.eq_ignore_ascii_case(CENTRAL_WAVELENGTH_UM) { - return positive(nm(v)); + return in_range(nm(v)); } let unit = units.map(|u| u.trim().to_lowercase()).unwrap_or_default(); let micro = match unit.as_str() { @@ -133,7 +135,7 @@ pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option "um" | "\u{b5}m" | "\u{3bc}m" | "micrometer" | "micrometers" | "micrometre" | "micrometres" | "micron" | "microns" => true, _ => return None, }; - positive(if micro { nm(v) } else { v }) + in_range(if micro { nm(v) } else { v }) } /// Keeps a band's first wavelength in `slot`: `true` when `nm` is another one, left out (the @@ -350,7 +352,7 @@ fn parse_sidecar(text: &str) -> Result { } if odd_wavelengths > 0 { pam.lost.push(format!( - "{odd_wavelengths} wavelength(s) that aren't a positive length in nanometres or micrometres, or for no band, left out" + "{odd_wavelengths} wavelength(s) that aren't a length from 1 nm to 1 m in nanometres or micrometres, or for no band, left out" )); } if other_wavelengths > 0 { @@ -495,7 +497,7 @@ pub fn parse_gdal_metadata(text: &str) -> Option { } if odd_wavelengths > 0 { pam.lost.push(format!( - "{odd_wavelengths} wavelength item(s) that aren't a positive length in nanometres or micrometres, or for no band from 1 to {MAX_BANDS}, left out" + "{odd_wavelengths} wavelength item(s) that aren't a length from 1 nm to 1 m in nanometres or micrometres, or for no band from 1 to {MAX_BANDS}, left out" )); } if other_wavelengths > 0 { @@ -993,10 +995,19 @@ mod tests { } assert_eq!(wavelength_nm(CENTRAL_WAVELENGTH_UM, v, None), None, "{v}"); } - let xml = r#"1e300842"#; + // Lengths past 1 nm to 1 m are left out: written in micrometres a tiny one would round + // to 0, and a huge one stretches the chart. + for v in ["1e-7", "0.5", "1e300", "2e9"] { + assert_eq!(wavelength_nm("wavelength", v, Some("nm")), None, "{v} nm"); + } + assert_eq!(wavelength_nm("wavelength", "1", Some("nm")), Some(1.0)); + assert_eq!(wavelength_nm("wavelength", "1e9", Some("nm")), Some(1e9)); + assert_eq!(wavelength_nm("wavelength", "1e-7", Some("micrometers")), None, "0.0001 nm"); + let xml = r#"1e300842nm1e-7"#; let pam = parse_gdal_metadata(xml).unwrap(); assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(842.0)); - assert!(pam.lost[0].starts_with("1 wavelength item(s)"), "{:?}", pam.lost); + assert_eq!(pam.band(2).and_then(|b| b.wavelength), None); + assert!(pam.lost[0].starts_with("2 wavelength item(s) that aren't a length from 1 nm to 1 m"), "{:?}", pam.lost); } /// A sidecar's band takes the first item that is a length, as the metadata tag's: one that @@ -1024,10 +1035,10 @@ mod tests { assert_eq!(twice.band(1).and_then(|b| b.wavelength), Some(1.6)); assert_eq!(twice.lost, ["1 other wavelength unit(s) for a band or the dataset left out (the first is kept)"]); // An empty unit names none, in the sidecar and the metadata tag alike: the next is read. - let sidecar = parse(r#"nm0.665"#).unwrap(); - let tag = parse_gdal_metadata(r#" nm0.665"#).unwrap(); + let sidecar = parse(r#"nm1.6"#).unwrap(); + let tag = parse_gdal_metadata(r#" nm1.6"#).unwrap(); for pam in [sidecar, tag] { - assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(0.665)); + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(1.6)); assert!(pam.lost.is_empty(), "{:?}", pam.lost); } // Lengths half a thousandth of a nanometre apart are two. diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 61168cddbb..3a620d712d 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -1768,12 +1768,29 @@ pub(crate) fn mosaic_work(s: &Session, p: &Value) -> Result = Vec::new(); + if !omitted.is_empty() { + messages.push(format!("{} image(s) have bands omitted from the {nb}-band mosaic: {}", omitted.len(), omitted.join("; "))); + } + // A band's wavelength is the images' when they all give it the same one; else it has none, + // and that is said. + let mut differ = 0usize; + let wavelengths: Vec> = (0..nb) + .map(|b| { + let mut each = imgs.iter().map(|r| r.bands.get(b).and_then(|band| band.wavelength)); + let first = each.next().flatten(); + if each.all(|w| w == first) { + first + } else { + differ += 1; + None + } + }) + .collect(); + if differ > 0 { + messages.push(format!("{differ} band(s) the images give different wavelengths have none in the mosaic")); + } + let extra = (!messages.is_empty()).then(|| json!({ "messages": messages })); Ok(Box::new(move || { let refs: Vec<&Raster> = imgs.iter().map(|r| &**r).collect(); let mut bands = rasterops::mosaic(&refs, &e, cell, (cols, rows), nb, w.method == "mean")?; @@ -1794,9 +1811,10 @@ pub(crate) fn mosaic_work(s: &Session, p: &Value) -> Result| { + let id = s.map().layer_by_name(layer).unwrap().id; + let data = s.map_mut().layer_mut(id).unwrap().raster.as_mut().unwrap().data.as_mut().unwrap(); + std::sync::Arc::make_mut(data).bands[0].wavelength = nm; + }; + give(&mut s, &dem, Some(842.0)); + give(&mut s, "dem_later", Some(842.0)); + let r = s.run("imagery.mosaic", &json!({"name": "Valley"})).unwrap(); + assert!(r.get("messages").is_none(), "{r}"); + let layer = s.map().layer(r["layer"].as_u64().unwrap() as u32).unwrap().raster.as_ref().unwrap(); + assert_eq!(layer.data.as_ref().unwrap().bands[0].wavelength, Some(842.0)); + let bytes = layer.embedded.clone().expect("the mosaic's GeoTIFF"); + let back = rustgis_data::raster::geotiff::read(std::io::Cursor::new(bytes.as_slice()), "back").unwrap(); + assert_eq!(back.bands[0].wavelength, Some(842.0)); + give(&mut s, "dem_later", Some(665.0)); + let r = s.run("imagery.mosaic", &json!({"name": "Valley"})).unwrap(); + assert_eq!(r["messages"], json!(["1 band(s) the images give different wavelengths have none in the mosaic"]), "{r}"); + let mo = s.map().layer(r["layer"].as_u64().unwrap() as u32).unwrap().raster.as_ref().unwrap().data.clone().unwrap(); + assert_eq!(mo.bands[0].wavelength, None); + give(&mut s, &dem, None); + give(&mut s, "dem_later", None); s.run("imagery.workspace", &json!({"kind": "reality", "images": ["dem_later"]})).unwrap(); let r = s.run("imagery.mosaic", &json!({"name": "Reality Mapping"})).unwrap(); let dsm = s.map().layer(r["layer"].as_u64().unwrap() as u32).unwrap().name.clone(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 60a2710979..b715e2b2f0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2191,6 +2191,8 @@ impl RustGisApp { assistantpane::peer_replaced(self); self.ui.web_file = None; self.map.deleting = None; + // A spectral profile's points were read on the old project's rasters. + self.workflow.spectral.clear(); self.retire_cog_probes(); if let Some(steps) = self.ws.recording.take() { self.session.notify( diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 8f4f4f80ec..f347786b8f 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -87,6 +87,12 @@ fn source_of(app: &RustGisApp, layer: &str) -> Option<(usize, u32)> { app.session.map().layer_by_name(layer).map(|l| (app.session.project.active_map, l.id)) } +/// Whether the points were read on the raster the profile's name finds now. +fn fresh(app: &RustGisApp) -> bool { + let sp = &app.workflow.spectral; + source_of(app, &sp.layer) == sp.source +} + /// The rasters a profile can be read on: those of two bands or more, by name, and how many /// more are left out because an earlier layer has their name (commands find a layer by its /// name, so they would read that one). @@ -176,6 +182,12 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { ui.add_space(6.0); ui.label(egui::RichText::new(tl!("Spectral profile")).font(semibold(12.5))); ui.add_space(2.0); + // Points read on a raster that has since gone (removed, added again, on another map) are + // dropped, even when no raster is left to choose. + let sp = &app.workflow.spectral; + if !sp.samples.is_empty() && !fresh(app) { + app.workflow.spectral.clear(); + } let (rasters, shadowed) = multiband(app); if shadowed > 0 { let n = shadowed.to_string(); @@ -209,10 +221,8 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { }) .response .on_hover_text(tl!("Layer")); - // Points on another raster aren't comparable with this one's, nor those on a raster of its - // name that has since gone (another map, the layer added again). - let sp = &app.workflow.spectral; - if !sp.samples.is_empty() && (sp.layer != app.workflow.layer || source_of(app, &sp.layer) != sp.source) { + // Points on another raster aren't comparable with this one's. + if !app.workflow.spectral.samples.is_empty() && app.workflow.spectral.layer != app.workflow.layer { app.workflow.spectral.clear(); } // Points clicked on the map while picking: each is read, and picking goes on. @@ -288,6 +298,8 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } if let Some(i) = remove { app.workflow.spectral.samples.remove(i); + // What the last point added said is no longer about these points. + app.workflow.spectral.message = None; } } @@ -416,7 +428,8 @@ pub fn csv(sp: &Spectral) -> String { /// The points' numbered markers on the map, in their colours, while the section shows. pub fn paint(app: &RustGisApp, p: &egui::Painter, t: &Tokens, to_screen: impl Fn(rustgis_engine::geom::Coord) -> Option) { - if app.ui.right != Some(crate::RightPane::Workflows) || app.workflow.section != SECTION { + // Points read on a raster since gone aren't drawn on the one there now. + if app.ui.right != Some(crate::RightPane::Workflows) || app.workflow.section != SECTION || !fresh(app) { return; } for s in &app.workflow.spectral.samples { @@ -547,6 +560,18 @@ mod tests { app.run("layer.addDataBytes", json!({"fileName": "scene.tif", "data": rgb_tiff_b64(false)})).unwrap(); add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); assert_eq!(app.workflow.spectral.samples.len(), 1); + // The scene removed with no raster left: the section drops its points. + app.run("layer.remove", json!({"layer": "scene"})).unwrap(); + assert!(!fresh(&app), "its points aren't drawn"); + let mut h = egui_kittest::Harness::builder().allow_missing_glyphs().with_size(egui::vec2(420.0, 320.0)).build_ui_state( + |ui, app: &mut RustGisApp| { + let t = crate::theme::Tokens::get(ui.ctx()); + section(app, ui, &t); + }, + app, + ); + h.run_steps(1); + assert!(h.state().workflow.spectral.samples.is_empty()); } /// Identify's pop-up offers the profile on a pixel of two bands or more, and its button opens diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 14813219b6..a0f6fbc6ef 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1127,8 +1127,8 @@ GeoTIFF's GDAL metadata or its `.aux.xml` sidecar: GDAL's `CENTRAL_WAVELENGTH_UM domain), or a `wavelength` or `central_wavelength` item (as rasterio, STAC exports and ENVI files through GDAL write them) in the unit a `wavelength_units` item gives. With no unit, a value under 100 is read as micrometres and any other as nanometres: imaging sensors see from about 0.3 to 15 µm, so -the two never meet. One that isn't a length, or another length for a band that has one, is said and -left out. A GeoTIFF export writes the wavelengths of the bands it writes (one band, or a raster's +the two never meet. One that isn't a length from 1 nm to 1 m, or another length for a band that has +one, is said and left out. A GeoTIFF export writes the wavelengths of the bands it writes (one band, or a raster's RGB) back as `CENTRAL_WAVELENGTH_UM`. Projects remember a raster's file path relative to the project and reopen it; rasters added in the browser are stored inside the project. From 7018460a6d3ed8aa000f0d44d713caefb0f44eff Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 01:48:33 -0300 Subject: [PATCH 13/25] spectral profile: say the points a raster change takes off Choosing another raster, a point read on another, or the raster read changing or going takes every point off, as before; the section now says how many went and why. The wavelength reader's doc comment is rewrapped. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 10 +++--- crates/ui-egui/src/spectralui.rs | 56 +++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index d2ba6ba81c..cec411dbbe 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -113,10 +113,12 @@ fn is_wavelength_item(name: &str) -> bool { } /// A wavelength item's value in nanometres: `None` when it isn't a length from 1 nm to 1 m (no -/// sensor's band lies outside), or its unit isn't a length this knows. `CENTRAL_WAVELENGTH_UM` is in micrometres by its name; another item -/// is in the unit `units` gives (nanometres or micrometres, as ENVI and others spell them). With no -/// unit (or ENVI's `Unknown`), a value under 100 is in micrometres and any other in nanometres: -/// imaging sensors see from about 0.3 to 15 µm (300 to 15,000 nm), so the two never meet. +/// sensor's band lies outside), or its unit isn't a length this knows. +/// +/// `CENTRAL_WAVELENGTH_UM` is in micrometres by its name; another item is in the unit `units` +/// gives (nanometres or micrometres, as ENVI and others spell them). With no unit (or ENVI's +/// `Unknown`), a value under 100 is in micrometres and any other in nanometres: imaging sensors +/// see from about 0.3 to 15 µm (300 to 15,000 nm), so the two never meet. pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option { let v = text.trim().parse::().ok().filter(|v| v.is_finite() && *v > 0.0)?; // Micrometres in nanometres, to a millionth of one: 0.665 µm is 665 nm, not 665.0000000000001. diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index f347786b8f..aab1edc58d 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -52,6 +52,26 @@ pub struct Spectral { } impl Spectral { + /// Takes every point off, as they aren't comparable with what is read now (`now`: the raster + /// chosen, or `None` when theirs has changed or gone), and says how many went. + pub fn restart(&mut self, now: Option<&str>) { + let n = self.samples.len(); + self.samples.clear(); + self.message = (n > 0).then(|| { + let (n, old) = (n.to_string(), self.layer.clone()); + let text = match now { + Some(now) => crate::i18n::fmt( + "{n} point(s) taken off: they were read on {old}, not on {now}.", + &[("n", &n), ("old", &old), ("now", now)], + ), + None => { + crate::i18n::fmt("{n} point(s) taken off: {old} has changed or gone since they were read.", &[("n", &n), ("old", &old)]) + } + }; + (text, false) + }); + } + /// Empties it. pub fn clear(&mut self) { self.samples.clear(); @@ -155,9 +175,12 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co }; let bands: Vec = v["bands"].as_array().into_iter().flatten().map(|b| b.as_str().unwrap_or_default().to_string()).collect(); let wavelengths: Vec> = v["wavelengths"].as_array().into_iter().flatten().map(Value::as_f64).collect(); - // Another raster, or the same one's bands changed: its points aren't comparable. + // Another raster, or the same one's bands changed: its points aren't comparable, and that + // they went is said. + let mut restarted = None; if sp.layer != layer || sp.source != source || sp.bands != bands || sp.wavelengths != wavelengths { - sp.samples.clear(); + sp.restart(if sp.layer.eq_ignore_ascii_case(layer) { None } else { Some(layer) }); + restarted = sp.message.take(); sp.layer = layer.to_string(); sp.source = source; sp.bands = bands; @@ -171,10 +194,13 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co sp.samples.drain(..excess.min(sp.samples.len())); let slot = (0..MAX_POINTS).find(|k| sp.samples.iter().all(|s| s.slot != *k)).unwrap_or(0); sp.samples.push(Sample { n: sp.last, slot, lon: at.x, lat: at.y, pixel, values }); - sp.message = (excess > 0).then(|| { - let n = MAX_POINTS.to_string(); - (crate::i18n::fmt("Up to {n} points are compared: the oldest one was taken off.", &[("n", &n)]), false) - }); + // What this point did is said: an older one going, or the points before it taken off. + sp.message = (excess > 0) + .then(|| { + let n = MAX_POINTS.to_string(); + (crate::i18n::fmt("Up to {n} points are compared: the oldest one was taken off.", &[("n", &n)]), false) + }) + .or(restarted); } /// The section: the raster, Pick Points, the chart and its points. @@ -186,7 +212,7 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { // dropped, even when no raster is left to choose. let sp = &app.workflow.spectral; if !sp.samples.is_empty() && !fresh(app) { - app.workflow.spectral.clear(); + app.workflow.spectral.restart(None); } let (rasters, shadowed) = multiband(app); if shadowed > 0 { @@ -198,6 +224,10 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { ui.label( egui::RichText::new(tl!("Add a raster of two or more bands first, such as a Landsat or Sentinel-2 scene.")).weak().small(), ); + // Points just taken off are said here too. + if let Some((text, _)) = &app.workflow.spectral.message { + ui.label(egui::RichText::new(text.as_str()).small().color(t.text_dim)); + } // With nothing to read, picking ends: a click would be taken and go nowhere. if app.workflow.pick.as_ref().is_some_and(|p| p.purpose == PURPOSE) { app.workflow.pick = None; @@ -223,7 +253,8 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { .on_hover_text(tl!("Layer")); // Points on another raster aren't comparable with this one's. if !app.workflow.spectral.samples.is_empty() && app.workflow.spectral.layer != app.workflow.layer { - app.workflow.spectral.clear(); + let now = app.workflow.layer.clone(); + app.workflow.spectral.restart(Some(&now)); } // Points clicked on the map while picking: each is read, and picking goes on. let clicked = match &mut app.workflow.pick { @@ -537,6 +568,11 @@ mod tests { add_point(&mut app, "other", rustgis_engine::geom::Coord::new(10.005, 44.995)); assert_eq!(app.workflow.spectral.samples.len(), 1); assert_eq!(app.workflow.spectral.layer, "other"); + assert_eq!( + app.workflow.spectral.message, + Some(("6 point(s) taken off: they were read on scene, not on other.".to_string(), false)), + "the points that went are said" + ); } /// A point added after one is taken off takes a colour no point kept has; a raster of the @@ -572,6 +608,10 @@ mod tests { ); h.run_steps(1); assert!(h.state().workflow.spectral.samples.is_empty()); + assert_eq!( + h.state().workflow.spectral.message, + Some(("1 point(s) taken off: scene has changed or gone since they were read.".to_string(), false)) + ); } /// Identify's pop-up offers the profile on a pixel of two bands or more, and its button opens From f5484ae04e52ba3ff0160b294183dfa95a573adc Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 02:17:31 -0300 Subject: [PATCH 14/25] spectral profile: one wavelength comparison, a rename followed, a map switch said - same_wavelength (a millionth of a nanometre, as the readers read) is shared by the readers and both mosaics, so a rounding apart is one wavelength there too. - The workspace mosaic's notice says the images don't all give the same wavelength, or some give none. - A raster renamed since its points were read keeps them under its new name. - Points taken off because another map is shown say so. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 14 +++-- crates/engine/src/cmd/imagery.rs | 10 ++-- crates/engine/src/tests.rs | 10 +++- crates/ui-egui/src/spectralui.rs | 88 ++++++++++++++++++++++++++++---- 4 files changed, 104 insertions(+), 18 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index cec411dbbe..8e83306a74 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -140,16 +140,24 @@ pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option in_range(if micro { nm(v) } else { v }) } +/// Whether two wavelengths (nanometres, `None` for none) are one: lengths a millionth of a +/// nanometre apart or less are, as micrometres are read to that, so only rounding is absorbed. +pub fn same_wavelength(a: Option, b: Option) -> bool { + match (a, b) { + (Some(a), Some(b)) => (a - b).abs() <= 1e-6, + (a, b) => a.is_none() && b.is_none(), + } +} + /// Keeps a band's first wavelength in `slot`: `true` when `nm` is another one, left out (the -/// same length under another item, to a millionth of a nanometre, is the same one: micrometres -/// are read to that, so only rounding is absorbed). +/// same length under another item is the same one, [`same_wavelength`]). fn keep_first_wavelength(slot: &mut Option, nm: f64) -> bool { match *slot { None => { *slot = Some(nm); false } - Some(w) => (w - nm).abs() > 1e-6, + Some(w) => !same_wavelength(Some(w), Some(nm)), } } diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 3a620d712d..feef97a217 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -1377,7 +1377,10 @@ fn raster_mosaic(s: &mut Session, p: &Value) -> CmdResult { .iter() .skip(1) .filter(|r| { - r.bands.iter().zip(&first.bands).any(|(b, f)| b.name != f.name || b.classes != f.classes || b.wavelength != f.wavelength) + r.bands + .iter() + .zip(&first.bands) + .any(|(b, f)| b.name != f.name || b.classes != f.classes || !raster::pam::same_wavelength(b.wavelength, f.wavelength)) }) .map(|r| r.name.as_str()) .collect(); @@ -1779,7 +1782,7 @@ pub(crate) fn mosaic_work(s: &Session, p: &Value) -> Result Result 0 { - messages.push(format!("{differ} band(s) the images give different wavelengths have none in the mosaic")); + messages + .push(format!("{differ} band(s) have no wavelength in the mosaic: the images don't all give the same one (or some give none)")); } let extra = (!messages.is_empty()).then(|| json!({ "messages": messages })); Ok(Box::new(move || { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index ebf0f2c658..a457a60213 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -9013,9 +9013,17 @@ fn layer_effects_and_imagery_tools() { let bytes = layer.embedded.clone().expect("the mosaic's GeoTIFF"); let back = rustgis_data::raster::geotiff::read(std::io::Cursor::new(bytes.as_slice()), "back").unwrap(); assert_eq!(back.bands[0].wavelength, Some(842.0)); + // A rounding apart is one wavelength, as the readers take it. + give(&mut s, "dem_later", Some(842.000_000_1)); + let r = s.run("imagery.mosaic", &json!({"name": "Valley"})).unwrap(); + assert!(r.get("messages").is_none(), "{r}"); give(&mut s, "dem_later", Some(665.0)); let r = s.run("imagery.mosaic", &json!({"name": "Valley"})).unwrap(); - assert_eq!(r["messages"], json!(["1 band(s) the images give different wavelengths have none in the mosaic"]), "{r}"); + assert_eq!( + r["messages"], + json!(["1 band(s) have no wavelength in the mosaic: the images don't all give the same one (or some give none)"]), + "{r}" + ); let mo = s.map().layer(r["layer"].as_u64().unwrap() as u32).unwrap().raster.as_ref().unwrap().data.clone().unwrap(); assert_eq!(mo.bands[0].wavelength, None); give(&mut s, &dem, None); diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index aab1edc58d..48aab15248 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -51,20 +51,33 @@ pub struct Spectral { pub message: Option<(String, bool)>, } +/// Why a profile's points are taken off. +pub enum Restart<'a> { + /// Another raster, by this name, is read now. + Other(&'a str), + /// Another map is shown: the points were read on the one before. + OtherMap, + /// Their raster has changed or gone. + Gone, +} + impl Spectral { - /// Takes every point off, as they aren't comparable with what is read now (`now`: the raster - /// chosen, or `None` when theirs has changed or gone), and says how many went. - pub fn restart(&mut self, now: Option<&str>) { + /// Takes every point off, as they aren't comparable with what is read now, and says how many + /// went and why. + pub fn restart(&mut self, why: Restart<'_>) { let n = self.samples.len(); self.samples.clear(); self.message = (n > 0).then(|| { let (n, old) = (n.to_string(), self.layer.clone()); - let text = match now { - Some(now) => crate::i18n::fmt( + let text = match why { + Restart::Other(now) => crate::i18n::fmt( "{n} point(s) taken off: they were read on {old}, not on {now}.", &[("n", &n), ("old", &old), ("now", now)], ), - None => { + Restart::OtherMap => { + crate::i18n::fmt("{n} point(s) taken off: they were read on {old} on another map.", &[("n", &n), ("old", &old)]) + } + Restart::Gone => { crate::i18n::fmt("{n} point(s) taken off: {old} has changed or gone since they were read.", &[("n", &n), ("old", &old)]) } }; @@ -107,6 +120,23 @@ fn source_of(app: &RustGisApp, layer: &str) -> Option<(usize, u32)> { app.session.map().layer_by_name(layer).map(|l| (app.session.project.active_map, l.id)) } +/// The raster the points were read on, renamed since: the profile follows its new name (when +/// commands find it by that name), so its points stay. +pub fn follow_rename(app: &mut RustGisApp) { + let Some((map, id)) = app.workflow.spectral.source else { return }; + if map != app.session.project.active_map { + return; + } + let Some(name) = app.session.map().layer(id).map(|l| l.name.clone()) else { return }; + if name == app.workflow.spectral.layer || app.session.map().layer_by_name(&name).map(|l| l.id) != Some(id) { + return; + } + if app.workflow.layer == app.workflow.spectral.layer { + app.workflow.layer = name.clone(); + } + app.workflow.spectral.layer = name; +} + /// Whether the points were read on the raster the profile's name finds now. fn fresh(app: &RustGisApp) -> bool { let sp = &app.workflow.spectral; @@ -179,7 +209,14 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co // they went is said. let mut restarted = None; if sp.layer != layer || sp.source != source || sp.bands != bands || sp.wavelengths != wavelengths { - sp.restart(if sp.layer.eq_ignore_ascii_case(layer) { None } else { Some(layer) }); + let why = if !sp.layer.eq_ignore_ascii_case(layer) { + Restart::Other(layer) + } else if sp.source.map(|s| s.0) != source.map(|s| s.0) { + Restart::OtherMap + } else { + Restart::Gone + }; + sp.restart(why); restarted = sp.message.take(); sp.layer = layer.to_string(); sp.source = source; @@ -208,11 +245,13 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { ui.add_space(6.0); ui.label(egui::RichText::new(tl!("Spectral profile")).font(semibold(12.5))); ui.add_space(2.0); - // Points read on a raster that has since gone (removed, added again, on another map) are - // dropped, even when no raster is left to choose. + // Points read on a raster renamed since stay; those read on one that has since gone + // (removed, added again, on another map) are dropped, even when no raster is left to choose. + follow_rename(app); let sp = &app.workflow.spectral; if !sp.samples.is_empty() && !fresh(app) { - app.workflow.spectral.restart(None); + let other_map = sp.source.is_some_and(|(map, _)| map != app.session.project.active_map); + app.workflow.spectral.restart(if other_map { Restart::OtherMap } else { Restart::Gone }); } let (rasters, shadowed) = multiband(app); if shadowed > 0 { @@ -254,7 +293,7 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { // Points on another raster aren't comparable with this one's. if !app.workflow.spectral.samples.is_empty() && app.workflow.spectral.layer != app.workflow.layer { let now = app.workflow.layer.clone(); - app.workflow.spectral.restart(Some(&now)); + app.workflow.spectral.restart(Restart::Other(&now)); } // Points clicked on the map while picking: each is read, and picking goes on. let clicked = match &mut app.workflow.pick { @@ -591,6 +630,13 @@ mod tests { add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); let slots: Vec = app.workflow.spectral.samples.iter().map(|s| s.slot).collect(); assert_eq!(slots, [2, 3, 4, 5, 1, 0]); + // The scene renamed: its points stay, under its new name. + app.run("layer.rename", json!({"layer": "scene", "name": "renamed"})).unwrap(); + follow_rename(&mut app); + assert!(fresh(&app)); + assert_eq!((app.workflow.spectral.layer.as_str(), app.workflow.spectral.samples.len()), ("renamed", MAX_POINTS)); + app.run("layer.rename", json!({"layer": "renamed", "name": "scene"})).unwrap(); + follow_rename(&mut app); // The scene removed and another of its name added: its points aren't the old ones. app.run("layer.remove", json!({"layer": "scene"})).unwrap(); app.run("layer.addDataBytes", json!({"fileName": "scene.tif", "data": rgb_tiff_b64(false)})).unwrap(); @@ -646,6 +692,26 @@ mod tests { assert_eq!((rasters.len(), shadowed), (1, 1), "{rasters:?}"); } + /// Another map shown: the points read on the one before are taken off, and that is said. + #[test] + fn another_map_takes_the_points_off_and_says_so() { + let mut app = app_with_scene(false); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + app.run("map.new", json!({"name": "Second"})).unwrap(); + assert_ne!(app.session.project.active_map, 0, "the new map is shown"); + let mut h = egui_kittest::Harness::builder().allow_missing_glyphs().with_size(egui::vec2(420.0, 320.0)).build_ui_state( + |ui, app: &mut RustGisApp| { + let t = crate::theme::Tokens::get(ui.ctx()); + section(app, ui, &t); + }, + app, + ); + h.run_steps(1); + let sp = &h.state().workflow.spectral; + assert!(sp.samples.is_empty()); + assert_eq!(sp.message, Some(("1 point(s) taken off: they were read on scene on another map.".to_string(), false))); + } + /// With no raster of two bands or more, the section says so and ends a pick left on. #[test] fn the_profile_ends_its_pick_when_no_raster_is_left() { From fd542167742d7dff08aae4dffced73273c4887da Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 09:47:02 -0300 Subject: [PATCH 15/25] spectral profile: a flat profile labelled with its values, a case-only name kept The chart pads a flat range only to draw it: the values beside it are the ones read (one when every point gives the same) and one shared wavelength reads "Wavelength {nm} nm". add_point compares the raster's name as commands find layers, so the same raster written in another case keeps its points. pam.rs: a band's wavelength unit no longer shadows the units counter. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 4 +- crates/ui-egui/src/spectralui.rs | 72 ++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 8e83306a74..3be5a90f91 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -289,12 +289,12 @@ fn parse_sidecar(text: &str) -> Result { .count(); // Its wavelength: the first that is a length of the items that give one, in that order and // in any domain (GDAL's in `IMAGERY`, ENVI's in the default one), as the metadata tag's. - let units = mdi_text(b, WAVELENGTH_UNITS).or(dataset_units); + let wavelength_units = mdi_text(b, WAVELENGTH_UNITS).or(dataset_units); units_again += other_units(mdi_texts(b, WAVELENGTH_UNITS)); let mut wavelength = None; for w in WAVELENGTH_ITEMS { for t in mdi_texts(b, w) { - match wavelength_nm(w, t, units) { + match wavelength_nm(w, t, wavelength_units) { Some(nm) => other_wavelengths += usize::from(keep_first_wavelength(&mut wavelength, nm)), None => odd_wavelengths += 1, } diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 48aab15248..94f4586742 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -206,9 +206,10 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co let bands: Vec = v["bands"].as_array().into_iter().flatten().map(|b| b.as_str().unwrap_or_default().to_string()).collect(); let wavelengths: Vec> = v["wavelengths"].as_array().into_iter().flatten().map(Value::as_f64).collect(); // Another raster, or the same one's bands changed: its points aren't comparable, and that - // they went is said. + // they went is said. Names are compared as commands find layers, so the same raster written + // in another case keeps its points. let mut restarted = None; - if sp.layer != layer || sp.source != source || sp.bands != bands || sp.wavelengths != wavelengths { + if !sp.layer.eq_ignore_ascii_case(layer) || sp.source != source || sp.bands != bands || sp.wavelengths != wavelengths { let why = if !sp.layer.eq_ignore_ascii_case(layer) { Restart::Other(layer) } else if sp.source.map(|s| s.0) != source.map(|s| s.0) { @@ -218,11 +219,11 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co }; sp.restart(why); restarted = sp.message.take(); - sp.layer = layer.to_string(); sp.source = source; sp.bands = bands; sp.wavelengths = wavelengths; } + sp.layer = layer.to_string(); let pixel = (v["pixel"][0].as_i64().unwrap_or(0), v["pixel"][1].as_i64().unwrap_or(0)); let values: Vec> = v["values"].as_array().into_iter().flatten().map(Value::as_f64).collect(); sp.last += 1; @@ -373,6 +374,31 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } +/// The range a chart axis is drawn over: `lo` to `hi`, or one either side of a single value, so a +/// flat profile is drawn across the middle. +fn scale(lo: f64, hi: f64) -> (f64, f64) { + if hi > lo { (lo, hi) } else { (lo - 1.0, hi + 1.0) } +} + +/// The values written beside the chart: the highest and lowest read, or the one value when every +/// point gives the same. +fn value_labels(lo: f64, hi: f64) -> Vec { + if hi > lo { vec![hi, lo] } else { vec![lo] } +} + +/// The x axis' title: the wavelengths the bands span (one when they share it), or their numbers. +fn axis_label(x0: f64, x1: f64, by_wavelength: bool, bands: usize) -> String { + let fmt = |v: f64| rustgis_engine::map::symbology::fmt_num(v); + if !by_wavelength { + let n = bands.to_string(); + crate::i18n::fmt("Bands 1–{n}", &[("n", &n)]) + } else if x1 > x0 { + crate::i18n::fmt("Wavelength {from}–{to} nm", &[("from", &fmt(x0)), ("to", &fmt(x1))]) + } else { + crate::i18n::fmt("Wavelength {nm} nm", &[("nm", &fmt(x0))]) + } +} + /// The chart: a line for each point through its bands, in the order of the x axis; hovering /// shows a band's name, wavelength and the points' values. fn chart(sp: &Spectral, ui: &mut Ui, t: &Tokens) { @@ -387,14 +413,16 @@ fn chart(sp: &Spectral, ui: &mut Ui, t: &Tokens) { ui.label(egui::RichText::new(tl!("No data at the points picked.")).weak().small()); return; } - let (x1, hi) = (if x1 > x0 { x1 } else { x0 + 1.0 }, if hi > lo { hi } else { lo + 1.0 }); + let labels = value_labels(lo, hi); + let axis = axis_label(x0, x1, by_wavelength, sp.bands.len()); + let ((x0, x1), (lo, hi)) = (scale(x0, x1), scale(lo, hi)); let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 190.0), Sense::hover()); let painter = ui.painter_at(rect); painter.rect_filled(rect, 4.0, t.canvas); let fmt = |v: f64| rustgis_engine::map::symbology::fmt_num(v); // A gutter on the left for the values at the top and bottom, clear of the lines, and room for // the axis' range under it. - let gutter = [hi, lo].iter().map(|v| crate::widgets::text_w(ui, &fmt(*v), regular(10.0))).fold(0.0_f32, f32::max) + 12.0; + let gutter = labels.iter().map(|v| crate::widgets::text_w(ui, &fmt(*v), regular(10.0))).fold(0.0_f32, f32::max) + 12.0; let plot = egui::Rect::from_min_max(rect.min + vec2(gutter, 12.0), rect.max - vec2(12.0, 30.0)); let to = |x: f64, v: f64| { pos2(plot.min.x + ((x - x0) / (x1 - x0)) as f32 * plot.width(), plot.max.y - ((v - lo) / (hi - lo)) as f32 * plot.height()) @@ -425,14 +453,9 @@ fn chart(sp: &Spectral, ui: &mut Ui, t: &Tokens) { } } } - painter.text(pos2(plot.min.x - 6.0, plot.min.y), egui::Align2::RIGHT_CENTER, fmt(hi), regular(10.0), t.text_dim); - painter.text(pos2(plot.min.x - 6.0, plot.max.y), egui::Align2::RIGHT_CENTER, fmt(lo), regular(10.0), t.text_dim); - let axis = if by_wavelength { - crate::i18n::fmt("Wavelength {from}–{to} nm", &[("from", &fmt(x0)), ("to", &fmt(x1))]) - } else { - let n = sp.bands.len().to_string(); - crate::i18n::fmt("Bands 1–{n}", &[("n", &n)]) - }; + for v in &labels { + painter.text(pos2(plot.min.x - 6.0, to(x0, *v).y), egui::Align2::RIGHT_CENTER, fmt(*v), regular(10.0), t.text_dim); + } painter.text(rect.center_bottom() + vec2(0.0, -4.0), egui::Align2::CENTER_BOTTOM, axis, regular(10.0), t.text_dim); // Hovering: the band nearest the pointer, marked, and every point's value there. let near = resp.hover_pos().and_then(|pos| { @@ -692,6 +715,29 @@ mod tests { assert_eq!((rasters.len(), shadowed), (1, 1), "{rasters:?}"); } + /// A flat profile's labels say what was read, not the room drawn around it. + #[test] + fn a_flat_profile_is_labelled_with_its_values() { + assert_eq!(value_labels(0.0, 0.0), [0.0]); + assert_eq!(value_labels(10.0, 35.0), [35.0, 10.0]); + assert_eq!(scale(0.0, 0.0), (-1.0, 1.0)); + assert_eq!(scale(10.0, 35.0), (10.0, 35.0)); + assert_eq!(axis_label(500.0, 500.0, true, 3), "Wavelength 500 nm"); + assert_eq!(axis_label(490.0, 665.0, true, 3), "Wavelength 490–665 nm"); + assert_eq!(axis_label(1.0, 1.0, false, 1), "Bands 1–1"); + } + + /// The raster the points were read on, written in another case, is the same raster: they stay. + #[test] + fn the_same_raster_in_another_case_keeps_its_points() { + let mut app = app_with_scene(false); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + add_point(&mut app, "SCENE", rustgis_engine::geom::Coord::new(10.015, 44.985)); + let sp = &app.workflow.spectral; + assert_eq!(sp.samples.len(), 2, "{:?}", sp.message); + assert_eq!(sp.message, None); + } + /// Another map shown: the points read on the one before are taken off, and that is said. #[test] fn another_map_takes_the_points_off_and_says_so() { From 8c1e4f03304bd7236a315f76e7f2a907429bd581 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 09:58:18 -0300 Subject: [PATCH 16/25] spectral profile: points read before an in-place edit are taken off Pixel Editor swaps a raster's pixels under the same layer id and name. The profile now keeps a Weak to the pixels its points were read from; when the layer holds other pixels, the points are taken off with the "has changed or gone" notice, in the section and when a point is added. Undo restores the same pixels, so it keeps them. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/spectralui.rs | 53 +++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 94f4586742..875edaa6c8 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -43,6 +43,9 @@ pub struct Spectral { /// The map (its place in the project) and layer the points were read on: a raster of the /// same name on another map, or added again, isn't the same one. pub source: Option<(usize, u32)>, + /// The pixels the points were read from: a raster edited in place (Pixel Editor, say) keeps + /// its layer and name but not these, so its points are no longer what it holds. + read_on: std::sync::Weak, /// Oldest first. pub samples: Vec, /// The last point's number. @@ -120,6 +123,12 @@ fn source_of(app: &RustGisApp, layer: &str) -> Option<(usize, u32)> { app.session.map().layer_by_name(layer).map(|l| (app.session.project.active_map, l.id)) } +/// The pixels of the raster `layer` names now, for [`Spectral::read_on`] (none when it has none). +fn data_of(app: &RustGisApp, layer: &str) -> std::sync::Weak { + let data = app.session.map().layer_by_name(layer).and_then(|l| l.raster.as_ref()?.data.as_ref()); + data.map(std::sync::Arc::downgrade).unwrap_or_default() +} + /// The raster the points were read on, renamed since: the profile follows its new name (when /// commands find it by that name), so its points stay. pub fn follow_rename(app: &mut RustGisApp) { @@ -137,10 +146,11 @@ pub fn follow_rename(app: &mut RustGisApp) { app.workflow.spectral.layer = name; } -/// Whether the points were read on the raster the profile's name finds now. +/// Whether the points were read on the raster the profile's name finds now, as it is now. (A +/// `Weak` keeps its allocation, so another raster can't take the address the points' one had.) fn fresh(app: &RustGisApp) -> bool { let sp = &app.workflow.spectral; - source_of(app, &sp.layer) == sp.source + source_of(app, &sp.layer) == sp.source && std::sync::Weak::ptr_eq(&data_of(app, &sp.layer), &sp.read_on) } /// The rasters a profile can be read on: those of two bands or more, by name, and how many @@ -194,7 +204,7 @@ pub fn from_identify(app: &mut RustGisApp, id: u32, at: rustgis_engine::geom::Co /// said in the section. pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Coord) { let r = app.run("raster.spectral", json!({"layer": layer, "lon": at.x, "lat": at.y})); - let source = source_of(app, layer); + let (source, data) = (source_of(app, layer), data_of(app, layer)); let sp = &mut app.workflow.spectral; let v = match r { Ok(v) => v, @@ -205,11 +215,12 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co }; let bands: Vec = v["bands"].as_array().into_iter().flatten().map(|b| b.as_str().unwrap_or_default().to_string()).collect(); let wavelengths: Vec> = v["wavelengths"].as_array().into_iter().flatten().map(Value::as_f64).collect(); - // Another raster, or the same one's bands changed: its points aren't comparable, and that - // they went is said. Names are compared as commands find layers, so the same raster written - // in another case keeps its points. + // Another raster, or the same one's pixels or bands changed: its points aren't comparable, + // and that they went is said. Names are compared as commands find layers, so the same raster + // written in another case keeps its points. let mut restarted = None; - if !sp.layer.eq_ignore_ascii_case(layer) || sp.source != source || sp.bands != bands || sp.wavelengths != wavelengths { + let changed = !std::sync::Weak::ptr_eq(&sp.read_on, &data) || sp.bands != bands || sp.wavelengths != wavelengths; + if !sp.layer.eq_ignore_ascii_case(layer) || sp.source != source || changed { let why = if !sp.layer.eq_ignore_ascii_case(layer) { Restart::Other(layer) } else if sp.source.map(|s| s.0) != source.map(|s| s.0) { @@ -220,6 +231,7 @@ pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Co sp.restart(why); restarted = sp.message.take(); sp.source = source; + sp.read_on = data; sp.bands = bands; sp.wavelengths = wavelengths; } @@ -738,6 +750,33 @@ mod tests { assert_eq!(sp.message, None); } + /// Pixels edited in place (same layer, same name): the points read before are taken off, and + /// that is said; a point read after is kept. + #[test] + fn an_edited_raster_takes_the_points_off_and_says_so() { + let mut app = app_with_scene(false); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.015, 44.985)); + let ring = json!([[10.0, 45.0], [10.04, 45.0], [10.04, 44.96]]); + app.run("raster.editPixels", json!({"layer": "scene", "polygon": ring, "value": 99})).unwrap(); + assert!(!fresh(&app), "the points were read on the pixels before the edit"); + let mut h = egui_kittest::Harness::builder().allow_missing_glyphs().with_size(egui::vec2(420.0, 320.0)).build_ui_state( + |ui, app: &mut RustGisApp| { + let t = crate::theme::Tokens::get(ui.ctx()); + section(app, ui, &t); + }, + app, + ); + h.run_steps(1); + let sp = &h.state().workflow.spectral; + assert!(sp.samples.is_empty()); + assert_eq!(sp.message, Some(("2 point(s) taken off: scene has changed or gone since they were read.".to_string(), false))); + // Read again on the edited pixels: the point is kept, and fresh. + add_point(h.state_mut(), "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + assert_eq!(h.state().workflow.spectral.samples.len(), 1); + assert!(fresh(h.state())); + } + /// Another map shown: the points read on the one before are taken off, and that is said. #[test] fn another_map_takes_the_points_off_and_says_so() { From e4204d52fc66e4a60a64a4e57c9efbc876329013 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 10:06:39 -0300 Subject: [PATCH 17/25] spectral profile: a point from Identify follows a rename too add_point runs follow_rename first, so a raster renamed while the section wasn't drawn keeps its points when Identify adds one, instead of being taken off as another raster. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/spectralui.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 875edaa6c8..759343e88b 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -203,6 +203,9 @@ pub fn from_identify(app: &mut RustGisApp, id: u32, at: rustgis_engine::geom::Co /// read so far starting it again; the oldest point goes past [`MAX_POINTS`]. What goes wrong is /// said in the section. pub fn add_point(app: &mut RustGisApp, layer: &str, at: rustgis_engine::geom::Coord) { + // A rename since the points were read (with the section not drawn, say, and the point from + // Identify) is the same raster: its points stay. + follow_rename(app); let r = app.run("raster.spectral", json!({"layer": layer, "lon": at.x, "lat": at.y})); let (source, data) = (source_of(app, layer), data_of(app, layer)); let sp = &mut app.workflow.spectral; @@ -750,6 +753,20 @@ mod tests { assert_eq!(sp.message, None); } + /// A point from Identify on a raster renamed while the section wasn't drawn: the same raster, + /// so the points read before stay. + #[test] + fn identify_on_a_renamed_raster_keeps_its_points() { + let mut app = app_with_scene(false); + add_point(&mut app, "scene", rustgis_engine::geom::Coord::new(10.005, 44.995)); + app.run("layer.rename", json!({"layer": "scene", "name": "renamed"})).unwrap(); + let id = app.session.map().layer_by_name("renamed").map(|l| l.id).unwrap(); + from_identify(&mut app, id, rustgis_engine::geom::Coord::new(10.015, 44.985)); + let sp = &app.workflow.spectral; + assert_eq!((sp.layer.as_str(), sp.samples.len()), ("renamed", 2), "{:?}", sp.message); + assert_eq!(sp.message, None); + } + /// Pixels edited in place (same layer, same name): the points read before are taken off, and /// that is said; a point read after is kept. #[test] From 9734f9536c1ee293142bb3f3f52c43739f5c3b6f Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 10:30:18 -0300 Subject: [PATCH 18/25] spectral profile: resample keeps band names, one unit in two spellings is one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit raster.process resample copies each band's name with its wavelength, as the other band-keeping commands do. Wavelength units are compared by the unit they name (unit_of), in the sidecar and the metadata tag alike, so "nm" and "nanometers" (or "um" and "µm") say nothing. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 55 +++++++++++++++++++++++++------- crates/engine/src/cmd/imagery.rs | 7 ++-- crates/engine/src/tests.rs | 6 ++-- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 3be5a90f91..6a4b0f151d 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -115,6 +115,30 @@ fn is_wavelength_item(name: &str) -> bool { /// A wavelength item's value in nanometres: `None` when it isn't a length from 1 nm to 1 m (no /// sensor's band lies outside), or its unit isn't a length this knows. /// +/// The length unit a wavelength unit item names, however it is spelt. +#[derive(Debug, PartialEq)] +enum Unit { + /// None: empty, or ENVI's `Unknown`. + Unstated, + Nanometres, + Micrometres, + /// Another, by its text (lowercase, spaces aside): not read. + Unread(String), +} + +/// The unit `text` names (nanometres or micrometres as ENVI and others spell them, any case). +fn unit_of(text: &str) -> Unit { + let u = text.trim().to_lowercase(); + match u.as_str() { + "" | "unknown" => Unit::Unstated, + "nm" | "nanometer" | "nanometers" | "nanometre" | "nanometres" => Unit::Nanometres, + "um" | "\u{b5}m" | "\u{3bc}m" | "micrometer" | "micrometers" | "micrometre" | "micrometres" | "micron" | "microns" => { + Unit::Micrometres + } + _ => Unit::Unread(u), + } +} + /// `CENTRAL_WAVELENGTH_UM` is in micrometres by its name; another item is in the unit `units` /// gives (nanometres or micrometres, as ENVI and others spell them). With no unit (or ENVI's /// `Unknown`), a value under 100 is in micrometres and any other in nanometres: imaging sensors @@ -130,12 +154,11 @@ pub fn wavelength_nm(item: &str, text: &str, units: Option<&str>) -> Option if item.eq_ignore_ascii_case(CENTRAL_WAVELENGTH_UM) { return in_range(nm(v)); } - let unit = units.map(|u| u.trim().to_lowercase()).unwrap_or_default(); - let micro = match unit.as_str() { - "" | "unknown" => v < 100.0, - "nm" | "nanometer" | "nanometers" | "nanometre" | "nanometres" => false, - "um" | "\u{b5}m" | "\u{3bc}m" | "micrometer" | "micrometers" | "micrometre" | "micrometres" | "micron" | "microns" => true, - _ => return None, + let micro = match unit_of(units.unwrap_or_default()) { + Unit::Unstated => v < 100.0, + Unit::Nanometres => false, + Unit::Micrometres => true, + Unit::Unread(_) => return None, }; in_range(if micro { nm(v) } else { v }) } @@ -161,12 +184,13 @@ fn keep_first_wavelength(slot: &mut Option, nm: f64) -> bool { } } -/// How many of `units` after the first that names one name another unit than it (case and -/// spaces aside): those are left out, the first being the one read. An empty item names none. +/// How many of `units` after the first that names one name another unit than it ([`unit_of`]: +/// `nm` and `nanometers` are one): those are left out, the first being the one read. An empty +/// item names none. fn other_units<'a>(units: impl Iterator) -> usize { let mut units = units.map(str::trim).filter(|u| !u.is_empty()); - let Some(first) = units.next() else { return 0 }; - units.filter(|u| !u.eq_ignore_ascii_case(first)).count() + let Some(first) = units.next().map(unit_of) else { return 0 }; + units.filter(|u| unit_of(u) != first).count() } /// A sidecar node's metadata items `key` (any domain, any case): their texts, empty for an item @@ -561,7 +585,7 @@ fn read_wavelengths(root: roxmltree::Node<'_, '_>, pam: &mut Pam) -> (usize, usi v.insert(t); } std::collections::hash_map::Entry::Occupied(o) => { - units_again += usize::from(!o.get().trim().eq_ignore_ascii_case(t.trim())); + units_again += usize::from(unit_of(o.get()) != unit_of(t)); } } } @@ -973,6 +997,11 @@ mod tests { let pam = parse_gdal_metadata(xml).unwrap(); assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(1.6)); assert_eq!(pam.lost, ["1 other wavelength unit item(s) for a band or the dataset left out (the first is kept)"]); + // One unit in two spellings is one unit: nothing is said. + let xml = r#"nmnanometersumµm1.6"#; + let pam = parse_gdal_metadata(xml).unwrap(); + assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(1.6)); + assert!(pam.lost.is_empty(), "{:?}", pam.lost); // A unit the dataset gives is a band's when it gives none; then a large value may be in µm. let xml = r#"micrometers120"#; @@ -1044,6 +1073,10 @@ mod tests { let twice = parse(r#"nmmicrometers1.6"#).unwrap(); assert_eq!(twice.band(1).and_then(|b| b.wavelength), Some(1.6)); assert_eq!(twice.lost, ["1 other wavelength unit(s) for a band or the dataset left out (the first is kept)"]); + // One unit in two spellings is one unit: nothing is said. + let spelt = parse(r#"nmNanometres1.6"#).unwrap(); + assert_eq!(spelt.band(1).and_then(|b| b.wavelength), Some(1.6)); + assert!(spelt.lost.is_empty(), "{:?}", spelt.lost); // An empty unit names none, in the sidecar and the metadata tag alike: the next is read. let sidecar = parse(r#"nm1.6"#).unwrap(); let tag = parse_gdal_metadata(r#" nm1.6"#).unwrap(); diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index fd853d6a6b..63ad89ef49 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -1241,8 +1241,11 @@ pub(crate) fn process_work(s: &Session, p: &Value) -> Result Date: Mon, 9 Nov 2026 10:36:46 -0300 Subject: [PATCH 19/25] pam: wavelength_nm's doc comment whole again The first paragraph of wavelength_nm's doc had stayed above the new Unit enum, where rustdoc gave it to Unit. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/pam.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 6a4b0f151d..72e71851b5 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -112,9 +112,6 @@ fn is_wavelength_item(name: &str) -> bool { name.eq_ignore_ascii_case(WAVELENGTH_UNITS) || WAVELENGTH_ITEMS.iter().any(|w| name.eq_ignore_ascii_case(w)) } -/// A wavelength item's value in nanometres: `None` when it isn't a length from 1 nm to 1 m (no -/// sensor's band lies outside), or its unit isn't a length this knows. -/// /// The length unit a wavelength unit item names, however it is spelt. #[derive(Debug, PartialEq)] enum Unit { @@ -139,6 +136,9 @@ fn unit_of(text: &str) -> Unit { } } +/// A wavelength item's value in nanometres: `None` when it isn't a length from 1 nm to 1 m (no +/// sensor's band lies outside), or its unit isn't a length this knows. +/// /// `CENTRAL_WAVELENGTH_UM` is in micrometres by its name; another item is in the unit `units` /// gives (nanometres or micrometres, as ENVI and others spell them). With no unit (or ENVI's /// `Unknown`), a value under 100 is in micrometres and any other in nanometres: imaging sensors From b31cc86b264d632ff08b507d72b4109826204abd Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 10:52:27 -0300 Subject: [PATCH 20/25] raster: an unreadable sidecar or GDAL tag says its wavelengths go too The notices for a sidecar or metadata tag that can't be read (pam.rs, geotiff.rs, both COG readers' tests) named only band and class names, or statistics, no-data and band names; wavelengths come from the same metadata and go with them, so they are named too. A broken tag with a wavelength in it is tested. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/cog.rs | 6 +++--- crates/data/src/raster/geotiff.rs | 4 ++-- crates/data/src/raster/pam.rs | 21 +++++++++++++++------ crates/tiles/src/cog.rs | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/crates/data/src/raster/cog.rs b/crates/data/src/raster/cog.rs index 4fc782937a..d2ac16380f 100644 --- a/crates/data/src/raster/cog.rs +++ b/crates/data/src/raster/cog.rs @@ -418,12 +418,12 @@ fn read_layout(r: Sparse) -> Result { Ok(Some(v)) => match v.into_string() { Ok(t) => super::pam::parse_gdal_metadata(&t), Err(e) => { - lost.push(format!("its GDAL metadata isn't text ({e}): band and class names are left out")); + lost.push(format!("its GDAL metadata isn't text ({e}): band and class names and wavelengths are left out")); None } }, Err(e) => { - lost.push(format!("its GDAL metadata doesn't decode ({e}): band and class names are left out")); + lost.push(format!("its GDAL metadata doesn't decode ({e}): band and class names and wavelengths are left out")); None } }; @@ -1073,7 +1073,7 @@ pub(crate) mod tests { bad[entry + 8..entry + 12].copy_from_slice(&(len + 1000).to_le_bytes()); let Parse::Done(l) = layout(&Sparse::default().with_len(u64::from(len)).with(0, bad)).unwrap() else { panic!("not read") }; let said: Vec<&String> = l.lost.iter().chain(l.described.iter().flat_map(|d| d.lost.iter())).collect(); - assert!(said.iter().any(|x| x.contains("band and class names are left out")), "{said:?}"); + assert!(said.iter().any(|x| x.contains("band and class names and wavelengths are left out")), "{said:?}"); } #[test] diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 82734c7a80..8909db0e26 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -1240,9 +1240,9 @@ pub(super) fn read_limited_with_nodata( Ok(None) => None, Ok(Some(v)) => match v.into_string() { Ok(t) => super::pam::parse_gdal_metadata(&t), - Err(e) => Some(super::pam::Pam::losing(format!("it isn't text ({e}): band and class names are left out"))), + Err(e) => Some(super::pam::Pam::losing(format!("it isn't text ({e}): band and class names and wavelengths are left out"))), }, - Err(e) => Some(super::pam::Pam::losing(format!("it doesn't decode ({e}): band and class names are left out"))), + Err(e) => Some(super::pam::Pam::losing(format!("it doesn't decode ({e}): band and class names and wavelengths are left out"))), }; // Who holds the rights to the picture, kept as it reads so an export of it says so too (the // writers make it the ASCII a tag holds, and the exports say what that changed); one that diff --git a/crates/data/src/raster/pam.rs b/crates/data/src/raster/pam.rs index 72e71851b5..c9d3ef4f8a 100644 --- a/crates/data/src/raster/pam.rs +++ b/crates/data/src/raster/pam.rs @@ -252,13 +252,16 @@ pub fn read(path: &Path) -> Option { [(_, e)] => e.clone(), all => all.iter().map(|(name, e)| format!("{name}: {e}")).collect::>().join("; "), }; - Some(Pam::losing(format!("{reason}: its statistics, no-data and band names are left out"))) + Some(Pam::losing(format!("{reason}: its statistics, no-data, band names and wavelengths are left out"))) } /// Parses a sidecar's text. One that doesn't read as XML, or isn't a PAM document, names nothing: /// it comes back with only [`Pam::lost`] saying so. Its size is checked by [`read`] only. pub fn parse(text: &str) -> Option { - Some(parse_sidecar(text).unwrap_or_else(|e| Pam::losing(format!("{e}: its statistics, no-data and band names are left out")))) + Some( + parse_sidecar(text) + .unwrap_or_else(|e| Pam::losing(format!("{e}: its statistics, no-data, band names and wavelengths are left out"))), + ) } /// [`parse`], with why a document isn't read as the error. @@ -426,12 +429,14 @@ pub fn parse_gdal_metadata(text: &str) -> Option { let opts = ParsingOptions { allow_dtd: false, nodes_limit: crate::io::XML_NODES, ..ParsingOptions::default() }; let doc = match crate::xml::parse(text, opts) { Ok(doc) => doc, - Err(e) => return Some(Pam::losing(format!("it doesn't read as XML ({e}): band and class names are left out"))), + Err(e) => return Some(Pam::losing(format!("it doesn't read as XML ({e}): band and class names and wavelengths are left out"))), }; let root = doc.root_element(); if root.tag_name().name() != "GDALMetadata" { let name = root.tag_name().name().chars().take(40).collect::(); - return Some(Pam::losing(format!("it is a <{name}> document, not GDAL's : band and class names are left out"))); + return Some(Pam::losing(format!( + "it is a <{name}> document, not GDAL's : band and class names and wavelengths are left out" + ))); } let mut pam = Pam::default(); // What is left out or cut, counted. @@ -849,7 +854,7 @@ mod tests { ] { let pam = parse(text).unwrap(); assert!(pam.bands.is_empty() && pam.lost.len() == 1 && pam.lost[0].contains(said), "{text}: {pam:?}"); - assert!(pam.lost[0].ends_with("its statistics, no-data and band names are left out"), "{text}: {pam:?}"); + assert!(pam.lost[0].ends_with("its statistics, no-data, band names and wavelengths are left out"), "{text}: {pam:?}"); } let deep = format!("{}{}", "".repeat(100_000), "".repeat(100_000)); let deep = parse(&deep).unwrap(); @@ -890,7 +895,7 @@ mod tests { && both.lost.len() == 1 && both.lost[0].starts_with("x.tif.aux.xml: it isn't UTF-8 text") && both.lost[0].contains("; x.aux.xml: ") - && both.lost[0].ends_with(": its statistics, no-data and band names are left out"), + && both.lost[0].ends_with(": its statistics, no-data, band names and wavelengths are left out"), "{both:?}" ); // A sidecar that parses first says nothing of the other. @@ -1002,6 +1007,10 @@ mod tests { let pam = parse_gdal_metadata(xml).unwrap(); assert_eq!(pam.band(1).and_then(|b| b.wavelength), Some(1.6)); assert!(pam.lost.is_empty(), "{:?}", pam.lost); + // A tag that doesn't read loses its wavelengths too, and says so. + let broken = parse_gdal_metadata(r#"665"#).unwrap(); + assert_eq!(broken.band(1).and_then(|b| b.wavelength), None); + assert!(broken.lost.iter().any(|l| l.ends_with("band and class names and wavelengths are left out")), "{:?}", broken.lost); // A unit the dataset gives is a band's when it gives none; then a large value may be in µm. let xml = r#"micrometers120"#; diff --git a/crates/tiles/src/cog.rs b/crates/tiles/src/cog.rs index 02ca65d8da..48e97027fb 100644 --- a/crates/tiles/src/cog.rs +++ b/crates/tiles/src/cog.rs @@ -1831,7 +1831,7 @@ mod tests { bad[entry + 8..entry + 12].copy_from_slice(&(len + 1000).to_le_bytes()); let (l, ..) = layout_by_ranges(&bad).expect("read"); let said: Vec<&String> = l.lost.iter().chain(l.described.iter().flat_map(|d| d.lost.iter())).collect(); - assert!(said.iter().any(|x| x.contains("band and class names are left out")), "{said:?}"); + assert!(said.iter().any(|x| x.contains("band and class names and wavelengths are left out")), "{said:?}"); } /// The header alone of a tiled GeoTIFF, `side` pixels a side in 256-pixel tiles, laid out as From eb0d83a221fbd69a0110e567eee5f40ac284145e Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 11:10:17 -0300 Subject: [PATCH 21/25] spectral profile: Identify's button keeps Pick Points on from_identify opens the section only when it isn't shown: ui.pane.workflows ends any map pick, so running it with the section already open turned Pick Points off. Switching section still ends it. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/spectralui.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 759343e88b..57324597db 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -191,7 +191,9 @@ pub fn from_identify(app: &mut RustGisApp, id: u32, at: rustgis_engine::geom::Co app.session.notify(text, true); return; } - if let Err(e) = app.run("ui.pane.workflows", json!({"section": SECTION})) { + // Opening the section ends a map pick; with it already shown, Pick Points stays as it is. + let shown = app.ui.right == Some(crate::RightPane::Workflows) && app.workflow.section == SECTION; + if !shown && let Err(e) = app.run("ui.pane.workflows", json!({"section": SECTION})) { app.session.notify(e, true); return; } @@ -753,6 +755,18 @@ mod tests { assert_eq!(sp.message, None); } + /// Identify's button with the section already shown and Pick Points on: picking stays on. + #[test] + fn identify_keeps_pick_points_on() { + let mut app = app_with_scene(false); + app.run("ui.pane.workflows", json!({"section": SECTION})).unwrap(); + app.workflow.pick = Some(crate::analysispane::Pick { purpose: PURPOSE, needed: 1, points: Vec::new() }); + let id = app.session.map().layer_by_name("scene").map(|l| l.id).unwrap(); + from_identify(&mut app, id, rustgis_engine::geom::Coord::new(10.005, 44.995)); + assert_eq!(app.workflow.spectral.samples.len(), 1); + assert!(app.workflow.pick.as_ref().is_some_and(|p| p.purpose == PURPOSE), "Pick Points stays on"); + } + /// A point from Identify on a raster renamed while the section wasn't drawn: the same raster, /// so the points read before stay. #[test] From 0396600167d7cc999587ed01312671abc47dbbd2 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 11:12:37 -0300 Subject: [PATCH 22/25] docs: rewrap the wavelength paragraph's long line Co-Authored-By: Claude Opus 5.5 --- docs/user-guide/data.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 64c0cc8023..9676988993 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1143,8 +1143,8 @@ domain), or a `wavelength` or `central_wavelength` item (as rasterio, STAC expor through GDAL write them) in the unit a `wavelength_units` item gives. With no unit, a value under 100 is read as micrometres and any other as nanometres: imaging sensors see from about 0.3 to 15 µm, so the two never meet. One that isn't a length from 1 nm to 1 m, or another length for a band that has -one, is said and left out. A GeoTIFF export writes the wavelengths of the bands it writes (one band, or a raster's -RGB) back as `CENTRAL_WAVELENGTH_UM`. Projects remember a raster's file +one, is said and left out. A GeoTIFF export writes the wavelengths of the bands it writes (one +band, or a raster's RGB) back as `CENTRAL_WAVELENGTH_UM`. Projects remember a raster's file path relative to the project and reopen it; rasters added in the browser are stored inside the project. ### Zarr stores From 404536854bb6b87d64a679b4c73e7e0ad7c5ac80 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 18:44:53 -0300 Subject: [PATCH 23/25] fix(ui): the spectral section says how many rasters couldn't be read A raster whose pixels failed to load was left out of the section's list without a word, so a failed six-band scene read as "Add a raster of two or more bands first". Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/spectralui.rs | 62 ++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/spectralui.rs b/crates/ui-egui/src/spectralui.rs index 57324597db..4fed09ae20 100644 --- a/crates/ui-egui/src/spectralui.rs +++ b/crates/ui-egui/src/spectralui.rs @@ -153,20 +153,36 @@ fn fresh(app: &RustGisApp) -> bool { source_of(app, &sp.layer) == sp.source && std::sync::Weak::ptr_eq(&data_of(app, &sp.layer), &sp.read_on) } -/// The rasters a profile can be read on: those of two bands or more, by name, and how many -/// more are left out because an earlier layer has their name (commands find a layer by its -/// name, so they would read that one). -pub fn multiband(app: &RustGisApp) -> (Vec, usize) { +/// The rasters a profile can be read on, and the ones left out of them. +pub struct Rasters { + /// Those of two bands or more, by name. + pub names: Vec, + /// Left out because an earlier layer has their name (commands find a layer by its name, so + /// they would read that one). + pub shadowed: usize, + /// Left out because their pixels couldn't be read (their bands aren't known). + pub unread: usize, +} + +/// The rasters of the active map a profile can be read on (see [`Rasters`]). +pub fn multiband(app: &RustGisApp) -> Rasters { let map = app.session.map(); - let (mut names, mut shadowed) = (Vec::new(), 0usize); - for l in map.layers.iter().filter(|l| l.raster.as_ref().and_then(|r| r.data.as_ref()).is_some_and(|r| r.band_count() >= 2)) { - if map.layer_by_name(&l.name).map(|f| f.id) == Some(l.id) { - names.push(l.name.clone()); - } else { - shadowed += 1; + let mut out = Rasters { names: Vec::new(), shadowed: 0, unread: 0 }; + for l in &map.layers { + let Some(r) = l.raster.as_ref() else { continue }; + match r.data.as_ref() { + Some(data) if data.band_count() >= 2 => { + if map.layer_by_name(&l.name).map(|f| f.id) == Some(l.id) { + out.names.push(l.name.clone()); + } else { + out.shadowed += 1; + } + } + Some(_) => {} + None => out.unread += usize::from(r.error.is_some()), } } - (names, shadowed) + out } /// Whether Identify's result `info` is a pixel of a raster of two bands or more: its layer's id. @@ -271,12 +287,17 @@ pub fn section(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let other_map = sp.source.is_some_and(|(map, _)| map != app.session.project.active_map); app.workflow.spectral.restart(if other_map { Restart::OtherMap } else { Restart::Gone }); } - let (rasters, shadowed) = multiband(app); + let Rasters { names: rasters, shadowed, unread } = multiband(app); if shadowed > 0 { let n = shadowed.to_string(); let text = crate::i18n::fmt("{n} raster(s) left out: another layer has the same name. Rename them to chart them.", &[("n", &n)]); ui.label(egui::RichText::new(text).small().color(t.red)); } + if unread > 0 { + let n = unread.to_string(); + let text = crate::i18n::fmt("{n} raster(s) couldn't be read, so they are left out.", &[("n", &n)]); + ui.label(egui::RichText::new(text).small().color(t.red)); + } if rasters.is_empty() { ui.label( egui::RichText::new(tl!("Add a raster of two or more bands first, such as a Landsat or Sentinel-2 scene.")).weak().small(), @@ -728,8 +749,21 @@ mod tests { let said: Vec = app.session.notices_since(mark).iter().map(|n| n.text.clone()).collect(); assert_eq!(said, [format!("{name}: another layer has this name, so its profile would be read from that one: rename one of them")]); // The section's list leaves it out too, and counts it. - let (rasters, shadowed) = multiband(&app); - assert_eq!((rasters.len(), shadowed), (1, 1), "{rasters:?}"); + let rasters = multiband(&app); + assert_eq!((rasters.names.len(), rasters.shadowed, rasters.unread), (1, 1, 0), "{:?}", rasters.names); + } + + /// A raster whose pixels couldn't be read is left out of the list, and counted. + #[test] + fn an_unread_raster_is_counted() { + let mut app = app_with_scene(false); + app.run("layer.addDataBytes", json!({"fileName": "broken.tif", "data": rgb_tiff_b64(false)})).unwrap(); + let id = app.session.map().layer_by_name("broken").map(|l| l.id).expect("the second scene"); + let r = app.session.map_mut().layer_mut(id).and_then(|l| l.raster.as_mut()).expect("a raster"); + r.data = None; + r.fail("the file is gone"); + let rasters = multiband(&app); + assert_eq!((rasters.names, rasters.shadowed, rasters.unread), (vec!["scene".to_string()], 0, 1)); } /// A flat profile's labels say what was read, not the room drawn around it. From ea396676accae1ba1bbc85e10f141d881df596ed Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 18:53:41 -0300 Subject: [PATCH 24/25] docs: one profile per point, a row per band Co-Authored-By: Claude Opus 5.5 --- docs/user-guide/analysis.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 30091cb9f8..c06aa04430 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1456,9 +1456,9 @@ The Analyze tab's imagery groups open their tools in the Workflows pane: nanometres when the file gives one for every band (see [Rasters](data.md#rasters)), drawn in wavelength order, and their numbers otherwise. Hovering the chart shows a band's name, wavelength and each point's value; **×** takes a point off, **Clear** all of them, and **Export CSV…** - (desktop) writes a row a band (its number, name and wavelength) and a column a point. From - scripts, `{"layer": "scene", "points": [[-43.95, -19.92], [-43.96, -19.93]]}` gives a profile a - point (up to 1,000; a point off the raster keeps its place with null values, and is said), + (desktop) writes a row per band (its number, name and wavelength) and a column per point. From + scripts, `{"layer": "scene", "points": [[-43.95, -19.92], [-43.96, -19.93]]}` gives one profile + per point (up to 1,000; a point off the raster keeps its place with null values, and is said), `{"lon", "lat"}` one pixel and `{"zone": "polygons"}` the mean in the selected or first polygon; every answer has the band names and, when the file gives any, `"wavelengths"` in nanometres. After GeoLibre's spectral profile. From 29677cec402a3ccf2eee1375144276cf110dd9a6 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 19:14:34 -0300 Subject: [PATCH 25/25] fix(engine): a tile-layer COG says its wavelengths need a window A cloud-optimised GeoTIFF drawn in place said only band and class names were left for a window; a header with only wavelengths, which this PR now reads, lost them without a word. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/cog.rs | 7 +++++++ crates/engine/src/cmd/layer.rs | 11 +++++++---- crates/engine/src/tests.rs | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/data/src/raster/cog.rs b/crates/data/src/raster/cog.rs index d2ac16380f..68f52b19cb 100644 --- a/crates/data/src/raster/cog.rs +++ b/crates/data/src/raster/cog.rs @@ -667,6 +667,9 @@ pub struct Example { pub sparse: bool, /// Without overviews: the full image alone (a file saved without them). pub no_overviews: bool, + /// Without `classes`: band `b`'s wavelength, 400 + 100 b nanometres, in GDAL's metadata + /// (its only item there). + pub wavelengths: bool, } impl Default for Example { @@ -688,6 +691,7 @@ impl Default for Example { no_table: false, sparse: false, no_overviews: false, + wavelengths: false, } } } @@ -826,6 +830,9 @@ pub fn example_with(e: &Example) -> Vec { r#"Forest FormationPasture"# }; t.push((42112, V::A([xml.as_bytes(), b"\0"].concat()))); + } else if e.wavelengths { + let items: String = (0..nb).map(|b| format!(r#"{}"#, 400 + 100 * b)).collect(); + t.push((42112, V::A([format!("{items}").as_bytes(), b"\0"].concat()))); } } if e.int8 { diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 165bd2da47..4cdceb185d 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -3814,14 +3814,17 @@ pub(crate) fn add_cog_with(s: &mut Session, p: &Value, url: &str, strict: bool, } r["bounds"] = json!(info.bounds); // Drawn in place, the tiles are drawn as values (a palette image's by its colour table): - // what the header holds beside them (band and class names) comes only with a window, and - // is said. + // what the header holds beside them (band and class names, wavelengths) comes only with a + // window, and is said. let l = &info.layout; - let named = l.described.as_ref().is_some_and(|d| d.bands.iter().any(|b| b.description.is_some() || !b.categories.is_empty())); + let named = l + .described + .as_ref() + .is_some_and(|d| d.bands.iter().any(|b| b.description.is_some() || !b.categories.is_empty() || b.wavelength.is_some())); let mut said: Vec = l.lost.clone(); said.extend(l.described.iter().flat_map(|d| d.lost.iter().map(|x| format!("{}: {x}", rustgis_data::raster::pam::GDAL_METADATA)))); if named { - said.push("drawn in place without its band or class names: a window (\"bbox\") reads them".into()); + said.push("drawn in place without its band names, class names or wavelengths: a window (\"bbox\") reads them".into()); } // A tile layer's own name, as added. let shown = r.get("name").and_then(Value::as_str).unwrap_or(name).to_string(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c2b8676324..ea3dc2e256 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -20070,6 +20070,24 @@ fn a_data_cubes_file_stays_a_tile_layer_when_its_project_reopens() { assert_eq!((t.map().tiles.len(), t.map().layers.iter().filter(|l| l.raster.is_some()).count()), (1, 0)); } +/// A tile layer's file whose header gives only its bands' wavelengths says they aren't drawn +/// with it, as it says of band and class names. +#[test] +fn a_tile_layer_says_its_wavelengths_need_a_window() { + use rustgis_data::raster::cog::{Example, example_with}; + let file = example_with(&Example { size: 512, bands: 3, wavelengths: true, ..Example::default() }); + let (base, _) = serve_ranges(file); + let mut s = Session::new(Project::default()); + let mark = s.notice_mark(); + s.run("layer.addUrl", &json!({"url": format!("{base}/cube-2020.tif"), "cube": true})).unwrap(); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + assert!( + said.iter() + .any(|t| t.ends_with("drawn in place without its band names, class names or wavelengths: a window (\"bbox\") reads them")), + "{said:?}" + ); +} + /// Dated-series keys given as `null` (a script's unused ones) ask for nothing: the GeoTIFF is a /// raster layer read in place, not a tile layer. #[test]