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
9 changes: 5 additions & 4 deletions crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,10 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
}
_ => (0.0, 0.0, 0.0, None, None, false, None, 0.0),
};
// And its terrain type, likewise.
let cur_terrain = match rl.style.renderer {
RasterRenderer::ShadedRelief { terrain_type, .. } => terrain_type,
_ => 0.0,
// And its terrain type and flat ground's detail, likewise.
let (cur_terrain, cur_flats) = match rl.style.renderer {
RasterRenderer::ShadedRelief { terrain_type, flats, .. } => (terrain_type, flats),
_ => (0.0, None),
};
RasterRenderer::ShadedRelief {
band: bi,
Expand Down Expand Up @@ -1085,6 +1085,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
Some(Value::Null) => 0.0,
Some(_) => relief_terrain_type(f(p, "terrain"))?,
},
flats: cur_flats,
}
}
other => return Err(format!("unknown raster symbology '{other}'")),
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 @@ -139,6 +139,7 @@ pub const CAPABILITIES: &[&str] = &[
"shaded-relief-shadow-softness",
"shaded-relief-aerial",
"shaded-relief-terrain-type",
"shaded-relief-flats",
];
/// 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
17 changes: 15 additions & 2 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,13 @@ fn sets_terrain_type(layer: &Layer) -> bool {
.is_some_and(|r| matches!(&r.style.renderer, RasterRenderer::ShadedRelief { terrain_type, .. } if *terrain_type != 0.0))
}

/// Whether a layer sets its Shaded Relief's detail on flat ground (#3143): an older reader would
/// draw flat ground even and save the layer again without the setting (even at an amount of 0,
/// which draws none but is saved, with its size).
fn sets_flats(layer: &Layer) -> bool {
layer.raster.as_ref().is_some_and(|r| matches!(&r.style.renderer, RasterRenderer::ShadedRelief { flats: Some(_), .. }))
}

/// 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 @@ -1146,6 +1153,7 @@ fn layer_capabilities(layer: &Layer) -> Vec<&'static str> {
(sets_shadow_softness(layer), "shaded-relief-shadow-softness"),
(sets_aerial(layer), "shaded-relief-aerial"),
(sets_terrain_type(layer), "shaded-relief-terrain-type"),
(sets_flats(layer), "shaded-relief-flats"),
(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 @@ -1182,7 +1190,8 @@ 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, mut aerial, mut terrain_type) = (false, false, false, false, false, false);
let (mut crisp, mut bed, mut walls_top, mut soft, mut aerial, mut terrain_type, mut flats) =
(false, false, 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 @@ -1222,6 +1231,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
soft |= map.layers.iter().any(sets_shadow_softness);
aerial |= map.layers.iter().any(sets_aerial);
terrain_type |= map.layers.iter().any(sets_terrain_type);
flats |= map.layers.iter().any(sets_flats);
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 @@ -1246,6 +1256,7 @@ fn map_capabilities<'a>(maps: impl IntoIterator<Item = &'a Map>) -> Vec<&'static
(soft, "shaded-relief-shadow-softness"),
(aerial, "shaded-relief-aerial"),
(terrain_type, "shaded-relief-terrain-type"),
(flats, "shaded-relief-flats"),
(legends, "normalized-legend-urls"),
(sized, super::SIZE_BY_CAPABILITY),
(pictures, "picture-markers"),
Expand Down Expand Up @@ -3501,10 +3512,11 @@ mod tests {
}))
.unwrap();
let clean = Layer::new_raster(1, "DEM", raster);
for field in ["shadow", "sky", "fill", "rock", "ambient", "balanced", "shadow_softness", "aerial", "terrain_type"] {
for field in ["shadow", "sky", "fill", "rock", "ambient", "balanced", "shadow_softness", "aerial", "terrain_type", "flats"] {
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(),
"flats" => json!({"amount": 0.0, "size": 6}),
"balanced" => json!(true),
_ => json!(0.4),
};
Expand All @@ -3521,6 +3533,7 @@ mod tests {
"shadow_softness" => "shaded-relief-shadow-softness",
"aerial" => "shaded-relief-aerial",
"terrain_type" => "shaded-relief-terrain-type",
"flats" => "shaded-relief-flats",
_ => "shaded-relief-effects",
};
assert_eq!(document["requires"], json!([needs]), "{field}: {document}");
Expand Down
25 changes: 25 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8060,6 +8060,31 @@ fn shaded_relief_keeps_its_terrain_type() {
assert_eq!(serde_json::to_value(&fixture).unwrap()["requires"], json!(["shaded-relief-terrain-type"]));
}

/// Shaded Relief's detail on flat ground (#3143) reads back from a project, required by name; a
/// new relief has none, and one set is kept when the relief is styled again, and saved.
#[test]
fn shaded_relief_keeps_its_flat_ground_detail() {
let fixture = Project::from_json(include_str!("../../../schema/fixtures/shaded_relief_flats.rgis")).unwrap();
assert_eq!(serde_json::to_value(&fixture).unwrap()["requires"], json!(["shaded-relief-flats"]));
let flats = |s: &Session| match s.map().layers.iter().find_map(|l| l.raster.as_ref()).map(|r| &r.style.renderer) {
Some(rustgis_map::RasterRenderer::ShadedRelief { flats, .. }) => *flats,
other => panic!("{other:?}"),
};
assert_eq!(flats(&Session::new(fixture)), Some(rustgis_map::raster::Flats::DEFAULT));
let mut s = with_raster();
s.run("raster.symbology", &json!({"type": "shadedRelief"})).unwrap();
assert_eq!(flats(&s), None, "none by default");
if let Some(rustgis_map::RasterRenderer::ShadedRelief { flats, .. }) =
s.map_mut().layers.iter_mut().find_map(|l| l.raster.as_mut()).map(|r| &mut r.style.renderer)
{
*flats = Some(rustgis_map::raster::Flats::DEFAULT);
}
s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 200, "shadow": 0.5})).unwrap();
assert_eq!(flats(&s), Some(rustgis_map::raster::Flats::DEFAULT), "kept");
let text = s.project.saved_text(None).unwrap().0;
assert!(text.contains(r#""flats":{"amount":1.0,"size":6}"#) && text.contains("shaded-relief-flats"), "{text}");
}

/// 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
1 change: 1 addition & 0 deletions crates/map/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ fn a_balanced_reliefs_colour_bar_is_its_flat_ground() {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
let Some(ColorScale::Ramp { lo, hi, colors }) = color_scale(&rl) else { panic!("a ramp") };
let colorizer = Colorizer::new(&rl.style, &r, None);
Expand Down
1 change: 1 addition & 0 deletions crates/map/src/qgis/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn renderer_style(node: Node, pipe: Node, r: Node, kind: &str, layer: &str, repo
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
(renderer, Stretch::None)
}
Expand Down
1 change: 1 addition & 0 deletions crates/map/src/qgis/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ fn raster_renderers_read_as_rustgis_raster_styles() {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
}
);
assert!(report.notes.iter().any(|n| n.text.contains("saturation")));
Expand Down
57 changes: 57 additions & 0 deletions crates/map/src/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,36 @@ impl Rock {
}
}

