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
12 changes: 10 additions & 2 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4047,7 +4047,7 @@ fn layout_bytes_inner(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool
// The map frames a PDF can't georeference, and those it georeferences by their corners only,
// each with the page (from 1) it is on.
let (mut left_out, mut by_corners): (Vec<(usize, u32, String, &'static str)>, Vec<(usize, u32, String)>) = (Vec::new(), Vec::new());
let mut render_pages = || -> Result<(Vec<rustgis_render::pdf::Page>, Option<Vec<u8>>), String> {
let render_pages = || -> Result<(Vec<rustgis_render::pdf::Page>, Option<Vec<u8>>), String> {
let mut out = Vec::new();
let mut png = None;
let mut held = 0usize;
Expand Down Expand Up @@ -4107,7 +4107,15 @@ fn layout_bytes_inner(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool
}
Ok((out, png))
};
let result = render_pages();
// Layers its frames drew without their cast shadows (read in place, too large for the
// browser, no memory, still being worked out on the interface's thread): said, as the map's
// export says them.
let (result, said) = rustgis_render::shadows_said(render_pages);
if result.is_ok() {
for w in super::share::shadow_warnings("The layout", &said) {
s.warn(w);
}
}
// Stepping through the series moved the frame; put the layout back however that went.
if stepping && let Some(cur) = s.project.layouts.get_mut(li) {
*cur = saved.clone();
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ pub fn export_bytes(s: &mut Session, i: usize, format: &str, dpi: f64) -> Result
}))
};
let before = revision(s);
let result = export_bytes_inner(s, i, format, dpi);
let result = super::share::saying_shadows(s, "The presentation", |s| export_bytes_inner(s, i, format, dpi));
before.finish_revision(revision(s), Some(result), "could not draw the presentation")?
}

Expand Down
24 changes: 16 additions & 8 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"?, "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), "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 @@ -777,13 +777,21 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
}
RasterRenderer::Colormap
}
"shadedrelief" | "hillshade" => RasterRenderer::ShadedRelief {
band: bi,
ramp: ramp.or(Some(Ramp::Terrain)).filter(|_| !b(p, "grey").unwrap_or(false)),
azimuth: f(p, "azimuth").unwrap_or(315.0).rem_euclid(360.0),
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),
},
"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,
};
RasterRenderer::ShadedRelief {
band: bi,
ramp: ramp.or(Some(Ramp::Terrain)).filter(|_| !b(p, "grey").unwrap_or(false)),
azimuth: f(p, "azimuth").unwrap_or(315.0).rem_euclid(360.0),
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),
}
}
other => return Err(format!("unknown raster symbology '{other}'")),
};
if matches!(rl.style.renderer, RasterRenderer::Colormap | RasterRenderer::UniqueValues { .. } | RasterRenderer::Classify { .. }) {
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ fn drawn_bytes(s: &mut Session, i: usize, dpi: f64, pdf: bool) -> Result<(Vec<u8
.unwrap_or_default()
};
let before = revision(s);
let result = report_pages_bytes(s, &doc, &pages, dpi, pdf);
let result = super::share::saying_shadows(s, "The report", |s| report_pages_bytes(s, &doc, &pages, dpi, pdf));
before.finish_revision(revision(s), Some(result), "could not draw the report")?
}

Expand Down
49 changes: 43 additions & 6 deletions crates/engine/src/cmd/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,38 @@ pub fn map_png_fitted(s: &mut Session, dpi: f64) -> Result<Vec<u8>, String> {
Ok(png)
}

/// Renders the active map at the current view (or a requested size) to PNG bytes.
/// Renders the active map at the current view (or a requested size) to PNG bytes. Layers drawn
/// without their cast shadows are said ([`saying_shadows`]).
pub fn map_png(s: &mut Session, width: Option<f64>, height: Option<f64>, dpi: Option<f64>) -> Result<Vec<u8>, String> {
let view_size = s.view_size;
png_of(s.map_mut(), view_size, width, height, dpi)
saying_shadows(s, "The map image", |s| png_of(s.map_mut(), view_size, width, height, dpi))
}

