Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fcbf8da
feat(render): a layout's PDF is a GeoPDF: its map frames georeference…
elpidiofilho Nov 8, 2026
18304a5
fix(render): GeoPDF review round 1: notices per kind by frame id, oth…
elpidiofilho Nov 8, 2026
ab22ca5
Merge main into feat/1191-geopdf
elpidiofilho Nov 8, 2026
120dc37
test(render): GeoPDF tests check the notices really, and SAD69 corner…
elpidiofilho Nov 8, 2026
69ceaa7
fix(render): a GeoPDF writes no approximate coordinates, and can be t…
elpidiofilho Nov 9, 2026
c9bfe7a
Merge main into feat/1191-geopdf
elpidiofilho Nov 9, 2026
4aef2f9
fix(render): a frame left out of a GeoPDF is measured on paper, not l…
elpidiofilho Nov 9, 2026
df4d6c6
fix(render): cut a frame drawn over another out of its viewport, no m…
elpidiofilho Nov 9, 2026
128ca43
fix(render): GeoPDF frame names whole, bounded cut-outs, a ribbon tog…
elpidiofilho Nov 9, 2026
f77192b
test(render): a frame's cut-out viewports fill it exactly, less the i…
elpidiofilho Nov 9, 2026
c1bd0f8
test(render): the cut-out pieces' box is the frame's own placement (#…
elpidiofilho Nov 9, 2026
04cc45d
fix(render): a map frame covered all over by others is said to be lef…
elpidiofilho Nov 9, 2026
4db021a
fix(render): round a frame's viewport origin as draw_page does, in f3…
elpidiofilho Nov 9, 2026
d107f89
fix(render): a shaped map frame drawn over another is left out of a G…
elpidiofilho Nov 9, 2026
fdbdde0
fix(render): GeoPDF corner fit on a grid, its own reasons, finite val…
elpidiofilho Nov 9, 2026
d6ac93c
docs(render): say the corner fit is measured on a grid, not proven be…
elpidiofilho Nov 9, 2026
70f97a2
docs(render): no links to private items in GeoPDF docs (#1191)
elpidiofilho Nov 9, 2026
b11495d
Merge remote-tracking branch 'origin/main' into pr-1585
giswqs Nov 9, 2026
706e132
docs(engine): the corner-fit notice says the 0.5 mm is measured on a …
elpidiofilho Nov 9, 2026
ca99215
fix(render): a GeoPDF corner fit with a sample that isn't a number fails
giswqs Nov 9, 2026
f51a4a2
fix(engine): a series' GeoPDF notices name the pages a frame is left …
elpidiofilho Nov 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/data/src/raster/crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,52 @@ pub(crate) fn transformer_of(def: &str) -> Result<Transformer, String> {
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, String> {
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<Coord> {
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()?;
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
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
Expand Down
65 changes: 61 additions & 4 deletions crates/data/src/raster/wkt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,27 @@ pub fn to_wkt(crs: Crs) -> Option<String> {
(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<String> {
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<u16>) -> Option<String> {
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
// 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('+'))
Expand All @@ -1245,7 +1265,7 @@ pub fn to_wkt(crs: Crs) -> Option<String> {
// 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).
Expand All @@ -1256,7 +1276,7 @@ pub fn to_wkt(crs: Crs) -> Option<String> {
};
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"),
},
Expand Down Expand Up @@ -1467,6 +1487,22 @@ pub fn to_wkt(crs: Crs) -> Option<String> {
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"
Comment thread
giswqs marked this conversation as resolved.
Resolved
}
"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
Expand Down Expand Up @@ -1499,7 +1535,7 @@ pub fn to_wkt(crs: Crs) -> Option<String> {
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),
Expand Down Expand Up @@ -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"]]"#;
Expand Down
110 changes: 103 additions & 7 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn commands() -> Vec<C> {
.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();
Expand Down Expand Up @@ -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<Vec<u8>, 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<Vec<u8>, String> {
let saved = s.project.layouts.get(li).ok_or("no layout")?.clone();
if let Some(note) = named_fonts_unread(&saved) {
s.warn(note);
Expand All @@ -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<rustgis_render::pdf::Page>, Option<Vec<u8>>), String> {
let mut out = Vec::new();
let mut png = None;
Expand All @@ -3558,17 +3566,25 @@ 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() {
pages_pictured.push(page + 1);
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 {
Expand All @@ -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<String>| {
let more = items.len().saturating_sub(10);
let mut text = items.into_iter().take(10).collect::<Vec<_>>().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<usize>)> = 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<String> = 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<String> = 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<String> = 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.
Expand Down Expand Up @@ -3646,15 +3738,19 @@ pub(crate) fn export_work(s: &Session, p: &Value) -> Result<super::exports::Work
if !pdf && !svg && !lower.ends_with(".png") {
return Err("export to a .png, .pdf or .svg file".into());
}
let (dpi, all) = (f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true));
let (dpi, all, georeference) = (f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true), b(p, "georeference").unwrap_or(true));
let mut project = s.project.clone();
for m in &mut project.maps {
m.detach_reads();
}
Ok(Box::new(move || {
let mut copy = Session::new(project);
let written = (|| {
let bytes = if svg { layout_svg(&mut copy, li, dpi)?.into_bytes() } else { layout_bytes(&mut copy, li, pdf, dpi, all)? };
let bytes = if svg {
layout_svg(&mut copy, li, dpi)?.into_bytes()
} else {
layout_bytes_with(&mut copy, li, pdf, dpi, all, georeference)?
};
rustgis_data::progress::check().map_err(|e| e.to_string())?;
rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?;
Ok(json!({"path": path, "bytes": bytes.len()}))
Expand Down
Loading
Loading