/// Shaded Relief's detail on flat ground (#3143): each cell's height above the mean height within
/// `size` cells around it (a square `2 × size + 1` cells a side, without its no data) moves its
/// direct light by [`Flats::shift`], on flat ground only.
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Flats {
pub amount: f64,
pub size: u32,
}

impl Flats {
/// What it is turned on without settings: all of it, over 6 cells, which drew a 90 m DEM's
/// plateau cleanly where 3 was grainy.
pub const DEFAULT: Flats = Flats { amount: 1.0, size: 6 };
/// The widest neighbourhood, in cells.
pub const MAX_SIZE: u32 = 32;
/// The slope, in degrees, from which ground isn't flat: its light is left as it was.
pub const SLOPE: f64 = 4.0;

/// How far (0–255) the direct light of a cell on a slope of `degrees` (the ground's own, see
/// [`slope`]) moves when it stands `above` its neighbourhood's mean (in the DEM's units):
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
/// `amount` (0 to 1) × a weight of 1 on flat ground fading to 0 at [`Flats::SLOPE`] × `40 ×
/// above` kept within ±80, so a metre up brightens by 40 and a channel darkens. 0 when that
/// isn't a number.
pub fn shift(&self, degrees: f64, above: f64) -> f64 {
let w = (1.0 - degrees / Self::SLOPE).clamp(0.0, 1.0);
let shift = self.amount.clamp(0.0, 1.0) * w * (40.0 * above).clamp(-80.0, 80.0);
if shift.is_nan() { 0.0 } else { shift }
}
}

/// The share of the colour a Shaded Relief slope turned fully from the sun keeps: its `ambient`
/// when set, or else 0.25 (the light of old), and 0.5 with the `balanced` light, which keeps about
/// the old light's contrast (#1977).
Expand Down Expand Up @@ -1751,6 +1781,8 @@ pub enum RasterRenderer {
/// [`relief_hillshade`]): above 0 gentle relief shows, below 0 steep ground stands out; 0, the
/// default, lights as before. Cast shadows, the sky, the fill light and rock keep the ground's
/// own slope.
/// `flats` brings out flat ground's detail (#3143, see [`Flats`]); none, the default, lights it
/// as before.
ShadedRelief {
band: usize,
ramp: Option<Ramp>,
Expand All @@ -1775,6 +1807,8 @@ pub enum RasterRenderer {
aerial: f64,
#[serde(default, skip_serializing_if = "is_no_light")]
terrain_type: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
flats: Option<Flats>,
},
}

Expand Down Expand Up @@ -2847,6 +2881,7 @@ mod tests {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
}
}

