From fcbf8dab97d1f0a04d08bb8b00d11988df62a644 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 00:49:47 -0300 Subject: [PATCH 01/18] feat(render): a layout's PDF is a GeoPDF: its map frames georeferenced (#1191) Each map frame of a layout's PDF is an ISO 32000 geospatial viewport (/VP, /Measure /GEO): its box on the page, its corners' latitude and longitude on its coordinate system's own datum, and that system as WKT with its EPSG code, so GDAL, QGIS, Acrobat and Avenza locate and measure in it. A tilted frame, one on the globe, one whose map is missing and one with a corner off the Earth are left out, and the export says which. A projection with no WKT form is georeferenced in longitude/latitude by its corners, and said. A georeferenced document is PDF 1.7 with Adobe's extension level 3. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/crs.rs | 21 ++++++ crates/data/src/raster/wkt.rs | 19 ++++- crates/engine/src/cmd/layout.rs | 26 ++++++- crates/engine/src/tests.rs | 50 +++++++++++++ crates/render/src/layout.rs | 87 ++++++++++++++++++++++ crates/render/src/pdf.rs | 127 ++++++++++++++++++++++++++++++-- docs/user-guide/data-formats.md | 2 +- docs/user-guide/layouts.md | 6 ++ 8 files changed, 324 insertions(+), 14 deletions(-) diff --git a/crates/data/src/raster/crs.rs b/crates/data/src/raster/crs.rs index a8e00f7038..9f520e191b 100644 --- a/crates/data/src/raster/crs.rs +++ b/crates/data/src/raster/crs.rs @@ -427,6 +427,27 @@ fn wgs84() -> Result { proj4rs::Proj::from_proj_string("+proj=longlat +datum=WGS84 +no_defs").map_err(|e| e.to_string()) } +/// A WGS 84 longitude/latitude on the datum of the PROJ definition `def`: where a reader that +/// takes `def`'s own geographic system puts the point (a GeoPDF's corners, #1191). The same +/// point where `def`'s datum has no shift from WGS 84. +pub fn lonlat_on_datum_of(def: &str, c: Coord) -> Option { + const DATUM: [&str; 9] = ["datum", "ellps", "towgs84", "a", "b", "rf", "f", "R", "pm"]; + let mut geographic = String::from("+proj=longlat"); + for t in def.split_whitespace() { + let key = t.trim_start_matches('+').split('=').next().unwrap_or(""); + if DATUM.contains(&key) { + geographic.push(' '); + geographic.push_str(t); + } + } + geographic.push_str(" +no_defs"); + let to = proj4rs::Proj::from_proj_string(&geographic).ok()?; + let mut p = (c.x.to_radians(), c.y.to_radians(), 0.0); + proj4rs::transform::transform(&wgs84().ok()?, &to, &mut p).ok()?; + let out = Coord::new(p.0.to_degrees(), p.1.to_degrees()); + out.is_finite().then_some(out) +} + /// The transformer for a PROJ definition of an equidistant cylindrical projection on an /// ellipsoid (EPSG:4087's), computed on it: `None` for any other definition, `eqc` on a sphere /// included. A map drawn in such a system uses it too (rustgis-map's `Projector`), so the map, diff --git a/crates/data/src/raster/wkt.rs b/crates/data/src/raster/wkt.rs index 34174fb88d..87186c35b6 100644 --- a/crates/data/src/raster/wkt.rs +++ b/crates/data/src/raster/wkt.rs @@ -1170,7 +1170,18 @@ pub fn to_wkt(crs: Crs) -> Option { (d.proj4.clone(), d.label.clone(), None) } }; - let proj = proj4rs::Proj::from_proj_string(&def).ok()?; + def_wkt(&def, &name, code) +} + +/// A PROJ definition as WKT1 named `name`, with no authority: a projection a map is drawn in that +/// has no EPSG code (a preset, `local:`), as [`to_wkt`] would write it. `None` as there. +pub fn proj4_to_wkt(def: &str, name: &str) -> Option { + def_wkt(def, name, None) +} + +/// A PROJ definition as WKT1, with `code` as its EPSG authority. +fn def_wkt(def: &str, name: &str, code: Option) -> Option { + let proj = proj4rs::Proj::from_proj_string(def).ok()?; let params: Vec<(&str, Option<&str>)> = def .split_whitespace() .filter_map(|t| t.strip_prefix('+')) @@ -1189,7 +1200,7 @@ pub fn to_wkt(crs: Crs) -> Option { // proj4rs computes `eqc` on a sphere of the semi-major axis and gives that sphere, but RustGIS // computes it on the datum's ellipsoid (`crs::ellipsoidal_eqc`), the one written here. let (a, b) = match text("proj") { - Some("eqc") => super::crs::eqc_geographic(&def).ok()?.ellipse_parameters(), + Some("eqc") => super::crs::eqc_geographic(def).ok()?.ellipse_parameters(), _ => proj.ellipse_parameters(), }; // A named ellipsoid's flattening as published; else from its axes (PROJ keeps a and b). @@ -1200,7 +1211,7 @@ pub fn to_wkt(crs: Crs) -> Option { }; let datum_name = match datum { Some(d) => d.1.to_string(), - None => match national_datum_name(&def, code) { + None => match national_datum_name(def, code) { Some(n) => n.to_string(), None => format!("Unknown based on {ellps_name} ellipsoid"), }, @@ -1443,7 +1454,7 @@ pub fn to_wkt(crs: Crs) -> Option { let parameters: String = ps.iter().map(|(n, v)| format!(",PARAMETER[{},{}]", quoted(n), num(*v))).collect(); Some(format!( "PROJCS[{},{},PROJECTION[{}]{parameters},UNIT[{},{}],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]{}]", - quoted(&name), + quoted(name), geogcs(""), quoted(method), quoted(unit), diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 2ed0c0afa3..6e983a6c49 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3518,6 +3518,8 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) // What the PDF draws as pictures because its vectors would grow too large: frames, and // whole pages (their surrounds). let (mut frames_pictured, mut pages_pictured) = (Vec::new(), Vec::new()); + // The map frames a PDF can't georeference, and those it georeferences by their corners only. + let (mut left_out, mut by_corners): (Vec<(String, &'static str)>, Vec) = (Vec::new(), Vec::new()); let mut render_pages = || -> Result<(Vec, Option>), String> { let mut out = Vec::new(); let mut png = None; @@ -3543,6 +3545,18 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) // a memory budget. if pdf { let (w, h) = (layout.width, layout.height); + // Where its map frames show, for readers to locate and measure in: a GeoPDF (#1191). + let geo = rustgis_render::layout::frame_viewports(layout, &s.project.maps); + for (name, why) in geo.left_out { + if !left_out.iter().any(|(n, _)| *n == name) { + left_out.push((name, why)); + } + } + for name in geo.by_corners { + if !by_corners.contains(&name) { + by_corners.push(name); + } + } let (drawing, pictured) = rustgis_render::layout::render_drawing(layout, &mut s.project.maps, dpi, &info) .ok_or("the page is too large to draw")?; if drawing.overflowed() { @@ -3550,10 +3564,10 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) let layout = s.project.layouts.get(li).ok_or("no layout")?; let img = rustgis_render::layout::render(layout, &mut s.project.maps, dpi, &info).ok_or("the page is too large to draw")?; - out.push(rustgis_render::pdf::Page::new(&img, w, h)); + out.push(rustgis_render::pdf::Page::new(&img, w, h).with_viewports(geo.viewports)); } else { frames_pictured.extend(pictured); - out.push(rustgis_render::pdf::Page::drawing(drawing, w, h)); + out.push(rustgis_render::pdf::Page::drawing(drawing, w, h).with_viewports(geo.viewports)); } held += out.last().map_or(0, rustgis_render::pdf::Page::len); if held > PDF_BUDGET { @@ -3579,6 +3593,14 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) } let (pages, png) = result?; pictured_notices(s, &frames_pictured, &pages_pictured); + for (name, why) in &left_out { + s.warn(format!("Map frame '{name}' isn't georeferenced in the PDF: {why}")); + } + for name in &by_corners { + s.warn(format!( + "Map frame '{name}' is georeferenced in the PDF by its corners in longitude/latitude, as its projection has no WKT form: positions between them are approximate" + )); + } if pdf { Ok(rustgis_render::pdf::write(&pages, &saved.name)) } else { png.ok_or_else(|| "nothing to draw".to_string()) } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 1fd5a20e18..b6fbc5f6bf 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -30848,6 +30848,56 @@ fn a_pdf_says_which_frames_and_pages_it_draws_as_pictures() { ); } +/// A layout's PDF georeferences its map frames (#1191): each as an ISO 32000 viewport with its +/// corners and its map's coordinate system, so that the frame's corners, taken back to the map, +/// make the frame's rectangle. A tilted frame is left out, and said. +#[test] +fn a_layout_pdf_georeferences_its_map_frames() { + let mut s = world(); + s.run("map.projection", &json!({"type": "EPSG:31983"})).unwrap(); + s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap(); + let frame = s.project.layouts[0].elements.iter().find(|e| e.kind.code() == "mapFrame").unwrap().clone(); + s.run("layout.update", &json!({"element": frame.id, "center": [-45.0, -20.0], "scale": 2_000_000.0})).unwrap(); + let path = std::env::temp_dir().join(format!("rustgis-geopdf-{}.pdf", std::process::id())); + let export = |s: &mut Session| { + let mark = s.notice_mark(); + s.run("layout.export", &json!({"path": path, "dpi": 72})).unwrap(); + let said: Vec = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); + (String::from_utf8_lossy(&std::fs::read(&path).unwrap()).into_owned(), said) + }; + let (pdf, said) = export(&mut s); + assert!(pdf.starts_with("%PDF-1.7") && pdf.contains("/ExtensionLevel 3"), "{}", &pdf[..20]); + assert!(pdf.contains("/VP [<< /Type /Viewport") && pdf.contains("/Subtype /GEO")); + assert!(pdf.contains("/GCS << /Type /PROJCS /WKT (PROJCS[") && pdf.contains("/EPSG 31983 >>"), "{pdf}"); + assert!(!said.iter().any(|t| t.contains("georeferenced")), "{said:?}"); + // The corners, back on the map, are the frame's rectangle: its top and bottom level, its + // sides upright, and as wide as the frame at its scale. + let gpts = pdf.split("/GPTS [").nth(1).and_then(|r| r.split(']').next()).unwrap(); + let n: Vec = gpts.split_whitespace().map(|v| v.parse().unwrap()).collect(); + assert_eq!(n.len(), 8, "{gpts}"); + let m: Vec = n.chunks(2).map(|c| s.map().to_map(rustgis_geom::Coord::new(c[1], c[0])).unwrap()).collect(); + let (tl, tr, br, bl) = (m[0], m[1], m[2], m[3]); + assert!((tl.y - tr.y).abs() < 0.01 && (bl.y - br.y).abs() < 0.01, "{m:?}"); + assert!((tl.x - bl.x).abs() < 0.01 && (tr.x - br.x).abs() < 0.01, "{m:?}"); + let wide = frame.w / 1000.0 * 2_000_000.0; + assert!(((tr.x - tl.x) / wide - 1.0).abs() < 1e-3, "{} m for {wide} m", tr.x - tl.x); + assert!(tl.y > bl.y, "north up"); + // Tilted, it shows no flat map to measure on: left out, and said. + s.run("layout.update", &json!({"element": frame.id, "pitch": 30})).unwrap(); + let (pdf, said) = export(&mut s); + assert!(pdf.starts_with("%PDF-1.4") && !pdf.contains("/VP [")); + assert!(said.iter().any(|t| t == &format!("Map frame '{}' isn't georeferenced in the PDF: it is tilted", frame.name)), "{said:?}"); + // A projection with no WKT form (Winkel Tripel): georeferenced in longitude/latitude by its + // corners, and said. + s.run("layout.update", &json!({"element": frame.id, "pitch": 0})).unwrap(); + s.run("map.projection", &json!({"type": "winkelTripel"})).unwrap(); + let (pdf, said) = export(&mut s); + assert!(pdf.contains("/GCS << /Type /GEOGCS /WKT (GEOGCS[") && pdf.contains("/EPSG 4326 >>"), "{pdf}"); + let corners = format!("Map frame '{}' is georeferenced in the PDF by its corners in longitude/latitude", frame.name); + assert!(said.iter().any(|t| t.starts_with(&corners)), "{said:?}"); + let _ = std::fs::remove_file(&path); +} + #[test] fn layer_invert_ramp_reverses_the_class_colors() { // #1355: Invert Color Ramp reverses the order of a layer's class colours. diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 7e0dbdac26..7c34124801 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -590,6 +590,93 @@ fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> ring } +/// The map frames of a page georeferenced for a PDF ([`frame_viewports`], #1191). +#[derive(Debug, Default)] +pub struct Georeferenced { + /// One for each visible frame that could be. + pub viewports: Vec, + /// The frames that couldn't be, by name, with why. + pub left_out: Vec<(String, &'static str)>, + /// The frames whose projection has no WKT form, by name: georeferenced by their corners in + /// longitude/latitude, so a reader is right at the corners and approximate between them. + pub by_corners: Vec, +} + +/// Each visible map frame of `layout` as a PDF viewport ([`crate::pdf::Viewport`]): its place on +/// the page, its corners' latitude and longitude, and its map's coordinate system as WKT. A +/// frame on the globe or tilted shows no flat map to measure on, so it is left out, as is one +/// whose map is missing or with a corner off the Earth. +pub fn frame_viewports(layout: &Layout, maps: &[Map]) -> Georeferenced { + let mut out = Georeferenced::default(); + for e in layout.elements.iter().filter(|e| e.visible) { + let ElementKind::MapFrame { map, center, scale, bearing, pitch, .. } = &e.kind else { continue }; + let Some(m) = map_index(maps, map).and_then(|i| maps.get(i)) else { + out.left_out.push((e.name.clone(), "its map is missing")); + continue; + }; + match frame_viewport(e, m, rustgis_map::layout::frame_camera(m, *center, *scale, *bearing, *pitch)) { + Ok((v, exact)) => { + if !exact { + out.by_corners.push(e.name.clone()); + } + out.viewports.push(v); + } + Err(why) => out.left_out.push((e.name.clone(), why)), + } + } + out +} + +/// Frame `e` of `map`, seen by `cam`, as a viewport, and whether it is in the map's own +/// coordinate system (else in longitude/latitude, by its corners). +fn frame_viewport( + e: &rustgis_map::layout::Element, + map: &Map, + cam: rustgis_map::Camera, +) -> Result<(crate::pdf::Viewport, bool), &'static str> { + use rustgis_data::raster::{crs, wkt}; + if map.projection.is_globe() { + return Err("it shows the globe"); + } + if cam.pitch.abs() > 1e-9 { + return Err("it is tilted"); + } + // The map's coordinate system: its WKT and EPSG code, and its PROJ definition (whose datum + // the corners are given on). + let (system, def) = match &map.projection { + rustgis_map::Projection::Custom(code) => { + let (def, label) = rustgis_map::projection::lookup(code).map_err(|_| "its projection can't be read")?; + let lower = code.trim().to_ascii_lowercase(); + let digits = lower.strip_prefix("epsg").map_or(lower.as_str(), |r| r.trim_start_matches([':', ' '])); + let epsg = digits.parse::().ok().filter(|_| digits.bytes().all(|b| b.is_ascii_digit())); + let text = match epsg { + Some(c) => wkt::to_wkt(crs::Crs::Epsg(c)), + None => wkt::proj4_to_wkt(&def, &label), + }; + (text.map(|t| (t, epsg)), Some(def)) + } + _ => (wkt::to_wkt(crs::Crs::WebMercator).map(|t| (t, Some(3857))), None), + }; + let (w, h) = (e.w * PT_PER_MM, e.h * PT_PER_MM); + let mut corners = [(0.0, 0.0); 4]; + for (i, (sx, sy)) in [(0.0, 0.0), (w, 0.0), (w, h), (0.0, h)].into_iter().enumerate() { + let c = cam.try_to_map(sx, sy, w, h).and_then(|m| map.to_lonlat(m)).ok_or("a corner is off the Earth")?; + // On the system's own datum, as a reader that takes its WKT reads them. + let c = match (&system, &def) { + (Some(_), Some(def)) => crs::lonlat_on_datum_of(def, c).ok_or("a corner is off the Earth")?, + _ => c, + }; + corners[i] = (c.y, c.x); + } + let exact = system.is_some(); + let (wkt, epsg) = match system { + Some(s) => s, + None => (wkt::to_wkt(crs::Crs::LonLat).ok_or("its projection can't be read")?, Some(4326)), + }; + let viewport = crate::pdf::Viewport { name: e.name.clone(), rect_mm: [e.x, e.y, e.w, e.h], corners, wkt, projected: exact, epsg }; + Ok((viewport, exact)) +} + /// Strokes the ring `ring` (longitude, latitude) on a frame's map `width` points wide: another /// frame's extent, as an extent indicator shows it. fn outline_lonlat(pm: &mut Canvas, map: &Map, view: &View, ring: &[Coord], color: Rgba, width: f32) { diff --git a/crates/render/src/pdf.rs b/crates/render/src/pdf.rs index 9f3c9689bd..db8e798e3b 100644 --- a/crates/render/src/pdf.rs +++ b/crates/render/src/pdf.rs @@ -1,7 +1,8 @@ //! A PDF writer. A page is either one picture (a rendered page, at the page's size) or a //! [`Drawing`] kept as vectors: its paths, text and pictures become the page's content, its text //! set in fonts whose glyphs are their outlines (Type 3), searchable and copyable through each -//! font's map back to Unicode. +//! font's map back to Unicode. A page's map frames can be georeferenced ([`Viewport`]), which +//! makes the document a GeoPDF. use tiny_skia::Pixmap; @@ -16,6 +17,46 @@ pub struct Page { content: Content, pub width_mm: f64, pub height_mm: f64, + viewports: Vec, +} + +/// A map frame's place on a page and where on the Earth it shows, so that a reader can locate and +/// measure in it: an ISO 32000 geospatial viewport, which makes the document a GeoPDF (#1191). +#[derive(Clone, Debug, PartialEq)] +pub struct Viewport { + /// The frame's name. + pub name: String, + /// Its rectangle on the page, in millimetres from the page's top-left corner: x, y, width, + /// height. + pub rect_mm: [f64; 4], + /// Its corners' latitude and longitude, in degrees on its coordinate system's own datum, + /// clockwise from the top-left. + pub corners: [(f64, f64); 4], + /// Its coordinate system as WKT (1), projected or geographic, and its EPSG code when it has one. + pub wkt: String, + pub projected: bool, + pub epsg: Option, +} + +impl Viewport { + /// As a PDF viewport dictionary on a page `page_h` points tall: the frame's box in points from + /// the page's bottom-left corner, its corners at the unit square's (the measure's `/LPTS`). + fn dictionary(&self, page_h: f64) -> String { + let k = 72.0 / 25.4; + let [x, y, w, h] = self.rect_mm; + let (x1, x2, top, bottom) = (x * k, (x + w) * k, page_h - y * k, page_h - (y + h) * k); + let gpts: Vec = self.corners.iter().map(|(lat, lon)| format!("{lat:.9} {lon:.9}")).collect(); + let kind = if self.projected { "PROJCS" } else { "GEOGCS" }; + let epsg = self.epsg.map(|c| format!(" /EPSG {c}")).unwrap_or_default(); + format!( + "<< /Type /Viewport /BBox [{x1:.3} {bottom:.3} {x2:.3} {top:.3}] /Name {} /Measure << /Type /Measure /Subtype /GEO \ + /Bounds [0 0 0 1 1 1 1 0] /GPTS [{}] /LPTS [0 1 1 1 1 0 0 0] /GCS << /Type /{kind} /WKT {}{epsg} >> \ + /PDU [/M /SQM /DEG] >> >>", + text_string(&self.name), + gpts.join(" "), + literal(&self.wkt), + ) + } } enum Content { @@ -72,7 +113,13 @@ impl Page { Some(rgb_z) => Content::Picture { width: image.width(), height: image.height(), rgb_z }, None => Content::Picture { width: 1, height: 1, rgb_z: miniz_oxide::deflate::compress_to_vec_zlib(&[255, 255, 255], 6) }, }; - Page { content, width_mm: page_mm(width_mm), height_mm: page_mm(height_mm) } + Page { content, width_mm: page_mm(width_mm), height_mm: page_mm(height_mm), viewports: Vec::new() } + } + + /// The page with its map frames georeferenced ([`Viewport`]). + pub fn with_viewports(mut self, viewports: Vec) -> Page { + self.viewports = viewports; + self } /// A page drawn as `drawing`, stretched over it; nothing paints its background but the @@ -84,6 +131,7 @@ impl Page { content: Content::Vector { width, height, items, cost, overflowed }, width_mm: page_mm(width_mm), height_mm: page_mm(height_mm), + viewports: Vec::new(), } } } @@ -103,7 +151,14 @@ pub(crate) struct Doc { impl Doc { /// A document with only its header written. fn new() -> Doc { - Doc { out: b"%PDF-1.4\n%\xE2\xE3\xCF\xD3\n".to_vec(), offsets: Vec::new() } + Doc::of_version("1.4") + } + + /// A document of PDF version `version` with only its header written. + fn of_version(version: &str) -> Doc { + let mut out = format!("%PDF-{version}\n").into_bytes(); + out.extend_from_slice(b"%\xE2\xE3\xCF\xD3\n"); + Doc { out, offsets: Vec::new() } } /// A new object's number. @@ -161,7 +216,10 @@ impl Doc { /// Writes the pages as a PDF document. pub fn write(pages: &[Page], title: &str) -> Vec { - let mut doc = Doc::new(); + // Georeferenced frames make the document PDF 1.7 with Adobe's extension level 3, where + // viewports are defined (since taken into ISO 32000-2); what else it holds is 1.4. + let geo = pages.iter().any(|p| !p.viewports.is_empty()); + let mut doc = if geo { Doc::of_version("1.7") } else { Doc::new() }; let (catalog, tree, info) = (doc.reserve(), doc.reserve(), doc.reserve()); let ids: Vec = pages.iter().map(|_| doc.reserve()).collect(); let mut fonts = fonts::Fonts::default(); @@ -185,20 +243,41 @@ pub fn write(pages: &[Page], title: &str) -> Vec { (resources.0, format!("{} 0 R", resources.1)) } }; + let viewports = if p.viewports.is_empty() { + String::new() + } else { + let each: Vec = p.viewports.iter().map(|v| v.dictionary(ph)).collect(); + format!(" /VP [{}]", each.join(" ")) + }; doc.put( page, - format!("<< /Type /Page /Parent {tree} 0 R /MediaBox [0 0 {pw:.2} {ph:.2}] /Resources {resources} /Contents {contents} 0 R >>") - .as_bytes(), + format!( + "<< /Type /Page /Parent {tree} 0 R /MediaBox [0 0 {pw:.2} {ph:.2}] /Resources {resources} /Contents {contents} 0 R{viewports} >>" + ) + .as_bytes(), ); } fonts.write(&mut doc); - doc.put(catalog, format!("<< /Type /Catalog /Pages {tree} 0 R >>").as_bytes()); + let extensions = if geo { " /Extensions << /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> >>" } else { "" }; + doc.put(catalog, format!("<< /Type /Catalog /Pages {tree} 0 R{extensions} >>").as_bytes()); let kids: Vec = ids.iter().map(|i| format!("{i} 0 R")).collect(); doc.put(tree, format!("<< /Type /Pages /Kids [{}] /Count {} >>", kids.join(" "), pages.len()).as_bytes()); doc.put(info, format!("<< /Title {} /Producer (RustGIS) >>", text_string(title)).as_bytes()); doc.finish(catalog, info) } +/// A PDF literal string of any length (a WKT): ASCII in parentheses, its backslashes and +/// parentheses escaped; anything else as UTF-16BE hex. +fn literal(s: &str) -> String { + if s.is_ascii() && !s.chars().any(|c| c.is_control()) { + let escaped: String = s.chars().flat_map(|c| if matches!(c, '(' | ')' | '\\') { vec!['\\', c] } else { vec![c] }).collect(); + format!("({escaped})") + } else { + let hex: String = s.encode_utf16().map(|u| format!("{u:04X}")).collect(); + format!("") + } +} + /// A PDF text string: plain ASCII in parentheses, anything else as UTF-16BE hex. fn text_string(s: &str) -> String { let s: String = s.chars().filter(|c| !c.is_control()).take(200).collect(); @@ -273,6 +352,40 @@ pub(crate) mod tests { assert_eq!((p.width_mm, p.height_mm), (210.0, 210.0)); } + /// A georeferenced frame is a viewport on its page (#1191): its box measured from the page's + /// bottom, its corners as latitude then longitude, its WKT whole and escaped. + #[test] + fn a_georeferenced_frame_is_a_viewport_on_its_page() { + let mut a = Pixmap::new(2, 2).unwrap(); + a.fill(tiny_skia::Color::WHITE); + let wkt = format!("PROJCS[\"A (b)\\c\",{}]", "x".repeat(400)); + let v = Viewport { + name: "Main map".into(), + rect_mm: [25.4, 25.4, 50.8, 25.4], + corners: [(-19.5, -45.5), (-19.5, -44.5), (-20.5, -44.5), (-20.5, -45.5)], + wkt: wkt.clone(), + projected: true, + epsg: Some(31983), + }; + let page_h = 297.0 * 72.0 / 25.4; + let d = v.dictionary(page_h); + // An inch in from the left and the top, two inches wide and one tall. + assert!(d.contains(&format!("/BBox [72.000 {:.3} 216.000 {:.3}]", page_h - 144.0, page_h - 72.0)), "{d}"); + assert!(d.contains("/GPTS [-19.500000000 -45.500000000 -19.500000000 -44.500000000 -20.500000000 -44.500000000"), "{d}"); + assert!(d.contains("/LPTS [0 1 1 1 1 0 0 0]") && d.contains("/Type /PROJCS") && d.contains("/EPSG 31983"), "{d}"); + assert!(d.contains(&format!("/WKT (PROJCS[\"A \\(b\\)\\\\c\",{}])", "x".repeat(400))), "{d}"); + let pdf = write(&[Page::new(&a, 210.0, 297.0).with_viewports(vec![v]), Page::new(&a, 210.0, 297.0)], "Geo"); + let text = String::from_utf8_lossy(&pdf); + assert!(text.starts_with("%PDF-1.7") && text.contains("/Extensions << /ADBE << /BaseVersion /1.7 /ExtensionLevel 3 >> >>")); + let objs = objects(&pdf); + let pages: Vec<&String> = objs.iter().map(|o| &o.0).filter(|d| d.contains("/Type /Page ")).collect(); + assert!(pages[0].contains(" /VP [<< /Type /Viewport") && !pages[1].contains("/VP"), "{pages:?}"); + // A document without a georeferenced frame stays as it was. + let plain = write(&[Page::new(&a, 210.0, 297.0)], "Plain"); + assert!(plain.starts_with(b"%PDF-1.4") && !String::from_utf8_lossy(&plain).contains("/Extensions")); + assert_eq!(literal("é"), ""); + } + #[test] fn unwritten_objects_become_nulls_and_writing_twice_keeps_the_first() { let mut doc = Doc::new(); diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 7292aa1502..32e74a73e3 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -572,7 +572,7 @@ were rounded, and Export Raster, which writes the rounded values, says so again. | GeoJSON, KML, GPX, CSV, CSV with WKT | Analyze › Workflows › Data Interop (`data.convert`; `"format": "geojson"`, `"kml"`, `"gpx"`, `"csv"` or `"wkt"`) | Returns text when `"path"` is omitted; writes a file on desktop when it is given. | | GeoTIFF / Cloud-optimised GeoTIFF | Data › Export Raster (`share.exportRaster`) | Untiled GeoTIFF by default; `"cog": true` writes 512-pixel tiles, Deflate compression and overviews. Writes the loaded raster's resolution (an overview when that is what was opened), the selected band or its colour bands. The web app's Export Raster downloads an ordinary GeoTIFF. | | PNG | Publish › Export Map (`share.exportMap`) | Configurable DPI, with labels, scale bar and tile credits. | -| PDF | Layouts, reports, presentations and the map view (Print Map), as vectors | | +| PDF | Layouts, reports, presentations and the map view (Print Map), as vectors; a layout's map frames georeferenced (GeoPDF) | | | Wavefront OBJ, STL, glTF | Publish › Export To 3D, Share As › Web Scene | Polygons extruded by a field. STL is binary and triangles only (no lines). | | Web map, OGC API Features, MapLibre style | Publish › Share As | Open-format files that need no server: see [Sharing](sharing.md). | | Raster tiles | `imagery.hostedImagery` | A zip of `tiles/z/x/y.png` tiles, `tiles.json` and a viewer page; at most 5,000 tiles and 256 MiB of tile data. | diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 33cd8e16c2..4fca8c9bd4 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -299,6 +299,12 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu are pictures cut to their shape. The `dpi` also sets how fine the vectors' detail is kept, as a PNG at that resolution would show it. A frame or page whose vectors would pass 256 MB (64 MB in the browser) is drawn as a picture instead, and the export says which. + A PDF is also a GeoPDF: each map frame is georeferenced (an ISO 32000 geospatial viewport, the + form GDAL, QGIS, Acrobat and Avenza read), with its map's coordinate system as WKT and its EPSG + code when it has one, so a reader shows coordinates and measures in it, and GDAL or QGIS open the + page as a georeferenced raster. A tilted frame, one on the globe, and one with a corner off the + Earth aren't georeferenced, and the export says which. A frame in a projection with no WKT form + is georeferenced by its corners in longitude/latitude, approximate between them, and said. In SVG each element is a group (an Inkscape layer) named after it: text stays text and rectangles, ellipses and lines stay shapes, and map frames and the other surrounds are kept as vectors as in a PDF (their labels as the letters' shapes, each run labelled with its text), so the page can be From 18304a5e3ff2ad772cd0382395788834f18a5257 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 00:58:21 -0300 Subject: [PATCH 02/18] =?UTF-8?q?fix(render):=20GeoPDF=20review=20round=20?= =?UTF-8?q?1:=20notices=20per=20kind=20by=20frame=20id,=20other=20bodies,?= =?UTF-8?q?=20180=C2=B0,=20drawn=20placement=20(#1191)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - What a PDF doesn't georeference, and what it georeferences by its corners only, are each one notice with a count (AGENTS.md, Never silent), a frame counted once by its id (two frames sharing a name are two). - A frame on another body than the Earth is left out: an Earth system would misplace it. - A corner past +-180 deg, or one that doesn't come back to its map position (Web Mercator's world repeated across 180 deg), leaves the frame out; in the longitude/latitude fallback the corners are kept within half a turn. - The viewport is the box the page draws: the frame's left and top on the whole pixel draw_page rounds them to at the export's dpi, the page's pixels stretched over its paper. - A datum shift that can't be computed says so; the corner-only notice names both reasons a system has no WKT. - Tests: SAD69 corners on SAD69 (engine, data), placement at 72 and 254 dpi, notices. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/crs.rs | 21 ++++++++++ crates/engine/src/cmd/layout.rs | 69 +++++++++++++++++++++++++-------- crates/engine/src/tests.rs | 51 ++++++++++++++++++++++-- crates/render/src/layout.rs | 69 ++++++++++++++++++++++++--------- crates/render/src/pdf.rs | 25 ++++++++++++ 5 files changed, 196 insertions(+), 39 deletions(-) diff --git a/crates/data/src/raster/crs.rs b/crates/data/src/raster/crs.rs index 9f520e191b..26bf944dcc 100644 --- a/crates/data/src/raster/crs.rs +++ b/crates/data/src/raster/crs.rs @@ -423,6 +423,27 @@ pub(crate) fn transformer_of(def: &str) -> Result { Ok(Transformer { kind: Kind::Proj { native: Box::new(native), wgs84: Box::new(wgs84()?), geographic } }) } +#[cfg(test)] +mod datum_tests { + use super::*; + + /// A corner on a datum shifted from WGS 84 (SAD69) moves by the shift, tens of metres; on one + /// with none (SIRGAS 2000) it stays (#1191). + #[test] + fn a_point_moves_onto_a_shifted_datum_and_not_onto_an_unshifted_one() { + let c = Coord::new(-45.0, -20.0); + let sirgas = lonlat_on_datum_of(epsg_proj4(31983).unwrap(), c).unwrap(); + assert!((sirgas.x - c.x).abs() < 1e-9 && (sirgas.y - c.y).abs() < 1e-9, "{sirgas:?}"); + let sad69 = epsg_proj4(29193).unwrap(); + assert!(sad69.contains("+towgs84="), "{sad69}"); + let on = lonlat_on_datum_of(sad69, c).unwrap(); + // About 40 m east and 40 m north at 20° S: well past a centimetre, short of a kilometre. + let (dx, dy) = ((on.x - c.x) * 111_320.0 * 20f64.to_radians().cos(), (on.y - c.y) * 110_574.0); + let moved = dx.hypot(dy); + assert!(moved > 10.0 && moved < 200.0, "{moved} m ({dx} m, {dy} m)"); + } +} + fn wgs84() -> Result { proj4rs::Proj::from_proj_string("+proj=longlat +datum=WGS84 +no_defs").map_err(|e| e.to_string()) } diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 6e983a6c49..d0db4e9244 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3519,7 +3519,7 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) // whole pages (their surrounds). let (mut frames_pictured, mut pages_pictured) = (Vec::new(), Vec::new()); // The map frames a PDF can't georeference, and those it georeferences by their corners only. - let (mut left_out, mut by_corners): (Vec<(String, &'static str)>, Vec) = (Vec::new(), Vec::new()); + let (mut left_out, mut by_corners): (Vec<(u32, String, &'static str)>, Vec<(u32, String)>) = (Vec::new(), Vec::new()); let mut render_pages = || -> Result<(Vec, Option>), String> { let mut out = Vec::new(); let mut png = None; @@ -3546,17 +3546,9 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) if pdf { let (w, h) = (layout.width, layout.height); // Where its map frames show, for readers to locate and measure in: a GeoPDF (#1191). - let geo = rustgis_render::layout::frame_viewports(layout, &s.project.maps); - for (name, why) in geo.left_out { - if !left_out.iter().any(|(n, _)| *n == name) { - left_out.push((name, why)); - } - } - for name in geo.by_corners { - if !by_corners.contains(&name) { - by_corners.push(name); - } - } + let geo = rustgis_render::layout::frame_viewports(layout, &s.project.maps, dpi); + left_out.extend(geo.left_out); + by_corners.extend(geo.by_corners); let (drawing, pictured) = rustgis_render::layout::render_drawing(layout, &mut s.project.maps, dpi, &info) .ok_or("the page is too large to draw")?; if drawing.overflowed() { @@ -3593,15 +3585,58 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) } let (pages, png) = result?; pictured_notices(s, &frames_pictured, &pages_pictured); - for (name, why) in &left_out { - s.warn(format!("Map frame '{name}' isn't georeferenced in the PDF: {why}")); + georeference_notices(s, &left_out, &by_corners); + if pdf { Ok(rustgis_render::pdf::write(&pages, &saved.name)) } else { png.ok_or_else(|| "nothing to draw".to_string()) } +} + +/// Says which map frames a PDF doesn't georeference, with why, and which it georeferences by their +/// corners only (#1191): one notice for each, with a count. A frame is counted once however many +/// pages of a series it is on (by its id: two frames sharing a name are two), with the first +/// page's reason. +pub(crate) fn georeference_notices(s: &mut Session, left_out: &[(u32, String, &'static str)], by_corners: &[(u32, String)]) { + let listed = |items: Vec| { + let more = items.len().saturating_sub(10); + let mut text = items.into_iter().take(10).collect::>().join(", "); + if more > 0 { + text.push_str(&format!(" and {more} more")); + } + text + }; + let plural = |n: usize| if n == 1 { "" } else { "s" }; + let mut seen = Vec::new(); + let out: Vec = left_out + .iter() + .filter(|(id, ..)| { + !seen.contains(id) && { + seen.push(*id); + true + } + }) + .map(|(_, n, why)| format!("{n}: {why}")) + .collect(); + if !out.is_empty() { + let n = out.len(); + s.warn(format!("The PDF doesn't georeference {n} map frame{} ({})", plural(n), listed(out))); } - for name in &by_corners { + let mut seen = Vec::new(); + let corners: Vec = by_corners + .iter() + .filter(|(id, _)| { + !seen.contains(id) && { + seen.push(*id); + true + } + }) + .map(|(_, n)| n.clone()) + .collect(); + if !corners.is_empty() { + let n = corners.len(); s.warn(format!( - "Map frame '{name}' is georeferenced in the PDF by its corners in longitude/latitude, as its projection has no WKT form: positions between them are approximate" + "The PDF georeferences {n} map frame{} by its corners in longitude/latitude, as its coordinate system can't be written as WKT (a grid shift, or a projection WKT1 has no name for): positions between the corners are approximate ({})", + plural(n), + listed(corners) )); } - if pdf { Ok(rustgis_render::pdf::write(&pages, &saved.name)) } else { png.ok_or_else(|| "nothing to draw".to_string()) } } /// Says which layout elements, map frames and surrounds (by name), and pages (by number) an export diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index b6fbc5f6bf..8c89c69e50 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -30886,18 +30886,63 @@ fn a_layout_pdf_georeferences_its_map_frames() { s.run("layout.update", &json!({"element": frame.id, "pitch": 30})).unwrap(); let (pdf, said) = export(&mut s); assert!(pdf.starts_with("%PDF-1.4") && !pdf.contains("/VP [")); - assert!(said.iter().any(|t| t == &format!("Map frame '{}' isn't georeferenced in the PDF: it is tilted", frame.name)), "{said:?}"); + assert!(said.iter().any(|t| t == &format!("The PDF doesn't georeference 1 map frame ({}: it is tilted)", frame.name)), "{said:?}"); // A projection with no WKT form (Winkel Tripel): georeferenced in longitude/latitude by its // corners, and said. s.run("layout.update", &json!({"element": frame.id, "pitch": 0})).unwrap(); s.run("map.projection", &json!({"type": "winkelTripel"})).unwrap(); let (pdf, said) = export(&mut s); assert!(pdf.contains("/GCS << /Type /GEOGCS /WKT (GEOGCS[") && pdf.contains("/EPSG 4326 >>"), "{pdf}"); - let corners = format!("Map frame '{}' is georeferenced in the PDF by its corners in longitude/latitude", frame.name); - assert!(said.iter().any(|t| t.starts_with(&corners)), "{said:?}"); + assert!( + said.iter().any(|t| t.starts_with("The PDF georeferences 1 map frame by its corners") && t.ends_with(&format!("({})", frame.name))), + "{said:?}" + ); + // On SAD69 (shifted from WGS 84) the corners are on SAD69, tens of metres from WGS 84's. + s.run("map.projection", &json!({"type": "EPSG:29193"})).unwrap(); + let (pdf, _) = export(&mut s); + let gpts = pdf.split("/GPTS [").nth(1).and_then(|r| r.split(']').next()).unwrap(); + let n: Vec = gpts.split_whitespace().map(|v| v.parse().unwrap()).collect(); + let frame_now = s.project.layouts[0].element(frame.id).unwrap().clone(); + let rustgis_map::layout::ElementKind::MapFrame { center, scale, .. } = &frame_now.kind else { panic!() }; + let cam = rustgis_map::layout::frame_camera(s.map(), *center, *scale, 0.0, 0.0); + let wgs84 = s + .map() + .to_lonlat(cam.to_map(0.0, 0.0, frame.w * rustgis_map::layout::PT_PER_MM, frame.h * rustgis_map::layout::PT_PER_MM)) + .unwrap(); + let moved = ((n[1] - wgs84.x) * 104_600.0).hypot((n[0] - wgs84.y) * 110_600.0); + assert!(pdf.contains("/EPSG 29193 >>") && moved > 10.0 && moved < 200.0, "{moved} m"); + // Across 180° in Web Mercator, a corner comes back on the other side of the world: left out. + s.run("map.projection", &json!({"type": "mercator"})).unwrap(); + s.run("layout.update", &json!({"element": frame.id, "center": [180.0, 0.0]})).unwrap(); + let (pdf, said) = export(&mut s); + assert!(!pdf.contains("/VP ["), "nothing georeferenced"); + assert!(said.iter().any(|t| t.contains("crosses its projection's edge (the 180° meridian)")), "{said:?}"); + // Another body's map isn't given an Earth system. + s.run("layout.update", &json!({"element": frame.id, "center": [-45.0, -20.0]})).unwrap(); + s.run("map.body", &json!({"name": "mars", "basemap": false})).unwrap(); + let (pdf, said) = export(&mut s); + assert!(!pdf.contains("/VP [")); + assert!(said.iter().any(|t| t.contains("it is on another body than the Earth")), "{said:?}"); let _ = std::fs::remove_file(&path); } +/// What a PDF doesn't georeference is one notice for each kind, with a count (#1191): a frame +/// once however many pages it is on, two frames sharing a name two. +#[test] +fn a_pdf_says_which_frames_it_doesnt_georeference() { + let mut s = Session::new(Project::default()); + let mark = s.notice_mark(); + crate::cmd::layout::georeference_notices(&mut s, &[], &[]); + assert!(s.notices_since(mark).is_empty(), "everything georeferenced, nothing said"); + let left = + [(1, "Main".to_string(), "it is tilted"), (1, "Main".to_string(), "it is tilted"), (4, "Main".to_string(), "it shows the globe")]; + crate::cmd::layout::georeference_notices(&mut s, &left, &[(2, "Inset".to_string()), (2, "Inset".to_string())]); + let said: Vec<&str> = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect(); + assert_eq!(said.len(), 2, "{said:?}"); + assert_eq!(said[0], "The PDF doesn't georeference 2 map frames (Main: it is tilted, Main: it shows the globe)"); + assert!(said[1].starts_with("The PDF georeferences 1 map frame by its corners") && said[1].ends_with("(Inset)"), "{}", said[1]); +} + #[test] fn layer_invert_ramp_reverses_the_class_colors() { // #1355: Invert Color Ramp reverses the order of a layer's class colours. diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 7c34124801..3e27c1ed8e 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -595,42 +595,51 @@ fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> pub struct Georeferenced { /// One for each visible frame that could be. pub viewports: Vec, - /// The frames that couldn't be, by name, with why. - pub left_out: Vec<(String, &'static str)>, - /// The frames whose projection has no WKT form, by name: georeferenced by their corners in - /// longitude/latitude, so a reader is right at the corners and approximate between them. - pub by_corners: Vec, + /// The frames that couldn't be, by id and name, with why. + pub left_out: Vec<(u32, String, &'static str)>, + /// The frames whose coordinate system can't be written as WKT (a grid shift, or a projection + /// WKT1 has no name for), by id and name: georeferenced by their corners in longitude/latitude, + /// so a reader is right at the corners and approximate between them. + pub by_corners: Vec<(u32, String)>, } /// Each visible map frame of `layout` as a PDF viewport ([`crate::pdf::Viewport`]): its place on -/// the page, its corners' latitude and longitude, and its map's coordinate system as WKT. A -/// frame on the globe or tilted shows no flat map to measure on, so it is left out, as is one -/// whose map is missing or with a corner off the Earth. -pub fn frame_viewports(layout: &Layout, maps: &[Map]) -> Georeferenced { +/// the page as a page drawn at `dpi` places it, its corners' latitude and longitude, and its +/// map's coordinate system as WKT. A frame on the globe or tilted shows no flat map to measure +/// on, so it is left out, as is one on another body than the Earth, one whose map is missing, +/// one with a corner off the Earth and one across its projection's edge (the 180° meridian). +pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced { let mut out = Georeferenced::default(); + // A frame is drawn from a whole pixel of the page ([`draw_page`]), whose pixels are stretched + // over its paper: the viewport is that box, in millimetres. + let (wpx, hpx, k) = page_geometry(layout, dpi); + let (sx, sy) = (layout.width / f64::from(wpx.max(1)), layout.height / f64::from(hpx.max(1))); for e in layout.elements.iter().filter(|e| e.visible) { let ElementKind::MapFrame { map, center, scale, bearing, pitch, .. } = &e.kind else { continue }; let Some(m) = map_index(maps, map).and_then(|i| maps.get(i)) else { - out.left_out.push((e.name.clone(), "its map is missing")); + out.left_out.push((e.id, e.name.clone(), "its map is missing")); continue; }; - match frame_viewport(e, m, rustgis_map::layout::frame_camera(m, *center, *scale, *bearing, *pitch)) { + let placed = [(e.x * k).round() * sx, (e.y * k).round() * sy, e.w * k * sx, e.h * k * sy]; + match frame_viewport(e, placed, m, rustgis_map::layout::frame_camera(m, *center, *scale, *bearing, *pitch)) { Ok((v, exact)) => { if !exact { - out.by_corners.push(e.name.clone()); + out.by_corners.push((e.id, e.name.clone())); } out.viewports.push(v); } - Err(why) => out.left_out.push((e.name.clone(), why)), + Err(why) => out.left_out.push((e.id, e.name.clone(), why)), } } out } -/// Frame `e` of `map`, seen by `cam`, as a viewport, and whether it is in the map's own -/// coordinate system (else in longitude/latitude, by its corners). +/// Frame `e` of `map`, placed at `placed` (millimetres: x, y, width, height) and seen by `cam`, as +/// a viewport, and whether it is in the map's own coordinate system (else in longitude/latitude, +/// by its corners). fn frame_viewport( e: &rustgis_map::layout::Element, + placed: [f64; 4], map: &Map, cam: rustgis_map::Camera, ) -> Result<(crate::pdf::Viewport, bool), &'static str> { @@ -638,6 +647,10 @@ fn frame_viewport( if map.projection.is_globe() { return Err("it shows the globe"); } + // Its coordinates are another body's, which an Earth system would misplace. + if !map.body().is_earth() { + return Err("it is on another body than the Earth"); + } if cam.pitch.abs() > 1e-9 { return Err("it is tilted"); } @@ -660,10 +673,20 @@ fn frame_viewport( let (w, h) = (e.w * PT_PER_MM, e.h * PT_PER_MM); let mut corners = [(0.0, 0.0); 4]; for (i, (sx, sy)) in [(0.0, 0.0), (w, 0.0), (w, h), (0.0, h)].into_iter().enumerate() { - let c = cam.try_to_map(sx, sy, w, h).and_then(|m| map.to_lonlat(m)).ok_or("a corner is off the Earth")?; + let m = cam.try_to_map(sx, sy, w, h).ok_or("a corner is off the Earth")?; + let c = map.to_lonlat(m).ok_or("a corner is off the Earth")?; + // A corner past the projection's edge (Web Mercator's world repeated across 180°) is at a + // longitude past ±180°, or comes back elsewhere: a reader, which takes longitudes within + // ±180°, would put it on the other side of the world. (In longitude/latitude, below, the + // corners are kept within half a turn of each other instead.) + let back = map.to_map(c).ok_or("a corner is off the Earth")?; + let elsewhere = (back.x - m.x).abs() > 1e-6 * m.x.abs().max(1.0) || (back.y - m.y).abs() > 1e-6 * m.y.abs().max(1.0); + if elsewhere || (system.is_some() && c.x.abs() > 180.0) { + return Err("it crosses its projection's edge (the 180° meridian)"); + } // On the system's own datum, as a reader that takes its WKT reads them. let c = match (&system, &def) { - (Some(_), Some(def)) => crs::lonlat_on_datum_of(def, c).ok_or("a corner is off the Earth")?, + (Some(_), Some(def)) => crs::lonlat_on_datum_of(def, c).ok_or("its corners can't be moved onto its datum")?, _ => c, }; corners[i] = (c.y, c.x); @@ -671,9 +694,17 @@ fn frame_viewport( let exact = system.is_some(); let (wkt, epsg) = match system { Some(s) => s, - None => (wkt::to_wkt(crs::Crs::LonLat).ok_or("its projection can't be read")?, Some(4326)), + None => { + // In longitude/latitude the corners are fitted as they are: across 180° they are kept + // within half a turn of the first, so that the frame isn't taken to span the world. + let first = corners[0].1; + for c in &mut corners { + c.1 = first + ((c.1 - first + 540.0).rem_euclid(360.0) - 180.0); + } + (wkt::to_wkt(crs::Crs::LonLat).ok_or("its projection can't be read")?, Some(4326)) + } }; - let viewport = crate::pdf::Viewport { name: e.name.clone(), rect_mm: [e.x, e.y, e.w, e.h], corners, wkt, projected: exact, epsg }; + let viewport = crate::pdf::Viewport { name: e.name.clone(), rect_mm: placed, corners, wkt, projected: exact, epsg }; Ok((viewport, exact)) } diff --git a/crates/render/src/pdf.rs b/crates/render/src/pdf.rs index db8e798e3b..084dd36365 100644 --- a/crates/render/src/pdf.rs +++ b/crates/render/src/pdf.rs @@ -386,6 +386,31 @@ pub(crate) mod tests { assert_eq!(literal("é"), ""); } + /// A frame's viewport is where the page draws it: from the whole pixel it starts on at the + /// export's dpi, the page's pixels stretched over its paper (#1191). + #[test] + fn a_viewport_is_where_the_page_draws_its_frame() { + let mut layout: rustgis_map::layout::Layout = serde_json::from_value(serde_json::json!({ + "name": "L", "width": 297.0, "height": 210.0, + "elements": [{"id": 1, "name": "Main", "x": 10.0, "y": 10.0, "w": 100.0, "h": 80.0, + "kind": {"MapFrame": {"map": "Map", "center": {"x": -45.0, "y": -20.0}, "scale": 50000.0}}}] + })) + .unwrap(); + layout.clamp(); + let maps = [rustgis_map::Map::new("Map")]; + let geo = crate::layout::frame_viewports(&layout, &maps, 72.0); + let [x, y, w, _] = geo.viewports[0].rect_mm; + // At 72 dpi a millimetre is about 2.83 pixels: 10 mm falls inside pixel 28, where the frame starts. + let (wpx, _, k) = crate::layout::page_geometry(&layout, 72.0); + let sx = 297.0 / f64::from(wpx); + assert!((x - (10.0 * k).round() * sx).abs() < 1e-9 && (x - 10.0).abs() > 0.05, "{x}"); + assert!((y - (10.0 * k).round() * (210.0 / f64::from(crate::layout::page_geometry(&layout, 72.0).1))).abs() < 1e-9, "{y}"); + assert!((w - 100.0 * k * sx).abs() < 1e-9, "{w}"); + // At 254 dpi, ten pixels to the millimetre, it is the frame's own box. + let geo = crate::layout::frame_viewports(&layout, &maps, 254.0); + assert!((geo.viewports[0].rect_mm[0] - 10.0).abs() < 1e-6, "{:?}", geo.viewports[0].rect_mm); + } + #[test] fn unwritten_objects_become_nulls_and_writing_twice_keeps_the_first() { let mut doc = Doc::new(); From 120dc37191195db2f82f613145878032e5341dc1 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 06:42:29 -0300 Subject: [PATCH 03/18] test(render): GeoPDF tests check the notices really, and SAD69 corners against PROJ The clean export's "no notice" check looked for "georeferenced", which neither georeferencing notice contains; it now looks for "georeference". The SAD69 corner is compared with the shift PROJ gives for EPSG:4326 to EPSG:4618 (within 10 m), so a shift the wrong way fails. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 10a4abd3dc..a0aaba777c 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31111,7 +31111,7 @@ fn a_layout_pdf_georeferences_its_map_frames() { assert!(pdf.starts_with("%PDF-1.7") && pdf.contains("/ExtensionLevel 3"), "{}", &pdf[..20]); assert!(pdf.contains("/VP [<< /Type /Viewport") && pdf.contains("/Subtype /GEO")); assert!(pdf.contains("/GCS << /Type /PROJCS /WKT (PROJCS[") && pdf.contains("/EPSG 31983 >>"), "{pdf}"); - assert!(!said.iter().any(|t| t.contains("georeferenced")), "{said:?}"); + assert!(!said.iter().any(|t| t.contains("georeference")), "{said:?}"); // The corners, back on the map, are the frame's rectangle: its top and bottom level, its // sides upright, and as wide as the frame at its scale. let gpts = pdf.split("/GPTS [").nth(1).and_then(|r| r.split(']').next()).unwrap(); @@ -31139,7 +31139,9 @@ fn a_layout_pdf_georeferences_its_map_frames() { said.iter().any(|t| t.starts_with("The PDF georeferences 1 map frame by its corners") && t.ends_with(&format!("({})", frame.name))), "{said:?}" ); - // On SAD69 (shifted from WGS 84) the corners are on SAD69, tens of metres from WGS 84's. + // On SAD69 (shifted from WGS 84) the corners are on SAD69, tens of metres from WGS 84's, and + // moved as PROJ moves them: EPSG:4326 to EPSG:4618 takes this corner (-46.888358 E, + // -18.436173 N) 0.000447° east and 0.000468° north (QGIS 3.32.1's gdaltransform). s.run("map.projection", &json!({"type": "EPSG:29193"})).unwrap(); let (pdf, _) = export(&mut s); let gpts = pdf.split("/GPTS [").nth(1).and_then(|r| r.split(']').next()).unwrap(); @@ -31153,6 +31155,8 @@ fn a_layout_pdf_georeferences_its_map_frames() { .unwrap(); let moved = ((n[1] - wgs84.x) * 104_600.0).hypot((n[0] - wgs84.y) * 110_600.0); assert!(pdf.contains("/EPSG 29193 >>") && moved > 10.0 && moved < 200.0, "{moved} m"); + let off = ((n[1] - wgs84.x - 0.000_447) * 104_600.0).hypot((n[0] - wgs84.y - 0.000_468) * 110_600.0); + assert!(off < 10.0, "{off} m from PROJ's shift"); // Across 180° in Web Mercator, a corner comes back on the other side of the world: left out. s.run("map.projection", &json!({"type": "mercator"})).unwrap(); s.run("layout.update", &json!({"element": frame.id, "center": [180.0, 0.0]})).unwrap(); From 69ceaa7a8220c106e5d76921a860f60a676c9b31 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 8 Nov 2026 21:52:05 -0300 Subject: [PATCH 04/18] fix(render): a GeoPDF writes no approximate coordinates, and can be turned off (#1191) Mavrick-F's review: - Natural Earth, Winkel Tripel and Eckert IV are written as WKT (Esri's names, which GDAL and PROJ read), so maps in them no longer fall back to corners. - A frame whose system has no WKT is georeferenced by its corners in longitude/latitude only if, at its centre and edge midpoints, that puts every place within 0.5 mm on paper of where it is drawn; otherwise it is left out, and said. - A frame left out keeps a viewport with no measure, so that a frame under it doesn't show through: readers take the last viewport holding a point (ISO 32000-1, 12.9.2). - "georeference": false on layout.export, and Layout > Export > Georeference PDF. - The notices are information, not warnings. Co-Authored-By: Claude Opus 5.5 --- crates/data/src/raster/wkt.rs | 48 ++++++++++++++++++++++++- crates/engine/src/cmd/layout.rs | 43 +++++++++++++++------- crates/engine/src/tests.rs | 60 +++++++++++++++++++++++++++---- crates/render/src/layout.rs | 61 +++++++++++++++++++++++++++---- crates/render/src/pdf.rs | 64 +++++++++++++++++++++++---------- crates/ui-egui/src/lib.rs | 14 +++++--- crates/ui-egui/src/ribbon.rs | 6 ++++ docs/user-guide/layouts.md | 10 ++++-- 8 files changed, 252 insertions(+), 54 deletions(-) diff --git a/crates/data/src/raster/wkt.rs b/crates/data/src/raster/wkt.rs index 87186c35b6..12e42c4aeb 100644 --- a/crates/data/src/raster/wkt.rs +++ b/crates/data/src/raster/wkt.rs @@ -1181,7 +1181,16 @@ pub fn proj4_to_wkt(def: &str, name: &str) -> Option { /// A PROJ definition as WKT1, with `code` as its EPSG authority. fn def_wkt(def: &str, name: &str, code: Option) -> Option { - let proj = proj4rs::Proj::from_proj_string(def).ok()?; + // proj4rs doesn't project the world projections RustGIS computes itself (rustgis-map's + // `world`): their ellipsoid, prime meridian and unit are read from the definition as a + // geographic one. + let proj = proj4rs::Proj::from_proj_string(def).ok().or_else(|| { + let kind = def.split_whitespace().find_map(|t| t.strip_prefix("+proj="))?; + if !matches!(kind, "natearth" | "wintri" | "eck4") { + return None; + } + proj4rs::Proj::from_proj_string(&def.replacen(&format!("+proj={kind}"), "+proj=longlat", 1)).ok() + })?; let params: Vec<(&str, Option<&str>)> = def .split_whitespace() .filter_map(|t| t.strip_prefix('+')) @@ -1422,6 +1431,22 @@ fn def_wkt(def: &str, name: &str, code: Option) -> Option { ps = vec![("central_meridian", lon0)]; "Equal_Earth" } + // Younger than WKT1 too, or never in it: the names GDAL and PROJ read (Esri's). + "natearth" => { + ps = vec![("central_meridian", lon0)]; + "Natural_Earth" + } + "eck4" => { + ps = vec![("central_meridian", lon0)]; + "Eckert_IV" + } + // Its standard parallel is PROJ's default, Aitoff's and Hammer's parallel of acos(2/π) + // (about 50.46°), unless the definition gives one. + "wintri" => { + let lat1 = get("lat_1").unwrap_or_else(|| std::f64::consts::FRAC_2_PI.acos().to_degrees()); + ps = vec![("standard_parallel_1", lat1), ("central_meridian", lon0)]; + "Winkel_Tripel" + } // Hotine's oblique Mercator by its azimuth, the general form proj4rs 0.3 projects (the // Swiss case is `somerc` above). EPSG's variant B puts the origin at the centre; its // variant A, PROJ's `+no_uoff`, at the natural origin on the centre line, which WKT1 says @@ -1593,6 +1618,27 @@ mod tests { } } + /// The world projections RustGIS computes itself and proj4rs doesn't (Natural Earth, Winkel + /// Tripel, Eckert IV) are written as WKT, on their datum, with their centre and offsets + /// (#1191: a GeoPDF of a map drawn in one). + #[test] + fn world_projections_proj4rs_lacks_are_written() { + for (def, method) in [ + ("+proj=natearth +lon_0=-60 +x_0=100 +y_0=-200 +datum=WGS84 +units=m +no_defs", "Natural_Earth"), + ("+proj=wintri +lon_0=-60 +x_0=100 +y_0=-200 +datum=WGS84 +units=m +no_defs", "Winkel_Tripel"), + ("+proj=eck4 +lon_0=-60 +x_0=100 +y_0=-200 +datum=WGS84 +units=m +no_defs", "Eckert_IV"), + ] { + let wkt = proj4_to_wkt(def, "World").unwrap_or_else(|| panic!("{def}")); + assert!(wkt.contains(&format!("PROJECTION[\"{method}\"]")), "{wkt}"); + assert!(wkt.contains("PARAMETER[\"central_meridian\",-60]") && wkt.contains("PARAMETER[\"false_easting\",100]"), "{wkt}"); + assert!(wkt.contains("SPHEROID[\"WGS 84\",6378137,298.257223563"), "{wkt}"); + } + let wkt = proj4_to_wkt("+proj=wintri +lon_0=0 +datum=WGS84 +units=m +no_defs", "W").unwrap(); + assert!(wkt.contains("PARAMETER[\"standard_parallel_1\",50.4597"), "{wkt}"); + // Another projection proj4rs can't read still has none. + assert_eq!(proj4_to_wkt("+proj=nosuch +datum=WGS84 +units=m +no_defs", "X"), None); + } + #[test] fn epsg_authority() { let wkt = r#"PROJCS["anything",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["central_meridian",-81],UNIT["metre",1],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32617"]]"#; diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index d0db4e9244..bf1e6e6bcb 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -123,7 +123,7 @@ pub fn commands() -> Vec { .params(r#"{"layout"?}"#), C::new("layout.export", "Export Layout", "Publish › Output", |s, p| super::exports::write_here(s, p, "layout.export")).inputs(layout_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() - .params(r#"{"layout"?, "path": "*.png|*.pdf|*.svg" (PNG/SVG: current page; SVG: each element a group of its own; text and shapes as SVG, map frames and surrounds as vectors, rasters and tiles in them as pictures at dpi), "dpi"?: 72..600, "allPages"?: bool (PDF only; default true: every map series page, up to 500; false: the current page)}"#), + .params(r#"{"layout"?, "path": "*.png|*.pdf|*.svg" (PNG/SVG: current page; SVG: each element a group of its own; text and shapes as SVG, map frames and surrounds as vectors, rasters and tiles in them as pictures at dpi), "dpi"?: 72..600, "allPages"?: bool (PDF only; default true: every map series page, up to 500; false: the current page), "georeference"?: bool (PDF only; default true: map frames carry their coordinates, a GeoPDF; false: none)}"#), C::new("layout.save", "Save Layout File", "Publish › Save As", |s, p| { let path = ps(p, "path").ok_or("path is required")?; let l = active(s, p)?.clone(); @@ -3491,9 +3491,14 @@ fn named_fonts_unread(layout: &Layout) -> Option<&'static str> { ) } -/// Layout `li` as a PDF (kept as vectors; every map series page when `all`) or a PNG of the -/// current page, at `dpi`. +/// Layout `li` as a PDF (kept as vectors; every map series page when `all`; its map frames +/// georeferenced) or a PNG of the current page, at `dpi`. pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) -> Result, String> { + layout_bytes_with(s, li, pdf, dpi, all, true) +} + +/// [`layout_bytes`], a PDF's map frames georeferenced only when `georeference` (#1191). +pub fn layout_bytes_with(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool, georeference: bool) -> Result, String> { let saved = s.project.layouts.get(li).ok_or("no layout")?.clone(); if let Some(note) = named_fonts_unread(&saved) { s.warn(note); @@ -3546,7 +3551,11 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) if pdf { let (w, h) = (layout.width, layout.height); // Where its map frames show, for readers to locate and measure in: a GeoPDF (#1191). - let geo = rustgis_render::layout::frame_viewports(layout, &s.project.maps, dpi); + let geo = if georeference { + rustgis_render::layout::frame_viewports(layout, &s.project.maps, dpi) + } else { + rustgis_render::layout::Georeferenced::default() + }; left_out.extend(geo.left_out); by_corners.extend(geo.by_corners); let (drawing, pictured) = rustgis_render::layout::render_drawing(layout, &mut s.project.maps, dpi, &info) @@ -3592,7 +3601,8 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool) /// Says which map frames a PDF doesn't georeference, with why, and which it georeferences by their /// corners only (#1191): one notice for each, with a count. A frame is counted once however many /// pages of a series it is on (by its id: two frames sharing a name are two), with the first -/// page's reason. +/// page's reason. Information, not warnings: nothing wrong is written (a frame left out carries no +/// coordinates, and one placed by its corners is checked to within half a millimetre). pub(crate) fn georeference_notices(s: &mut Session, left_out: &[(u32, String, &'static str)], by_corners: &[(u32, String)]) { let listed = |items: Vec| { let more = items.len().saturating_sub(10); @@ -3616,7 +3626,7 @@ pub(crate) fn georeference_notices(s: &mut Session, left_out: &[(u32, String, &' .collect(); if !out.is_empty() { let n = out.len(); - s.warn(format!("The PDF doesn't georeference {n} map frame{} ({})", plural(n), listed(out))); + s.notify(format!("The PDF doesn't georeference {n} map frame{} ({})", plural(n), listed(out)), false); } let mut seen = Vec::new(); let corners: Vec = by_corners @@ -3631,11 +3641,14 @@ pub(crate) fn georeference_notices(s: &mut Session, left_out: &[(u32, String, &' .collect(); if !corners.is_empty() { let n = corners.len(); - s.warn(format!( - "The PDF georeferences {n} map frame{} by its corners in longitude/latitude, as its coordinate system can't be written as WKT (a grid shift, or a projection WKT1 has no name for): positions between the corners are approximate ({})", - plural(n), - listed(corners) - )); + s.notify( + format!( + "The PDF georeferences {n} map frame{} by its corners in longitude/latitude, as its coordinate system can't be written as WKT: between them it puts places within 0.5 mm of where they are drawn ({})", + plural(n), + listed(corners) + ), + false, + ); } } @@ -3688,7 +3701,7 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result Result>"), "{pdf}"); - assert!( - said.iter().any(|t| t.starts_with("The PDF georeferences 1 map frame by its corners") && t.ends_with(&format!("({})", frame.name))), - "{said:?}" - ); + assert!(pdf.contains("/GCS << /Type /PROJCS /WKT (PROJCS[") && pdf.contains("PROJECTION[\"Winkel_Tripel\"]"), "{pdf}"); + assert!(!said.iter().any(|t| t.contains("georeference")), "{said:?}"); + // A projection with no WKT form (the GOES-16 fixed grid): at a large scale its corners in + // longitude/latitude put every place within half a millimetre, so it is georeferenced by + // them, and said; at a small one they wouldn't, so it is left out with a measureless viewport. + s.run("map.projection", &json!({"type": "ESRI:102498"})).unwrap(); + s.run("layout.update", &json!({"element": frame.id, "center": [-60.0, -10.0], "scale": 50_000.0})).unwrap(); + let (pdf, said) = export(&mut s); + assert!(pdf.contains("/GCS << /Type /GEOGCS /WKT (GEOGCS[") && pdf.contains("/EPSG 4326 >>"), "{said:?}"); + let corners = said.iter().find(|t| t.starts_with("The PDF georeferences 1 map frame by its corners")); + assert!(corners.is_some_and(|t| t.ends_with(&format!("({})", frame.name))), "{said:?}"); + let level = s.notices_since(0).iter().rev().find(|n| n.text.starts_with("The PDF georeferences")).unwrap().severity; + assert_eq!(level, rustgis_data::diagnostics::Severity::Info, "information, not a warning"); + s.run("layout.update", &json!({"element": frame.id, "scale": 10_000_000.0})).unwrap(); + let (pdf, said) = export(&mut s); + assert!(!pdf.contains("/Subtype /GEO") && !pdf.contains("/VP ["), "nothing to measure in, no viewports"); + assert!(said.iter().any(|t| t.contains("its corners alone would put places more than 0.5 mm off")), "{said:?}"); + // Turned off, nothing is georeferenced and nothing said. + s.run("map.projection", &json!({"type": "EPSG:31983"})).unwrap(); + s.run("layout.update", &json!({"element": frame.id, "center": [-45.0, -20.0], "scale": 2_000_000.0})).unwrap(); + let mark = s.notice_mark(); + s.run("layout.export", &json!({"path": path, "dpi": 72, "georeference": false})).unwrap(); + let plain = String::from_utf8_lossy(&std::fs::read(&path).unwrap()).into_owned(); + assert!(plain.starts_with("%PDF-1.4") && !plain.contains("/VP [")); + assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("georeference"))); // On SAD69 (shifted from WGS 84) the corners are on SAD69, tens of metres from WGS 84's, and // moved as PROJ moves them: EPSG:4326 to EPSG:4618 takes this corner (-46.888358 E, // -18.436173 N) 0.000447° east and 0.000468° north (QGIS 3.32.1's gdaltransform). @@ -31187,6 +31206,33 @@ fn a_pdf_says_which_frames_it_doesnt_georeference() { assert_eq!(said.len(), 2, "{said:?}"); assert_eq!(said[0], "The PDF doesn't georeference 2 map frames (Main: it is tilted, Main: it shows the globe)"); assert!(said[1].starts_with("The PDF georeferences 1 map frame by its corners") && said[1].ends_with("(Inset)"), "{}", said[1]); + // Information, not warnings: nothing wrong is written. + assert!(s.notices_since(mark).iter().all(|n| n.severity == rustgis_data::diagnostics::Severity::Info)); +} + +/// A frame left out of georeferencing over one that is (a tilted inset on the main map) has a +/// viewport with no measure after the main map's, so that a reader, which takes the last viewport +/// holding a point (ISO 32000-1, 12.9.2), gives the inset no coordinates rather than the main +/// map's (#1191). +#[test] +fn a_frame_left_out_covers_the_frame_under_it() { + let mut s = world(); + s.run("map.projection", &json!({"type": "EPSG:31983"})).unwrap(); + s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap(); + let frame = s.project.layouts[0].elements.iter().find(|e| e.kind.code() == "mapFrame").unwrap().clone(); + s.run("layout.update", &json!({"element": frame.id, "center": [-45.0, -20.0], "scale": 2_000_000.0})).unwrap(); + let mut inset = frame.clone(); + inset.id = 9_001; + inset.name = "Inset".into(); + (inset.x, inset.y, inset.w, inset.h) = (frame.x + 10.0, frame.y + 10.0, 40.0, 30.0); + if let rustgis_map::layout::ElementKind::MapFrame { pitch, .. } = &mut inset.kind { + *pitch = 30.0; + } + s.project.layouts[0].elements.push(inset); + let geo = rustgis_render::layout::frame_viewports(&s.project.layouts[0], &s.project.maps, 72.0); + let names: Vec<(&str, bool)> = geo.viewports.iter().map(|v| (v.name.as_str(), v.measure.is_some())).collect(); + assert_eq!(names, [(frame.name.as_str(), true), ("Inset", false)], "drawing order, the inset last"); + assert_eq!(geo.left_out.len(), 1, "{:?}", geo.left_out); } #[test] diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 3e27c1ed8e..17059ec20c 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -593,21 +593,29 @@ fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> /// The map frames of a page georeferenced for a PDF ([`frame_viewports`], #1191). #[derive(Debug, Default)] pub struct Georeferenced { - /// One for each visible frame that could be. + /// One for each visible frame, in drawing order: georeferenced, or with no measure for a frame + /// that couldn't be. None when no frame could be. pub viewports: Vec, /// The frames that couldn't be, by id and name, with why. pub left_out: Vec<(u32, String, &'static str)>, /// The frames whose coordinate system can't be written as WKT (a grid shift, or a projection - /// WKT1 has no name for), by id and name: georeferenced by their corners in longitude/latitude, - /// so a reader is right at the corners and approximate between them. + /// WKT1 has no name for), by id and name: + /// georeferenced by their corners in longitude/latitude, which put every place of the frame + /// within [`MAX_CORNER_FIT_MM`] of where it is drawn. pub by_corners: Vec<(u32, String)>, } +/// How far, on paper, a frame georeferenced by its corners in longitude/latitude may put a place +/// from where the frame draws it: past it, the frame is left out (#1191). +pub const MAX_CORNER_FIT_MM: f64 = 0.5; + /// Each visible map frame of `layout` as a PDF viewport ([`crate::pdf::Viewport`]): its place on /// the page as a page drawn at `dpi` places it, its corners' latitude and longitude, and its /// map's coordinate system as WKT. A frame on the globe or tilted shows no flat map to measure /// on, so it is left out, as is one on another body than the Earth, one whose map is missing, -/// one with a corner off the Earth and one across its projection's edge (the 180° meridian). +/// one with a corner off the Earth, one across its projection's edge (the 180° meridian) and one +/// whose system has no WKT and whose corners alone would misplace it. A frame left out keeps a +/// viewport with no measure, so that a frame it covers doesn't show through. pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced { let mut out = Georeferenced::default(); // A frame is drawn from a whole pixel of the page ([`draw_page`]), whose pixels are stretched @@ -616,11 +624,13 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced let (sx, sy) = (layout.width / f64::from(wpx.max(1)), layout.height / f64::from(hpx.max(1))); for e in layout.elements.iter().filter(|e| e.visible) { let ElementKind::MapFrame { map, center, scale, bearing, pitch, .. } = &e.kind else { continue }; + let placed = [(e.x * k).round() * sx, (e.y * k).round() * sy, e.w * k * sx, e.h * k * sy]; + let bare = || crate::pdf::Viewport { name: e.name.clone(), rect_mm: placed, measure: None }; let Some(m) = map_index(maps, map).and_then(|i| maps.get(i)) else { out.left_out.push((e.id, e.name.clone(), "its map is missing")); + out.viewports.push(bare()); continue; }; - let placed = [(e.x * k).round() * sx, (e.y * k).round() * sy, e.w * k * sx, e.h * k * sy]; match frame_viewport(e, placed, m, rustgis_map::layout::frame_camera(m, *center, *scale, *bearing, *pitch)) { Ok((v, exact)) => { if !exact { @@ -628,12 +638,38 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced } out.viewports.push(v); } - Err(why) => out.left_out.push((e.id, e.name.clone(), why)), + Err(why) => { + out.left_out.push((e.id, e.name.clone(), why)); + out.viewports.push(bare()); + } } } + // Only measureless viewports: nothing to locate in, so none. + if out.viewports.iter().all(|v| v.measure.is_none()) { + out.viewports.clear(); + } out } +/// How far, in millimetres on paper, a reader that fits a frame's corners in longitude/latitude +/// (`corners`, latitude then longitude, clockwise from the top-left) puts the places at its +/// centre and its edges' midpoints from where the frame, `w` × `h` points seen by `cam`, draws +/// them: the worst of the five, the fit being exact at the corners. `None` when one of them +/// isn't on the map. +fn corner_fit_mm(corners: &[(f64, f64); 4], map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> Option { + let [tl, tr, br, bl] = *corners; + let mut worst: f64 = 0.0; + for (u, t) in [(0.5, 0.5), (0.5, 0.0), (1.0, 0.5), (0.5, 1.0), (0.0, 0.5)] { + // Between the corners, as the unit square's (`/LPTS`) is filled: bilinearly. + let mix = |a: f64, b: f64, c: f64, d: f64| (1.0 - u) * (1.0 - t) * a + u * (1.0 - t) * b + u * t * c + (1.0 - u) * t * d; + let (lat, lon) = (mix(tl.0, tr.0, br.0, bl.0), mix(tl.1, tr.1, br.1, bl.1)); + let at = map.to_map(Coord::new((lon + 540.0).rem_euclid(360.0) - 180.0, lat))?; + let (x, y) = cam.try_to_screen(at, w, h)?; + worst = worst.max((x - u * w).hypot(y - t * h)); + } + (worst.is_finite()).then_some(worst / PT_PER_MM) +} + /// Frame `e` of `map`, placed at `placed` (millimetres: x, y, width, height) and seen by `cam`, as /// a viewport, and whether it is in the map's own coordinate system (else in longitude/latitude, /// by its corners). @@ -701,10 +737,21 @@ fn frame_viewport( for c in &mut corners { c.1 = first + ((c.1 - first + 540.0).rem_euclid(360.0) - 180.0); } + // A reader is right at the corners only: the frame is written if, between them, it + // puts no place more than half a millimetre on paper from where the frame draws it. + match corner_fit_mm(&corners, map, &cam, w, h) { + Some(off) if off <= MAX_CORNER_FIT_MM => {} + _ => { + return Err( + "its coordinate system can't be written as WKT, and its corners alone would put places more than 0.5 mm off", + ); + } + } (wkt::to_wkt(crs::Crs::LonLat).ok_or("its projection can't be read")?, Some(4326)) } }; - let viewport = crate::pdf::Viewport { name: e.name.clone(), rect_mm: placed, corners, wkt, projected: exact, epsg }; + let measure = crate::pdf::GeoMeasure { corners, wkt, projected: exact, epsg }; + let viewport = crate::pdf::Viewport { name: e.name.clone(), rect_mm: placed, measure: Some(measure) }; Ok((viewport, exact)) } diff --git a/crates/render/src/pdf.rs b/crates/render/src/pdf.rs index 084dd36365..f561ac14e4 100644 --- a/crates/render/src/pdf.rs +++ b/crates/render/src/pdf.rs @@ -20,8 +20,11 @@ pub struct Page { viewports: Vec, } -/// A map frame's place on a page and where on the Earth it shows, so that a reader can locate and -/// measure in it: an ISO 32000 geospatial viewport, which makes the document a GeoPDF (#1191). +/// A map frame's place on a page and, when it is georeferenced, where on the Earth it shows, so +/// that a reader can locate and measure in it: an ISO 32000 geospatial viewport, which makes the +/// document a GeoPDF (#1191). A frame that isn't georeferenced has a viewport with no measure, so +/// that a georeferenced frame under it doesn't show through: a reader takes the last viewport +/// holding a point, the page's viewports being in drawing order (ISO 32000-1, 12.9.2). #[derive(Clone, Debug, PartialEq)] pub struct Viewport { /// The frame's name. @@ -29,6 +32,13 @@ pub struct Viewport { /// Its rectangle on the page, in millimetres from the page's top-left corner: x, y, width, /// height. pub rect_mm: [f64; 4], + /// Where on the Earth it shows; `None` for a frame that isn't georeferenced. + pub measure: Option, +} + +/// Where a georeferenced frame shows on the Earth ([`Viewport`]). +#[derive(Clone, Debug, PartialEq)] +pub struct GeoMeasure { /// Its corners' latitude and longitude, in degrees on its coordinate system's own datum, /// clockwise from the top-left. pub corners: [(f64, f64); 4], @@ -45,17 +55,21 @@ impl Viewport { let k = 72.0 / 25.4; let [x, y, w, h] = self.rect_mm; let (x1, x2, top, bottom) = (x * k, (x + w) * k, page_h - y * k, page_h - (y + h) * k); - let gpts: Vec = self.corners.iter().map(|(lat, lon)| format!("{lat:.9} {lon:.9}")).collect(); - let kind = if self.projected { "PROJCS" } else { "GEOGCS" }; - let epsg = self.epsg.map(|c| format!(" /EPSG {c}")).unwrap_or_default(); - format!( - "<< /Type /Viewport /BBox [{x1:.3} {bottom:.3} {x2:.3} {top:.3}] /Name {} /Measure << /Type /Measure /Subtype /GEO \ - /Bounds [0 0 0 1 1 1 1 0] /GPTS [{}] /LPTS [0 1 1 1 1 0 0 0] /GCS << /Type /{kind} /WKT {}{epsg} >> \ - /PDU [/M /SQM /DEG] >> >>", - text_string(&self.name), - gpts.join(" "), - literal(&self.wkt), - ) + let measure = match &self.measure { + None => String::new(), + Some(m) => { + let gpts: Vec = m.corners.iter().map(|(lat, lon)| format!("{lat:.9} {lon:.9}")).collect(); + let kind = if m.projected { "PROJCS" } else { "GEOGCS" }; + let epsg = m.epsg.map(|c| format!(" /EPSG {c}")).unwrap_or_default(); + format!( + " /Measure << /Type /Measure /Subtype /GEO /Bounds [0 0 0 1 1 1 1 0] /GPTS [{}] /LPTS [0 1 1 1 1 0 0 0] \ + /GCS << /Type /{kind} /WKT {}{epsg} >> /PDU [/M /SQM /DEG] >>", + gpts.join(" "), + literal(&m.wkt), + ) + } + }; + format!("<< /Type /Viewport /BBox [{x1:.3} {bottom:.3} {x2:.3} {top:.3}] /Name {}{measure} >>", text_string(&self.name)) } } @@ -217,8 +231,8 @@ impl Doc { /// Writes the pages as a PDF document. pub fn write(pages: &[Page], title: &str) -> Vec { // Georeferenced frames make the document PDF 1.7 with Adobe's extension level 3, where - // viewports are defined (since taken into ISO 32000-2); what else it holds is 1.4. - let geo = pages.iter().any(|p| !p.viewports.is_empty()); + // geospatial measures are defined (since taken into ISO 32000-2); what else it holds is 1.4. + let geo = pages.iter().any(|p| p.viewports.iter().any(|v| v.measure.is_some())); let mut doc = if geo { Doc::of_version("1.7") } else { Doc::new() }; let (catalog, tree, info) = (doc.reserve(), doc.reserve(), doc.reserve()); let ids: Vec = pages.iter().map(|_| doc.reserve()).collect(); @@ -362,10 +376,12 @@ pub(crate) mod tests { let v = Viewport { name: "Main map".into(), rect_mm: [25.4, 25.4, 50.8, 25.4], - corners: [(-19.5, -45.5), (-19.5, -44.5), (-20.5, -44.5), (-20.5, -45.5)], - wkt: wkt.clone(), - projected: true, - epsg: Some(31983), + measure: Some(GeoMeasure { + corners: [(-19.5, -45.5), (-19.5, -44.5), (-20.5, -44.5), (-20.5, -45.5)], + wkt: wkt.clone(), + projected: true, + epsg: Some(31983), + }), }; let page_h = 297.0 * 72.0 / 25.4; let d = v.dictionary(page_h); @@ -384,6 +400,16 @@ pub(crate) mod tests { let plain = write(&[Page::new(&a, 210.0, 297.0)], "Plain"); assert!(plain.starts_with(b"%PDF-1.4") && !String::from_utf8_lossy(&plain).contains("/Extensions")); assert_eq!(literal("é"), ""); + // A frame left out is a viewport with no measure, after the one it covers (drawing order). + let bare = Viewport { name: "Inset".into(), rect_mm: [30.0, 30.0, 10.0, 10.0], measure: None }; + assert_eq!( + bare.dictionary(page_h), + format!( + "<< /Type /Viewport /BBox [85.039 {:.3} 113.386 {:.3}] /Name (Inset) >>", + page_h - 40.0 * 72.0 / 25.4, + page_h - 30.0 * 72.0 / 25.4 + ) + ); } /// A frame's viewport is where the page draws it: from the whole pixel it starts on at the diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index aba70cf030..cab0671e7d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -709,6 +709,8 @@ pub struct UiState { pub hidden_tabs: Vec, /// Extra Quick Access Toolbar commands. pub quick_access: Vec, + /// Whether a layout's PDF georeferences its map frames (Layout › Export › Georeference PDF). + pub(crate) pdf_georeference: bool, /// The native title bar the header is drawn into; set by the desktop app on macOS. pub titlebar: Option, /// Saved pane arrangements: (name, left pane, right pane in view, ribbon tab, right panes open). @@ -980,6 +982,7 @@ impl Default for UiState { geolibre_zarr: Vec::new(), hidden_tabs: Vec::new(), quick_access: Vec::new(), + pdf_georeference: true, titlebar: None, pane_sets: Vec::new(), layer_filter: String::new(), @@ -1545,11 +1548,13 @@ impl RustGisApp { r } - /// A layout as PDF or PNG bytes, guarded like `Session::run`. + /// A layout as PDF or PNG bytes, guarded like `Session::run`; a PDF georeferenced as Layout › + /// Export › Georeference PDF says. fn layout_bytes(&mut self, li: usize, pdf: bool, dpi: f64) -> Result, String> { + let georeference = self.ui.pdf_georeference; let session = &mut self.session; let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - rustgis_engine::cmd::layout::layout_bytes(session, li, pdf, dpi, true) + rustgis_engine::cmd::layout::layout_bytes_with(session, li, pdf, dpi, true, georeference) })) .unwrap_or_else(|_| Err("the layout export failed unexpectedly".into())); if let Err(e) = &r { @@ -3408,10 +3413,11 @@ impl RustGisApp { if pdf_only { (&["pdf"], tl!("PDF")) } else { (&["pdf", "png", "svg"], tl!("PDF, PNG or SVG")) }; if let Some(path) = dialogs::pick_save(&format!("{name}.pdf"), exts, kinds) { // Drawn on a thread of its own, with progress and Cancel in the status bar. + let params = json!({"layout": li, "path": path, "dpi": 200, "georeference": self.ui.pdf_georeference}); #[cfg(not(target_arch = "wasm32"))] - return exportjob::start(self, "layout.export", json!({"layout": li, "path": path, "dpi": 200})); + return exportjob::start(self, "layout.export", params); #[cfg(target_arch = "wasm32")] - return self.run("layout.export", json!({"layout": li, "path": path, "dpi": 200})); + return self.run("layout.export", params); } } } diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 7c919bac6d..049cc8d0da 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3209,6 +3209,12 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) { small(ui, app, "save_as", "Save Layout File", "Save the layout as a layout file.", "ui.saveLayout", Value::Null, false); small(ui, app, "map", "Show Map", "Close the layout and show the map.", "layout.close", Value::Null, false); }); + col(ui, |ui| { + // A PDF's map frames carry their coordinates (a GeoPDF) unless this is cleared. + ui.checkbox(&mut app.ui.pdf_georeference, tl!("Georeference PDF")).on_hover_text(tl!( + "Export and print the layout's map frames with their coordinates, so PDF readers can locate and measure in them." + )); + }); }); } diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 4fca8c9bd4..5a9d9aaaf2 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -302,9 +302,13 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu A PDF is also a GeoPDF: each map frame is georeferenced (an ISO 32000 geospatial viewport, the form GDAL, QGIS, Acrobat and Avenza read), with its map's coordinate system as WKT and its EPSG code when it has one, so a reader shows coordinates and measures in it, and GDAL or QGIS open the - page as a georeferenced raster. A tilted frame, one on the globe, and one with a corner off the - Earth aren't georeferenced, and the export says which. A frame in a projection with no WKT form - is georeferenced by its corners in longitude/latitude, approximate between them, and said. + page as a georeferenced raster. It carries coordinates only: no layers and no feature + attributes. A tilted frame, one on the globe, and one with a corner off the Earth aren't + georeferenced, and the export says which; such a frame over another carries no coordinates, so + a reader doesn't show the other's through it. A frame in a projection with no WKT form is + georeferenced by its corners in longitude/latitude when, between them, that puts every place + within 0.5 mm of where it is drawn, and left out otherwise. **Georeference PDF** in the layout's + Export group (`"georeference": false` for `layout.export`) leaves the coordinates out. In SVG each element is a group (an Inkscape layer) named after it: text stays text and rectangles, ellipses and lines stay shapes, and map frames and the other surrounds are kept as vectors as in a PDF (their labels as the letters' shapes, each run labelled with its text), so the page can be From 4aef2f972b471794998c1054ef2c549cbd7ef5b4 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 00:17:43 -0300 Subject: [PATCH 05/18] fix(render): a frame left out of a GeoPDF is measured on paper, not left measureless (#1191) Acrobat Reader shows no coordinates anywhere on a page that has a viewport with no /Measure. A frame left out of georeferencing now gets a rectilinear measure in paper millimetres, so it still covers the frame under it (the last viewport holding a point wins, ISO 32000-1 12.9.2) while the main map keeps its coordinates. Checked in Acrobat Reader and with GDAL. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 8 ++++---- crates/render/src/layout.rs | 8 ++++---- crates/render/src/pdf.rs | 31 +++++++++++++++++-------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 52a62c0bf7..3b44d380ce 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31725,7 +31725,7 @@ fn a_layout_pdf_georeferences_its_map_frames() { assert!(!said.iter().any(|t| t.contains("georeference")), "{said:?}"); // A projection with no WKT form (the GOES-16 fixed grid): at a large scale its corners in // longitude/latitude put every place within half a millimetre, so it is georeferenced by - // them, and said; at a small one they wouldn't, so it is left out with a measureless viewport. + // them, and said; at a small one they wouldn't, so it is left out: no viewports. s.run("map.projection", &json!({"type": "ESRI:102498"})).unwrap(); s.run("layout.update", &json!({"element": frame.id, "center": [-60.0, -10.0], "scale": 50_000.0})).unwrap(); let (pdf, said) = export(&mut s); @@ -31799,9 +31799,9 @@ fn a_pdf_says_which_frames_it_doesnt_georeference() { } /// A frame left out of georeferencing over one that is (a tilted inset on the main map) has a -/// viewport with no measure after the main map's, so that a reader, which takes the last viewport -/// holding a point (ISO 32000-1, 12.9.2), gives the inset no coordinates rather than the main -/// map's (#1191). +/// viewport without coordinates after the main map's, so that a reader, which takes the last +/// viewport holding a point (ISO 32000-1, 12.9.2), gives the inset no coordinates rather than the +/// main map's (#1191). #[test] fn a_frame_left_out_covers_the_frame_under_it() { let mut s = world(); diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 17059ec20c..b248beb3fd 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -593,8 +593,8 @@ fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> /// The map frames of a page georeferenced for a PDF ([`frame_viewports`], #1191). #[derive(Debug, Default)] pub struct Georeferenced { - /// One for each visible frame, in drawing order: georeferenced, or with no measure for a frame - /// that couldn't be. None when no frame could be. + /// One for each visible frame, in drawing order: georeferenced, or measured on paper only for + /// a frame that couldn't be. None when no frame could be. pub viewports: Vec, /// The frames that couldn't be, by id and name, with why. pub left_out: Vec<(u32, String, &'static str)>, @@ -615,7 +615,7 @@ pub const MAX_CORNER_FIT_MM: f64 = 0.5; /// on, so it is left out, as is one on another body than the Earth, one whose map is missing, /// one with a corner off the Earth, one across its projection's edge (the 180° meridian) and one /// whose system has no WKT and whose corners alone would misplace it. A frame left out keeps a -/// viewport with no measure, so that a frame it covers doesn't show through. +/// viewport with no coordinates, so that a frame it covers doesn't show through. pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced { let mut out = Georeferenced::default(); // A frame is drawn from a whole pixel of the page ([`draw_page`]), whose pixels are stretched @@ -644,7 +644,7 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced } } } - // Only measureless viewports: nothing to locate in, so none. + // Only frames without coordinates: nothing to locate in, so none. if out.viewports.iter().all(|v| v.measure.is_none()) { out.viewports.clear(); } diff --git a/crates/render/src/pdf.rs b/crates/render/src/pdf.rs index f561ac14e4..aaec9e14d4 100644 --- a/crates/render/src/pdf.rs +++ b/crates/render/src/pdf.rs @@ -22,9 +22,11 @@ pub struct Page { /// A map frame's place on a page and, when it is georeferenced, where on the Earth it shows, so /// that a reader can locate and measure in it: an ISO 32000 geospatial viewport, which makes the -/// document a GeoPDF (#1191). A frame that isn't georeferenced has a viewport with no measure, so -/// that a georeferenced frame under it doesn't show through: a reader takes the last viewport -/// holding a point, the page's viewports being in drawing order (ISO 32000-1, 12.9.2). +/// document a GeoPDF (#1191). A frame that isn't georeferenced has a viewport too, so that a +/// georeferenced frame under it doesn't show through: a reader takes the last viewport holding a +/// point, the page's viewports being in drawing order (ISO 32000-1, 12.9.2). Its measure is +/// millimetres on paper (a rectilinear one), with no coordinates: Acrobat Reader shows no +/// coordinates anywhere on a page one of whose viewports has no measure. #[derive(Clone, Debug, PartialEq)] pub struct Viewport { /// The frame's name. @@ -32,7 +34,8 @@ pub struct Viewport { /// Its rectangle on the page, in millimetres from the page's top-left corner: x, y, width, /// height. pub rect_mm: [f64; 4], - /// Where on the Earth it shows; `None` for a frame that isn't georeferenced. + /// Where on the Earth it shows; `None` for a frame that isn't georeferenced (measured in + /// millimetres on paper). pub measure: Option, } @@ -56,7 +59,11 @@ impl Viewport { let [x, y, w, h] = self.rect_mm; let (x1, x2, top, bottom) = (x * k, (x + w) * k, page_h - y * k, page_h - (y + h) * k); let measure = match &self.measure { - None => String::new(), + // Millimetres on paper: a point is 25.4/72 of one. + None => " /Measure << /Type /Measure /Subtype /RL /R (1 mm = 1 mm) \ + /X [<< /Type /NumberFormat /U (mm) /C 0.352778 >>] /D [<< /Type /NumberFormat /U (mm) /C 1 >>] \ + /A [<< /Type /NumberFormat /U (sq mm) /C 1 >>] >>" + .to_string(), Some(m) => { let gpts: Vec = m.corners.iter().map(|(lat, lon)| format!("{lat:.9} {lon:.9}")).collect(); let kind = if m.projected { "PROJCS" } else { "GEOGCS" }; @@ -400,16 +407,12 @@ pub(crate) mod tests { let plain = write(&[Page::new(&a, 210.0, 297.0)], "Plain"); assert!(plain.starts_with(b"%PDF-1.4") && !String::from_utf8_lossy(&plain).contains("/Extensions")); assert_eq!(literal("é"), ""); - // A frame left out is a viewport with no measure, after the one it covers (drawing order). + // A frame left out is a viewport measured in millimetres on paper, with no coordinates. let bare = Viewport { name: "Inset".into(), rect_mm: [30.0, 30.0, 10.0, 10.0], measure: None }; - assert_eq!( - bare.dictionary(page_h), - format!( - "<< /Type /Viewport /BBox [85.039 {:.3} 113.386 {:.3}] /Name (Inset) >>", - page_h - 40.0 * 72.0 / 25.4, - page_h - 30.0 * 72.0 / 25.4 - ) - ); + let d = bare.dictionary(page_h); + let bbox = format!("/BBox [85.039 {:.3} 113.386 {:.3}] /Name (Inset)", page_h - 40.0 * 72.0 / 25.4, page_h - 30.0 * 72.0 / 25.4); + assert!(d.starts_with("<< /Type /Viewport ") && d.contains(&bbox), "{d}"); + assert!(d.contains("/Subtype /RL") && d.contains("/U (mm) /C 0.352778") && !d.contains("/GEO") && !d.contains("/GPTS"), "{d}"); } /// A frame's viewport is where the page draws it: from the whole pixel it starts on at the From df4d6c618d465177d05d5b08dab0399c91291db5 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 01:23:02 -0300 Subject: [PATCH 06/18] fix(render): cut a frame drawn over another out of its viewport, no measureless ones (#1191) Acrobat Reader shows no coordinates on a page with a viewport that has no /Measure, and crashes (26.3, access violation) when its Geospatial Location pointer enters one with a rectilinear measure, so the previous approach broke it. Viewports no longer overlap: a frame drawn over another, georeferenced or not, is cut out of the other's viewport, which becomes up to four rectangles around it, each a /GEO viewport with its own corners on the same projection. A frame left out has no viewport. Checked in Acrobat Reader 26.3 on an exported PDF: the main map reads right all around a tilted inset, the inset reads nothing, no crash. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 40 ++++++++--- crates/render/src/layout.rs | 130 +++++++++++++++++++++++++++--------- crates/render/src/pdf.rs | 57 ++++++---------- docs/user-guide/layouts.md | 5 +- 4 files changed, 155 insertions(+), 77 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 3b44d380ce..08f26fcc5b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31798,18 +31798,19 @@ fn a_pdf_says_which_frames_it_doesnt_georeference() { assert!(s.notices_since(mark).iter().all(|n| n.severity == rustgis_data::diagnostics::Severity::Info)); } -/// A frame left out of georeferencing over one that is (a tilted inset on the main map) has a -/// viewport without coordinates after the main map's, so that a reader, which takes the last -/// viewport holding a point (ISO 32000-1, 12.9.2), gives the inset no coordinates rather than the -/// main map's (#1191). +/// A frame drawn over another (an inset on the main map) is cut out of the other's viewport, so +/// that a reader shows the main map's coordinates around the inset and not in it: none there when +/// the inset is left out of georeferencing (tilted), the inset's own when it isn't (#1191). No two +/// viewports overlap, and none goes without coordinates, which Acrobat Reader doesn't take. #[test] -fn a_frame_left_out_covers_the_frame_under_it() { +fn a_frame_drawn_over_another_is_cut_out_of_its_viewport() { let mut s = world(); s.run("map.projection", &json!({"type": "EPSG:31983"})).unwrap(); s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap(); let frame = s.project.layouts[0].elements.iter().find(|e| e.kind.code() == "mapFrame").unwrap().clone(); s.run("layout.update", &json!({"element": frame.id, "center": [-45.0, -20.0], "scale": 2_000_000.0})).unwrap(); - let mut inset = frame.clone(); + // The inset shows the main map's place, as it is once moved there. + let mut inset = s.project.layouts[0].elements.iter().find(|e| e.id == frame.id).unwrap().clone(); inset.id = 9_001; inset.name = "Inset".into(); (inset.x, inset.y, inset.w, inset.h) = (frame.x + 10.0, frame.y + 10.0, 40.0, 30.0); @@ -31817,10 +31818,33 @@ fn a_frame_left_out_covers_the_frame_under_it() { *pitch = 30.0; } s.project.layouts[0].elements.push(inset); + let inside = |v: &rustgis_render::pdf::Viewport, (x, y): (f64, f64)| { + let [vx, vy, w, h] = v.rect_mm; + x > vx && x < vx + w && y > vy && y < vy + h + }; + let (in_inset, beside) = ((frame.x + 30.0, frame.y + 25.0), (frame.x + 5.0, frame.y + 5.0)); let geo = rustgis_render::layout::frame_viewports(&s.project.layouts[0], &s.project.maps, 72.0); - let names: Vec<(&str, bool)> = geo.viewports.iter().map(|v| (v.name.as_str(), v.measure.is_some())).collect(); - assert_eq!(names, [(frame.name.as_str(), true), ("Inset", false)], "drawing order, the inset last"); assert_eq!(geo.left_out.len(), 1, "{:?}", geo.left_out); + assert_eq!(geo.viewports.len(), 4, "the main frame around the inset: above, below, left and right of it"); + assert!(geo.viewports.iter().all(|v| v.name == frame.name && v.measure.projected)); + assert!(!geo.viewports.iter().any(|v| inside(v, in_inset)) && geo.viewports.iter().any(|v| inside(v, beside))); + let area: f64 = geo.viewports.iter().map(|v| v.rect_mm[2] * v.rect_mm[3]).sum(); + let whole = frame.w * frame.h - 40.0 * 30.0; + assert!((area - whole).abs() < 0.02 * whole, "{area} {whole}"); + // Where the main map's pieces meet, they give the same place: the band above the inset ends + // where the strip left of it starts. + let (top, left) = (&geo.viewports[0], &geo.viewports[2]); + let (a, b) = (top.measure.corners[3], left.measure.corners[0]); + assert!((a.0 - b.0).abs() < 1e-9 && (a.1 - b.1).abs() < 1e-9, "{a:?} {b:?}"); + // An inset that is georeferenced has its own viewport, drawn last, over no other. + let inset = s.project.layouts[0].elements.iter_mut().find(|e| e.id == 9_001).unwrap(); + if let rustgis_map::layout::ElementKind::MapFrame { pitch, .. } = &mut inset.kind { + *pitch = 0.0; + } + let geo = rustgis_render::layout::frame_viewports(&s.project.layouts[0], &s.project.maps, 72.0); + let names: Vec<&str> = geo.viewports.iter().map(|v| v.name.as_str()).collect(); + assert_eq!(names, [frame.name.as_str(), frame.name.as_str(), frame.name.as_str(), frame.name.as_str(), "Inset"], "{:?}", geo.left_out); + assert!(geo.left_out.is_empty() && inside(&geo.viewports[4], in_inset)); } #[test] diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index b248beb3fd..4a3774ee94 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -593,8 +593,8 @@ fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> /// The map frames of a page georeferenced for a PDF ([`frame_viewports`], #1191). #[derive(Debug, Default)] pub struct Georeferenced { - /// One for each visible frame, in drawing order: georeferenced, or measured on paper only for - /// a frame that couldn't be. None when no frame could be. + /// The georeferenced frames, in drawing order, each in the rectangles no frame drawn over it + /// covers ([`uncovered`]): no two overlap, so a reader never has two to choose from. pub viewports: Vec, /// The frames that couldn't be, by id and name, with why. pub left_out: Vec<(u32, String, &'static str)>, @@ -614,43 +614,76 @@ pub const MAX_CORNER_FIT_MM: f64 = 0.5; /// map's coordinate system as WKT. A frame on the globe or tilted shows no flat map to measure /// on, so it is left out, as is one on another body than the Earth, one whose map is missing, /// one with a corner off the Earth, one across its projection's edge (the 180° meridian) and one -/// whose system has no WKT and whose corners alone would misplace it. A frame left out keeps a -/// viewport with no coordinates, so that a frame it covers doesn't show through. +/// whose system has no WKT and whose corners alone would misplace it. A frame drawn over another, +/// left out or not, is cut out of the other's viewport, so that the other's coordinates don't show +/// through it: a viewport without coordinates would do it by the standard (the last viewport +/// holding a point is taken, ISO 32000-1, 12.9.2), but Acrobat Reader doesn't take one. pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced { let mut out = Georeferenced::default(); // A frame is drawn from a whole pixel of the page ([`draw_page`]), whose pixels are stretched // over its paper: the viewport is that box, in millimetres. let (wpx, hpx, k) = page_geometry(layout, dpi); let (sx, sy) = (layout.width / f64::from(wpx.max(1)), layout.height / f64::from(hpx.max(1))); - for e in layout.elements.iter().filter(|e| e.visible) { + let frames: Vec<_> = layout + .elements + .iter() + .filter(|e| e.visible && matches!(e.kind, ElementKind::MapFrame { .. })) + .map(|e| (e, [(e.x * k).round() * sx, (e.y * k).round() * sy, e.w * k * sx, e.h * k * sy])) + .collect(); + for (i, (e, placed)) in frames.iter().enumerate() { let ElementKind::MapFrame { map, center, scale, bearing, pitch, .. } = &e.kind else { continue }; - let placed = [(e.x * k).round() * sx, (e.y * k).round() * sy, e.w * k * sx, e.h * k * sy]; - let bare = || crate::pdf::Viewport { name: e.name.clone(), rect_mm: placed, measure: None }; let Some(m) = map_index(maps, map).and_then(|i| maps.get(i)) else { out.left_out.push((e.id, e.name.clone(), "its map is missing")); - out.viewports.push(bare()); continue; }; - match frame_viewport(e, placed, m, rustgis_map::layout::frame_camera(m, *center, *scale, *bearing, *pitch)) { + // What the frames drawn over it leave of it. + let mut shown = vec![*placed]; + for (_, over) in &frames[i + 1..] { + shown = shown.into_iter().flat_map(|r| uncovered(r, *over)).collect(); + } + if shown.is_empty() { + continue; + } + if shown.len() > MAX_VIEWPORTS_A_FRAME { + out.left_out.push((e.id, e.name.clone(), "too many frames are drawn over it")); + continue; + } + match frame_viewport(e, *placed, &shown, m, rustgis_map::layout::frame_camera(m, *center, *scale, *bearing, *pitch)) { Ok((v, exact)) => { if !exact { out.by_corners.push((e.id, e.name.clone())); } - out.viewports.push(v); - } - Err(why) => { - out.left_out.push((e.id, e.name.clone(), why)); - out.viewports.push(bare()); + out.viewports.extend(v); } + Err(why) => out.left_out.push((e.id, e.name.clone(), why)), } } - // Only frames without coordinates: nothing to locate in, so none. - if out.viewports.iter().all(|v| v.measure.is_none()) { - out.viewports.clear(); - } out } +/// The most rectangles a frame's viewport is cut into by the frames drawn over it; past it, the +/// frame is left out. +const MAX_VIEWPORTS_A_FRAME: usize = 64; + +/// A strip narrower than this, in millimetres, left of a frame beside one drawn over it, is +/// dropped: no one points into it. +const MIN_STRIP_MM: f64 = 0.01; + +/// Rectangle `r` (x, y, width, height) without `cut`: up to four rectangles, the bands above and +/// below the cut, and beside it between them. +fn uncovered(r: [f64; 4], cut: [f64; 4]) -> Vec<[f64; 4]> { + let (x0, y0, x1, y1) = (r[0], r[1], r[0] + r[2], r[1] + r[3]); + let (cx0, cy0, cx1, cy1) = (cut[0].max(x0), cut[1].max(y0), (cut[0] + cut[2]).min(x1), (cut[1] + cut[3]).min(y1)); + if cx0 >= cx1 || cy0 >= cy1 { + return vec![r]; + } + [(x0, y0, x1, cy0), (x0, cy1, x1, y1), (x0, cy0, cx0, cy1), (cx1, cy0, x1, cy1)] + .into_iter() + .filter(|(a, b, c, d)| c - a >= MIN_STRIP_MM && d - b >= MIN_STRIP_MM) + .map(|(a, b, c, d)| [a, b, c - a, d - b]) + .collect() +} + /// How far, in millimetres on paper, a reader that fits a frame's corners in longitude/latitude /// (`corners`, latitude then longitude, clockwise from the top-left) puts the places at its /// centre and its edges' midpoints from where the frame, `w` × `h` points seen by `cam`, draws @@ -671,14 +704,15 @@ fn corner_fit_mm(corners: &[(f64, f64); 4], map: &Map, cam: &rustgis_map::Camera } /// Frame `e` of `map`, placed at `placed` (millimetres: x, y, width, height) and seen by `cam`, as -/// a viewport, and whether it is in the map's own coordinate system (else in longitude/latitude, -/// by its corners). +/// a viewport for each rectangle of it in `shown`, and whether it is in the map's own coordinate +/// system (else in longitude/latitude, by its corners). fn frame_viewport( e: &rustgis_map::layout::Element, placed: [f64; 4], + shown: &[[f64; 4]], map: &Map, cam: rustgis_map::Camera, -) -> Result<(crate::pdf::Viewport, bool), &'static str> { +) -> Result<(Vec, bool), &'static str> { use rustgis_data::raster::{crs, wkt}; if map.projection.is_globe() { return Err("it shows the globe"); @@ -707,8 +741,8 @@ fn frame_viewport( _ => (wkt::to_wkt(crs::Crs::WebMercator).map(|t| (t, Some(3857))), None), }; let (w, h) = (e.w * PT_PER_MM, e.h * PT_PER_MM); - let mut corners = [(0.0, 0.0); 4]; - for (i, (sx, sy)) in [(0.0, 0.0), (w, 0.0), (w, h), (0.0, h)].into_iter().enumerate() { + // The latitude and longitude of the frame's point `(sx, sy)` (points from its top-left). + let place = |sx: f64, sy: f64| -> Result<(f64, f64), &'static str> { let m = cam.try_to_map(sx, sy, w, h).ok_or("a corner is off the Earth")?; let c = map.to_lonlat(m).ok_or("a corner is off the Earth")?; // A corner past the projection's edge (Web Mercator's world repeated across 180°) is at a @@ -725,11 +759,16 @@ fn frame_viewport( (Some(_), Some(def)) => crs::lonlat_on_datum_of(def, c).ok_or("its corners can't be moved onto its datum")?, _ => c, }; - corners[i] = (c.y, c.x); - } + Ok((c.y, c.x)) + }; + // Clockwise from the top-left, as `/LPTS` takes them. + let corners_of = |[x0, y0, x1, y1]: [f64; 4]| -> Result<[(f64, f64); 4], &'static str> { + Ok([place(x0, y0)?, place(x1, y0)?, place(x1, y1)?, place(x0, y1)?]) + }; + let mut corners = corners_of([0.0, 0.0, w, h])?; let exact = system.is_some(); - let (wkt, epsg) = match system { - Some(s) => s, + let (wkt, epsg) = match &system { + Some((text, code)) => (text.clone(), *code), None => { // In longitude/latitude the corners are fitted as they are: across 180° they are kept // within half a turn of the first, so that the frame isn't taken to span the world. @@ -750,9 +789,23 @@ fn frame_viewport( (wkt::to_wkt(crs::Crs::LonLat).ok_or("its projection can't be read")?, Some(4326)) } }; - let measure = crate::pdf::GeoMeasure { corners, wkt, projected: exact, epsg }; - let viewport = crate::pdf::Viewport { name: e.name.clone(), rect_mm: placed, measure: Some(measure) }; - Ok((viewport, exact)) + let first = corners[0].1; + let mut viewports = Vec::with_capacity(shown.len()); + for r in shown { + // The rectangle's corners in the frame's points, the frame being `placed` on the page. + let to = |mm: f64, from: f64, size: f64, pts: f64| (mm - from) / size * pts; + let (x0, y0) = (to(r[0], placed[0], placed[2], w), to(r[1], placed[1], placed[3], h)); + let (x1, y1) = (to(r[0] + r[2], placed[0], placed[2], w), to(r[1] + r[3], placed[1], placed[3], h)); + let mut corners = if *r == placed { corners } else { corners_of([x0, y0, x1, y1])? }; + if !exact { + for c in &mut corners { + c.1 = first + ((c.1 - first + 540.0).rem_euclid(360.0) - 180.0); + } + } + let measure = crate::pdf::GeoMeasure { corners, wkt: wkt.clone(), projected: exact, epsg }; + viewports.push(crate::pdf::Viewport { name: e.name.clone(), rect_mm: *r, measure }); + } + Ok((viewports, exact)) } /// Strokes the ring `ring` (longitude, latitude) on a frame's map `width` points wide: another @@ -2357,6 +2410,23 @@ pub fn map_index(maps: &[rustgis_map::Map], name: &str) -> Option { mod tests { use super::*; + /// A frame drawn over another takes its rectangle out of the other's: the bands above and below + /// it and the strips beside it are left, a strip too narrow to point into is dropped, and one + /// that covers it all leaves nothing (#1191). + #[test] + fn a_frame_drawn_over_another_leaves_the_rest_of_it() { + let r = [0.0, 0.0, 100.0, 50.0]; + assert_eq!(uncovered(r, [200.0, 0.0, 10.0, 10.0]), [r], "apart"); + assert_eq!( + uncovered(r, [10.0, 10.0, 20.0, 20.0]), + [[0.0, 0.0, 100.0, 10.0], [0.0, 30.0, 100.0, 20.0], [0.0, 10.0, 10.0, 20.0], [30.0, 10.0, 70.0, 20.0]] + ); + // Over its top-left corner, and past it. + assert_eq!(uncovered(r, [-5.0, -5.0, 15.0, 15.0]), [[0.0, 10.0, 100.0, 40.0], [10.0, 0.0, 90.0, 10.0]]); + assert_eq!(uncovered(r, [0.0, 0.0, 100.0, 49.995]), Vec::<[f64; 4]>::new(), "a 0.005 mm strip is dropped"); + assert!(uncovered(r, [-1.0, -1.0, 102.0, 52.0]).is_empty()); + } + /// A page with a frame of the built-in world and a title. fn vector_page() -> (Layout, Vec) { let mut layout = Layout::new("Vectors", 120.0, 90.0); diff --git a/crates/render/src/pdf.rs b/crates/render/src/pdf.rs index aaec9e14d4..d8bff804f6 100644 --- a/crates/render/src/pdf.rs +++ b/crates/render/src/pdf.rs @@ -20,13 +20,11 @@ pub struct Page { viewports: Vec, } -/// A map frame's place on a page and, when it is georeferenced, where on the Earth it shows, so -/// that a reader can locate and measure in it: an ISO 32000 geospatial viewport, which makes the -/// document a GeoPDF (#1191). A frame that isn't georeferenced has a viewport too, so that a -/// georeferenced frame under it doesn't show through: a reader takes the last viewport holding a -/// point, the page's viewports being in drawing order (ISO 32000-1, 12.9.2). Its measure is -/// millimetres on paper (a rectilinear one), with no coordinates: Acrobat Reader shows no -/// coordinates anywhere on a page one of whose viewports has no measure. +/// A georeferenced map frame's place on a page, or the part of it no other frame is drawn over, +/// and where on the Earth it shows, so that a reader can locate and measure in it: an ISO 32000 +/// geospatial viewport, which makes the document a GeoPDF (#1191). Every viewport has a +/// geospatial measure: Acrobat Reader shows no coordinates on a page one of whose viewports has +/// none, and stops when its pointer enters one measured otherwise (a rectilinear measure). #[derive(Clone, Debug, PartialEq)] pub struct Viewport { /// The frame's name. @@ -34,9 +32,8 @@ pub struct Viewport { /// Its rectangle on the page, in millimetres from the page's top-left corner: x, y, width, /// height. pub rect_mm: [f64; 4], - /// Where on the Earth it shows; `None` for a frame that isn't georeferenced (measured in - /// millimetres on paper). - pub measure: Option, + /// Where on the Earth it shows. + pub measure: GeoMeasure, } /// Where a georeferenced frame shows on the Earth ([`Viewport`]). @@ -58,24 +55,16 @@ impl Viewport { let k = 72.0 / 25.4; let [x, y, w, h] = self.rect_mm; let (x1, x2, top, bottom) = (x * k, (x + w) * k, page_h - y * k, page_h - (y + h) * k); - let measure = match &self.measure { - // Millimetres on paper: a point is 25.4/72 of one. - None => " /Measure << /Type /Measure /Subtype /RL /R (1 mm = 1 mm) \ - /X [<< /Type /NumberFormat /U (mm) /C 0.352778 >>] /D [<< /Type /NumberFormat /U (mm) /C 1 >>] \ - /A [<< /Type /NumberFormat /U (sq mm) /C 1 >>] >>" - .to_string(), - Some(m) => { - let gpts: Vec = m.corners.iter().map(|(lat, lon)| format!("{lat:.9} {lon:.9}")).collect(); - let kind = if m.projected { "PROJCS" } else { "GEOGCS" }; - let epsg = m.epsg.map(|c| format!(" /EPSG {c}")).unwrap_or_default(); - format!( - " /Measure << /Type /Measure /Subtype /GEO /Bounds [0 0 0 1 1 1 1 0] /GPTS [{}] /LPTS [0 1 1 1 1 0 0 0] \ - /GCS << /Type /{kind} /WKT {}{epsg} >> /PDU [/M /SQM /DEG] >>", - gpts.join(" "), - literal(&m.wkt), - ) - } - }; + let m = &self.measure; + let gpts: Vec = m.corners.iter().map(|(lat, lon)| format!("{lat:.9} {lon:.9}")).collect(); + let kind = if m.projected { "PROJCS" } else { "GEOGCS" }; + let epsg = m.epsg.map(|c| format!(" /EPSG {c}")).unwrap_or_default(); + let measure = format!( + " /Measure << /Type /Measure /Subtype /GEO /Bounds [0 0 0 1 1 1 1 0] /GPTS [{}] /LPTS [0 1 1 1 1 0 0 0] \ + /GCS << /Type /{kind} /WKT {}{epsg} >> /PDU [/M /SQM /DEG] >>", + gpts.join(" "), + literal(&m.wkt), + ); format!("<< /Type /Viewport /BBox [{x1:.3} {bottom:.3} {x2:.3} {top:.3}] /Name {}{measure} >>", text_string(&self.name)) } } @@ -239,7 +228,7 @@ impl Doc { pub fn write(pages: &[Page], title: &str) -> Vec { // Georeferenced frames make the document PDF 1.7 with Adobe's extension level 3, where // geospatial measures are defined (since taken into ISO 32000-2); what else it holds is 1.4. - let geo = pages.iter().any(|p| p.viewports.iter().any(|v| v.measure.is_some())); + let geo = pages.iter().any(|p| !p.viewports.is_empty()); let mut doc = if geo { Doc::of_version("1.7") } else { Doc::new() }; let (catalog, tree, info) = (doc.reserve(), doc.reserve(), doc.reserve()); let ids: Vec = pages.iter().map(|_| doc.reserve()).collect(); @@ -383,12 +372,12 @@ pub(crate) mod tests { let v = Viewport { name: "Main map".into(), rect_mm: [25.4, 25.4, 50.8, 25.4], - measure: Some(GeoMeasure { + measure: GeoMeasure { corners: [(-19.5, -45.5), (-19.5, -44.5), (-20.5, -44.5), (-20.5, -45.5)], wkt: wkt.clone(), projected: true, epsg: Some(31983), - }), + }, }; let page_h = 297.0 * 72.0 / 25.4; let d = v.dictionary(page_h); @@ -407,12 +396,6 @@ pub(crate) mod tests { let plain = write(&[Page::new(&a, 210.0, 297.0)], "Plain"); assert!(plain.starts_with(b"%PDF-1.4") && !String::from_utf8_lossy(&plain).contains("/Extensions")); assert_eq!(literal("é"), ""); - // A frame left out is a viewport measured in millimetres on paper, with no coordinates. - let bare = Viewport { name: "Inset".into(), rect_mm: [30.0, 30.0, 10.0, 10.0], measure: None }; - let d = bare.dictionary(page_h); - let bbox = format!("/BBox [85.039 {:.3} 113.386 {:.3}] /Name (Inset)", page_h - 40.0 * 72.0 / 25.4, page_h - 30.0 * 72.0 / 25.4); - assert!(d.starts_with("<< /Type /Viewport ") && d.contains(&bbox), "{d}"); - assert!(d.contains("/Subtype /RL") && d.contains("/U (mm) /C 0.352778") && !d.contains("/GEO") && !d.contains("/GPTS"), "{d}"); } /// A frame's viewport is where the page draws it: from the whole pixel it starts on at the diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 5a9d9aaaf2..c8a35626b5 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -304,8 +304,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu code when it has one, so a reader shows coordinates and measures in it, and GDAL or QGIS open the page as a georeferenced raster. It carries coordinates only: no layers and no feature attributes. A tilted frame, one on the globe, and one with a corner off the Earth aren't - georeferenced, and the export says which; such a frame over another carries no coordinates, so - a reader doesn't show the other's through it. A frame in a projection with no WKT form is + georeferenced, and the export says which. Where a frame is drawn over another, only the one on + top carries coordinates there (none when it isn't georeferenced), so a reader doesn't show the + other's through it. A frame in a projection with no WKT form is georeferenced by its corners in longitude/latitude when, between them, that puts every place within 0.5 mm of where it is drawn, and left out otherwise. **Georeference PDF** in the layout's Export group (`"georeference": false` for `layout.export`) leaves the coordinates out. From 128ca43964689d3f2c3f04df5441ca5cc071b8ea Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 01:47:55 -0300 Subject: [PATCH 07/18] fix(render): GeoPDF frame names whole, bounded cut-outs, a ribbon toggle (#1191) - /Name goes through literal, so a long frame name isn't cut at 200 characters. - Cutting a frame's viewport stops once it is past 64 rectangles (the frame is left out, as before), so crossing frames can't make work grow before the check. - Georeference PDF is a small toggle in the Publish tab's Export group, like Snap and Rulers: as a checkbox its label wrapped a letter a line in the narrow ribbon column. - The cut-out test checks that no two viewports overlap. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 8 ++++++++ crates/render/src/layout.rs | 4 ++++ crates/render/src/pdf.rs | 6 +++++- crates/ui-egui/src/lib.rs | 2 +- crates/ui-egui/src/ribbon.rs | 12 ++++++++---- docs/user-guide/layouts.md | 2 +- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 08f26fcc5b..c80a14fd75 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31828,6 +31828,14 @@ fn a_frame_drawn_over_another_is_cut_out_of_its_viewport() { assert_eq!(geo.viewports.len(), 4, "the main frame around the inset: above, below, left and right of it"); assert!(geo.viewports.iter().all(|v| v.name == frame.name && v.measure.projected)); assert!(!geo.viewports.iter().any(|v| inside(v, in_inset)) && geo.viewports.iter().any(|v| inside(v, beside))); + // No two overlap. + for (i, a) in geo.viewports.iter().enumerate() { + for b in &geo.viewports[i + 1..] { + let ([ax, ay, aw, ah], [bx, by, bw, bh]) = (a.rect_mm, b.rect_mm); + let apart = ax + aw <= bx + 1e-9 || bx + bw <= ax + 1e-9 || ay + ah <= by + 1e-9 || by + bh <= ay + 1e-9; + assert!(apart, "{:?} {:?}", a.rect_mm, b.rect_mm); + } + } let area: f64 = geo.viewports.iter().map(|v| v.rect_mm[2] * v.rect_mm[3]).sum(); let whole = frame.w * frame.h - 40.0 * 30.0; assert!((area - whole).abs() < 0.02 * whole, "{area} {whole}"); diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 4a3774ee94..167a88872f 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -640,6 +640,10 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced let mut shown = vec![*placed]; for (_, over) in &frames[i + 1..] { shown = shown.into_iter().flat_map(|r| uncovered(r, *over)).collect(); + // Past the most, it is left out: cutting on would only make more. + if shown.len() > MAX_VIEWPORTS_A_FRAME { + break; + } } if shown.is_empty() { continue; diff --git a/crates/render/src/pdf.rs b/crates/render/src/pdf.rs index d8bff804f6..c96d80c172 100644 --- a/crates/render/src/pdf.rs +++ b/crates/render/src/pdf.rs @@ -65,7 +65,7 @@ impl Viewport { gpts.join(" "), literal(&m.wkt), ); - format!("<< /Type /Viewport /BBox [{x1:.3} {bottom:.3} {x2:.3} {top:.3}] /Name {}{measure} >>", text_string(&self.name)) + format!("<< /Type /Viewport /BBox [{x1:.3} {bottom:.3} {x2:.3} {top:.3}] /Name {}{measure} >>", literal(&self.name)) } } @@ -396,6 +396,10 @@ pub(crate) mod tests { let plain = write(&[Page::new(&a, 210.0, 297.0)], "Plain"); assert!(plain.starts_with(b"%PDF-1.4") && !String::from_utf8_lossy(&plain).contains("/Extensions")); assert_eq!(literal("é"), ""); + // A frame's name is written whole, however long. + let measure = GeoMeasure { corners: [(0.0, 0.0); 4], wkt: "W".into(), projected: true, epsg: None }; + let long = Viewport { name: "n".repeat(300), rect_mm: [0.0, 0.0, 1.0, 1.0], measure }; + assert!(long.dictionary(page_h).contains(&format!("/Name ({})", "n".repeat(300)))); } /// A frame's viewport is where the page draws it: from the whole pixel it starts on at the diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index db065d1a8e..68f0be5ce1 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1556,7 +1556,7 @@ impl RustGisApp { r } - /// A layout as PDF or PNG bytes, guarded like `Session::run`; a PDF georeferenced as Layout › + /// A layout as PDF or PNG bytes, guarded like `Session::run`; a PDF georeferenced as Publish › /// Export › Georeference PDF says. fn layout_bytes(&mut self, li: usize, pdf: bool, dpi: f64) -> Result, String> { let georeference = self.ui.pdf_georeference; diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 3a72bd2ec1..7db5445f77 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3210,10 +3210,14 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) { small(ui, app, "map", "Show Map", "Close the layout and show the map.", "layout.close", Value::Null, false); }); col(ui, |ui| { - // A PDF's map frames carry their coordinates (a GeoPDF) unless this is cleared. - ui.checkbox(&mut app.ui.pdf_georeference, tl!("Georeference PDF")).on_hover_text(tl!( - "Export and print the layout's map frames with their coordinates, so PDF readers can locate and measure in them." - )); + // A PDF's map frames carry their coordinates (a GeoPDF) unless this is turned off: a + // toggle, as Snap and Rulers are. + let on = app.ui.pdf_georeference; + let desc = + "Export and print the layout's map frames to PDF with their coordinates, so PDF readers can locate and measure in them."; + if small(ui, app, "globe_view", "Georeference PDF", desc, "ui.menu", Value::Null, on).clicked() { + app.ui.pdf_georeference = !on; + } }); }); } diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index c8a35626b5..1725661149 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -308,7 +308,7 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu top carries coordinates there (none when it isn't georeferenced), so a reader doesn't show the other's through it. A frame in a projection with no WKT form is georeferenced by its corners in longitude/latitude when, between them, that puts every place - within 0.5 mm of where it is drawn, and left out otherwise. **Georeference PDF** in the layout's + within 0.5 mm of where it is drawn, and left out otherwise. **Georeference PDF** in the Publish tab's Export group (`"georeference": false` for `layout.export`) leaves the coordinates out. In SVG each element is a group (an Inkscape layer) named after it: text stays text and rectangles, ellipses and lines stay shapes, and map frames and the other surrounds are kept as vectors as in a From f77192b4165ebe66c2daad2370155bee66d9ba8f Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 01:59:11 -0300 Subject: [PATCH 08/18] test(render): a frame's cut-out viewports fill it exactly, less the inset (#1191) Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c80a14fd75..3803f89540 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31853,6 +31853,19 @@ fn a_frame_drawn_over_another_is_cut_out_of_its_viewport() { let names: Vec<&str> = geo.viewports.iter().map(|v| v.name.as_str()).collect(); assert_eq!(names, [frame.name.as_str(), frame.name.as_str(), frame.name.as_str(), frame.name.as_str(), "Inset"], "{:?}", geo.left_out); assert!(geo.left_out.is_empty() && inside(&geo.viewports[4], in_inset)); + // Exactly the frame without the inset, as both are placed on the page: the pieces fill the + // frame's box less the inset's, none reaches into the inset, and none overlaps another (above). + let [ix, iy, iw, ih] = geo.viewports[4].rect_mm; + let main = &geo.viewports[..4]; + let (x0, y0) = main.iter().fold((f64::MAX, f64::MAX), |(x, y), v| (x.min(v.rect_mm[0]), y.min(v.rect_mm[1]))); + let (x1, y1) = + main.iter().fold((f64::MIN, f64::MIN), |(x, y), v| (x.max(v.rect_mm[0] + v.rect_mm[2]), y.max(v.rect_mm[1] + v.rect_mm[3]))); + let area: f64 = main.iter().map(|v| v.rect_mm[2] * v.rect_mm[3]).sum(); + assert!((area - ((x1 - x0) * (y1 - y0) - iw * ih)).abs() < 1e-6, "{area}"); + for v in main { + let [vx, vy, vw, vh] = v.rect_mm; + assert!(vx + vw <= ix + 1e-9 || ix + iw <= vx + 1e-9 || vy + vh <= iy + 1e-9 || iy + ih <= vy + 1e-9, "{:?}", v.rect_mm); + } } #[test] From c1bd0f86480dcf6c53d1586f0ae381878b6c8e19 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 02:09:47 -0300 Subject: [PATCH 09/18] test(render): the cut-out pieces' box is the frame's own placement (#1191) Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 3803f89540..f76a5368b0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31866,6 +31866,12 @@ fn a_frame_drawn_over_another_is_cut_out_of_its_viewport() { let [vx, vy, vw, vh] = v.rect_mm; assert!(vx + vw <= ix + 1e-9 || ix + iw <= vx + 1e-9 || vy + vh <= iy + 1e-9 || iy + ih <= vy + 1e-9, "{:?}", v.rect_mm); } + // Their box is the frame's own, as it is placed with nothing over it. + s.project.layouts[0].elements.iter_mut().find(|e| e.id == 9_001).unwrap().visible = false; + let alone = rustgis_render::layout::frame_viewports(&s.project.layouts[0], &s.project.maps, 72.0); + let [fx, fy, fw, fh] = alone.viewports[0].rect_mm; + assert_eq!(alone.viewports.len(), 1); + assert!([x0 - fx, y0 - fy, x1 - (fx + fw), y1 - (fy + fh)].iter().all(|d| d.abs() < 1e-9), "{:?}", (x0, y0, x1, y1)); } #[test] From 04cc45d856a294d3fc2d8f31803d29acd984f71e Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 02:15:42 -0300 Subject: [PATCH 10/18] fix(render): a map frame covered all over by others is said to be left out (#1191) Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 10 ++++++++++ crates/render/src/layout.rs | 1 + 2 files changed, 11 insertions(+) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f76a5368b0..39d5e3b675 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31872,6 +31872,16 @@ fn a_frame_drawn_over_another_is_cut_out_of_its_viewport() { let [fx, fy, fw, fh] = alone.viewports[0].rect_mm; assert_eq!(alone.viewports.len(), 1); assert!([x0 - fx, y0 - fy, x1 - (fx + fw), y1 - (fy + fh)].iter().all(|d| d.abs() < 1e-9), "{:?}", (x0, y0, x1, y1)); + // Covered all over by a frame drawn over it, it has no viewport, and that is said. + let over = s.project.layouts[0].elements.iter_mut().find(|e| e.id == 9_001).unwrap(); + (over.visible, over.x, over.y, over.w, over.h) = (true, frame.x - 1.0, frame.y - 1.0, frame.w + 2.0, frame.h + 2.0); + let covered = rustgis_render::layout::frame_viewports(&s.project.layouts[0], &s.project.maps, 72.0); + assert!(covered.viewports.iter().all(|v| v.name == "Inset"), "{:?}", covered.viewports.len()); + assert!( + covered.left_out.contains(&(frame.id, frame.name.clone(), "the frames drawn over it cover all of it")), + "{:?}", + covered.left_out + ); } #[test] diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 167a88872f..22d60d0a81 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -646,6 +646,7 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced } } if shown.is_empty() { + out.left_out.push((e.id, e.name.clone(), "the frames drawn over it cover all of it")); continue; } if shown.len() > MAX_VIEWPORTS_A_FRAME { From 4db021af8426a5d427f11ae3117aa3af186de8ad Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 02:32:17 -0300 Subject: [PATCH 11/18] fix(render): round a frame's viewport origin as draw_page does, in f32 (#1191) draw_page rounds a frame's origin in single precision; the viewport rounded it in f64, so near a half pixel the box could sit a pixel from the drawn frame. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/layout.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 22d60d0a81..23d7dc441e 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -628,7 +628,8 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced .elements .iter() .filter(|e| e.visible && matches!(e.kind, ElementKind::MapFrame { .. })) - .map(|e| (e, [(e.x * k).round() * sx, (e.y * k).round() * sy, e.w * k * sx, e.h * k * sy])) + // Rounded as `draw_page` rounds them, in single precision, so the box is the drawn one. + .map(|e| (e, [f64::from(((e.x * k) as f32).round()) * sx, f64::from(((e.y * k) as f32).round()) * sy, e.w * k * sx, e.h * k * sy])) .collect(); for (i, (e, placed)) in frames.iter().enumerate() { let ElementKind::MapFrame { map, center, scale, bearing, pitch, .. } = &e.kind else { continue }; From d107f895465d284cc5b0de2d7095691c48fb15e7 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 02:54:20 -0300 Subject: [PATCH 12/18] fix(render): a shaped map frame drawn over another is left out of a GeoPDF (#1191) A viewport is a rectangle: a circle, ellipse or polygon frame over another would give its places around its shape, where the page shows the other's map. It is now left out and said; it still cuts the frame under it, which has no coordinates in its box (the user guide says so). Alone on the page it is georeferenced as before. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 10 ++++++++++ crates/render/src/layout.rs | 14 +++++++++++++- docs/user-guide/layouts.md | 3 ++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 39d5e3b675..c102302b0f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31872,6 +31872,16 @@ fn a_frame_drawn_over_another_is_cut_out_of_its_viewport() { let [fx, fy, fw, fh] = alone.viewports[0].rect_mm; assert_eq!(alone.viewports.len(), 1); assert!([x0 - fx, y0 - fy, x1 - (fx + fw), y1 - (fy + fh)].iter().all(|d| d.abs() < 1e-9), "{:?}", (x0, y0, x1, y1)); + // A round inset over it is left out, and said: its box would give its places around the circle, + // where the page shows the main map. The main map is still cut around that box. + let inset = s.project.layouts[0].elements.iter_mut().find(|e| e.id == 9_001).unwrap(); + inset.visible = true; + if let rustgis_map::layout::ElementKind::MapFrame { shape, .. } = &mut inset.kind { + *shape = rustgis_map::layout::FrameShape::Circle; + } + let round = rustgis_render::layout::frame_viewports(&s.project.layouts[0], &s.project.maps, 72.0); + assert!(round.viewports.iter().all(|v| v.name == frame.name) && round.viewports.len() == 4, "{:?}", round.left_out); + assert!(round.left_out.iter().any(|(id, _, why)| *id == 9_001 && why.contains("isn't a rectangle")), "{:?}", round.left_out); // Covered all over by a frame drawn over it, it has no viewport, and that is said. let over = s.project.layouts[0].elements.iter_mut().find(|e| e.id == 9_001).unwrap(); (over.visible, over.x, over.y, over.w, over.h) = (true, frame.x - 1.0, frame.y - 1.0, frame.w + 2.0, frame.h + 2.0); diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 23d7dc441e..09d94a1e4a 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -632,7 +632,19 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced .map(|e| (e, [f64::from(((e.x * k) as f32).round()) * sx, f64::from(((e.y * k) as f32).round()) * sy, e.w * k * sx, e.h * k * sy])) .collect(); for (i, (e, placed)) in frames.iter().enumerate() { - let ElementKind::MapFrame { map, center, scale, bearing, pitch, .. } = &e.kind else { continue }; + let ElementKind::MapFrame { map, center, scale, bearing, pitch, shape, .. } = &e.kind else { continue }; + // A viewport is a rectangle: one for a shaped frame over another would give its places + // where the page shows the other's map, around the shape. (It still cuts the other.) + let over = + |[x, y, w, h]: [f64; 4]| x < placed[0] + placed[2] && placed[0] < x + w && y < placed[1] + placed[3] && placed[1] < y + h; + if *shape != rustgis_map::layout::FrameShape::Rectangle && frames[..i].iter().any(|(_, r)| over(*r)) { + out.left_out.push(( + e.id, + e.name.clone(), + "it isn't a rectangle and is drawn over another frame, whose map shows around its shape", + )); + continue; + } let Some(m) = map_index(maps, map).and_then(|i| maps.get(i)) else { out.left_out.push((e.id, e.name.clone(), "its map is missing")); continue; diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 1725661149..536980be25 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -306,7 +306,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu attributes. A tilted frame, one on the globe, and one with a corner off the Earth aren't georeferenced, and the export says which. Where a frame is drawn over another, only the one on top carries coordinates there (none when it isn't georeferenced), so a reader doesn't show the - other's through it. A frame in a projection with no WKT form is + other's through it. A viewport is a rectangle, so a shaped frame (circle, ellipse, polygon) + drawn over another is left out, and the frame under it has no coordinates in its box. A frame in a projection with no WKT form is georeferenced by its corners in longitude/latitude when, between them, that puts every place within 0.5 mm of where it is drawn, and left out otherwise. **Georeference PDF** in the Publish tab's Export group (`"georeference": false` for `layout.export`) leaves the coordinates out. From fdbdde0b6c168a2301fa8602a1876a665b8bc9d0 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 03:04:40 -0300 Subject: [PATCH 13/18] fix(render): GeoPDF corner fit on a grid, its own reasons, finite values; series test (#1191) - The corners-only fit is checked on a 9 x 9 grid over the frame, not five points, so an error inside a wide frame isn't missed. - A place between the corners that is off the map has its own reason, not the 0.5 mm one. - A corner that isn't a finite number, or a frame whose place on the page isn't, is left out and said, so no NaN reaches /GPTS or /BBox. - A map series PDF is checked to georeference each page with its own corners. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 19 +++++++++++++++++++ crates/render/src/layout.rs | 23 +++++++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c102302b0f..86db57b793 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -31798,6 +31798,25 @@ fn a_pdf_says_which_frames_it_doesnt_georeference() { assert!(s.notices_since(mark).iter().all(|n| n.severity == rustgis_data::diagnostics::Severity::Info)); } +/// Each page of a map series is georeferenced with its own view, not the first page's (#1191). +#[test] +fn a_map_series_pdf_georeferences_each_page_with_its_view() { + let mut s = world(); + s.run("map.projection", &json!({"type": "EPSG:3857"})).unwrap(); + s.run("layout.new", &json!({"page": "a4", "orientation": "landscape", "template": "map"})).unwrap(); + let pages = s.run("layout.series", &json!({"layer": "Lakes", "nameField": "name"})).unwrap()["pages"].as_u64().unwrap(); + let path = std::env::temp_dir().join(format!("rustgis-geopdf-series-{}.pdf", std::process::id())); + s.run("layout.export", &json!({"path": path, "dpi": 36})).unwrap(); + let pdf = String::from_utf8_lossy(&std::fs::read(&path).unwrap()).into_owned(); + let _ = std::fs::remove_file(&path); + let gpts: Vec<&str> = pdf.split("/GPTS [").skip(1).filter_map(|r| r.split(']').next()).collect(); + assert_eq!(gpts.len() as u64, pages, "one viewport a page"); + let mut distinct = gpts.clone(); + distinct.sort_unstable(); + distinct.dedup(); + assert_eq!(distinct.len(), gpts.len(), "no page has another's corners"); +} + /// A frame drawn over another (an inset on the main map) is cut out of the other's viewport, so /// that a reader shows the main map's coordinates around the inset and not in it: none there when /// the inset is left out of georeferencing (tilted), the inset's own when it isn't (#1191). No two diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 09d94a1e4a..55436a7e17 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -649,6 +649,10 @@ pub fn frame_viewports(layout: &Layout, maps: &[Map], dpi: f64) -> Georeferenced out.left_out.push((e.id, e.name.clone(), "its map is missing")); continue; }; + if !placed.iter().all(|v| v.is_finite()) { + out.left_out.push((e.id, e.name.clone(), "its place on the page isn't a number")); + continue; + } // What the frames drawn over it leave of it. let mut shown = vec![*placed]; for (_, over) in &frames[i + 1..] { @@ -703,14 +707,16 @@ fn uncovered(r: [f64; 4], cut: [f64; 4]) -> Vec<[f64; 4]> { } /// How far, in millimetres on paper, a reader that fits a frame's corners in longitude/latitude -/// (`corners`, latitude then longitude, clockwise from the top-left) puts the places at its -/// centre and its edges' midpoints from where the frame, `w` × `h` points seen by `cam`, draws -/// them: the worst of the five, the fit being exact at the corners. `None` when one of them -/// isn't on the map. +/// (`corners`, latitude then longitude, clockwise from the top-left) puts the places on a 9 × 9 +/// grid over it, its edges included, from where the frame, `w` × `h` points seen by `cam`, draws +/// them: the worst of them, the fit being exact at the corners. `None` when one of them isn't on +/// the map. fn corner_fit_mm(corners: &[(f64, f64); 4], map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> Option { let [tl, tr, br, bl] = *corners; let mut worst: f64 = 0.0; - for (u, t) in [(0.5, 0.5), (0.5, 0.0), (1.0, 0.5), (0.5, 1.0), (0.0, 0.5)] { + // A 9 × 9 grid over the frame, its edges included (the corners are exact). + let grid = (0..=8).flat_map(|i| (0..=8).map(move |j| (f64::from(i) / 8.0, f64::from(j) / 8.0))); + for (u, t) in grid { // Between the corners, as the unit square's (`/LPTS`) is filled: bilinearly. let mix = |a: f64, b: f64, c: f64, d: f64| (1.0 - u) * (1.0 - t) * a + u * (1.0 - t) * b + u * t * c + (1.0 - u) * t * d; let (lat, lon) = (mix(tl.0, tr.0, br.0, bl.0), mix(tl.1, tr.1, br.1, bl.1)); @@ -777,6 +783,10 @@ fn frame_viewport( (Some(_), Some(def)) => crs::lonlat_on_datum_of(def, c).ok_or("its corners can't be moved onto its datum")?, _ => c, }; + // A value no PDF number can be (from a hostile project) is no place. + if !(c.x.is_finite() && c.y.is_finite()) { + return Err("a corner is off the Earth"); + } Ok((c.y, c.x)) }; // Clockwise from the top-left, as `/LPTS` takes them. @@ -798,11 +808,12 @@ fn frame_viewport( // puts no place more than half a millimetre on paper from where the frame draws it. match corner_fit_mm(&corners, map, &cam, w, h) { Some(off) if off <= MAX_CORNER_FIT_MM => {} - _ => { + Some(_) => { return Err( "its coordinate system can't be written as WKT, and its corners alone would put places more than 0.5 mm off", ); } + None => return Err("its coordinate system can't be written as WKT, and a place between its corners is off the map"), } (wkt::to_wkt(crs::Crs::LonLat).ok_or("its projection can't be read")?, Some(4326)) } From d6ac93ca84206d4de62021133ecc1f30efc651e1 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 03:09:28 -0300 Subject: [PATCH 14/18] docs(render): say the corner fit is measured on a grid, not proven between its points (#1191) Co-Authored-By: Claude Opus 5.5 --- crates/render/src/layout.rs | 4 ++-- docs/user-guide/layouts.md | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 55436a7e17..57dc8f9812 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -600,8 +600,8 @@ pub struct Georeferenced { pub left_out: Vec<(u32, String, &'static str)>, /// The frames whose coordinate system can't be written as WKT (a grid shift, or a projection /// WKT1 has no name for), by id and name: - /// georeferenced by their corners in longitude/latitude, which put every place of the frame - /// within [`MAX_CORNER_FIT_MM`] of where it is drawn. + /// georeferenced by their corners in longitude/latitude, which put the places of a 9 × 9 grid + /// over the frame within [`MAX_CORNER_FIT_MM`] of where they are drawn ([`corner_fit_mm`]). pub by_corners: Vec<(u32, String)>, } diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 536980be25..a9a2abfb05 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -308,8 +308,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu top carries coordinates there (none when it isn't georeferenced), so a reader doesn't show the other's through it. A viewport is a rectangle, so a shaped frame (circle, ellipse, polygon) drawn over another is left out, and the frame under it has no coordinates in its box. A frame in a projection with no WKT form is - georeferenced by its corners in longitude/latitude when, between them, that puts every place - within 0.5 mm of where it is drawn, and left out otherwise. **Georeference PDF** in the Publish tab's + georeferenced by its corners in longitude/latitude when that puts the places on a 9 × 9 grid + over it within 0.5 mm of where they are drawn (measured there, not proven between them), and + left out otherwise. **Georeference PDF** in the Publish tab's Export group (`"georeference": false` for `layout.export`) leaves the coordinates out. In SVG each element is a group (an Inkscape layer) named after it: text stays text and rectangles, ellipses and lines stay shapes, and map frames and the other surrounds are kept as vectors as in a From 70f97a27dbf1d95023777659431399387b863655 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 03:09:42 -0300 Subject: [PATCH 15/18] docs(render): no links to private items in GeoPDF docs (#1191) Co-Authored-By: Claude Opus 5.5 --- crates/render/src/layout.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 57dc8f9812..8069c6c134 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -594,14 +594,14 @@ fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> #[derive(Debug, Default)] pub struct Georeferenced { /// The georeferenced frames, in drawing order, each in the rectangles no frame drawn over it - /// covers ([`uncovered`]): no two overlap, so a reader never has two to choose from. + /// covers (`uncovered`): no two overlap, so a reader never has two to choose from. pub viewports: Vec, /// The frames that couldn't be, by id and name, with why. pub left_out: Vec<(u32, String, &'static str)>, /// The frames whose coordinate system can't be written as WKT (a grid shift, or a projection /// WKT1 has no name for), by id and name: /// georeferenced by their corners in longitude/latitude, which put the places of a 9 × 9 grid - /// over the frame within [`MAX_CORNER_FIT_MM`] of where they are drawn ([`corner_fit_mm`]). + /// over the frame within [`MAX_CORNER_FIT_MM`] of where they are drawn (`corner_fit_mm`). pub by_corners: Vec<(u32, String)>, } From 706e132d59461dbe2173debc97180fd61a87cf79 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 18:28:12 -0300 Subject: [PATCH 16/18] docs(engine): the corner-fit notice says the 0.5 mm is measured on a 9 x 9 grid (#1191) Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 0a1c355cee..85925a4181 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3658,7 +3658,7 @@ pub(crate) fn georeference_notices(s: &mut Session, left_out: &[(u32, String, &' let n = corners.len(); s.notify( format!( - "The PDF georeferences {n} map frame{} by its corners in longitude/latitude, as its coordinate system can't be written as WKT: between them it puts places within 0.5 mm of where they are drawn ({})", + "The PDF georeferences {n} map frame{} by its corners in longitude/latitude, as its coordinate system can't be written as WKT: on a 9 × 9 grid over each, it puts places within 0.5 mm of where they are drawn, not proven between them ({})", plural(n), listed(corners) ), From ca9921595590d12ababfca5cdefaa8226685ba51 Mon Sep 17 00:00:00 2001 From: giswqs Date: Mon, 9 Nov 2026 17:42:18 -0500 Subject: [PATCH 17/18] fix(render): a GeoPDF corner fit with a sample that isn't a number fails f64::max ignores NaN, so a non-finite grid sample would have passed the 0.5 mm check unseen; treat it as no fit, as an off-map sample is (#1191). --- crates/render/src/layout.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 8069c6c134..a694357bb9 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -722,7 +722,12 @@ fn corner_fit_mm(corners: &[(f64, f64); 4], map: &Map, cam: &rustgis_map::Camera let (lat, lon) = (mix(tl.0, tr.0, br.0, bl.0), mix(tl.1, tr.1, br.1, bl.1)); let at = map.to_map(Coord::new((lon + 540.0).rem_euclid(360.0) - 180.0, lat))?; let (x, y) = cam.try_to_screen(at, w, h)?; - worst = worst.max((x - u * w).hypot(y - t * h)); + // `f64::max` ignores NaN: a sample that isn't a number is no fit. + let off = (x - u * w).hypot(y - t * h); + if !off.is_finite() { + return None; + } + worst = worst.max(off); } (worst.is_finite()).then_some(worst / PT_PER_MM) } From f51a4a23a2f20bd875b610830ebe51ba8a2abe93 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 9 Nov 2026 20:05:42 -0300 Subject: [PATCH 18/18] fix(engine): a series' GeoPDF notices name the pages a frame is left out on (#1191) A series moves each frame, so one may be georeferenced on some pages and left out or placed by its corners on others: the notices now name those pages, so a frame doesn't read as having no coordinates anywhere in the PDF. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layout.rs | 80 +++++++++++++++++++++------------ crates/engine/src/tests.rs | 31 ++++++++++--- docs/user-guide/layouts.md | 5 ++- 3 files changed, 80 insertions(+), 36 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 85925a4181..e058c2e8d0 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -3538,8 +3538,9 @@ pub fn layout_bytes_with(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: b // What the PDF draws as pictures because its vectors would grow too large: frames, and // whole pages (their surrounds). let (mut frames_pictured, mut pages_pictured) = (Vec::new(), Vec::new()); - // The map frames a PDF can't georeference, and those it georeferences by their corners only. - let (mut left_out, mut by_corners): (Vec<(u32, String, &'static str)>, Vec<(u32, String)>) = (Vec::new(), Vec::new()); + // 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, Option>), String> { let mut out = Vec::new(); let mut png = None; @@ -3571,8 +3572,8 @@ pub fn layout_bytes_with(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: b } else { rustgis_render::layout::Georeferenced::default() }; - left_out.extend(geo.left_out); - by_corners.extend(geo.by_corners); + left_out.extend(geo.left_out.into_iter().map(|(id, n, why)| (page + 1, id, n, why))); + by_corners.extend(geo.by_corners.into_iter().map(|(id, n)| (page + 1, id, n))); let (drawing, pictured) = rustgis_render::layout::render_drawing(layout, &mut s.project.maps, dpi, &info) .ok_or("the page is too large to draw")?; if drawing.overflowed() { @@ -3609,16 +3610,24 @@ pub fn layout_bytes_with(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: b } let (pages, png) = result?; pictured_notices(s, &frames_pictured, &pages_pictured); - georeference_notices(s, &left_out, &by_corners); + georeference_notices(s, &left_out, &by_corners, pages.len()); if pdf { Ok(rustgis_render::pdf::write(&pages, &saved.name)) } else { png.ok_or_else(|| "nothing to draw".to_string()) } } /// Says which map frames a PDF doesn't georeference, with why, and which it georeferences by their -/// corners only (#1191): one notice for each, with a count. A frame is counted once however many -/// pages of a series it is on (by its id: two frames sharing a name are two), with the first -/// page's reason. Information, not warnings: nothing wrong is written (a frame left out carries no -/// coordinates, and one placed by its corners is checked to within half a millimetre). -pub(crate) fn georeference_notices(s: &mut Session, left_out: &[(u32, String, &'static str)], by_corners: &[(u32, String)]) { +/// corners only (#1191): one notice for each, with a count. Each entry has the page (from 1) it +/// is on, of the `pages` exported. A frame is counted once however many pages of a series it is +/// on (by its id: two frames sharing a name are two), with the first page's reason, and named with +/// its pages when it is so on some of them only: a series moves each frame, which may cross the +/// 180° meridian on one page and not the next. Information, not warnings: nothing wrong is +/// written (a frame left out carries no coordinates, and one placed by its corners is checked to +/// within half a millimetre). +pub(crate) fn georeference_notices( + s: &mut Session, + left_out: &[(usize, u32, String, &'static str)], + by_corners: &[(usize, u32, String)], + pages: usize, +) { let listed = |items: Vec| { let more = items.len().saturating_sub(10); let mut text = items.into_iter().take(10).collect::>().join(", "); @@ -3628,31 +3637,44 @@ pub(crate) fn georeference_notices(s: &mut Session, left_out: &[(u32, String, &' text }; let plural = |n: usize| if n == 1 { "" } else { "s" }; - let mut seen = Vec::new(); - let out: Vec = left_out - .iter() - .filter(|(id, ..)| { - !seen.contains(id) && { - seen.push(*id); - true + // Each frame once, in the order first met: its name, the first page's reason and its pages. + let frames = |entries: Vec<(usize, u32, &str, &'static str)>| { + let mut frames: Vec<(u32, String, &'static str, Vec)> = Vec::new(); + for (page, id, name, why) in entries { + match frames.iter_mut().find(|f| f.0 == id) { + Some(f) if !f.3.contains(&page) => f.3.push(page), + Some(_) => {} + None => frames.push((id, name.to_string(), why, vec![page])), } - }) - .map(|(_, n, why)| format!("{n}: {why}")) + } + frames + }; + // A frame's name, with its pages when it is so on some of the pages only. + let named = |name: &str, on: &[usize]| { + if pages < 2 || on.len() >= pages { + return name.to_string(); + } + let mut shown: Vec = on.iter().map(usize::to_string).collect(); + let more = shown.len().saturating_sub(10); + shown.truncate(10); + let listed = match (shown.split_last(), more) { + (Some((last, rest)), 0) if !rest.is_empty() => format!("{} and {last}", rest.join(", ")), + (_, 0) => shown.join(", "), + (_, more) => format!("{} and {more} more", shown.join(", ")), + }; + format!("{name} on page{} {listed}", plural(on.len())) + }; + let out: Vec = frames(left_out.iter().map(|(p, id, n, why)| (*p, *id, n.as_str(), *why)).collect()) + .into_iter() + .map(|(_, n, why, on)| format!("{}: {why}", named(&n, &on))) .collect(); if !out.is_empty() { let n = out.len(); s.notify(format!("The PDF doesn't georeference {n} map frame{} ({})", plural(n), listed(out)), false); } - let mut seen = Vec::new(); - let corners: Vec = by_corners - .iter() - .filter(|(id, _)| { - !seen.contains(id) && { - seen.push(*id); - true - } - }) - .map(|(_, n)| n.clone()) + let corners: Vec = frames(by_corners.iter().map(|(p, id, n)| (*p, *id, n.as_str(), "")).collect()) + .into_iter() + .map(|(_, n, _, on)| named(&n, &on)) .collect(); if !corners.is_empty() { let n = corners.len(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 21ae688e18..18f13ae311 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -33476,22 +33476,43 @@ fn a_layout_pdf_georeferences_its_map_frames() { } /// What a PDF doesn't georeference is one notice for each kind, with a count (#1191): a frame -/// once however many pages it is on, two frames sharing a name two. +/// once however many pages it is on, two frames sharing a name two. A frame so on some pages of a +/// series only is named with them, so that one georeferenced on the others doesn't read as having +/// no coordinates anywhere in the PDF. #[test] fn a_pdf_says_which_frames_it_doesnt_georeference() { let mut s = Session::new(Project::default()); let mark = s.notice_mark(); - crate::cmd::layout::georeference_notices(&mut s, &[], &[]); + crate::cmd::layout::georeference_notices(&mut s, &[], &[], 1); assert!(s.notices_since(mark).is_empty(), "everything georeferenced, nothing said"); - let left = - [(1, "Main".to_string(), "it is tilted"), (1, "Main".to_string(), "it is tilted"), (4, "Main".to_string(), "it shows the globe")]; - crate::cmd::layout::georeference_notices(&mut s, &left, &[(2, "Inset".to_string()), (2, "Inset".to_string())]); + let left = [ + (1, 1, "Main".to_string(), "it is tilted"), + (1, 1, "Main".to_string(), "it is tilted"), + (1, 4, "Main".to_string(), "it shows the globe"), + ]; + crate::cmd::layout::georeference_notices(&mut s, &left, &[(1, 2, "Inset".to_string()), (1, 2, "Inset".to_string())], 1); let said: Vec<&str> = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect(); assert_eq!(said.len(), 2, "{said:?}"); assert_eq!(said[0], "The PDF doesn't georeference 2 map frames (Main: it is tilted, Main: it shows the globe)"); assert!(said[1].starts_with("The PDF georeferences 1 map frame by its corners") && said[1].ends_with("(Inset)"), "{}", said[1]); // Information, not warnings: nothing wrong is written. assert!(s.notices_since(mark).iter().all(|n| n.severity == rustgis_data::diagnostics::Severity::Info)); + // A series of five pages: Main left out on pages 3 and 4 only (the first page's reason), the + // inset by its corners on every page, an overview by its corners on page 2 only. + let mark = s.notice_mark(); + let left = [(3, 1, "Main".to_string(), "it crosses the 180° meridian"), (4, 1, "Main".to_string(), "it shows the globe")]; + let mut corners: Vec<(usize, u32, String)> = (1..=5).map(|p| (p, 2, "Inset".to_string())).collect(); + corners.push((2, 3, "Overview".to_string())); + crate::cmd::layout::georeference_notices(&mut s, &left, &corners, 5); + let said: Vec<&str> = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect(); + assert_eq!(said[0], "The PDF doesn't georeference 1 map frame (Main on pages 3 and 4: it crosses the 180° meridian)"); + assert!(said[1].ends_with("(Inset, Overview on page 2)"), "{}", said[1]); + // Many pages are cut to ten, with how many more. + let mark = s.notice_mark(); + let left: Vec<_> = (1..=13).map(|p| (p, 1, "Main".to_string(), "it is tilted")).collect(); + crate::cmd::layout::georeference_notices(&mut s, &left, &[], 20); + let said = &s.notices_since(mark)[0].text; + assert!(said.contains("(Main on pages 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and 3 more: it is tilted)"), "{said}"); } /// Each page of a map series is georeferenced with its own view, not the first page's (#1191). diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index a9a2abfb05..146b41037a 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -304,8 +304,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu code when it has one, so a reader shows coordinates and measures in it, and GDAL or QGIS open the page as a georeferenced raster. It carries coordinates only: no layers and no feature attributes. A tilted frame, one on the globe, and one with a corner off the Earth aren't - georeferenced, and the export says which. Where a frame is drawn over another, only the one on - top carries coordinates there (none when it isn't georeferenced), so a reader doesn't show the + georeferenced, and the export says which (in a map series, on which pages when not on all: each + page moves the frame). Where a frame is drawn over another, only the one on top carries + coordinates there (none when it isn't georeferenced), so a reader doesn't show the other's through it. A viewport is a rectangle, so a shaped frame (circle, ellipse, polygon) drawn over another is left out, and the frame under it has no coordinates in its box. A frame in a projection with no WKT form is georeferenced by its corners in longitude/latitude when that puts the places on a 9 × 9 grid