diff --git a/crates/analysis/src/warp.rs b/crates/analysis/src/warp.rs index 6d54fe150a..31e0ce122f 100644 --- a/crates/analysis/src/warp.rs +++ b/crates/analysis/src/warp.rs @@ -652,6 +652,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); if matches!(method, Method::Sample(Resampling::Nearest) | Method::Mode) && let Some(source) = r.bands.get(i) { diff --git a/crates/data/src/raster/cog.rs b/crates/data/src/raster/cog.rs index 4fc782937a..68f52b19cb 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 } }; @@ -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 { @@ -1073,7 +1080,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 20946f1829..7807716515 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/mod.rs b/crates/data/src/raster/mod.rs index d640bedf1f..73a6bde470 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -502,6 +502,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 { @@ -609,11 +612,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 @@ -740,6 +745,7 @@ impl Raster { stats: if measure { BandStats::compute(&data, valid) } else { BandStats::default() }, data, classes: Vec::new(), + wavelength: None, }) .collect(); let mut r = Raster { @@ -776,14 +782,18 @@ 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() - .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())); + 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(); @@ -836,6 +846,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); @@ -1621,7 +1634,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 064089fe6f..c9d3ef4f8a 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,124 @@ 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)) +} + +/// 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), + } +} + +/// 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. +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: 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 in_range(nm(v)); + } + 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 }) +} + +/// 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 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) => !same_wavelength(Some(w), Some(nm)), + } +} + +/// 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().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 +/// 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))) + .map(|n| n.text().unwrap_or_default()) +} + +/// A sidecar node's metadata item `key` (any domain, any case): the text of the first that has +/// one (an empty item says nothing). +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()) + .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`. pub fn sidecar_paths(path: &Path) -> Vec { let mut long = path.as_os_str().to_owned(); @@ -132,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. @@ -155,8 +278,14 @@ 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, 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. + 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::() @@ -183,8 +312,21 @@ 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 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 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, wavelength_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); @@ -222,7 +364,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 +387,17 @@ 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 length from 1 nm to 1 m 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)")); + } + 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")); } @@ -276,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. @@ -312,6 +467,7 @@ pub fn parse_gdal_metadata(text: &str) -> Option { twice += 1; } } + 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 @@ -345,6 +501,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 +534,17 @@ 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 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 { + 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)")); } @@ -391,6 +560,67 @@ 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 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, 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, 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) }; + let Some(at) = at else { + odd += 1; + continue; + }; + // 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); + } + std::collections::hash_map::Entry::Occupied(o) => { + units_again += usize::from(unit_of(o.get()) != unit_of(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(); + 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)) { + 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) => 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() }); + } + } + } + } + (odd, other, units_again) +} + /// 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 +633,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 +662,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 +678,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")); } @@ -615,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(); @@ -656,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. @@ -722,6 +961,172 @@ 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 + nm + 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(), 2, "{:?}", 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"#; + 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)"]); + // 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 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"#; + 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); + } + + /// 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}"); + } + // 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_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 + /// 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); + // 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); + // 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)"]); + // 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(); + for pam in [sidecar, tag] { + 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. + 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)"), + "{:?}", + 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 3a64026896..555e6d3b8e 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) } @@ -874,11 +875,91 @@ 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 { let r = raster(s, p)?; + // 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); + let point = given("lon") || given("lat"); + 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"), + (false, false) => None, + }; + let v = spectral_read(s, p, &r, zone)?; + if let Some(what) = left_out { + 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 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(); - if let Some(z) = spectral_zone(p) { + // 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.is_none()) { + let profiles = spectral_points(s, r, points)?; + return answer(json!({"bands": names, "profiles": profiles})); + } + if let Some(z) = zone { let l = s .map() .layer_by_name(z) @@ -889,13 +970,13 @@ 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")?; - return Ok(json!({"bands": names, "values": means, "pixels": n})); + 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, 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 { @@ -1160,6 +1241,11 @@ pub(crate) fn process_work(s: &Session, p: &Value) -> Result 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(); + o.wavelength = i.wavelength; }); out.copyright = first.copyright.clone(); // The first input's metadata accompanies its pixels; report other credits rather than @@ -1330,11 +1417,19 @@ 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 || !raster::pam::same_wavelength(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 { @@ -1423,7 +1518,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) @@ -1515,7 +1613,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)?; @@ -1717,12 +1818,30 @@ 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| raster::pam::same_wavelength(w, first)) { + first + } else { + differ += 1; + None + } + }) + .collect(); + if differ > 0 { + 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 || { let refs: Vec<&Raster> = imgs.iter().map(|r| &**r).collect(); let mut bands = rasterops::mosaic(&refs, &e, cell, (cols, rows), nb, w.method == "mean")?; @@ -1743,9 +1862,10 @@ pub(crate) fn mosaic_work(s: &Session, p: &Value) -> Result = 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 c1d9ec20e5..ea3dc2e256 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -9044,6 +9044,37 @@ fn layer_effects_and_imagery_tools() { let r = s.run("imagery.mosaic", &json!({"name": "Valley"})).unwrap(); let mo = s.map().layer(r["layer"].as_u64().unwrap() as u32).unwrap().raster.as_ref().unwrap().data.clone().unwrap(); assert_eq!((mo.width, mo.height), (20, 20)); + // Images of one wavelength give the mosaic it, read back from its GeoTIFF too; images of + // two give it none, and that is said. + let give = |s: &mut Session, layer: &str, nm: Option| { + 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)); + // 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) 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); + 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(); @@ -20039,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] @@ -27607,12 +27656,34 @@ 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); + // 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. let file = dir.join("west.tif"); @@ -36747,6 +36818,101 @@ fn bundled_ca_roots_are_said_once_a_session() { 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), name) in r.bands.iter_mut().zip([665.0, 560.0, 490.0]).zip(["Red", "Green", "Blue"]) { + band.wavelength = Some(nm); + band.name = name.to_string(); + } + } + 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])); + // A resampled raster keeps its bands' names and wavelengths. + s.run("raster.process", &json!({"layer": "scene", "operation": "resample", "factor": 2, "name": "half"})).unwrap(); + let half = s.run("raster.spectral", &json!({"layer": "half", "lon": 10.005, "lat": 44.995})).unwrap(); + assert_eq!(half["wavelengths"], json!([665.0, 560.0, 490.0])); + assert_eq!((&one["bands"], &half["bands"]), (&json!(["Red", "Green", "Blue"]), &json!(["Red", "Green", "Blue"])), "{half}"); + 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"]); + // 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}"); + 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")]); + } + // 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"), + (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}"); + } +} + #[test] fn area_display_expressions_survive_refused_geometry_edits() { use rustgis_data::{Coord, Dataset, Geometry, GeometryType}; 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 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 da0c07788c..c12aa0a6c0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -96,6 +96,7 @@ mod shortcutfile; mod shortcutui; mod showme; mod sourcespane; +mod spectralui; mod sqlbox; mod sqlpane; mod stacui; @@ -2201,6 +2202,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( @@ -2318,6 +2321,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 332f34103c..14f4db2693 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -3093,6 +3093,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(); @@ -3564,6 +3566,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; @@ -3581,7 +3584,10 @@ fn popup(app: &mut RustGisApp, ui: &mut Ui, rect: Rect) { }; let mut step: Option = None; let mut zoom = false; - // The feature shown, as its layer id and feature id (a raster pixel has neither). + // 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 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); @@ -3660,6 +3666,16 @@ 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); + 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; + } + } // 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()); @@ -3839,6 +3855,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 de80b16908..4418538b8d 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 dfd6034999..2688fbc119 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -4089,7 +4089,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..4fed09ae20 --- /dev/null +++ b/crates/ui-egui/src/spectralui.rs @@ -0,0 +1,880 @@ +//! 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): 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}; + +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, + /// 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), + 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>, + /// 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. + last: u32, + /// What the last action said (an error when true). + 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, 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 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)], + ), + 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)]) + } + }; + (text, false) + }); + } + + /// 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 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), + 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(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 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) { + 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, 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 && std::sync::Weak::ptr_eq(&data_of(app, &sp.layer), &sp.read_on) +} + +/// 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 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()), + } + } + out +} + +/// 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 }; + // 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; + } + // 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; + } + 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) { + // 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; + 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 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; + 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) { + Restart::OtherMap + } else { + Restart::Gone + }; + sp.restart(why); + restarted = sp.message.take(); + sp.source = source; + sp.read_on = data; + 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; + // 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 }); + // 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. +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 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) { + 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 { 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(), + ); + // 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; + } + 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.samples.is_empty() && app.workflow.spectral.layer != app.workflow.layer { + let now = app.workflow.layer.clone(); + 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 { + 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) => (crate::i18n::fmt("{path}: {error}", &[("path", &path), ("error", &e.to_string())]), 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.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( + "● {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(tl!("×")).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); + // What the last point added said is no longer about these points. + app.workflow.spectral.message = None; + } +} + +/// 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) { + 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 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 = 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()) + }; + // 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.slot); + // 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))); + } + } + } + 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| { + (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)); + // 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| { + 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))); + } + } + }); + } +} + +/// 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) { + // 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 { + let Some(at) = to_screen(rustgis_engine::geom::Coord::new(s.lon, s.lat)) else { continue }; + 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); + } +} + +#[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"); + 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 + /// 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 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(); + 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()); + 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 + /// 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); + // 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")]); + // The section's list leaves it out too, and counts it. + 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. + #[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); + } + + /// 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] + 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] + 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() { + 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() { + 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()); + } +} diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 02428abc69..c06aa04430 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1447,10 +1447,25 @@ 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 - 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). +- **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 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. +- **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 b99b7f1860..667293e7f1 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -1159,7 +1159,16 @@ 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 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. ### Zarr stores