From b6986fcdeb8e09939d73ac19fa77ece2953417e4 Mon Sep 17 00:00:00 2001 From: mavrick Date: Tue, 1 Dec 2026 08:42:25 -0600 Subject: [PATCH] fix(engine): Identify gives a pixel's values in full A raster's values in Identify were written as a legend's numbers are, so 10602 showed as "11K" and 0.004 as "0.00". They are now the shortest text that reads back the same value. Part of #2761. --- crates/engine/src/cmd/raster.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 41c9d08500..68f20339ce 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -1806,6 +1806,13 @@ fn geotiff_note(r: &Raster, bi: usize, cog: bool, band_hint: &str) -> Result String { + if v == 0.0 || (1e-5..1e16).contains(&v.abs()) { v.to_string() } else { format!("{v:e}") } +} + /// Pixel values of the topmost visible raster under a longitude/latitude point. pub fn identify(s: &Session, lon: f64, lat: f64) -> Option { pixels_at(s, lon, lat, |_| true, true).into_iter().next().map(|(_, v)| v) @@ -1832,7 +1839,7 @@ pub fn pixels_at(s: &Session, lon: f64, lat: f64, keep: impl Fn(&rustgis_map::La } let mut attrs = serde_json::Map::new(); for (b, v) in r.bands.iter().zip(&vals) { - attrs.insert(b.name.clone(), json!(v.map(|v| rustgis_map::symbology::fmt_num(v as f64)).unwrap_or_else(|| "NoData".into()))); + attrs.insert(b.name.clone(), json!(v.map(pixel_text).unwrap_or_else(|| "NoData".into()))); } attrs.insert("Pixel".into(), json!(format!("{col}, {row}"))); Some((i, json!({"layer": l.name, "layerId": l.id, "raster": true, "attributes": attrs}))) @@ -1859,6 +1866,29 @@ fn like_name(p: &Value) -> Result, String> { mod tests { use super::*; + /// Identify gives each band's value in full, whatever its size (#2761). + #[test] + fn identify_gives_pixel_values_in_full() { + let values = [10_602.0, 65_535.0, 1_234_567.0, 0.1, 273.153, -0.004, 0.0, 1e-30, -3.4e38]; + let r = rustgis_data::raster::Raster::new( + "dn", + 1, + 1, + values.iter().map(|v| vec![*v]).collect(), + rustgis_data::raster::GeoTransform { origin_x: 10.0, origin_y: 46.0, pixel_width: 1.0, pixel_height: -1.0 }, + rustgis_data::raster::Crs::LonLat, + None, + rustgis_data::raster::ColorHint::Gray, + "Float32", + ) + .unwrap(); + let mut s = Session::new(crate::Project::default()); + s.map_mut().add_raster("dn", RasterLayer::from_path(r, "dn.tif")); + let v = identify(&s, 10.5, 45.5).unwrap(); + let got: Vec<&str> = (1..=values.len()).map(|b| v["attributes"][format!("Band {b}")].as_str().unwrap()).collect(); + assert_eq!(got, ["10602", "65535", "1234567", "0.1", "273.153", "-0.004", "0", "1e-30", "-3.4e38"]); + } + /// The last padded quartet must count only the bytes it will decode into. #[test] fn add_data_bytes_accepts_padded_files_at_the_exact_limit() {