Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,21 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
m.graticule = src.graticule;
m.body = src.body.clone();
m.set_projection(rustgis_map::Projection::Globe);
let globe = globe_scale(&m, center, side * 0.92);
// 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);
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 @@ -2031,9 +2045,9 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
}
None => s.project.maps.push(m),
}
Some((name, globe, FrameShape::Circle))
Some((name, globe, FrameShape::Circle, at))
}
Locator::Inset => Some((s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle)),
Locator::Inset => Some((s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle, center)),
};
// The legend lists what the frame draws: its map synced to the frame's view first, as an
// export does, so a streamed layer has its features there. (This changes the session's map,
Expand Down Expand Up @@ -2704,7 +2718,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
let card = style == Style::Modular && column.is_some();
let (card_r, card_pad) = (3.0 * k, 2.5 * k);
let surround_box = |fill: Rgba| if style.cards() { pub_card(fill, clear, 0.0, 2.0 * k) } else { pub_box(fill, clear, 0.0) };
if let (Some((map, locator_scale, shape)), Some(r)) = (&locator_map, locator_rect) {
if let (Some((map, locator_scale, shape, locator_center)), Some(r)) = (&locator_map, locator_rect) {
if let (true, Some(c)) = (card, column) {
l.add(
"Locator Card",
Expand All @@ -2717,7 +2731,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult {
r,
ElementKind::MapFrame {
map: map.clone(),
center,
center: *locator_center,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
scale: *locator_scale,
shape: shape.clone(),
grid: None,
Expand Down Expand Up @@ -5758,6 +5772,20 @@ fn drawable_globe_center(map: &Map, view: Coord, scale: f64) -> Coord {
Coord::new(view.x, fit.copysign(view.y))
}

/// Where a globe locator `side_mm` across at 1:`scale` centres the globe, and whether it shows it
/// whole: as near `area` as the camera draws a globe that small ([`drawable_globe_center`], the
/// equator when it draws it that small nowhere), the disc within the circle and the area on it.
/// A locator too small for the globe even on the equator (under about 20 mm), or one whose nearest
/// centre leaves the area behind the globe's rim, keeps the area in the middle, its circle
/// clipping the globe (#2348). Whether it is whole is measured on the disc drawn, not on the
/// camera's limit: a circle a little over the smallest globe holds it at that limit.
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 at = drawable_globe_center(map, area, scale);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
let g = rustgis_map::globe::Globe::new(&rustgis_map::layout::frame_camera(map, at, scale, 0.0, 0.0), side, side);
if 2.0 * g.disc_radius() <= side && g.forward(area).is_some() { (at, true) } else { (area, false) }
}

/// Parses the publication's locator choice with its default: a globe on a sheet, none on a figure
/// or where the map is itself the globe seen whole (it would only show it again, smaller).
fn publication_locator(p: &Value, sheet: bool, whole_globe: bool) -> Result<Locator, String> {
Expand Down
107 changes: 105 additions & 2 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10347,7 +10347,109 @@ fn a_publication_of_the_whole_globe_fills_its_frame() {
let l = s.project.layouts.last().unwrap();
assert!(r["locator"].is_string() && l.elements.iter().any(|e| e.name == "Scale Bar"));
let warnings = r["warnings"].to_string();
assert!(!warnings.contains("the whole globe"), "{warnings}");
// Neither whole-globe notice (no scale bar, no locator).
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);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}
}
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;
assert_eq!(crate::cmd::layout::globe_locator_center(&m, area, scale, over), (equator, true), "a {over:.1} mm locator");
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.
#[test]
fn a_globe_locator_near_a_pole_fits_its_circle() {
use rustgis_map::layout::{ElementKind, PT_PER_MM, frame_camera};
let mut s = world();
let fits = |s: &Session, case: &str| {
let l = s.project.layouts.last().unwrap();
let locator = l.elements.iter().find(|e| e.name == "Locator").unwrap_or_else(|| panic!("{case}: no locator"));
let ElementKind::MapFrame { map, center, scale, .. } = &locator.kind else { panic!() };
let m = s.project.maps.iter().find(|m| &m.name == map).unwrap();
let cam = frame_camera(m, *center, *scale, 0.0, 0.0);
assert!(cam.res < rustgis_map::camera::MAX_RES, "{case}: the camera at its coarsest, 1:{scale} at {center:?}");
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();
let centre = |n: &str| match l.elements.iter().find(|e| e.name == n).map(|e| &e.kind) {
Some(ElementKind::MapFrame { center, .. }) => *center,
_ => panic!("{case}: no {n}"),
};
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"]);
// A figure's locator, too small for the globe even on the equator: clipped wherever it is, it
// keeps the area in the middle and says so.
let r =
s.run("layout.publication", &json!({"profile": "figure-1col", "extent": [10.0, 83.0, 30.0, 84.0], "locator": "globe"})).unwrap();
assert!(said(&r, CLIPPED) && !said(&r, MOVED), "a figure's locator: {}", r["warnings"]);
centred_with_the_map(&s, "a figure's locator");
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();
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");
}

/// Arrangements of a publication layout's surrounds (#1379): by name, corner by corner, the
Expand Down Expand Up @@ -51614,7 +51716,8 @@ fn a_globe_locator_says_what_simplifying_did() {
.unwrap()
.iter()
.filter_map(|w| w.as_str())
.filter(|w| w.starts_with("the globe locator"))
// What simplifying did, not where the locator is centred (#2348).
.filter(|w| w.starts_with("the globe locator, simplifying"))
.map(String::from)
.collect::<Vec<_>>()
};
Expand Down
Loading
Loading