Expand Down Expand Up @@ -3440,6 +3475,7 @@ mod tests {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
let (mut rocky, mut plain) = (RasterStyle::default_for(&r), RasterStyle::default_for(&r));
(rocky.renderer, plain.renderer) = (relief(Some(rock)), relief(None));
Expand Down Expand Up @@ -3498,6 +3534,7 @@ mod tests {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
let relief = |altitude, ambient, balanced| tinted_relief(None, altitude, ambient, balanced);
// Grey relief: white shaded, so a channel is 255 times the light.
Expand Down Expand Up @@ -3587,6 +3624,7 @@ mod tests {
shadow_softness: None,
aerial,
terrain_type: 0.0,
flats: None,
}
}

Expand Down Expand Up @@ -3790,6 +3828,25 @@ mod tests {
assert!(hillshade(&wall, 50.0, 50.0, 270.0, 45.0, 2.0) > 0, "lit without a terrain type");
}

/// Flat ground's detail (#3143): a metre above the neighbourhood brightens by 40 at full
/// amount, kept within ±80, scaled by the amount and by a weight fading from flat ground to
/// none at 4°; past that, and for what isn't a number, nothing moves. A project from before it
/// reads as none and saves as it was.
#[test]
fn flat_ground_detail_moves_the_light_by_height_on_flat_ground_only() {
let shift = |amount, degrees, above| Flats { amount, size: 6 }.shift(degrees, above);
let flat = [shift(1.0, 0.0, 1.0), shift(1.0, 0.0, -0.5), shift(1.0, 0.0, 3.0), shift(1.0, 0.0, -9.0), shift(0.5, 0.0, 1.0)];
assert_eq!(flat, [40.0, -20.0, 80.0, -80.0, 20.0]);
let sloped = [shift(1.0, 1.0, 1.0), shift(1.0, 2.0, -1.0), shift(1.0, 4.0, 1.0), shift(1.0, 30.0, -5.0)];
assert_eq!(sloped, [30.0, -20.0, 0.0, 0.0]);
let odd = [shift(0.0, 0.0, 1.0), shift(2.0, 0.0, 1.0), shift(1.0, 0.0, f64::NAN), shift(1.0, f64::NAN, 1.0)];
assert_eq!(odd, [0.0, 40.0, 0.0, 0.0]);
let before = r#"{"ShadedRelief":{"band":0,"ramp":"Terrain","azimuth":315.0,"altitude":45.0,"z_factor":2.0}}"#;
let read: RasterRenderer = serde_json::from_str(before).unwrap();
assert!(matches!(read, RasterRenderer::ShadedRelief { flats: None, .. }));
assert_eq!(serde_json::to_string(&read).unwrap(), before);
}

/// 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 };
Expand Down
5 changes: 5 additions & 0 deletions crates/render/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,7 @@ fn a_page_says_why_a_frame_drew_no_cast_shadows() {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
for shadow in [0.0, 1.0] {
let map = ramp_map(relief(shadow), rustgis_map::raster::Stretch::MinMax);
Expand Down Expand Up @@ -3075,6 +3076,7 @@ fn page_settles_with_suns(suns: &[f64]) {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
let mut m = Map::new(name.clone());
assert_eq!(m.add_raster("dem", copy), 1);
Expand Down Expand Up @@ -3149,6 +3151,7 @@ fn a_reliefs_shadow_softness_changes_its_page_only_off_the_default() {
shadow_softness: softness,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
let mut m = Map::new("m");
assert_eq!(m.add_raster("dem", rl), 1);
Expand Down Expand Up @@ -3205,6 +3208,7 @@ fn a_reliefs_aerial_perspective_lightens_its_page() {
shadow_softness: None,
aerial,
terrain_type: 0.0,
flats: None,
};
let map = ramp_map(relief, rustgis_map::raster::Stretch::MinMax);
colorbar_page(&map, Orientation::Horizontal, false, "").data().iter().map(|c| u64::from(*c)).sum::<u64>()
Expand Down Expand Up @@ -3241,6 +3245,7 @@ fn a_reliefs_terrain_type_changes_its_frames_contrast_not_its_colour_bar() {
shadow_softness: None,
aerial: 0.0,
terrain_type,
flats: None,
};
let mut m = Map::new("m");
assert_eq!(m.add_raster("dem", rl), 1);
Expand Down
3 changes: 3 additions & 0 deletions crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12638,6 +12638,7 @@ mod tests {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
};
let camera = Camera { center: lonlat_to_merc(Coord::new(0.0, 0.0)), res: 1200.0, bearing: 0.0, pitch: 0.0 };
Expand Down Expand Up @@ -12678,6 +12679,7 @@ mod tests {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
// Drawn only zoomed out past a scale between the centre's and the far band's.
l.max_scale = Some((centre + farthest) / 2.0);
Expand Down Expand Up @@ -12729,6 +12731,7 @@ mod tests {
shadow_softness: None,
aerial: 0.0,
terrain_type: 0.0,
flats: None,
};
// Drawn only zoomed in past 1:1: or in a group drawn only there.
if grouped {
Expand Down
Loading
Loading