diff --git a/crates/data/src/raster/crs.rs b/crates/data/src/raster/crs.rs index fbbe4ef34d..2707790be5 100644 --- a/crates/data/src/raster/crs.rs +++ b/crates/data/src/raster/crs.rs @@ -423,10 +423,52 @@ 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()) } +/// 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), using PROJ 9.7.1's sphere of its semi-major axis for the projection and /// keeping the ellipsoid for datum transformations: `None` for any other definition, `eqc` on a sphere diff --git a/crates/data/src/raster/wkt.rs b/crates/data/src/raster/wkt.rs index 0431271912..995f109001 100644 --- a/crates/data/src/raster/wkt.rs +++ b/crates/data/src/raster/wkt.rs @@ -1226,7 +1226,27 @@ 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 { + // 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('+')) @@ -1245,7 +1265,7 @@ pub fn to_wkt(crs: Crs) -> Option { // proj4rs computes `eqc` on a sphere of the semi-major axis and gives that sphere. Keep the // datum's original ellipsoid in the definition, as PROJ/GDAL do. 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). @@ -1256,7 +1276,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"), }, @@ -1467,6 +1487,22 @@ pub fn to_wkt(crs: Crs) -> 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 @@ -1499,7 +1535,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), @@ -1638,6 +1674,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 ce419d609a..e058c2e8d0 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -126,7 +126,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(); @@ -3506,9 +3506,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); @@ -3533,6 +3538,9 @@ 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, + // 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; @@ -3558,6 +3566,14 @@ 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 = if georeference { + rustgis_render::layout::frame_viewports(layout, &s.project.maps, dpi) + } else { + rustgis_render::layout::Georeferenced::default() + }; + 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() { @@ -3565,10 +3581,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 { @@ -3594,9 +3610,85 @@ 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); + 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. 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(", "); + if more > 0 { + text.push_str(&format!(" and {more} more")); + } + text + }; + let plural = |n: usize| if n == 1 { "" } else { "s" }; + // 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])), + } + } + 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 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(); + 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: 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) + ), + false, + ); + } +} + /// Says which layout elements, map frames and surrounds (by name), and pages (by number) an export /// draws as pictures because their /// vectors would pass the size a page's content may come to: one notice for each, with a count. @@ -3646,7 +3738,7 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result Result = 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("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(); + 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!("The PDF doesn't georeference 1 map frame ({}: it is tilted)", frame.name)), "{said:?}"); + // A world projection RustGIS computes itself (Winkel Tripel) is written as WKT too. + 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 /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: 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); + 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). + 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"); + 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(); + 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. 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, &[], &[], 1); + assert!(s.notices_since(mark).is_empty(), "everything georeferenced, nothing said"); + 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). +#[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 +/// viewports overlap, and none goes without coordinates, which Acrobat Reader doesn't take. +#[test] +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(); + // 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); + if let rustgis_map::layout::ElementKind::MapFrame { pitch, .. } = &mut inset.kind { + *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); + 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))); + // 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}"); + // 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)); + // 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); + } + // 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)); + // 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); + 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] 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..a694357bb9 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -590,6 +590,258 @@ 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 { + /// 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)>, + /// 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`). + 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, 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 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))); + let frames: Vec<_> = layout + .elements + .iter() + .filter(|e| e.visible && matches!(e.kind, ElementKind::MapFrame { .. })) + // 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, 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; + }; + 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..] { + 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() { + 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 { + 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.extend(v); + } + Err(why) => out.left_out.push((e.id, e.name.clone(), why)), + } + } + 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 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; + // 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)); + 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)?; + // `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) +} + +/// Frame `e` of `map`, placed at `placed` (millimetres: x, y, width, height) and seen by `cam`, as +/// 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<(Vec, bool), &'static str> { + use rustgis_data::raster::{crs, wkt}; + 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"); + } + // 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); + // 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 + // 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("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. + 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((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. + let first = corners[0].1; + 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 => {} + 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)) + } + }; + 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 /// 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) { @@ -2192,6 +2444,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 9f3c9689bd..c96d80c172 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,56 @@ pub struct Page { content: Content, pub width_mm: f64, pub height_mm: f64, + viewports: Vec, +} + +/// 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. + 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], + /// Where on the Earth it shows. + pub measure: GeoMeasure, +} + +/// 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], + /// 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 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} >>", literal(&self.name)) + } } enum Content { @@ -72,7 +123,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 +141,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 +161,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 +226,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 + // 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.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 +253,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 +362,71 @@ 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], + 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); + // 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("é"), ""); + // 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 + /// 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(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index da0c07788c..424f55c56d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -713,6 +713,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). @@ -984,6 +986,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(), @@ -1564,11 +1567,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 Publish › + /// 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 { @@ -3438,10 +3443,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 dfd6034999..3b806d7454 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3213,6 +3213,16 @@ 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 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/data-formats.md b/docs/user-guide/data-formats.md index e296cc9379..65ca085087 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -587,7 +587,7 @@ were rounded, and Export Raster, which writes the rounded values, says so again. | 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. A WMS, WMTS or XYZ tile layer (`"tiles"`) is written as its tiles at one zoom, RGBA in Web Mercator (see [Sharing](sharing.md#exporting)). 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. | | GeoTIFF of the map view | Publish › Export Map to `.tif` (`share.exportMap`) | The view as an RGB GeoTIFF in the map's projection, with labels but no scale bar; tile credits in the Copyright tag; `"cog": true` for a cloud-optimised one. Flat, untilted maps of the Earth only (see [Exporting](sharing.md#exporting)). | -| 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..146b41037a 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -299,6 +299,20 @@ 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. 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 (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 + 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 PDF (their labels as the letters' shapes, each run labelled with its text), so the page can be