/// Runs `draw`, which draws maps or pages for `s` (an image, an export, a shared item), and says
/// as warnings about `what` what its frames said of their cast shadows once it drew (see
/// [`shadow_warnings`]). Whatever the engine draws for someone goes through it, or through
/// [`rustgis_render::shadows_said`] off the session (Never silent).
pub(crate) fn saying_shadows<T>(s: &mut Session, what: &str, draw: impl FnOnce(&mut Session) -> Result<T, String>) -> Result<T, String> {
let (drawn, said) = rustgis_render::shadows_said(|| draw(s));
if drawn.is_ok() {
for w in shadow_warnings(what, &said) {
s.warn(w);
}
}
drawn
}

/// What frames said of their cast shadows, as warnings about `what`: layers drawn without them,
/// and, drawn on the interface's thread (which doesn't wait for them), that some were still
/// being worked out. What was drawn is written all the same.
pub(crate) fn shadow_warnings(what: &str, said: &rustgis_render::ShadowsSaid) -> Vec<String> {
let mut out: Vec<String> = said.notices.iter().map(|e| format!("{what}: {e}")).collect();
if said.to_come {
out.push(format!(
"{what}: some layers are drawn without their cast shadows, still being worked out: draw it again once the map shows them"
));
}
out
}

/// Renders `map` at its camera in a view of `(vw, vh)` points (or a requested size) to PNG bytes.
Expand Down Expand Up @@ -344,7 +372,7 @@ fn export_view(
/// inch of screen, kept as vectors with what is pixels by nature drawn at `dpi`.
pub fn map_pdf(s: &mut Session, dpi: f64) -> Result<Vec<u8>, String> {
let revisions = rustgis_render::archive_revisions([s.map()]);
let result = map_pdf_inner(s, dpi);
let result = saying_shadows(s, "The map PDF", |s| map_pdf_inner(s, dpi));
revisions.finish_revision(rustgis_render::archive_revisions([s.map()]), Some(result), "could not draw the map")?
}

Expand Down Expand Up @@ -411,14 +439,23 @@ pub(crate) fn map_work(s: &Session, p: &Value) -> Result<Work, String> {
}
let (mut map, view_size) = (s.map().detached(), s.view_size);
Ok(Box::new(move || {
let mut shadows = Vec::new();
let written = (|| {
let png = png_of(&mut map, view_size, width, height, dpi)?;
let (png, said) = rustgis_render::shadows_said(|| png_of(&mut map, view_size, width, height, dpi));
let png = png?;
shadows = shadow_warnings(&path, &said);
rustgis_data::progress::check().map_err(|e| e.to_string())?;
rustgis_data::io::write_atomic(Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?;
Ok(json!({"path": path, "bytes": png.len()}))
})();
// What the size limit did is said of a file written.
let sized = if written.is_ok() { sized } else { Vec::new() };
// What the size limit did, and layers drawn without their cast shadows, are said of a
// file written.
let sized = if written.is_ok() {
let warn = rustgis_data::diagnostics::Severity::Warning;
sized.into_iter().chain(shadows.into_iter().map(|e| (warn, e))).collect()
} else {
Vec::new()
};
super::exports::drawn(std::slice::from_ref(&map), sized, written, path)
}))
}
Expand Down
12 changes: 11 additions & 1 deletion crates/engine/src/cmd/sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,18 @@ pub fn commands() -> Vec<C> {
]
}

/// The shared item bytes of a kind: its name, bytes and source.
/// The shared item bytes of a kind: its name, bytes and source. Rasters drawn into it without
/// their cast shadows are said (see [`super::share::saying_shadows`]).
pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec<u8>, String), String> {
let what = match kind {
"webMap" => "The web map",
"webLayer" => "The web layer",
_ => "The shared item",
};
super::share::saying_shadows(s, what, |s| share_bytes_inner(s, kind, p))
}

