Skip to content
Open
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
7 changes: 3 additions & 4 deletions crates/render/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3305,10 +3305,9 @@ fn a_refused_export_says_the_refused_layers_own_reason() {
assert_eq!(refused, "dem: dem.tif: only http:// and https:// addresses are fetched, so the image isn't written");
}

/// A map PDF says a raster read in place that is drawn from less than it needs: unlike a PNG or a
/// GeoTIFF, it doesn't refuse one.
/// A map PDF refuses an incomplete raster, preserving its reason and its drawing notice.
#[test]
fn a_map_pdf_says_a_raster_read_in_place_drawn_from_less() {
fn a_map_pdf_refuses_a_raster_read_in_place_drawn_from_less() {
let mut map = ramp_map(rustgis_map::raster::RasterRenderer::Rgb { red: 0, green: 0, blue: 0 }, rustgis_map::raster::Stretch::MinMax);
let rl = map.layers.iter_mut().find_map(|l| l.raster.as_mut()).unwrap();
rl.in_place = true;
Expand All @@ -3318,7 +3317,7 @@ fn a_map_pdf_says_a_raster_read_in_place_drawn_from_less() {
let camera = rustgis_map::Camera { center: rustgis_geom::lonlat_to_merc(Coord::new(0.5, 0.995)), res: 100.0, bearing: 0.0, pitch: 0.0 };
let view = crate::View { width: 64.0, height: 64.0, pixels_per_point: 1.0, camera, sky: false };
let (pdf, said) = crate::shadows_said(|| crate::render_drawing(&mut map, &view));
assert!(pdf.is_ok());
assert_eq!(pdf.err().unwrap(), "dem: dem.tif: only http:// and https:// addresses are fetched, so the drawing isn't written");
let undrawn = "a layer couldn't be drawn in full, so parts of it may be empty or coarse: dem: dem.tif: ";
assert_eq!(said.notices.iter().filter(|n| n.starts_with(undrawn)).count(), 1, "{:?}", said.notices);
}
Expand Down
146 changes: 126 additions & 20 deletions crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5615,23 +5615,34 @@ fn render_frame(
let mut draw_it = |pm: &mut Pixmap, opacity: f32| match address.as_deref() {
// Drawn from less than the view needs (a coarser level, tiles that couldn't be
// read): shown, and said with the tile errors.
Some(a) => match raster::draw_in_place(pm, a, data, &rl.style, stats, view, opacity, full_res_rasters, place, light) {
Ok(pending) => {
tile_out.pending |= pending;
if pending && !tile_out.pending_in_place.contains(&layer.id) {
tile_out.pending_in_place.push(layer.id);
}
}
Err(e) => {
let e = format!("{}: {e}", layer.name);
if !tile_out.errors.contains(&e) {
tile_out.errors.push(e.clone());
Some(a) => {
#[cfg(not(target_arch = "wasm32"))]
let result =
if full_res_rasters && !NO_TILE_WAIT.with(std::cell::Cell::get) && !NO_SHADOW_WAIT.with(std::cell::Cell::get) {
raster::export_in_place(pm, a, data, &rl.style, stats, view, opacity, place, light)
} else {
raster::draw_in_place(pm, a, data, &rl.style, stats, view, opacity, full_res_rasters, place, light)
};
#[cfg(target_arch = "wasm32")]
let result = raster::draw_in_place(pm, a, data, &rl.style, stats, view, opacity, full_res_rasters, place, light);
match result {
Ok(pending) => {
tile_out.pending |= pending;
if pending && !tile_out.pending_in_place.contains(&layer.id) {
tile_out.pending_in_place.push(layer.id);
}
}
if !tile_out.partial.iter().any(|(id, _)| *id == layer.id) {
tile_out.partial.push((layer.id, e));
Err(e) => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let e = format!("{}: {e}", layer.name);
if !tile_out.errors.contains(&e) {
tile_out.errors.push(e.clone());
}
if !tile_out.partial.iter().any(|(id, _)| *id == layer.id) {
tile_out.partial.push((layer.id, e));
}
}
}
},
}
None => raster::draw(pm, data, &rl.style, stats, view, opacity, full_res_rasters, place, light),
};
if offscreen {
Expand Down Expand Up @@ -9938,26 +9949,39 @@ fn complete_passes(map: &mut Map, view: &View, record: bool, failed_source: &mut
pub fn render_drawing(map: &mut Map, view: &View) -> Result<vector::Drawing, String> {
let revisions = archive_revisions([&*map]);
let mut failed_source = None;
let result = render_drawing_inner(map, view, &mut failed_source);
revisions.finish(map, failed_source, result, "could not draw the map")
match render_drawing_inner(map, view, &mut failed_source) {
Ok(drawing) => revisions.finish(map, failed_source, Some(drawing), "could not draw the map"),
Err(why) => revisions.finish(map, failed_source, None, &why),
}
}

/// Completes the recording, retaining a fatal source's identity through the pixel fallback.
fn render_drawing_inner(map: &mut Map, view: &View, failed_source: &mut Option<u64>) -> Option<vector::Drawing> {
let r = render_complete_recorded(map, view, failed_source)?;
fn render_drawing_inner(map: &mut Map, view: &View, failed_source: &mut Option<u64>) -> Result<vector::Drawing, String> {
let r = render_complete_recorded(map, view, failed_source).ok_or_else(|| "could not draw the map".to_string())?;
rustgis_data::progress::check().map_err(|e| e.to_string())?;
if cancel::requested() {
return Err(rustgis_data::DataError::Cancelled.to_string());
}
if let Some((_, why)) = r.partial.first() {
return Err(format!("{why}, so the drawing isn't written"));
}
if let Some(l) = r.pending_in_place.first().and_then(|id| map.layers.iter().find(|l| l.id == *id)) {
return Err(format!("{}: its tiles are still on their way: export again once the map has drawn it", l.name));
}
let mut pm = match r.recorded {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Some(c) => c,
None => {
// Not recorded: its pixels, placed on a recording.
let mut c = Canvas::recording(r.pixmap.width(), r.pixmap.height())?;
let mut c =
Canvas::recording(r.pixmap.width(), r.pixmap.height()).ok_or_else(|| "could not allocate the drawing".to_string())?;
c.draw_pixmap(0, 0, r.pixmap.as_ref(), &PixmapPaint::default(), Transform::identity(), None);
c
}
};
draw_labels(&mut pm, (&r.unplaced, &r.labels, &r.label_layers), view.pixels_per_point as f32);
text::scale_bar(&mut pm, view, map.ground_res_at(&view.camera));
draw_export_attribution(&mut pm, &r.attribution, view.pixels_per_point as f32);
pm.into_drawing()
pm.into_drawing().ok_or_else(|| "could not complete the drawing".to_string())
}

/// A map layer's name as a recording's layer is named (#1815): a layer with none is named after
Expand Down Expand Up @@ -10222,6 +10246,88 @@ mod tests {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn output_bands_cancelled_vector_export_returns_no_drawing() {
use std::sync::{
Arc,
atomic::{AtomicU64, Ordering},
};
struct AfterBand(AtomicU64);
impl rustgis_data::progress::Probe for AfterBand {
fn cancelled(&self) -> bool {
self.0.load(Ordering::Relaxed) >= 128
}
fn begin(&self, _: u64) {
self.0.store(0, Ordering::Relaxed);
}
fn advance(&self, n: u64) {
self.0.fetch_add(n, Ordering::Relaxed);
}
}
let r = crate::testing::grid(rustgis_data::raster::Crs::WebMercator, (-150.0, 150.0), 10.0, (30, 30), |_, _| 50.0).unwrap();
let mut layer = rustgis_map::RasterLayer::from_path(r, "unused.tif");
layer.in_place = true;
let mut map = Map::new("Map");
map.basemap = rustgis_map::Basemap::Blank;
map.add_raster("raster", layer);
let view = View { width: 300.0, height: 300.0, pixels_per_point: 1.0, camera: Camera::new(Coord::new(0.0, 0.0), 1.0), sky: false };
// The same real vector-export entry point succeeds before cancellation is installed.
assert!(render_drawing(&mut map, &view).is_ok());
let probe = Arc::new(AfterBand(AtomicU64::new(0)));
let _installed = rustgis_data::progress::install(Some(probe.clone()));
assert!(render_drawing(&mut map, &view).is_err());
assert_eq!(probe.0.load(Ordering::Relaxed), 128);
Comment thread
Mavrick-F marked this conversation as resolved.
Resolved
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn output_bands_failed_raster_returns_no_vector_drawing() {
use rustgis_data::raster::cog::{self, Example};
use std::sync::{
Arc,
atomic::{AtomicU64, Ordering},
};
struct RemoveAfterBand {
path: std::path::PathBuf,
rows: AtomicU64,
}
impl rustgis_data::progress::Probe for RemoveAfterBand {
fn cancelled(&self) -> bool {
false
}
fn begin(&self, _: u64) {}
fn advance(&self, n: u64) {
if self.rows.fetch_add(n, Ordering::Relaxed) == 0 {
std::fs::remove_file(&self.path).unwrap();
rustgis_tiles::cog::forget_local_file(self.path.to_str().unwrap());
}
}
}
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("fails-after-band.tif");
std::fs::write(&path, cog::example_with(&Example { size: 4096, tile: 128, ..Example::default() })).unwrap();
let r = rustgis_tiles::cog::raster::overview_blocking(path.to_str().unwrap()).unwrap();
let mut layer = rustgis_map::RasterLayer::from_path(r, path.to_str().unwrap());
layer.in_place = true;
let mut map = Map::new("Map");
map.basemap = rustgis_map::Basemap::Blank;
map.add_raster("raster", layer);
let view = View {
width: 317.0,
height: 389.0,
pixels_per_point: 1.0,
camera: Camera::new(rustgis_geom::lonlat_to_merc(Coord::new(10.47, 45.4)), 60.0),
sky: false,
};
assert!(render_drawing(&mut map, &view).is_ok());
let probe = Arc::new(RemoveAfterBand { path, rows: AtomicU64::new(0) });
let _installed = rustgis_data::progress::install(Some(probe.clone()));
let why = render_drawing(&mut map, &view).err().expect("an incomplete raster must fail the drawing");
assert!(why.starts_with("raster: "), "{why}");
assert_eq!(probe.rows.load(Ordering::Relaxed), 128);
}

/// Labels of two map layers that overlap (a class that shows all, a pinned label) are
/// recorded in the placement's order, as the pixels draw them, so the one on top stays on
/// top: each run of one layer's labels is marked as that layer, and its runs are one PDF
Expand Down
Loading
Loading