Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,13 @@ fn geotiff_note(r: &Raster, bi: usize, cog: bool, band_hint: &str) -> Result<Str
Ok(note)
}

/// A pixel's value in full, as the shortest text that reads back the same `f32`: not a legend's
/// rounded number, which gave 10602 as "11K" (#2761). One too large or too small to read as plain
/// digits has an exponent.
Comment on lines +1809 to +1811

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc says the text is the value "in full", and the title says the same. For a 32-bit integer or 64-bit float band the value was already rounded to f32 when the raster was read, so that is the value shown: for example 123456789 on an Int32 band shows as 123456790. The reader already reports that rounding (r.rounded), and the PR body says so, so nothing is lost silently here. The doc should still say "the layer's f32 value in full" so it doesn't promise more. The (#2761) and "11K" references describe the old output, not this code, so they belong in the PR, not in the function's doc.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on the wording: "the layer's f32 value in full", without the old output. Left for the release quality pass.

fn pixel_text(v: f32) -> 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<Value> {
pixels_at(s, lon, lat, |_| true, true).into_iter().next().map(|(_, v)| v)
Expand All @@ -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})))
Expand All @@ -1859,6 +1866,29 @@ fn like_name(p: &Value) -> Result<Option<&str>, 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];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test covers the exponent form only at 1e-30 and -3.4e38, and the plain form only in the middle of the range. The two thresholds that pick the form, 1e-5 (plain from there up) and 1e16 (exponent from there up), are not pinned. Adding 1e-5 (expected "0.00001") and 1e-6 (expected "1e-6") and 1e16 (expected "1e16") to values would cover both edges cheaply. Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed: 1e-5, 1e-6 and 1e16 would pin both edges. Left for the release quality pass.

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() {
Expand Down
Loading