fn share_bytes_inner(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec<u8>, String), String> {
match kind {
"webMap" => {
let mut m = map_of(s, p)?;
Expand Down
124 changes: 124 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,130 @@ fn custom_projections_switch_keep_scale_query_and_render() {
}
}

/// Shaded Relief's cast shadows: set, kept when left out, clamped, a new one without; saved with
/// the project only when there are some, and drawn in an export (waited for).
#[test]
fn shaded_relief_keeps_its_cast_shadows() {
let mut s = with_raster();
let shadow = |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, .. }) => *shadow,
other => panic!("{other:?}"),
};
s.run("raster.symbology", &json!({"type": "shadedRelief"})).unwrap();
assert_eq!(shadow(&s), 0.0);
let shadows_saved = |s: &mut Session| s.project.saved_text(None).unwrap().0.matches("\"shadow\"").count();
let before = shadows_saved(&mut s);
s.run("raster.symbology", &json!({"type": "shadedRelief", "shadow": 0.7})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 200})).unwrap();
assert_eq!(shadow(&s), 0.7, "left out, kept");
s.run("raster.symbology", &json!({"type": "shadedRelief", "shadow": 4})).unwrap();
assert_eq!(shadow(&s), 1.0);
assert_eq!(shadows_saved(&mut s), before + 1, "saved only when there are some");
assert!(crate::cmd::share::map_png(&mut s, Some(300.0), Some(200.0), None).unwrap().len() > 1000);
s.run("raster.symbology", &json!({"type": "stretch"})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief"})).unwrap();
assert_eq!(shadow(&s), 0.0, "a new Shaded Relief casts none");
}

