From 9d7c90f1765103ab64c8c2619f622a112c6ce911 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 14:30:12 -0300 Subject: [PATCH 1/2] feat(map): a Shaded Relief's aerial perspective lightens and flattens its low ground (#3141) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Part 1 of #3141. A new Shaded Relief setting, "aerial" (0 to 1, default 0). With t a cell's height on the layer's stretch and k = aerial × (1 − t)^1.5, the cell's light is pulled 0.7 k toward flat ground's light, lowering the contrast of low ground, and its colour is mixed 0.45 k toward a light haze (#ECECEC). Summits keep their relief, so the high ground stands out. It is painted in the Colorizer, so the map, exports, layouts and the 3D drape all draw it; a colour bar shows the haze but not the pull, which is the shading's contrast and not a colour of the ramp. raster.symbology takes "aerial" (left out, kept; outside 0 to 1, refused; null or 0, none); a layer or saved step setting it requires shaded-relief-aerial. The style pane's field and the user guide come in part 2. At 0 a relief is drawn pixel for pixel as before: no_aerial_perspective_paints_as_before checks every pixel against main's formula, copied into the test, over heights, hillshades, sky, fill, rock, ambient, balanced light, sun altitude and brightness. Co-Authored-By: Claude Opus 5.5 --- crates/engine/command-surface.txt | 2 +- crates/engine/src/cmd/raster.rs | 28 ++- crates/engine/src/project.rs | 1 + crates/engine/src/project_capabilities.rs | 30 ++- crates/engine/src/tests.rs | 50 +++++ crates/map/src/layout/colorbar.rs | 2 +- crates/map/src/layout/tests.rs | 1 + crates/map/src/qgis/raster.rs | 1 + crates/map/src/qgis/tests.rs | 1 + crates/map/src/raster.rs | 239 ++++++++++++++++++++-- crates/render/src/layout/tests.rs | 3 + crates/render/src/lib.rs | 3 + crates/render/src/raster.rs | 4 + crates/render/src/raster_export.rs | 2 + crates/render/src/terrain/water.rs | 5 + crates/ui-egui/src/mapview.rs | 1 + crates/ui-egui/src/panes.rs | 2 + docs/maintaining.md | 2 + schema/fixtures/shaded_relief_aerial.rgis | 1 + schema/project.schema.json | 6 +- 20 files changed, 359 insertions(+), 25 deletions(-) create mode 100644 schema/fixtures/shaded_relief_aerial.rgis diff --git a/crates/engine/command-surface.txt b/crates/engine/command-surface.txt index 0854ed4f51..55252e7cb5 100644 --- a/crates/engine/command-surface.txt +++ b/crates/engine/command-surface.txt @@ -486,7 +486,7 @@ raster.rotation layer? angle raster.setDefaultStyle fromLayer? layer? ramp? invert? stretch? low? high? n? resampling? reset? raster.spectral layer lon? lat? points? zone? raster.stretch layer? type low? high? n? min? max? like? -raster.symbology layer? type band? ramp? invert? classes? method? azimuth? altitude? zFactor? shadow? sky? fill? ambient? balanced? shadowSoftness? rock? grey? +raster.symbology layer? type band? ramp? invert? classes? method? azimuth? altitude? zFactor? shadow? sky? fill? ambient? balanced? shadowSoftness? aerial? rock? grey? raster.zarrInfo path url bbox? variable? report.activate report report.attachments report? field diff --git a/crates/engine/src/cmd/raster.rs b/crates/engine/src/cmd/raster.rs index 20a63d8b7f..c74f53b6bb 100644 --- a/crates/engine/src/cmd/raster.rs +++ b/crates/engine/src/cmd/raster.rs @@ -39,7 +39,7 @@ pub fn commands() -> Vec { C::new("layer.addDataBytes", "Add Data (from file contents)", "Home › Add", add_bytes::).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"fileName": "name.tif|.tiff|.asc|.grb|.grib|.grb2|.grib2 (GRIB)|.hgt|.dt0|.dt1|.dt2|.geojson|.csv|.zip|.kml|.kmz|.gpx|.parquet|.fgb|.gpkg|.mbtiles|.jp2|.j2k|.dxf|.pdf (JPEG 2000: desktop, through GDAL; PDF: desktop, command line and Python)", "data": base64, "zoom"?: bool, "layer"?: "one layer of a zip, KML or GPX by name, or a PDF's page number (the first when left out or blank)", "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "blocks"?, "stream"?, "as"? (as layer.addData)}"#), C::new("raster.symbology", "Symbology", "Style › Rendering", symbology).when(need_raster).roles(&[]).effects(crate::policy::Effects::NONE).params( - r#"{"layer"?, "type": "stretch"|"rgb"|"classify"|"unique"|"colormap"|"shadedRelief", "band"?: 1.., "ramp"?, "invert"?: bool, "classes"?: 1..32, "method"?, "azimuth"?, "altitude"?, "zFactor"?, "shadow"?: 0..1 (shadedRelief: how dark the shadows the terrain casts are; left out, kept; 0 casts none), "sky"?: 0..1 (shadedRelief: how much the share of the sky each cell sees dims its ambient light; left out, kept; 0 leaves it whole), "fill"?: 0..1 (shadedRelief: a fill light from the side opposite the sun that lightens the slopes turned toward it; left out, kept; 0 has none), "ambient"?: 0..1|null (shadedRelief: the share of the colour a slope turned fully from the sun keeps; left out, kept; null, the default: 0.25, or 0.5 balanced), "balanced"?: bool (shadedRelief: flat ground keeps the ramp's colour and slopes facing the sun brighten, up to 1.3; left out, kept; false, the default, lights as before), "shadowSoftness"?: 0.5..15|null (shadedRelief: how soft the edges of its cast shadows are, as half the sun's apparent size in degrees; left out, kept; outside 0.5 to 15, refused; null, the default: 5), "rock"?: {"from"?: 0..90, "to"?: 0..90, "color"?: #RRGGBB} | false (shadedRelief: steep slopes tinted toward a rock colour before the light, from the ground's own slope in degrees, without the Z factor: the ramp below from, the colour above to, blended between; left out, kept; an object's missing keys keep the current rock's, or 25, 40 and #B2ACA4; false or null removes it), "grey"?: bool (shadedRelief without its colour ramp)}"#, + r#"{"layer"?, "type": "stretch"|"rgb"|"classify"|"unique"|"colormap"|"shadedRelief", "band"?: 1.., "ramp"?, "invert"?: bool, "classes"?: 1..32, "method"?, "azimuth"?, "altitude"?, "zFactor"?, "shadow"?: 0..1 (shadedRelief: how dark the shadows the terrain casts are; left out, kept; 0 casts none), "sky"?: 0..1 (shadedRelief: how much the share of the sky each cell sees dims its ambient light; left out, kept; 0 leaves it whole), "fill"?: 0..1 (shadedRelief: a fill light from the side opposite the sun that lightens the slopes turned toward it; left out, kept; 0 has none), "ambient"?: 0..1|null (shadedRelief: the share of the colour a slope turned fully from the sun keeps; left out, kept; null, the default: 0.25, or 0.5 balanced), "balanced"?: bool (shadedRelief: flat ground keeps the ramp's colour and slopes facing the sun brighten, up to 1.3; left out, kept; false, the default, lights as before), "shadowSoftness"?: 0.5..15|null (shadedRelief: how soft the edges of its cast shadows are, as half the sun's apparent size in degrees; left out, kept; outside 0.5 to 15, refused; null, the default: 5), "aerial"?: 0..1|null (shadedRelief: aerial perspective, low ground on the layer's stretch lit with less contrast and hazed toward #ECECEC, the more the lower; left out, kept; outside 0 to 1, refused; null or 0, the default: none), "rock"?: {"from"?: 0..90, "to"?: 0..90, "color"?: #RRGGBB} | false (shadedRelief: steep slopes tinted toward a rock colour before the light, from the ground's own slope in degrees, without the Z factor: the ramp below from, the colour above to, blended between; left out, kept; an object's missing keys keep the current rock's, or 25, 40 and #B2ACA4; false or null removes it), "grey"?: bool (shadedRelief without its colour ramp)}"#, ), C::new("raster.stretch", "Stretch Type", "Style › Rendering", stretch).inputs(stretch_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_raster) @@ -1035,12 +1035,14 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult { } "shadedrelief" | "hillshade" => { // Left out, a Shaded Relief keeps its own shadows (and their softness), sky, fill - // light, rock, ambient and balanced light; a new one has none (and the defaults). - let (cur_shadow, cur_sky, cur_fill, cur_rock, cur_ambient, cur_balanced, cur_softness) = match rl.style.renderer { - RasterRenderer::ShadedRelief { shadow, sky, fill, rock, ambient, balanced, shadow_softness, .. } => { - (shadow, sky, fill, rock, ambient, balanced, shadow_softness) + // light, rock, ambient and balanced light and aerial perspective; a new one has none + // (and the defaults). + let (cur_shadow, cur_sky, cur_fill, cur_rock, cur_ambient, cur_balanced, cur_softness, cur_aerial) = match rl.style.renderer + { + RasterRenderer::ShadedRelief { shadow, sky, fill, rock, ambient, balanced, shadow_softness, aerial, .. } => { + (shadow, sky, fill, rock, ambient, balanced, shadow_softness, aerial) } - _ => (0.0, 0.0, 0.0, None, None, false, None), + _ => (0.0, 0.0, 0.0, None, None, false, None, 0.0), }; RasterRenderer::ShadedRelief { band: bi, @@ -1067,6 +1069,11 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult { Some(Value::Null) => None, Some(_) => relief_shadow_softness(f(p, "shadowSoftness"))?, }, + aerial: match p.get("aerial") { + None => cur_aerial, + Some(Value::Null) => 0.0, + Some(_) => relief_aerial(f(p, "aerial"))?, + }, } } other => return Err(format!("unknown raster symbology '{other}'")), @@ -1093,6 +1100,15 @@ fn relief_shadow_softness(v: Option) -> Result, String> { } } +/// A Shaded Relief's `"aerial"` (see `raster.symbology`), `v` being the number given, if it is one: +/// refused by name outside 0 to 1, never brought into it. +fn relief_aerial(v: Option) -> Result { + match v { + Some(v) if (0.0..=1.0).contains(&v) => Ok(v), + _ => Err("\"aerial\" must be a number from 0 to 1, or null for none".into()), + } +} + /// The rock of a Shaded Relief's `"rock"` (see `raster.symbology`): `false` or null for none, or /// an object whose missing keys keep `cur`'s, or else [`Rock::DEFAULT`]'s. fn relief_rock(v: &Value, cur: Option) -> Result, String> { diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 9c32ea295f..79862658a6 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -132,6 +132,7 @@ pub const CAPABILITIES: &[&str] = &[ "layout-publication-parts", "layout-publication-legend", "shaded-relief-shadow-softness", + "shaded-relief-aerial", ]; /// An offline area keeping the versions that lost a sync or a Bring Back /// (`OfflineArea::conflicts`, #2468): an older reader would save it again without them. diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 201f64f145..c622e4145d 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -183,6 +183,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[ ("layout-publication-parts", command_sets_publication_parts), ("layout-publication-legend", command_sets_publication_legend), ("shaded-relief-shadow-softness", command_sets_shadow_softness), + ("shaded-relief-aerial", command_sets_aerial), ]; /// A saved `layout.publication` that leaves out or sets up its grid, scale bar or north arrow @@ -226,6 +227,17 @@ fn command_sets_shadow_softness(id: &str, params: &Value) -> bool { } } +/// A saved `raster.symbology` that sets a Shaded Relief's aerial perspective (#3141): an older reader +/// runs it without the key, saying nothing, so the low ground would be drawn without its haze. +/// `null` and 0 ask for none, which an older reader draws. +fn command_sets_aerial(id: &str, params: &Value) -> bool { + match id { + "raster.symbology" => params.get("aerial").is_some_and(|v| !v.is_null() && v.as_f64() != Some(0.0)), + "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_sets_aerial)), + _ => false, + } +} + /// A saved `labels.placement` that has a class remove duplicates, read as the command reads it /// (#2847): an older reader runs it without the key, saying nothing, so every label would show. /// False or null is what an older reader keeps. @@ -999,6 +1011,12 @@ fn sets_shadow_softness(layer: &Layer) -> bool { .is_some_and(|r| matches!(&r.style.renderer, RasterRenderer::ShadedRelief { shadow_softness, .. } if shadow_softness.is_some())) } +/// Whether a layer sets its Shaded Relief's aerial perspective (#3141): an older reader would draw +/// the low ground without its haze and save the layer again without the setting. +fn sets_aerial(layer: &Layer) -> bool { + layer.raster.as_ref().is_some_and(|r| matches!(&r.style.renderer, RasterRenderer::ShadedRelief { aerial, .. } if *aerial != 0.0)) +} + /// Whether a layer is a window of a Zarr variable following the time slider (#1720): an older /// reader would drop the window and read the whole map at each step. fn zarr_window(layer: &Layer) -> bool { @@ -1042,6 +1060,7 @@ fn layer_capabilities(layer: &Layer) -> Vec<&'static str> { (relief_effects(layer), "shaded-relief-effects"), (relief_light(layer), "shaded-relief-light"), (sets_shadow_softness(layer), "shaded-relief-shadow-softness"), + (sets_aerial(layer), "shaded-relief-aerial"), (layer.size_by.is_some(), super::SIZE_BY_CAPABILITY), (layer.has_picture_markers(), "picture-markers"), (layer.any_picture(svg_picture), "svg-markers"), @@ -1078,7 +1097,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator) -> Vec<&'static let (mut polygon, mut svg) = (false, false); let (mut offline_area_id, mut paper, mut native, mut guided, mut weighed, mut cleared, mut deduped) = (false, false, false, false, false, false, false); - let (mut crisp, mut bed, mut soft) = (false, false, false); + let (mut crisp, mut bed, mut soft, mut aerial) = (false, false, false, false); for map in maps { offline_area_id |= map.offline_area_id.is_some(); // A blank basemap's paper colour (#2532), which an older reader would draw and save white. @@ -1114,6 +1133,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator) -> Vec<&'static relief |= map.layers.iter().any(relief_effects); light |= map.layers.iter().any(relief_light); soft |= map.layers.iter().any(sets_shadow_softness); + aerial |= map.layers.iter().any(sets_aerial); sized |= map.layers.iter().any(|layer| layer.size_by.is_some()); pictures |= map.layers.iter().any(Layer::has_picture_markers); svg |= map.layers.iter().any(|layer| layer.any_picture(svg_picture)); @@ -1135,6 +1155,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator) -> Vec<&'static (relief, "shaded-relief-effects"), (light, "shaded-relief-light"), (soft, "shaded-relief-shadow-softness"), + (aerial, "shaded-relief-aerial"), (legends, "normalized-legend-urls"), (sized, super::SIZE_BY_CAPABILITY), (pictures, "picture-markers"), @@ -1602,6 +1623,10 @@ mod tests { (&["shaded-relief-shadow-softness"], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": 1.5})), // `null` is the default an older reader draws. (&[], "raster.symbology", json!({"type": "shadedRelief", "shadowSoftness": null})), + (&["shaded-relief-aerial"], "raster.symbology", json!({"type": "shadedRelief", "aerial": 0.6})), + // null and 0 are the none an older reader draws. + (&[], "raster.symbology", json!({"type": "shadedRelief", "aerial": null})), + (&[], "raster.symbology", json!({"type": "shadedRelief", "aerial": 0})), // An SVG image is a picture marker as well, in a node as in a saved command. (&["picture-markers", "svg-markers"], "layer.marker", json!({"image": svg})), (&[], "buffer", json!({"input": "Cities", "distance": 1})), @@ -3251,7 +3276,7 @@ mod tests { })) .unwrap(); let clean = Layer::new_raster(1, "DEM", raster); - for field in ["shadow", "sky", "fill", "rock", "ambient", "balanced", "shadow_softness"] { + for field in ["shadow", "sky", "fill", "rock", "ambient", "balanced", "shadow_softness", "aerial"] { let mut value = serde_json::to_value(&clean).unwrap(); value["raster"]["style"]["renderer"]["ShadedRelief"][field] = match field { "rock" => serde_json::to_value(rustgis_map::raster::Rock::DEFAULT).unwrap(), @@ -3269,6 +3294,7 @@ mod tests { let needs = match field { "ambient" | "balanced" => "shaded-relief-light", "shadow_softness" => "shaded-relief-shadow-softness", + "aerial" => "shaded-relief-aerial", _ => "shaded-relief-effects", }; assert_eq!(document["requires"], json!([needs]), "{field}: {document}"); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 228a95f994..9dd55b3926 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7877,6 +7877,56 @@ fn shaded_relief_keeps_its_shadow_softness() { assert_eq!(serde_json::to_value(&fixture).unwrap()["requires"], json!(["shaded-relief-effects", "shaded-relief-shadow-softness"])); } +/// Shaded Relief's aerial perspective (#3141) is set, kept when left out, refused by name outside 0 +/// to 1 (never brought into it), saved only when set, and back when the project is opened again; +/// null or 0 is none again, which saves nothing new. +#[test] +fn shaded_relief_keeps_its_aerial_perspective() { + let mut s = with_raster(); + let aerial = |s: &Session| match s.map().layers.iter().find_map(|l| l.raster.as_ref()).map(|r| &r.style.renderer) { + Some(rustgis_map::RasterRenderer::ShadedRelief { aerial, .. }) => *aerial, + other => panic!("{other:?}"), + }; + let saved = |s: &mut Session| s.project.saved_text(None).unwrap().0; + s.run("raster.symbology", &json!({"type": "shadedRelief", "shadow": 0.8})).unwrap(); + assert_eq!(aerial(&s), 0.0); + assert!(!saved(&mut s).contains("\"aerial\":"), "none saves nothing new"); + s.run("raster.symbology", &json!({"type": "shadedRelief", "aerial": 0.6})).unwrap(); + s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 200})).unwrap(); + assert_eq!(aerial(&s), 0.6, "left out, kept"); + for refused in [json!(-0.1), json!(1.5), json!("haze"), json!(true)] { + let e = s.run("raster.symbology", &json!({"type": "shadedRelief", "aerial": refused})).unwrap_err(); + assert!(e.contains("\"aerial\"") && e.contains("0 to 1"), "{refused}: {e}"); + assert_eq!(aerial(&s), 0.6, "{refused}: refused, nothing changed"); + } + s.run("raster.symbology", &json!({"type": "shadedRelief", "aerial": 1})).unwrap(); + assert_eq!(aerial(&s), 1.0); + let text = saved(&mut s); + assert!(text.contains("\"aerial\":1.0") && text.contains("shaded-relief-aerial"), "saved, and required by name"); + let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); + let dir = std::env::temp_dir().join(format!("rustgis-aerial-{}-{nanos}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("aerial.rgis"); + s.run("project.saveAs", &json!({"path": path.display().to_string()})).unwrap(); + let mut t = Session::new(Project::default()); + t.run("project.open", &json!({"path": path.display().to_string()})).unwrap(); + let _ = std::fs::remove_dir_all(&dir); + assert_eq!(aerial(&t), 1.0, "back when opened"); + for none in [json!(null), json!(0)] { + s.run("raster.symbology", &json!({"type": "shadedRelief", "aerial": 0.3})).unwrap(); + s.run("raster.symbology", &json!({"type": "shadedRelief", "aerial": none})).unwrap(); + assert_eq!(aerial(&s), 0.0, "{none}: none"); + } + s.run("raster.symbology", &json!({"type": "shadedRelief", "aerial": 0.3})).unwrap(); + s.run("edit.undo", &json!({})).unwrap(); + assert_eq!(aerial(&s), 0.0, "undone"); + assert!(!saved(&mut s).contains("\"aerial\":"), "none saves nothing new"); + // The fixture reads back as saved, requiring what it sets by name. + let fixture = Project::from_json(include_str!("../../../schema/fixtures/shaded_relief_aerial.rgis")).unwrap(); + assert_eq!(aerial(&Session::new(fixture.clone())), 0.6); + assert_eq!(serde_json::to_value(&fixture).unwrap()["requires"], json!(["shaded-relief-aerial"])); +} + /// Shaded Relief's rock on steep slopes (#1913): an object turns it on, its missing keys from the /// current rock or the defaults; kept when left out; `false` removes it; a `from` not below `to` /// or a bad colour is refused; saved with the project only when set. diff --git a/crates/map/src/layout/colorbar.rs b/crates/map/src/layout/colorbar.rs index 4002415d0a..9e88ecc5a6 100644 --- a/crates/map/src/layout/colorbar.rs +++ b/crates/map/src/layout/colorbar.rs @@ -86,7 +86,7 @@ pub fn color_scale(rl: &RasterLayer) -> Option { RasterRenderer::ShadedRelief { .. } => Some(255), _ => None, }; - let at = |v: f64| colorizer.color(&[v as f32], shade).map(Rgba); + let at = |v: f64| colorizer.key_color(&[v as f32], shade).map(Rgba); match &style.renderer { RasterRenderer::Stretch { .. } | RasterRenderer::ShadedRelief { ramp: Some(_), .. } => { let n = Normalizer::new(style.stretch, stats, &r.data_type); diff --git a/crates/map/src/layout/tests.rs b/crates/map/src/layout/tests.rs index 0e9e307513..c38e05f231 100644 --- a/crates/map/src/layout/tests.rs +++ b/crates/map/src/layout/tests.rs @@ -770,6 +770,7 @@ fn a_balanced_reliefs_colour_bar_is_its_flat_ground() { ambient: None, balanced, shadow_softness: None, + aerial: 0.0, }; let Some(ColorScale::Ramp { lo, hi, colors }) = color_scale(&rl) else { panic!("a ramp") }; let colorizer = Colorizer::new(&rl.style, &r, None); diff --git a/crates/map/src/qgis/raster.rs b/crates/map/src/qgis/raster.rs index 26c65f0363..f1155715c2 100644 --- a/crates/map/src/qgis/raster.rs +++ b/crates/map/src/qgis/raster.rs @@ -101,6 +101,7 @@ fn renderer_style(node: Node, pipe: Node, r: Node, kind: &str, layer: &str, repo ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; (renderer, Stretch::None) } diff --git a/crates/map/src/qgis/tests.rs b/crates/map/src/qgis/tests.rs index 9c877e8929..480b04348c 100644 --- a/crates/map/src/qgis/tests.rs +++ b/crates/map/src/qgis/tests.rs @@ -913,6 +913,7 @@ fn raster_renderers_read_as_rustgis_raster_styles() { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, } ); assert!(report.notes.iter().any(|n| n.text.contains("saturation"))); diff --git a/crates/map/src/raster.rs b/crates/map/src/raster.rs index c5fdd76a67..aa757fe065 100644 --- a/crates/map/src/raster.rs +++ b/crates/map/src/raster.rs @@ -1452,6 +1452,27 @@ pub fn relief_ambient(ambient: Option, balanced: bool) -> f64 { /// The most a balanced Shaded Relief light multiplies a colour by, on slopes facing the sun. const BALANCED_MAX: f64 = 1.3; +/// The light haze a Shaded Relief's aerial perspective mixes low ground's colour toward (#3141): +/// each channel of `#ECECEC`. +pub const AERIAL_HAZE: f64 = 236.0; +/// At a cell's full share of aerial perspective (see [`aerial_share`]), how far its light is pulled +/// toward flat ground's ... +const AERIAL_LIGHT: f64 = 0.7; +/// ... and how far its colour is mixed toward [`AERIAL_HAZE`]. +const AERIAL_MIX: f64 = 0.45; + +/// The share of a Shaded Relief's aerial perspective a cell gets (#3141): `aerial` (0 to 1) times +/// `(1 − t)^1.5`, `t` being the cell's height on the layer's stretch (0 at its low end, 1 at its high +/// end). The power leaves most of the haze to the plains and most of their relief to the middle +/// heights. 0 for a setting of 0 or less, or not a number; a setting past 1 counts as 1. +pub fn aerial_share(aerial: f64, t: f64) -> f64 { + if aerial.is_nan() || aerial <= 0.0 { + return 0.0; + } + let low = 1.0 - if t.is_nan() { 0.0 } else { t.clamp(0.0, 1.0) }; + (aerial.min(1.0) * low.powf(1.5)).clamp(0.0, 1.0) +} + fn is_no_light(v: &f64) -> bool { *v == 0.0 } @@ -1672,7 +1693,9 @@ pub enum RasterRenderer { /// lights as before. `shadow_softness` is half the sun's apparent size its cast shadows are /// traced with, in degrees ([`crate::shadow::SOFT_EDGE_MIN`] to /// [`crate::shadow::SOFT_EDGE_MAX`]): how soft their edges are (#3144); none, the default, is - /// [`crate::shadow::SOFT_EDGE`]. + /// [`crate::shadow::SOFT_EDGE`]. `aerial` (0 to 1) is its aerial perspective (#3141): low ground, + /// by the cell's height on the layer's stretch, is lit with less contrast and hazed toward + /// [`AERIAL_HAZE`] (see [`aerial_share`]); 0, the default, draws it as before. ShadedRelief { band: usize, ramp: Option, @@ -1693,6 +1716,8 @@ pub enum RasterRenderer { balanced: bool, #[serde(default, skip_serializing_if = "Option::is_none")] shadow_softness: Option, + #[serde(default, skip_serializing_if = "is_no_light")] + aerial: f64, }, } @@ -2285,10 +2310,22 @@ impl<'a> Colorizer<'a> { /// `fill` (0–255, a share of the colour) added for its fill light (its `fill`), and its colour /// moved `rock` (0–255) of the way to its rock colour (its `rock`, see [`Rock::share`]). pub fn color_lit(&self, vals: &[f32], hillshade: Option, sky: Option, fill: Option, rock: Option) -> Option<[u8; 4]> { + self.unmasked(vals, |vals| self.paint(vals, hillshade, sky, fill, rock, true)) + } + + /// The colour a colour bar shows for `vals` lit by `hillshade`: [`Colorizer::color`]'s, but a + /// Shaded Relief's aerial perspective (#3141) only hazes it, without pulling its light toward + /// flat ground's: that lowers the shading's contrast, it isn't a colour of the ramp. + pub fn key_color(&self, vals: &[f32], hillshade: Option) -> Option<[u8; 4]> { + self.unmasked(vals, |vals| self.paint(vals, hillshade, None, None, None, false)) + } + + /// `paint` of `vals`, its masked values as no data. + fn unmasked(&self, vals: &[f32], paint: impl Fn(&[f32]) -> Option<[u8; 4]>) -> Option<[u8; 4]> { // The renderer masks values as it samples them (they arrive as NaN); this is for other callers. let mask = &self.style.mask; if mask.is_empty() || !vals.iter().any(|v| mask.hides(*v)) { - return self.paint(vals, hillshade, sky, fill, rock); + return paint(vals); } // Masked values are no data. The renderer bands are at most three. let mut kept = [f32::NAN; 3]; @@ -2297,10 +2334,20 @@ impl<'a> Colorizer<'a> { *k = *v; } } - self.paint(kept.get(..vals.len().min(3)).unwrap_or(&kept), hillshade, sky, fill, rock) + paint(kept.get(..vals.len().min(3)).unwrap_or(&kept)) } - fn paint(&self, vals: &[f32], hillshade: Option, sky: Option, fill: Option, rock: Option) -> Option<[u8; 4]> { + /// `pull`: whether aerial perspective pulls the light toward flat ground's (see + /// [`Colorizer::key_color`]). + fn paint( + &self, + vals: &[f32], + hillshade: Option, + sky: Option, + fill: Option, + rock: Option, + pull: bool, + ) -> Option<[u8; 4]> { let v0 = *vals.first()?; let c = match &self.style.renderer { RasterRenderer::Rgb { .. } => { @@ -2327,7 +2374,7 @@ impl<'a> Colorizer<'a> { } *self.palette.get(i as usize)? } - RasterRenderer::ShadedRelief { ramp, rock: steep, altitude, ambient: setting, balanced, .. } => { + RasterRenderer::ShadedRelief { ramp, rock: steep, altitude, ambient: setting, balanced, aerial, .. } => { let h = hillshade.unwrap_or(180) as f64 / 255.0; let base = if ramp.is_some() { *self.ramp_lut.get(self.byte(0, v0) as usize)? } else { [255, 255, 255, 255] }; // Steep ground first takes on the rock colour; the light then shades either. @@ -2343,15 +2390,31 @@ impl<'a> Colorizer<'a> { // Flat ground's hillshade, rounded as the hillshade is: 0 for a sun on the horizon, // which lights no flat ground to balance against, so that is lit as unbalanced. let flat = (255.0 * altitude.clamp(0.0, 90.0).to_radians().sin()).round(); - let lit = if *balanced && flat > 0.0 { - // The sun's light over its value on flat ground: flat ground keeps the ramp's - // colour, slopes facing the sun brighten. - (a + (1.0 - share) * h * 255.0 / flat).min(BALANCED_MAX) - } else { - a + (1.0 - share) * h + let light = |h: f64| { + if *balanced && flat > 0.0 { + // The sun's light over its value on flat ground: flat ground keeps the + // ramp's colour, slopes facing the sun brighten. + (a + (1.0 - share) * h * 255.0 / flat).min(BALANCED_MAX) + } else { + a + (1.0 - share) * h + } }; - let m = |c: u8| (c as f64 * (lit + f)).round().clamp(0.0, 255.0) as u8; - [m(base[0]), m(base[1]), m(base[2]), 255] + let lit = light(h); + // Aerial perspective (#3141): low ground's light is pulled toward flat ground's, + // which lowers its contrast, and its colour mixed toward a light haze. + let k = aerial_share(*aerial, f64::from(self.byte(0, v0)) / 255.0); + if k > 0.0 { + let lit = if pull { lit + (light(flat / 255.0) - lit) * AERIAL_LIGHT * k } else { lit }; + let haze = AERIAL_MIX * k; + let m = |c: u8| { + let v = (c as f64 * (lit + f)).clamp(0.0, 255.0); + (v + (AERIAL_HAZE - v) * haze).round().clamp(0.0, 255.0) as u8 + }; + [m(base[0]), m(base[1]), m(base[2]), 255] + } else { + let m = |c: u8| (c as f64 * (lit + f)).round().clamp(0.0, 255.0) as u8; + [m(base[0]), m(base[1]), m(base[2]), 255] + } } }; (c[3] > 0).then(|| self.enhance(c)) @@ -2725,6 +2788,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, } } @@ -3316,6 +3380,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; let (mut rocky, mut plain) = (RasterStyle::default_for(&r), RasterStyle::default_for(&r)); (rocky.renderer, plain.renderer) = (relief(Some(rock)), relief(None)); @@ -3372,6 +3437,7 @@ mod tests { ambient, balanced, shadow_softness: None, + aerial: 0.0, }; let relief = |altitude, ambient, balanced| tinted_relief(None, altitude, ambient, balanced); // Grey relief: white shaded, so a channel is 255 times the light. @@ -3425,6 +3491,153 @@ mod tests { assert_eq!(serde_json::from_str::(&saved).unwrap(), set); } + /// A Shaded Relief over heights 0 to 100 with the stretch on them, so a value is its height on + /// the stretch in hundredths. + fn heights_relief(renderer: RasterRenderer) -> (Raster, RasterStyle) { + let gt = GeoTransform { origin_x: 0.0, origin_y: 10.0, pixel_width: 1.0, pixel_height: -1.0 }; + let data = (0..100).map(|i| i as f32).collect(); + let r = Raster::new("dem", 10, 10, vec![data], gt, Crs::WebMercator, None, ColorHint::Gray, "Float32").unwrap(); + let mut style = RasterStyle::default_for(&r); + style.stretch = Stretch::Custom { min: 0.0, max: 100.0, bands: None }; + style.renderer = renderer; + (r, style) + } + + /// A Shaded Relief with the lights asked for and the rest of the defaults. + fn lit_relief( + ramp: Option, + altitude: f64, + ambient: Option, + balanced: bool, + rock: Option, + aerial: f64, + ) -> RasterRenderer { + RasterRenderer::ShadedRelief { + band: 0, + ramp, + azimuth: 315.0, + altitude, + z_factor: 1.0, + shadow: 0.0, + sky: 0.0, + fill: 0.0, + rock, + ambient, + balanced, + shadow_softness: None, + aerial, + } + } + + /// Without aerial perspective (#3141), its default, a Shaded Relief is painted exactly as before + /// it existed: every pixel against the formula main drew with, copied here, over heights, every + /// hillshade, the sky, the fill light, rock, ambient, the balanced light, the sun's altitude and + /// the brightness. A project from before it reads as none. + #[test] + fn no_aerial_perspective_paints_as_before() { + // Main's Shaded Relief colour before #3141, as it was. + let before = |c: &Colorizer, v0: f32, hillshade: Option, sky: Option, fill: Option, rock: Option| { + let RasterRenderer::ShadedRelief { ramp, rock: steep, altitude, ambient: setting, balanced, .. } = &c.style.renderer else { + panic!("a relief") + }; + let h = hillshade.unwrap_or(180) as f64 / 255.0; + let base = if ramp.is_some() { *c.ramp_lut.get(c.byte(0, v0) as usize).unwrap() } else { [255, 255, 255, 255] }; + let base = match (steep, rock) { + (Some(steep), Some(share)) if share > 0 => Rgba(base).lerp(steep.color.with_alpha(255), f64::from(share) / 255.0).0, + _ => base, + }; + let share = relief_ambient(*setting, *balanced); + let a = share * sky.map_or(1.0, |s| f64::from(s) / 255.0); + let f = fill.map_or(0.0, |f| f64::from(f) / 255.0); + let flat = (255.0 * altitude.clamp(0.0, 90.0).to_radians().sin()).round(); + let lit = + if *balanced && flat > 0.0 { (a + (1.0 - share) * h * 255.0 / flat).min(BALANCED_MAX) } else { a + (1.0 - share) * h }; + let m = |c: u8| (c as f64 * (lit + f)).round().clamp(0.0, 255.0) as u8; + c.enhance([m(base[0]), m(base[1]), m(base[2]), 255]) + }; + let (mut painted, lights) = (0, [(None, None, None), (Some(128), Some(40), Some(200)), (Some(0), Some(255), Some(0))]); + for ramp in [None, Some(Ramp::Terrain)] { + for (ambient, balanced) in [(None, false), (Some(0.4), false), (None, true), (Some(0.8), true)] { + for rock in [None, Some(Rock::DEFAULT)] { + for altitude in [0.0, 30.0, 45.0, 89.0] { + for brightness in [0.0, 20.0] { + let (r, mut style) = heights_relief(lit_relief(ramp.clone(), altitude, ambient, balanced, rock, 0.0)); + style.brightness = brightness; + let c = Colorizer::new(&style, &r, None); + for v in [0.0, 12.0, 37.5, 50.0, 99.0, 100.0, 140.0, -5.0] { + for h in (0..=255u8).step_by(15).chain([255]).map(Some).chain([None]) { + for (sky, fill, steep) in lights { + let want = Some(before(&c, v, h, sky, fill, steep)); + assert_eq!(c.color_lit(&[v], h, sky, fill, steep), want, "{ramp:?} {altitude}° {v} {h:?}"); + painted += 1; + } + } + } + } + } + } + } + } + assert!(painted > 50_000, "{painted}"); + } + + /// Aerial perspective (#3141): low ground on the stretch is lit with less contrast, pulled + /// toward flat ground's light, and hazed toward #ECECEC, the more the lower, with a power of 1.5; + /// the top of the stretch keeps its relief. A colour bar shows the haze but not the pull. + #[test] + fn aerial_perspective_hazes_and_flattens_low_ground() { + // None for none or not a number, all of it at the bottom of the stretch, none at the top. + for (aerial, t, k) in [(0.0, 0.0, 0.0), (-1.0, 0.0, 0.0), (f64::NAN, 0.0, 0.0), (0.6, 0.0, 0.6), (0.6, 1.0, 0.0), (2.0, -1.0, 1.0)] + { + assert_eq!(aerial_share(aerial, t), k, "{aerial} {t}"); + } + assert!((aerial_share(1.0, 0.5) - 0.5f64.powf(1.5)).abs() < 1e-12 && aerial_share(0.6, f64::NAN) == 0.6); + + let grey = |aerial: f64, balanced: bool| { + let (r, style) = heights_relief(lit_relief(None, 45.0, None, balanced, None, aerial)); + move |v: f32, h: u8| Colorizer::new(&style, &r, None).color_lit(&[v], Some(h), None, None, None).map(|c| c[0]).unwrap() + }; + let (none, set) = (grey(0.0, false), grey(0.6, false)); + // The summit, at the top of the stretch, is drawn as without it, whatever the light. + for h in [0u8, 90, 180, 255] { + assert_eq!(set(100.0, h), none(100.0, h), "{h}"); + } + // The lowest ground, by the formula: the light pulled 0.7 × 0.6 toward flat ground's, then + // the colour 0.45 × 0.6 toward the haze. + let flat = (255.0 * 45f64.to_radians().sin()).round() / 255.0; + for h in [0u8, 90, 180, 255] { + let lit = 0.25 + 0.75 * f64::from(h) / 255.0; + let lit = lit + (0.25 + 0.75 * flat - lit) * 0.7 * 0.6; + let v = 255.0 * lit; + assert_eq!(set(0.0, h), (v + (236.0 - v) * 0.45 * 0.6).round() as u8, "{h}"); + } + // Contrast, between a slope turned from the sun and one facing it, grows with the height, + // and is lower than without it everywhere below the top; the low ground is lighter. + let contrast = |paint: &dyn Fn(f32, u8) -> u8, v: f32| i32::from(paint(v, 255)) - i32::from(paint(v, 0)); + let mut last = 0; + for v in [0.0, 25.0, 50.0, 75.0] { + let c = contrast(&set, v); + assert!(c > last && c < contrast(&none, v), "{v}: {c}"); + assert!(set(v, 0) > none(v, 0), "{v}: lighter"); + last = c; + } + // Balanced, flat ground's own hillshade is its light already: only the haze changes it. + let balanced = grey(1.0, true); + let flat_h = hillshade(&[0.0; 9], 1.0, 1.0, 315.0, 45.0, 1.0); + assert_eq!(balanced(0.0, flat_h), (255.0 + (236.0 - 255.0) * 0.45f64).round() as u8); + + // A colour bar's key: the haze, not the pull toward flat ground's light. + let (r, style) = heights_relief(lit_relief(Some(Ramp::Terrain), 45.0, None, false, None, 0.6)); + let c = Colorizer::new(&style, &r, None); + let ramp = |v: f32| *c.ramp_lut.get(c.byte(0, v) as usize).unwrap(); + for v in [0.0f32, 30.0, 100.0] { + let k = aerial_share(0.6, f64::from(c.byte(0, v)) / 255.0) * 0.45; + let hazed = ramp(v).map(|x| (f64::from(x) + (236.0 - f64::from(x)) * k).round() as u8); + assert_eq!(c.key_color(&[v], Some(255)), Some([hazed[0], hazed[1], hazed[2], 255]), "{v}"); + } + assert_eq!(c.key_color(&[100.0], Some(255)), Some(ramp(100.0)), "the top: the ramp itself"); + } + /// A tower lit from the east, drawn as Shaded Relief with `shadow`. fn tower(shadow: f64) -> RasterLayer { let gt = GeoTransform { origin_x: 0.0, origin_y: 0.0, pixel_width: 30.0, pixel_height: -30.0 }; diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index 16830c7100..58abd5e6f1 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -2924,6 +2924,7 @@ fn a_page_says_why_a_frame_drew_no_cast_shadows() { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; for shadow in [0.0, 1.0] { let map = ramp_map(relief(shadow), rustgis_map::raster::Stretch::MinMax); @@ -2977,6 +2978,7 @@ fn page_settles_with_suns(suns: &[f64]) { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; let mut m = Map::new(name.clone()); assert_eq!(m.add_raster("dem", copy), 1); @@ -3049,6 +3051,7 @@ fn a_reliefs_shadow_softness_changes_its_page_only_off_the_default() { ambient: None, balanced: false, shadow_softness: softness, + aerial: 0.0, }; let mut m = Map::new("m"); assert_eq!(m.add_raster("dem", rl), 1); diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index ef4ed7823c..4c680c7da0 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -12309,6 +12309,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; }; let camera = Camera { center: lonlat_to_merc(Coord::new(0.0, 0.0)), res: 1200.0, bearing: 0.0, pitch: 0.0 }; @@ -12347,6 +12348,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; // Drawn only zoomed out past a scale between the centre's and the far band's. l.max_scale = Some((centre + farthest) / 2.0); @@ -12396,6 +12398,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; // Drawn only zoomed in past 1:1: or in a group drawn only there. if grouped { diff --git a/crates/render/src/raster.rs b/crates/render/src/raster.rs index 516fe90b18..04d3fb7684 100644 --- a/crates/render/src/raster.rs +++ b/crates/render/src/raster.rs @@ -1139,6 +1139,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, } } @@ -1236,6 +1237,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; style.resampling = Resampling::Nearest; let mut pm = Pixmap::new(1, 1).unwrap(); @@ -1412,6 +1414,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; style.resampling = Resampling::Nearest; let view = View { @@ -1778,6 +1781,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; style.resampling = resampling; let view = View { diff --git a/crates/render/src/raster_export.rs b/crates/render/src/raster_export.rs index 3e0c768f73..3148f8dda2 100644 --- a/crates/render/src/raster_export.rs +++ b/crates/render/src/raster_export.rs @@ -266,6 +266,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; let e = export(&layer, 2, 2, 1.0).err().unwrap_or_else(|| panic!("an archive without its shadows")); assert!(e.contains("cast shadows") && e.contains("read in place"), "{e}"); @@ -301,6 +302,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; let _cancelled = rustgis_data::progress::install(Some(std::sync::Arc::new(Cancelled))); let e = export(&layer, 0, 0, 1.0).err().unwrap_or_else(|| panic!("an archive without its shadows")); diff --git a/crates/render/src/terrain/water.rs b/crates/render/src/terrain/water.rs index 98c94cd61f..6c31e79334 100644 --- a/crates/render/src/terrain/water.rs +++ b/crates/render/src/terrain/water.rs @@ -1392,6 +1392,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; }; let camera = rustgis_map::Camera { center: lonlat_to_merc(Coord::new(0.35, -0.05)), res: 300.0, bearing: 0.0, pitch: 0.0 }; @@ -1448,6 +1449,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; camera.center = lonlat_to_merc(Coord::new(0.35, -0.3)); let view = crate::View { width: 300.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; @@ -1500,6 +1502,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; let camera = rustgis_map::Camera { center: lonlat_to_merc(Coord::new(0.25, -0.1)), res: 300.0, bearing: 0.0, pitch: 0.0 }; let view = crate::View { width: 200.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; @@ -1535,6 +1538,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; let camera = rustgis_map::Camera { center: lonlat_to_merc(Coord::new(0.35, -0.05)), res: 300.0, bearing: 0.0, pitch: 0.0 }; let view = crate::View { width: 200.0, height: 200.0, pixels_per_point: 1.0, camera, sky: false }; @@ -1581,6 +1585,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; m.view.terrain.as_mut().unwrap().water.as_mut().unwrap().glint = glint; camera.center = lonlat_to_merc(Coord::new(0.35, -0.3)); diff --git a/crates/ui-egui/src/mapview.rs b/crates/ui-egui/src/mapview.rs index dddaf738d0..bee7c00fbe 100644 --- a/crates/ui-egui/src/mapview.rs +++ b/crates/ui-egui/src/mapview.rs @@ -5822,6 +5822,7 @@ mod tests { ambient: None, balanced: false, shadow_softness: None, + aerial: 0.0, }; let set = |map: &mut Map, shadow: f64, visible: bool| { let l = map.layer_mut(id).unwrap(); diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index aa1b9ca889..54b2064242 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -7527,6 +7527,8 @@ fn raster_symbology(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { ambient, balanced, shadow_softness, + // Not in this pane yet (#3141). + aerial: _, } => { use rustgis_engine::map::shadow::{SOFT_EDGE_MAX, SOFT_EDGE_MIN, soft_edge}; let (az, alt, zf, sh, sk, fl) = (*azimuth, *altitude, *z_factor, *shadow, *sky, *fill); diff --git a/docs/maintaining.md b/docs/maintaining.md index f8e490dde3..96fb73b1d1 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -119,6 +119,8 @@ diagram cut to a circle or to polygons), `terrain-block-polygon` (one cut to pol `shaded-relief-light` (a Shaded Relief's ambient or balanced light), `shaded-relief-shadow-softness` (a softness stored for a Shaded Relief's cast shadows, or set by a saved step; `raster.symbology` stores the default as none), +`shaded-relief-aerial` (a Shaded Relief's aerial perspective, stored or set by a saved step; 0 and +`null` are none), `blank-paper` (a blank basemap's paper colour) and `normalized-legend-urls` for normalized legend addresses. Project requirements include offline map snapshots. `table-export-order` protects saved task commands, diff --git a/schema/fixtures/shaded_relief_aerial.rgis b/schema/fixtures/shaded_relief_aerial.rgis new file mode 100644 index 0000000000..4138d66114 --- /dev/null +++ b/schema/fixtures/shaded_relief_aerial.rgis @@ -0,0 +1 @@ +{"requires":["shaded-relief-aerial"],"format":2,"name":"Shaded relief aerial perspective","maps":[{"name":"Map","layers":[{"id":1,"name":"dem","visible":true,"dataset":{"name":"dem","geometry_type":"None","fields":[],"features":[],"source":null},"renderer":{"Single":{"fill":[224,123,57,170],"stroke":[134,74,34,255],"stroke_width":0.7,"size":0.0,"marker":"Circle"}},"labels":{"enabled":false,"field":"","size":10.0,"color":[43,43,43,255],"halo":true,"bold":false,"min_scale":null,"max_scale":null,"closest":null,"farthest":null,"placement":"Auto"},"transparency":0.0,"min_scale":null,"max_scale":null,"definition_query":null,"selection":[],"expanded":true,"selectable":false,"graphics":false,"blend":"Normal","mask":null,"display_filters":[],"aggregation":null,"extrusion":null,"priority":0,"raster":{"path":"dem.tif","file_name":null,"style":{"renderer":{"ShadedRelief":{"band":0,"ramp":"Terrain","azimuth":315.0,"altitude":45.0,"z_factor":2.0,"aerial":0.6}},"stretch":{"PercentClip":{"low":0.5,"high":0.5}},"dra":false,"resampling":"Nearest","brightness":0.0,"contrast":0.0,"gamma":1.0,"rotation":0.0}},"geometry_rev":0}],"basemap":"Paper","camera":{"center":{"x":-4654314.293187932,"y":-2329385.2820357964},"res":64.69445270508425,"bearing":0.0,"pitch":0.0},"bookmarks":[],"graticule":true,"projection":"WebMercator","popup_mode":"Topmost","label_display":{"paused":false,"locked":false,"show_unplaced":false},"editing":{"relationships":[],"snapping":{"enabled":false,"tolerance_px":10.0,"vertex":true,"edge":true,"end":true},"topology":[],"settings":{"enabled":true,"show_vertices":true,"elevation":"None","ground_to_grid":null}},"swipe":null,"view":{"effects":{"stars":true,"sun_time":null,"weather":"None","vignette":false,"edges":false,"tilt_shift":false,"grain":false,"brightness":0.0,"contrast":0.0,"saturation":0.0,"sepia":0.0},"wireframe":false,"clip_distance":null,"terrain":null},"animation":[],"next_layer_id":2}],"active_map":0,"tables":[],"next_table_id":1,"gp_history":[],"layouts":[],"folders":[],"styles":[],"ramps":[],"tasks":[],"gp_env":{"extent":null,"cell_size":null},"models":[],"workspaces":[],"link_views":false,"jobs":[],"offline_areas":[],"reports":[],"presentations":[],"notebooks":[],"dl_models":[],"shared":[],"data_stores":[]} diff --git a/schema/project.schema.json b/schema/project.schema.json index 480dd51cb4..84337af985 100644 --- a/schema/project.schema.json +++ b/schema/project.schema.json @@ -1293,7 +1293,8 @@ "layout-text-statistics", "layout-publication-parts", "layout-publication-legend", - "shaded-relief-shadow-softness" + "shaded-relief-shadow-softness", + "shaded-relief-aerial" ], "type": "string" }, @@ -1415,7 +1416,8 @@ "layout-text-statistics", "layout-publication-parts", "layout-publication-legend", - "shaded-relief-shadow-softness" + "shaded-relief-shadow-softness", + "shaded-relief-aerial" ], "x-rustgis-reads-format": 2, "x-rustgis-writes-format": 2 From 0ab3e386db37d1b5a017c214bcc5b2a2664c394e Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 16:47:14 -0300 Subject: [PATCH 2/2] fix(engine): a saved aerial perspective is read as the command reads it (#3141) raster.symbology reads "aerial" through the commands' number parser, which takes a numeric string, so "0" sets none; the capability check read the raw JSON number, so a saved step with "aerial": "0" still required shaded-relief-aerial and an older RustGIS would refuse a file that needs none. The check now uses the same parser. The capability samples and a saved script's step cover "0" and "0.5"; both failed before. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/project_capabilities.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index c622e4145d..2f4e2529fd 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -229,10 +229,11 @@ fn command_sets_shadow_softness(id: &str, params: &Value) -> bool { /// A saved `raster.symbology` that sets a Shaded Relief's aerial perspective (#3141): an older reader /// runs it without the key, saying nothing, so the low ground would be drawn without its haze. -/// `null` and 0 ask for none, which an older reader draws. +/// Read as the command reads it (a numeric string is its number): `null` and 0 ask for none, +/// which an older reader draws. fn command_sets_aerial(id: &str, params: &Value) -> bool { match id { - "raster.symbology" => params.get("aerial").is_some_and(|v| !v.is_null() && v.as_f64() != Some(0.0)), + "raster.symbology" => crate::cmd::f(params, "aerial").is_some_and(|v| v != 0.0), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_sets_aerial)), _ => false, } @@ -1627,6 +1628,9 @@ mod tests { // null and 0 are the none an older reader draws. (&[], "raster.symbology", json!({"type": "shadedRelief", "aerial": null})), (&[], "raster.symbology", json!({"type": "shadedRelief", "aerial": 0})), + // Read as the command reads it: a numeric string is its number, so "0" is none too. + (&[], "raster.symbology", json!({"type": "shadedRelief", "aerial": "0"})), + (&["shaded-relief-aerial"], "raster.symbology", json!({"type": "shadedRelief", "aerial": "0.5"})), // An SVG image is a picture marker as well, in a node as in a saved command. (&["picture-markers", "svg-markers"], "layer.marker", json!({"image": svg})), (&[], "buffer", json!({"input": "Cities", "distance": 1})), @@ -1646,6 +1650,11 @@ mod tests { } let missing: Vec<&str> = COMMAND_CAPABILITIES.iter().map(|&(name, _)| name).filter(|name| !covered.contains(name)).collect(); assert!(missing.is_empty(), "add a sample for each command capability, missing: {missing:?}"); + // A saved script's step is read as the command reads it too. + for (aerial, needs) in [("0", false), ("0.5", true)] { + let script = format!(r#"raster.symbology {{"type": "shadedRelief", "aerial": "{aerial}"}}"#); + assert_eq!(command_sets_aerial("script.run", &json!({ "script": script })), needs, "{aerial}"); + } } /// An inline graph is read as the run reads it: only an object with `nodes`, only nodes of kind