Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crates/engine/command-surface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn commands() -> Vec<C> {
C::new("layer.addDataBytes", "Add Data (from file contents)", "Home › Add", add_bytes::<MAX_UPLOAD_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)
Expand Down Expand Up @@ -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,
Expand All @@ -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}'")),
Expand All @@ -1093,6 +1100,15 @@ fn relief_shadow_softness(v: Option<f64>) -> Result<Option<f64>, 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<f64>) -> Result<f64, String> {
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<Rock>) -> Result<Option<Rock>, String> {
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,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.
Expand Down
39 changes: 37 additions & 2 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,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
Expand Down Expand Up @@ -227,6 +228,18 @@ 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.
/// 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" => crate::cmd::f(params, "aerial").is_some_and(|v| v != 0.0),
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
"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.
Expand Down Expand Up @@ -1025,6 +1038,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 {
Expand Down Expand Up @@ -1068,6 +1087,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"),
Expand Down Expand Up @@ -1104,7 +1124,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> 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 walls_top, mut soft) = (false, false, false, false);
let (mut crisp, mut bed, mut walls_top, mut soft, mut aerial) = (false, 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.
Expand Down Expand Up @@ -1142,6 +1162,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> 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));
Expand All @@ -1164,6 +1185,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> 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"),
Expand Down Expand Up @@ -1653,6 +1675,13 @@ 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})),
// 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})),
Expand All @@ -1672,6 +1701,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}");
}
}

/// `report.existingTemplate` imports a layout file as `layout.import` does, so a step of it
Expand Down Expand Up @@ -3355,7 +3389,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(),
Expand All @@ -3373,6 +3407,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}");
Expand Down
50 changes: 50 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7896,6 +7896,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.
Expand Down
2 changes: 1 addition & 1 deletion crates/map/src/layout/colorbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn color_scale(rl: &RasterLayer) -> Option<ColorScale> {
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);
Expand Down
1 change: 1 addition & 0 deletions crates/map/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading