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
11 changes: 6 additions & 5 deletions crates/engine/src/cmd/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn commands() -> Vec<C> {
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|.geojson|.csv|.zip|.kml|.kmz|.gpx|.parquet|.fgb|.gpkg|.mbtiles|.jp2|.j2k (JPEG 2000: desktop, through GDAL)", "data": base64, "zoom"?: bool, "layer"?: "one layer of a zip, KML or GPX by name", "name"?: text, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "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), "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), "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 @@ -778,10 +778,10 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
RasterRenderer::Colormap
}
"shadedrelief" | "hillshade" => {
// Left out, a Shaded Relief keeps its own shadows; a new one casts none.
let cur_shadow = match rl.style.renderer {
RasterRenderer::ShadedRelief { shadow, .. } => shadow,
_ => 0.0,
// Left out, a Shaded Relief keeps its own shadows and sky; a new one has neither.
let (cur_shadow, cur_sky) = match rl.style.renderer {
RasterRenderer::ShadedRelief { shadow, sky, .. } => (shadow, sky),
_ => (0.0, 0.0),
};
RasterRenderer::ShadedRelief {
band: bi,
Expand All @@ -790,6 +790,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
altitude: f(p, "altitude").unwrap_or(45.0).clamp(0.0, 90.0),
z_factor: f(p, "zFactor").unwrap_or(1.0).clamp(0.001, 1000.0),
shadow: f(p, "shadow").unwrap_or(cur_shadow).clamp(0.0, 1.0),
sky: f(p, "sky").unwrap_or(cur_sky).clamp(0.0, 1.0),
}
}
other => return Err(format!("unknown raster symbology '{other}'")),
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub(crate) fn shadow_warnings(what: &str, said: &rustgis_render::ShadowsSaid) ->
let mut out: Vec<String> = said.notices.iter().map(|e| format!("{what}: {e}")).collect();
if said.to_come {
out.push(format!(
"{what}: made without the cast shadows still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again"
"{what}: made without the cast shadows or sky visibility still being worked out, as the window doesn't wait for them: once the map shows them, draw or export it again"
));
}
out
Expand Down
58 changes: 58 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3576,6 +3576,64 @@ fn shaded_relief_keeps_its_cast_shadows() {
assert_eq!(shadow(&s), 0.0, "a new Shaded Relief casts none");
}

/// Shaded Relief's sky visibility is set, clamped, kept when left out (the shadows' too), saved
/// with the project only when there is some, and drawn in an export (waited for) with nothing to
/// say.
#[test]
fn shaded_relief_keeps_its_sky_visibility() {
let mut s = with_raster();
let light = |s: &Session| match s.map().layers.first().and_then(|l| l.raster.as_ref()).map(|r| &r.style.renderer) {
Some(rustgis_map::RasterRenderer::ShadedRelief { shadow, sky, .. }) => (*shadow, *sky),
other => panic!("{other:?}"),
};
s.run("raster.symbology", &json!({"type": "shadedRelief"})).unwrap();
assert_eq!(light(&s), (0.0, 0.0));
let skies_saved = |s: &mut Session| s.project.saved_text(None).unwrap().0.matches("\"sky\"").count();
let before = skies_saved(&mut s);
s.run("raster.symbology", &json!({"type": "shadedRelief", "sky": 0.6, "shadow": 0.5})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 200})).unwrap();
assert_eq!(light(&s), (0.5, 0.6), "left out, kept");
s.run("raster.symbology", &json!({"type": "shadedRelief", "sky": -3})).unwrap();
assert_eq!(light(&s), (0.5, 0.0));
assert_eq!(skies_saved(&mut s), before, "saved only when there is some");
s.run("raster.symbology", &json!({"type": "shadedRelief", "sky": 4})).unwrap();
assert_eq!(light(&s), (0.5, 1.0));
assert_eq!(skies_saved(&mut s), before + 1);
s.notices.clear();
assert!(crate::cmd::share::map_png(&mut s, Some(300.0), Some(200.0), None).unwrap().len() > 1000);
assert!(s.notices.iter().all(|n| !n.text.contains("sky")), "{:?}", s.notices);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// An export draws the sky a pit hides: its floor darker with `sky` 1 than with 0, the ground
/// around it (which sees the whole sky) the same. The first export with the sky, from a cold
/// cache, waits for the grid: it is the same image as one drawn once the grid is kept.
#[test]
fn an_export_draws_the_sky_a_pit_hides_and_waits_for_it() {
use rustgis_data::raster::{ColorHint, Crs, GeoTransform, Raster, geotiff};
let mut s = world();
// A round pit 16 cells across and 400 m deep, in flat ground 41 cells of 30 m a side.
let pit: Vec<f32> =
(0..41 * 41).map(|i| if f64::from(i % 41 - 20).hypot(f64::from(i / 41 - 20)) <= 8.0 { -400.0 } else { 0.0 }).collect();
let gt = GeoTransform { origin_x: 500_000.0, origin_y: 7_000_000.0, pixel_width: 30.0, pixel_height: -30.0 };
let r = Raster::new("pit", 41, 41, vec![pit], gt, Crs::from_epsg(31983).unwrap(), None, ColorHint::Gray, "Float32").unwrap();
let bytes = geotiff::write(&r, 0).unwrap();
crate::cmd::raster::add_raster(&mut s, "pit", rustgis_map::RasterLayer::from_bytes(r, "pit.tif".to_string(), bytes)).unwrap();
s.run("layer.zoomTo", &json!({"layer": "pit"})).unwrap();
let drawn = |s: &mut Session, sky: f64| {
s.run("raster.symbology", &json!({"layer": "pit", "type": "shadedRelief", "grey": true, "sky": sky})).unwrap();
let png = crate::cmd::share::map_png(s, Some(200.0), Some(200.0), None).unwrap();
crate::render::decode_png(&png).unwrap()
};
let open = drawn(&mut s, 0.0);
let cold = drawn(&mut s, 1.0);
let ready = drawn(&mut s, 1.0);
assert!(cold.data() == ready.data(), "the first export waited for the sky");
let red = |p: &tiny_skia::Pixmap, x: u32, y: u32| p.pixel(x, y).unwrap().red();
// The pit's floor, in the middle, sees less sky; flat ground well outside it, all of it.
assert!(red(&cold, 100, 100) < red(&open, 100, 100), "{} vs {}", red(&cold, 100, 100), red(&open, 100, 100));
assert_eq!(red(&cold, 170, 100), red(&open, 170, 100));
}

/// A map export that draws a layer without its cast shadows says so, as such (not as a tile layer
/// that failed), in a PNG, a GeoTIFF and the app's map image alike; one with nothing missing says
/// nothing.
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 @@ -95,6 +95,7 @@ fn renderer_style(node: Node, pipe: Node, r: Node, kind: &str, layer: &str, repo
altitude: num("angle", 45.0).clamp(0.0, 90.0),
z_factor: num("zfactor", 1.0).clamp(0.0001, 10_000.0),
shadow: 0.0,
sky: 0.0,
};
(renderer, Stretch::None)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/map/src/qgis/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ fn raster_renderers_read_as_rustgis_raster_styles() {
);
assert_eq!(
s.raster.unwrap().renderer,
RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 300.0, altitude: 35.0, z_factor: 2.0, shadow: 0.0 }
RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 300.0, altitude: 35.0, z_factor: 2.0, shadow: 0.0, sky: 0.0 }
);
assert!(report.notes.iter().any(|n| n.text.contains("saturation")));
let (s, report) = read(r#"<rasterrenderer type="contour" band="1"/>"#, "");
Expand Down
Loading
Loading