Repository navigation
fix(engine): Identify gives a pixel's values in full #3543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| 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) | ||
|
|
@@ -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<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]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
|
||
There was a problem hiding this comment.
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
f32when 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'sf32value 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
There was a problem hiding this comment.
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
f32value in full", without the old output. Left for the release quality pass.