diff --git a/crates/analysis/src/rasterops.rs b/crates/analysis/src/rasterops.rs index a8b0dfb51a..863aed59a7 100644 --- a/crates/analysis/src/rasterops.rs +++ b/crates/analysis/src/rasterops.rs @@ -840,7 +840,8 @@ fn viewshed_on( ) -> Result { let (oc, or) = r.pixel_at(observer).ok_or("the observer is outside the raster")?; let z0 = r.value(b, oc, or).ok_or("the observer stands on no-data")? as f64 + height; - let (cx, cy) = r.cell_size_m(); + // The radius and the Earth's curvature are metres: so are the cells, in a system in feet too. + let (cx, cy) = r.cell_size_ground_m(); if !(cx > 0.0 && cy > 0.0 && radius_m.is_finite() && radius_m > 0.0) { return Err("the raster's cells or the radius have no size".into()); } @@ -3986,4 +3987,27 @@ mod tests { assert_eq!(got, Some((vec![1.0], inside)), "{x0} {x1} {y0} {y1}"); } } + + /// A DEM in feet sees as far as the same ground in metres: its radius is metres, not feet + /// (#3346). 200 × 200 flat cells of 10 US survey feet (3.048 m), seen 1.7 m up from the + /// middle, 300 m around: π · 98.4² cells, in either system. + #[test] + fn a_viewshed_on_a_dem_in_feet_takes_its_radius_in_metres() { + let seen = |crs: Crs, cell: f64, x0: f64, y0: f64| { + let transform = GeoTransform { origin_x: x0, origin_y: y0, pixel_width: cell, pixel_height: -cell }; + let flat = Raster::new("flat", 200, 200, vec![vec![0.0; 40_000]], transform, crs, None, ColorHint::Gray, "Float32") + .unwrap_or_else(|e| panic!("{e}")); + let eye = flat.transformer().to_lonlat(transform.to_world(100.5, 100.5)).unwrap_or_else(|| panic!("no eye")); + let vs = viewshed(&flat, 0, eye, 1.7, 300.0, Heights::Nearest).unwrap_or_else(|e| panic!("{e}")); + vs.cells.iter().filter(|v| **v == 1.0).count() + }; + let feet = Crs::Epsg(2263); + let metre = feet.metres_per_unit(); + assert!((metre - 1200.0 / 3937.0).abs() < 1e-12, "{metre}"); + let in_feet = seen(feet, 10.0, 980_000.0, 200_000.0); + let in_metres = seen(Crs::Epsg(32618), 10.0 * metre, 585_000.0, 4_510_000.0); + assert_eq!(in_feet, in_metres); + let circle = std::f64::consts::PI * (300.0 / (10.0 * metre)).powi(2); + assert!((in_feet as f64 / circle - 1.0).abs() < 0.02, "{in_feet} cells, {circle} in the circle"); + } } diff --git a/crates/data/src/raster/mod.rs b/crates/data/src/raster/mod.rs index 7426564699..d55135e149 100644 --- a/crates/data/src/raster/mod.rs +++ b/crates/data/src/raster/mod.rs @@ -1380,6 +1380,16 @@ impl Raster { self.cell_size_m_on(&self.extent) } + /// Ground size of a pixel in metres whatever the system's unit: [`Raster::cell_size_m`] with + /// a projected system's unit (a foot, a US survey foot) turned into metres, for lengths and + /// areas given in metres (#3346). `cell_size_m` keeps that unit: slope, aspect and hillshade + /// take heights in the coordinates' unit. + pub fn cell_size_ground_m(&self) -> (f64, f64) { + let (x, y) = self.cell_size_m(); + let unit = if self.crs.is_geographic() { 1.0 } else { self.crs.metres_per_unit() }; + (x * unit, y * unit) + } + /// [`Raster::cell_size_m`] as on `ground` (longitude/latitude): a cell of a geographic raster /// measured at its middle's latitude, as a raster covering that ground measures its cells. A /// part of a raster measures its cells so as the whole of it does. diff --git a/crates/engine/src/cmd/imagery.rs b/crates/engine/src/cmd/imagery.rs index 31127f4f70..bd46a2e1f4 100644 --- a/crates/engine/src/cmd/imagery.rs +++ b/crates/engine/src/cmd/imagery.rs @@ -3024,7 +3024,7 @@ fn measure(s: &mut Session, p: &Value) -> CmdResult { } let base = f(p, "base"); let rings = vec![pts.clone()]; - let (cx, cy) = d.cell_size_m(); + let (cx, cy) = d.cell_size_ground_m(); // A geographic raster's cells are degrees, measured on the Earth: on the map's body. let (cx, cy) = if d.crs.is_geographic() { (body.length(cx), body.length(cy)) } else { (cx, cy) }; let (mut above, mut below, mut n, mut lowest) = (0.0, 0.0, 0usize, f64::INFINITY); @@ -3188,7 +3188,7 @@ pub(crate) fn mosaic_work(s: &Session, p: &Value) -> Result Result { let dem_name = dem.held().name.clone(); Ok(Box::new(move || { let dem = dem.read()?; - let (cx, cy) = dem.cell_size_m(); + let (cx, cy) = dem.cell_size_ground_m(); // The whole raster by default, however large: its diagonal reaches every cell from any // observer, and only the raster's cells are worked out. A radius given past the diagonal // covers the same cells, so it is taken as the diagonal: nothing is left out. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 07ab954bb8..62573faaac 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -58076,6 +58076,54 @@ fn rgbi_mosaics_keep_nir_without_alpha_and_reject_mixed_band_roles() { assert_eq!((r.band_count(), r.color.clone()), (4, ColorHint::Rgb { alpha: false })); } +/// A DEM in feet measures its volume in cubic metres, as the same ground in metres does: a box +/// 10 high on 10 × 10 cells of 10 US survey feet is 100 · 10 · 3.048² m³ (#3346). +#[test] +fn a_volume_on_a_dem_in_feet_is_in_cubic_metres() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster}; + let mut s = Session::new(Project::default()); + let feet = Crs::Epsg(2263); + let metre = feet.metres_per_unit(); + let mut volume = |name: &str, crs: Crs, cell: f64, x0: f64, y0: f64| { + let gt = GeoTransform { origin_x: x0, origin_y: y0, pixel_width: cell, pixel_height: -cell }; + let z: Vec = (0..400).map(|i| if (5..15).contains(&(i % 20)) && (5..15).contains(&(i / 20)) { 10.0 } else { 0.0 }).collect(); + let r = Raster::new(name, 20, 20, vec![z], gt, crs, None, ColorHint::Gray, "Float32").unwrap(); + let ring: Vec<_> = [(0.25, 0.25), (19.75, 0.25), (19.75, 19.75), (0.25, 19.75)] + .into_iter() + .map(|(c, r_)| r.transformer().to_lonlat(gt.to_world(c, r_)).map(|ll| [ll.x, ll.y]).unwrap()) + .collect(); + s.map_mut().add_raster(name, rustgis_map::RasterLayer::from_path(r, format!("{name}.tif"))); + let m = s.run("raster.measure", &json!({"layer": name, "kind": "volume", "points": ring, "base": 0.0})).unwrap(); + assert_eq!(m["cells"], 400, "{m}"); + m["cubicMetresAbove"].as_f64().unwrap() + }; + let in_feet = volume("feet", feet, 10.0, 980_000.0, 200_000.0); + let in_metres = volume("metres", Crs::Epsg(32618), 10.0 * metre, 585_000.0, 4_510_000.0); + let want = 100.0 * 10.0 * (10.0 * metre).powi(2); + assert!((in_feet - want).abs() < 1e-6 * want && (in_metres - want).abs() < 1e-6 * want, "{in_feet} {in_metres} {want}"); +} + +/// A mosaic in degrees of an image in feet has the image's own pixel by default: 10 US survey +/// feet, 3.048 m, in degrees at its latitude, not 10 m (#3346). +#[test] +fn a_mosaic_in_degrees_of_an_image_in_feet_keeps_its_pixel() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff}; + use rustgis_map::raster::RasterLayer; + let mut s = Session::new(Project::default()); + let feet = Crs::Epsg(2263); + let gt = GeoTransform { origin_x: 980_000.0, origin_y: 200_000.0, pixel_width: 10.0, pixel_height: -10.0 }; + let r = Raster::new("feet", 10, 10, vec![vec![1.0; 100]], gt, feet, None, ColorHint::Gray, "Float32").unwrap(); + let lat = r.extent().center().y.to_radians().cos(); + let bytes = geotiff::write(&r, 0).unwrap(); + s.map_mut().add_raster("feet", RasterLayer::from_bytes(r, "feet.tif", bytes)); + s.run("imagery.workspace", &json!({"kind": "ortho", "name": "Feet", "images": ["feet"], "crs": "EPSG:4326"})).unwrap(); + let v = s.run("imagery.mosaic", &json!({"name": "Feet"})).unwrap(); + let mo = s.map().layer(v["layer"].as_u64().unwrap() as u32).unwrap().raster.as_ref().unwrap().data.clone().unwrap(); + let want = 10.0 * feet.metres_per_unit() / (111_320.0 * lat); + assert_eq!(mo.crs, Crs::LonLat, "{v}"); + assert!((mo.transform.pixel_width / want - 1.0).abs() < 1e-9, "{} against {want}", mo.transform.pixel_width); +} + /// An ortho workspace's mosaic is made in the projected system its images share unless it sets /// one, and says so; images in different systems, or a cell size in degrees, keep degrees and say /// why; "EPSG:4326" asks for degrees, another geographic system is refused (#1743 step 3).