Skip to content
Merged
100 changes: 23 additions & 77 deletions crates/engine/src/cmd/layout/publication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,13 +1071,6 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
let (inner_w, inner_h) = inner_frame((fw, fh), (room_side, room_end));
let (center, scale) = fit_frame(s.map(), &extent, rectangle.as_ref(), whole_globe, (inner_w, inner_h))
.ok_or("the extent can't be drawn on the map's projection")?;
if whole_globe && center != s.map().center_lonlat() {
warnings.push(format!(
"the globe is centred at {:.1}° rather than the view's {:.1}° of latitude: nearer the pole, the frame can't draw it small enough to fit",
center.y,
s.map().center_lonlat().y
));
}
// A name given past the limit is refused, as layout.new refuses it, not cut (#2639).
let given = crate::cmd::within("name", "a layout's name", ps(p, "name").unwrap_or("Publication").trim(), crate::cmd::MAX_NAME)?;
let (name, name_cut) = unique_name(s, "New Publication Layout", "the name given", &given);
Expand Down Expand Up @@ -1169,21 +1162,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
m.graticule = src.graticule;
m.body = src.body.clone();
m.set_projection(rustgis_map::Projection::Globe);
// Sized on the equator and centred as near the area as the camera draws a globe that
// small, as the whole globe's frame is (#2337): at the area's own latitude near a pole
// the camera stops at its coarsest resolution, and the disc came out larger than its
// circle, clipped. Further from the pole, the area is still on the disc and outlined.
let globe = globe_scale(&m, Coord::new(center.x, 0.0), side * 0.92);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let (at, whole) = globe_locator_center(&m, center, globe, side);
if at != center {
warnings.push(format!(
"the globe locator is centred at {:.1}° rather than the map frame's {:.1}° of latitude: nearer the pole, it can't draw the globe small enough to fit",
at.y, center.y
));
}
if !whole {
warnings.push("the globe locator is too small to show the whole globe: its circle clips it".into());
}
// Each polygon simplified to a tenth of a millimetre on the disc (its scale in metres),
// so the project keeps a sketch of the layers for it, not a second full copy of them.
let tolerance_m = globe * 1e-4;
Expand Down Expand Up @@ -1274,13 +1253,12 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
}
None => s.project.maps.push(m),
}
Some((name, globe, FrameShape::Circle, at))
Some((name, globe, FrameShape::Circle, center))
}
};
// The inset first: it outlines the area, and the globe after it outlines the inset.
let inset_locator = locators
.contains(&Locator::Inset)
.then(|| (s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle, center));
// The inset's centre and scale here are the frame's: its own are worked out as it is added.
let inset_locator = locators.contains(&Locator::Inset).then(|| (s.map().name.clone(), scale, FrameShape::Rectangle, center));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let locator_maps: Vec<_> = [(Locator::Inset, inset_locator), (Locator::Globe, globe_locator)]
.into_iter()
.filter_map(|(kind, frame)| frame.map(|frame| (kind, frame)))
Expand Down Expand Up @@ -1963,13 +1941,19 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
Locator::Inset => "Locator Inset",
Locator::Globe => "Locator Globe",
};
// The inset follows the map frame as placed: the legend's room may have moved or
// zoomed it out (#3362).
let (locator_center, locator_scale) = match kind {
Locator::Inset => (center, inset_scale(s.map(), center, scale, (fw, fh), side)),
Locator::Globe => (*locator_center, *locator_scale),
};
target = l.add(
name,
(r.0 + slot as f64 * (side + locator_gap), r.1, side, r.3),
ElementKind::MapFrame {
map: map.clone(),
center: *locator_center,
scale: *locator_scale,
center: locator_center,
scale: locator_scale,
shape: shape.clone(),
grid: None,
sections: Vec::new(),
Expand Down Expand Up @@ -2281,11 +2265,10 @@ fn fit_frame(
}
// The globe seen whole fills the frame, centred where the view looks (#2337): fitting the
// whole Earth's flat footprint instead drew it a fraction of the frame, around 0°, 0°.
// Its size at a scale is the same at any latitude, so it is worked out on the equator,
// where the probe stays within the camera's resolutions.
// Its size at a scale is the same at any latitude, so it is worked out on the equator.
let view = map.center_lonlat();
let scale = round_scale(globe_scale(map, Coord::new(view.x, 0.0), w.min(h) * WHOLE_GLOBE_FILL));
Some((drawable_globe_center(map, view, scale), scale))
Some((view, scale))
}

/// Reads `map`'s streamed layers as a frame `size` (mm) at 1:`scale` around `center` draws them,
Expand Down Expand Up @@ -2416,53 +2399,16 @@ pub(in crate::cmd::layout) fn shows_whole_globe(map: &rustgis_map::Map, p: &Valu
/// less than all of it, for the atmosphere's glow at its rim.
pub(in crate::cmd::layout) const WHOLE_GLOBE_FILL: f64 = 0.94;

/// Where a frame of `map` at 1:`scale` centres the globe seen whole: at `view`, or as near it on
/// its meridian as the frame's camera can draw the globe that small. Towards a pole the camera's
/// resolution for a scale grows as one over the cosine of the latitude, and past its coarsest
/// ([`rustgis_map::camera::MAX_RES`]) the globe would be drawn larger than the frame (#2337).
pub(in crate::cmd::layout) fn drawable_globe_center(map: &Map, view: Coord, scale: f64) -> Coord {
let fits =
|lat: f64| rustgis_map::layout::frame_camera(map, Coord::new(view.x, lat), scale, 0.0, 0.0).res < rustgis_map::camera::MAX_RES;
if fits(view.y) {
return view;
}
// The latitude in between that the camera still draws at the scale, found by halving.
let (mut fit, mut over) = (0.0f64, view.y.abs());
for _ in 0..40 {
let mid = (fit + over) / 2.0;
if fits(mid.copysign(view.y)) { fit = mid } else { over = mid }
}
Coord::new(view.x, fit.copysign(view.y))
}

/// Centres the globe locator as close to the area as its circle can hold the rendered disc.
/// If even the equator does not fit, or the area would be behind the rim, keep the locator
/// centred on the area and let its circle clip the globe (#2394).
pub(crate) fn globe_locator_center(map: &Map, area: Coord, scale: f64, side_mm: f64) -> (Coord, bool) {
let side = side_mm * rustgis_map::layout::PT_PER_MM;
let globe = |at| {
let camera = rustgis_map::layout::frame_camera(map, at, scale, 0.0, 0.0);
rustgis_map::globe::Globe::new(&camera, side, side)
};
let fits = |at| 2.0 * globe(at).disc_radius() <= side;
if fits(area) {
return (area, true);
}
let equator = Coord::new(area.x, 0.0);
if !fits(equator) {
return (area, false);
}
// At a fixed scale the disc only grows towards the pole, even after resolution is clamped.
let (mut inside, mut outside) = (equator, area);
for _ in 0..40 {
let mid = Coord::new(area.x, (inside.y + outside.y) / 2.0);
if fits(mid) {
inside = mid;
} else {
outside = mid;
}
}
if globe(inside).forward(area).is_some() { (inside, true) } else { (area, false) }
/// The scale of a square inset locator `side` mm across, of a frame `(w, h)` mm at 1:`scale`
/// around `center`: four times the frame's width, but no farther out than the map view zooms, past
/// which a flat world is a small picture in an empty box (#3362); far enough out to hold all the
/// frame shows, up to [`rustgis_map::layout::MAX_FRAME_SCALE`], which only a locator a long note
/// has squeezed under about a centimetre reaches (the publication says the column has little room).
fn inset_scale(map: &Map, center: Coord, scale: f64, (w, h): (f64, f64), side: f64) -> f64 {
let wide = round_scale(scale * w / side * 4.0);
let res = rustgis_map::layout::frame_camera(map, center, wide, 0.0, 0.0).res;
let holds = scale * w.max(h) / side;
wide.min(wide * rustgis_map::camera::MAX_RES / res).max(holds).min(rustgis_map::layout::MAX_FRAME_SCALE)
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}

/// Parses the publication's locator choice, a name or a list of "inset" and "globe", with its
Expand Down
109 changes: 21 additions & 88 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ mod layout_publication_locators;
#[path = "tests/layout_publication_crs.rs"]
mod layout_publication_crs;

#[path = "tests/layout_frame_scale.rs"]
mod layout_frame_scale;

#[path = "tests/sde_connections.rs"]
mod sde_connections;

Expand Down Expand Up @@ -15640,20 +15643,18 @@ fn a_publication_of_the_whole_globe_fills_its_frame() {
assert!((nx - cx).hypot(ny - cy) >= across / 2.0, "{profile}: the legend's corner at {nx:.0}, {ny:.0} is over the globe");
}
}
// Looking at the Arctic, where the frame's camera can't draw the globe that small at the
// view's latitude: centred as near it as fits, said, the globe still within the frame.
// Looking at the Arctic, the globe is centred where the view looks there too (#3362), still
// within the frame.
s.run("map.goToXY", &json!({"lon": 20.0, "lat": 84.0})).unwrap();
for profile in ["figure-1col", "a4-portrait"] {
let r = s.run("layout.publication", &json!({"profile": profile})).unwrap();
s.run("layout.publication", &json!({"profile": profile})).unwrap();
let l = s.project.layouts.last().unwrap();
let frame = l.elements.iter().find(|e| e.name == "Map Frame").unwrap();
let ElementKind::MapFrame { center, scale, .. } = &frame.kind else { panic!() };
let cam = frame_camera(s.map(), *center, *scale, 0.0, 0.0);
let across = 2.0 * rustgis_map::globe::Globe::new(&cam, frame.w * PT_PER_MM, frame.h * PT_PER_MM).disc_radius() / PT_PER_MM;
assert!(across < frame.w.min(frame.h), "{profile}: a disc {across:.0} mm across in {:.0} × {:.0} mm", frame.w, frame.h);
assert!((center.x - 20.0).abs() < 1e-6 && center.y > 0.0 && center.y <= 84.0, "{profile}: {center:?}");
let said = r["warnings"].to_string();
assert_eq!(said.contains("rather than the view's 84.0°"), center.y < 84.0 - 1e-6, "{profile}: {said}");
assert!((center.x - 20.0).abs() < 1e-6 && (center.y - 84.0).abs() < 1e-6, "{profile}: {center:?}");
}
// A locator asked for still comes, and "none" asked for is no news; an extent given keeps
// the scale bar and the locator.
Expand All @@ -15669,69 +15670,9 @@ fn a_publication_of_the_whole_globe_fills_its_frame() {
assert!(!warnings.contains("the map shows the whole globe"), "{warnings}");
}

/// A globe locator moved off a polar area keeps the area on the disc (#2348): at the sizes where
/// the nearest centre the camera draws would leave it behind the rim (a locator just large enough
/// for the globe on the equator), it keeps the area in the middle, clipped, instead.
#[test]
fn a_globe_locator_never_hides_its_area_behind_the_rim() {
use rustgis_geom::Coord;
use rustgis_map::layout::{PT_PER_MM, frame_camera};
let mut m = rustgis_map::Map::new("Locator");
m.set_projection(rustgis_map::Projection::Globe);
let (area, side) = (Coord::new(20.0, 89.0), 40.0);
let (mut moved, mut hidden) = (0, 0);
// Steps of 0.2 %: the sizes that would hide it span about 1 % of the scale.
for i in 0..4000 {
let scale = 1e8 * 1.002f64.powi(i);
let (at, whole) = crate::cmd::layout::globe_locator_center(&m, area, scale, side);
let equator = frame_camera(&m, Coord::new(area.x, 0.0), scale, 0.0, 0.0).res < rustgis_map::camera::MAX_RES;
if whole {
let g = rustgis_map::globe::Globe::new(&frame_camera(&m, at, scale, 0.0, 0.0), side * PT_PER_MM, side * PT_PER_MM);
assert!(g.forward(area).is_some(), "1:{scale:.0}: the area behind the rim from {at:?}");
moved += usize::from(at != area);
} else {
assert_eq!(at, area, "1:{scale:.0}: a clipped locator keeps the area in the middle");
hidden += usize::from(equator);
}
}
assert!(moved > 0 && hidden > 0, "{moved} moved, {hidden} kept on the area though the equator fits");
}

/// A globe locator a little over the smallest globe the camera draws holds it whole at that limit,
/// on the equator, and says nothing is clipped; one under it is clipped (#2348).
#[test]
fn a_globe_locator_at_the_camera_limit_is_whole_when_its_disc_fits() {
use rustgis_geom::Coord;
use rustgis_map::layout::{PT_PER_MM, frame_camera};
let mut m = rustgis_map::Map::new("Locator");
m.set_projection(rustgis_map::Projection::Globe);
// A scale past the camera's coarsest everywhere: the smallest globe it draws, on the equator.
let (area, scale) = (Coord::new(20.0, 30.0), 1e11);
let equator = Coord::new(area.x, 0.0);
let smallest = |side: f64| {
let cam = frame_camera(&m, equator, scale, 0.0, 0.0);
assert!(cam.res >= rustgis_map::camera::MAX_RES);
2.0 * rustgis_map::globe::Globe::new(&cam, side * PT_PER_MM, side * PT_PER_MM).disc_radius() / PT_PER_MM
};
let over = smallest(19.0) + 0.5;
let (at, whole) = crate::cmd::layout::globe_locator_center(&m, area, scale, over);
assert!(whole && at.x == area.x && (0.0..area.y).contains(&at.y) && at.y > 0.0, "a {over:.1} mm locator at {at:?}");
let globe_at = |center| {
let cam = frame_camera(&m, center, scale, 0.0, 0.0);
let side = over * PT_PER_MM;
rustgis_map::globe::Globe::new(&cam, side, side)
};
let globe = globe_at(at);
assert!(2.0 * globe.disc_radius() <= over * PT_PER_MM && globe.forward(area).is_some());
assert!(2.0 * globe_at(Coord::new(at.x, at.y + 0.01)).disc_radius() > over * PT_PER_MM);
let under = smallest(19.0) - 0.5;
assert_eq!(crate::cmd::layout::globe_locator_center(&m, area, scale, under), (area, false), "a {under:.1} mm locator");
}

/// A publication's globe locator near a pole shows the globe whole in its circle (#2337): sized on
/// the equator and centred as near the area as the camera draws it, not at the area's latitude,
/// where the camera stops at its coarsest resolution and the disc came out larger than the circle.
/// On a flat map with an Arctic extent, and on the globe seen whole with a locator asked for.
/// A publication's globe locator near a pole shows the globe whole in its circle (#2337), centred
/// on the area there as anywhere (#3362). On a flat map with an Arctic extent, and on the globe
/// seen whole with a locator asked for.
#[test]
fn a_globe_locator_near_a_pole_fits_its_circle() {
use rustgis_map::layout::{ElementKind, PT_PER_MM, frame_camera};
Expand All @@ -15745,7 +15686,6 @@ fn a_globe_locator_near_a_pole_fits_its_circle() {
let across = 2.0 * rustgis_map::globe::Globe::new(&cam, locator.w * PT_PER_MM, locator.h * PT_PER_MM).disc_radius() / PT_PER_MM;
assert!(across <= locator.w.min(locator.h), "{case}: a disc {across:.0} mm across in a {:.0} mm circle", locator.w);
};
let said = |r: &Value, start: &str| r["warnings"].as_array().unwrap().iter().filter_map(Value::as_str).any(|w| w.starts_with(start));
// The locator centred where the map frame is.
let centred_with_the_map = |s: &Session, case: &str| {
let l = s.project.layouts.last().unwrap();
Expand All @@ -15755,27 +15695,20 @@ fn a_globe_locator_near_a_pole_fits_its_circle() {
};
assert_eq!(centre("Locator"), centre("Map Frame"), "{case}");
};
const MOVED: &str = "the globe locator is centred at";
const CLIPPED: &str = "the globe locator is too small";
let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": [10.0, 76.0, 30.0, 81.0]})).unwrap();
fits(&s, "Svalbard on a flat map");
assert!(said(&r, MOVED) && !said(&r, CLIPPED), "Svalbard: {}", r["warnings"]);
// At the camera's coarsest resolution the globe still fits this 18 mm circle,
// and the polar extent is visible from the closest fitting centre (#2394).
let r =
s.run("layout.publication", &json!({"profile": "figure-1col", "extent": [10.0, 83.0, 30.0, 84.0], "locator": "globe"})).unwrap();
fits(&s, "an 18 mm polar locator");
assert!(said(&r, MOVED) && !said(&r, CLIPPED), "a figure's locator: {}", r["warnings"]);
for (case, p) in [
("Svalbard on a flat map", json!({"profile": "a4-portrait", "extent": [10.0, 76.0, 30.0, 81.0]})),
("an 18 mm polar locator", json!({"profile": "figure-1col", "extent": [10.0, 83.0, 30.0, 84.0], "locator": "globe"})),
("north-east Brazil", json!({"profile": "a4-portrait", "extent": [-46.8, -11.6, -38.5, -2.2]})),
] {
s.run("layout.publication", &p).unwrap();
fits(&s, case);
centred_with_the_map(&s, case);
}
s.run("map.projection", &json!({"type": "globe"})).unwrap();
s.run("map.goToXY", &json!({"lon": 20.0, "lat": 84.0})).unwrap();
let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "locator": "globe"})).unwrap();
s.run("layout.publication", &json!({"profile": "a4-portrait", "locator": "globe"})).unwrap();
fits(&s, "the whole globe at 84° N");
assert!(said(&r, MOVED) && !said(&r, CLIPPED), "the whole globe: {}", r["warnings"]);
// Away from the poles the locator stays centred on the area, as before, and says nothing.
let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": [-46.8, -11.6, -38.5, -2.2]})).unwrap();
fits(&s, "north-east Brazil");
assert!(!said(&r, MOVED) && !said(&r, CLIPPED), "north-east Brazil: {}", r["warnings"]);
centred_with_the_map(&s, "north-east Brazil");
centred_with_the_map(&s, "the whole globe at 84° N");
}

/// Arrangements of a publication layout's surrounds (#1379): by name, corner by corner, the
Expand Down
Loading
Loading