/// 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.
#[test]
fn a_map_export_says_a_layer_drawn_without_its_cast_shadows() {
let mut s = with_raster();
s.run("layer.zoomTo", &json!({"layer": "scene"})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief", "shadow": 1})).unwrap();
let dir = std::env::temp_dir().join(format!("rustgis-cast-notice-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let said =
|s: &Session| s.notices.iter().filter(|n| n.text.contains("cast shadows aren't drawn")).map(|n| n.text.clone()).collect::<Vec<_>>();
s.run("share.exportMap", &json!({"path": dir.join("clean.png").display().to_string(), "width": 200, "height": 150})).unwrap();
assert!(said(&s).is_empty(), "{:?}", said(&s));
// Read in place with only its overview held: no whole grid to trace.
let rl = s.map_mut().layers.iter_mut().find_map(|l| l.raster.as_mut()).unwrap();
rl.in_place = true;
let mut r = (**rl.data.as_ref().unwrap()).clone();
r.overview = Some((800, 600));
rl.data = Some(std::sync::Arc::new(r));
for name in ["shadowless.png", "shadowless.tif"] {
let before = said(&s).len();
s.run("share.exportMap", &json!({"path": dir.join(name).display().to_string(), "width": 200, "height": 150})).unwrap();
let now = said(&s);
assert_eq!(now.len(), before + 1, "{name}: {now:?}");
let last = now.last().unwrap();
assert!(last.contains("scene: its cast shadows aren't drawn") && last.contains("read in place"), "{name}: {last}");
assert!(!last.contains("tile layer"), "{name}: {last}");
}
// The app's Capture To Clipboard and Export Map download (`map_png`) say it too.
let before = said(&s).len();
crate::cmd::share::map_png(&mut s, Some(200.0), Some(150.0), None).unwrap();
assert_eq!(said(&s).len(), before + 1, "{:?}", said(&s));
std::fs::remove_dir_all(&dir).ok();
}

/// Whatever draws the map for someone says a layer drawn without its cast shadows, named by the
/// layer: the map's image, PDF and web map, and the exports of a layout, a presentation and a
/// report showing it. Drawn on the interface's thread, which doesn't wait for them, shadows still
/// being worked out are said too.
#[test]
fn every_drawing_of_the_map_says_a_layer_drawn_without_its_cast_shadows() {
let mut s = with_raster();
s.run("layer.zoomTo", &json!({"layer": "scene"})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief", "shadow": 1})).unwrap();
s.run("layout.new", &json!({"page": "a5", "template": "map"})).unwrap();
s.run("presentation.new", &json!({"name": "Relief"})).unwrap();
// First, as a PNG of the presentation is its first page.
s.run("presentation.mapPage", &json!({"title": "Relief", "at": 0})).unwrap();
assert_eq!(s.project.presentations[0].pages[0].kind.code(), "map");
s.run("report.new", &json!({"layer": "Countries", "name": "Relief", "fields": ["name"], "where": "name LIKE 'F%'"})).unwrap();
s.run("report.newTemplate", &json!({"report": "Relief"})).unwrap();
// Read in place with only its overview held: no whole grid to trace.
let rl = s.map_mut().layers.iter_mut().find_map(|l| l.raster.as_mut()).unwrap();
rl.in_place = true;
let mut r = (**rl.data.as_ref().unwrap()).clone();
r.overview = Some((800, 600));
rl.data = Some(std::sync::Arc::new(r));
let said = |s: &Session| s.notices.iter().filter(|n| n.text.contains("scene: its cast shadows aren't drawn")).count();
let dir = std::env::temp_dir().join(format!("rustgis-cast-every-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = |name: &str| dir.join(name).display().to_string();
let draws: Vec<(&str, Box<dyn Fn(&mut Session)>)> = vec![
("map image", Box::new(|s| drop(crate::cmd::share::map_png(s, Some(200.0), Some(150.0), None).unwrap()))),
("map PDF", Box::new(|s| drop(crate::cmd::share::map_pdf(s, 72.0).unwrap()))),
(
"map export",
Box::new(|s| drop(s.run("share.exportMap", &json!({"path": path("map.png"), "width": 200, "height": 150})).unwrap())),
),
("web map", Box::new(|s| drop(crate::cmd::sharing::share_bytes(s, "webMap", &json!({})).unwrap()))),
("layout PNG", Box::new(|s| drop(s.run("layout.export", &json!({"layout": 0, "path": path("page.png"), "dpi": 36})).unwrap()))),
("layout PDF", Box::new(|s| drop(s.run("layout.export", &json!({"layout": 0, "path": path("page.pdf"), "dpi": 36})).unwrap()))),
("presentation PDF", Box::new(|s| drop(s.run("presentation.export", &json!({"path": path("deck.pdf"), "dpi": 36})).unwrap()))),
("presentation PNG", Box::new(|s| drop(s.run("presentation.export", &json!({"path": path("deck.png"), "dpi": 36})).unwrap()))),
(
"report PDF",
Box::new(|s| drop(s.run("report.export", &json!({"report": "Relief", "path": path("report.pdf"), "dpi": 36})).unwrap())),
),
];
for (what, draw) in &draws {
// A notice repeating the last one is kept once (two presentation exports in a row).
s.notices.clear();
draw(&mut s);
assert!(said(&s) > 0, "{what}: {:?}", s.notices.iter().map(|n| &n.text).collect::<Vec<_>>());
}
std::fs::remove_dir_all(&dir).ok();
// On the interface's thread a new sun's shadows aren't waited for: the image says so.
let mut s = with_raster();
s.run("layer.zoomTo", &json!({"layer": "scene"})).unwrap();
s.run("raster.symbology", &json!({"type": "shadedRelief", "azimuth": 123, "shadow": 1})).unwrap();
crate::render::on_the_interface_thread();
crate::cmd::share::map_png(&mut s, Some(200.0), Some(150.0), None).unwrap();
assert!(
s.notices.iter().any(|n| n.text.contains("still being worked out")),
"{:?}",
s.notices.iter().map(|n| &n.text).collect::<Vec<_>>()
);
}

#[test]
fn raster_layers_open_style_identify_and_round_trip() {
let mut s = with_raster();
Expand Down
1 change: 1 addition & 0 deletions crates/map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod projection;
pub mod qgis;
pub mod raster;
pub mod report;
pub mod shadow;
pub mod slots;
pub mod source;
pub use slots::{LayerRole, Slot, SlotAt, SlotPos, TileRole};
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 @@ -94,6 +94,7 @@ fn renderer_style(node: Node, pipe: Node, r: Node, kind: &str, layer: &str, repo
azimuth: num("azimuth", 315.0).rem_euclid(360.0),
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,
};
(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 @@ -845,7 +845,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 }
RasterRenderer::ShadedRelief { band: 0, ramp: None, azimuth: 300.0, altitude: 35.0, z_factor: 2.0, shadow: 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