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
23 changes: 19 additions & 4 deletions crates/engine/src/cmd/layout/publication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
let at = flat.forward_checked(center).filter(|_| !plane.is_empty());
if let Some(at) = at.filter(|_| coverage.as_ref().is_some_and(|c| need(free_room(c)).is_none())) {
let (centred, asked_scale) = (center, scale);
let ground_at = flat.ground_per_unit(at);
let continuous = coverage.as_ref().is_some_and(|(gw, gh, cells)| {
let mm = 1000.0 * flat.ground_per_unit(plane.center()) * s.map().body().ratio() / asked_scale;
let (half_w, half_h) = (plane.width() * mm / 2.0, plane.height() * mm / 2.0);
Expand Down Expand Up @@ -1629,8 +1630,14 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
// by `dx` and south by `dy` is the wide one's part that far west and north of
// its middle while that scale moves a corner of it one cell of the coverage grid
// at most, which the cut is exact to anyway; past that, it is drawn on its own
// (what is context then judged on itself, for the legend too), once.
let here_mm = mm * flat.ground_per_unit(Coord::new(cx, cy)) / flat.ground_per_unit(plane.center());
// (what is context then judged on itself, for the legend too), once. A move that
// would draw the extent more than `MOVE_SIZE_CHANGE` larger or smaller isn't made
// either: a smaller extent fits any margins, so the margins check can't refuse it.
// A sheet then goes on to the scales tried after this one (zooming out, said); a
// figure keeps its scale, so with no move left the legend is laid out where the
// map is, as at any place no move makes room (#3514).
let ground_here = flat.ground_per_unit(Coord::new(cx, cy));
let here_mm = mm * ground_here / flat.ground_per_unit(plane.center());
let clip = (
fx + fw / 2.0 + (plane.min_x - cx) * here_mm,
fy + fh / 2.0 - (plane.max_y - cy) * here_mm,
Expand All @@ -1641,10 +1648,11 @@ pub(in crate::cmd::layout) fn publication(s: &mut Session, p: &Value) -> CmdResu
&& clip.1 >= fy + top - 0.5
&& clip.0 + clip.2 <= fx + fw - right + 0.5
&& clip.1 + clip.3 <= fy + fh - bottom + 0.5;
if !still && !kept_whole {
let size_change = (1.0 - ground_here / ground_at).abs();
if !still && (!kept_whole || size_change > MOVE_SIZE_CHANGE) {
continue;
}
let drift = (1.0 - flat.ground_per_unit(Coord::new(cx, cy)) / flat.ground_per_unit(at)).abs() * fw.hypot(fh) / 2.0;
let drift = size_change * fw.hypot(fh) / 2.0;
let (mut moved, legend_sizes, legend_context) = if drift <= fw.max(fh) / 384.0 {
(crop_cells(whole_frame, wide, (slack_x - dx, slack_y - dy), (fw, fh)), *here, here_context.clone())
} else if let Some((_, own, own_context, own_sizes)) = own_drawn.iter().find(|o| o.0 == (tried, dx, dy)) {
Expand Down Expand Up @@ -2398,6 +2406,13 @@ pub(in crate::cmd::layout) fn publication_flag(p: &Value, key: &str) -> Result<O
/// everywhere, and they give north themselves (#1379).
pub(crate) const CONTINENTAL_DEG: f64 = 20.0;

/// How much a move of the map for the legend's room may change the size the extent is drawn at
/// (Web Mercator's ground scale changes with the latitude): 20 %, which is 1 - 1/1.25, the step the
/// scale search zooms out by. A move that would draw the extent this much larger or smaller isn't
/// made, since a smaller extent passes the margins check that would otherwise refuse it. Tighter
/// (10 %) refuses the moves of a high-latitude extent that #1379 keeps (#3514).
const MOVE_SIZE_CHANGE: f64 = 0.20;

/// An extent's span in degrees on its longer side, its width as long at its middle latitude.
pub(crate) fn extent_span_deg(e: &Extent) -> f64 {
let mid = ((e.min_y + e.max_y) / 2.0).to_radians();
Expand Down
3 changes: 3 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ mod layout_publication_crs;
#[path = "tests/layout_frame_scale.rs"]
mod layout_frame_scale;

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

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

Expand Down
44 changes: 44 additions & 0 deletions crates/engine/src/tests/layout_publication_moves.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! A move of the publication's map for the legend's room keeps the extent's drawn size (#3514).

use super::*;
use rustgis_geom::Coord;
use rustgis_map::layout::ElementKind;

/// The centre of the layout's last map frame.
fn frame_centre(s: &Session) -> Coord {
let l = s.project.layouts.last().unwrap();
let ElementKind::MapFrame { center, .. } = &l.elements.iter().find(|e| e.name == "Map Frame").unwrap().kind else {
panic!("no Map Frame")
};
*center
}

/// A world extent with a one-column legend asks for the room the map leaves free; on Web Mercator
/// a move toward a pole there drew the extent several times smaller, and said nothing (#3514).
#[test]
fn a_move_for_the_legends_room_does_not_shrink_the_extent_toward_a_pole() {
for profile in ["a4-portrait", "a5-portrait", "thesis-full"] {
// Where the extent sits centred (no legend, nothing to move aside), and with the legend.
let extent = json!([-111.5, -16.6, 146.5, 62.6]);
let mut centred = world();
let centred_answer = centred.run("layout.publication", &json!({"profile": profile, "extent": extent, "legend": "none"})).unwrap();
let mut s = world();
let answer = s.run("layout.publication", &json!({"profile": profile, "extent": extent, "legend": {"columns": 1}})).unwrap();
let flat = s.map().flat();
let at = |lonlat: Coord| flat.ground_per_unit(flat.forward_checked(lonlat).unwrap());
let stretch = at(frame_centre(&s)) / at(frame_centre(&centred));
// The legend asked for in one column is laid out in one column, with no move or veil.
assert_eq!(
(answer["legendColumns"].as_u64(), answer["veiled"].as_bool(), answer["mapShift"][0].as_f64()),
(Some(1), Some(false), Some(0.0)),
"{profile}: {answer}"
);
// Not drawn at a smaller scale instead: the same scale as centred.
assert_eq!(answer["scale"], centred_answer["scale"], "{profile}: {answer}");
// 0.20 is `MOVE_SIZE_CHANGE` in `cmd/layout/publication/mod.rs`; keep them equal.
assert!(
(stretch - 1.0).abs() <= 0.20,
"{profile}: the frame's centre draws the extent {stretch} times its size, more than 20 % (the search's own zoom-out step, 1.25, as a fraction)"
);
}
}
3 changes: 2 additions & 1 deletion docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
nearest, the one with the most room is kept. Where the projection stretches, as Web Mercator
does away from the equator, the
moved frame is drawn at its new centre's ground scale, and the extent has to stay whole at that
scale. Only when no move makes room for it, a continuous map with a long legend is veiled: at
scale and keep its size within 20%: a move that would draw it 20% larger or smaller isn't made.
Only when no move makes room for it, a continuous map with a long legend is veiled: at
the asked scale, at least 80% of the extent's part of the frame and 30% of the rest are drawn
(not counting the layers just named), and the legend's box in one column, its colorbar included,
is taller than a quarter of the frame; a sparser map stays unveiled. The veil covers what is
Expand Down
Loading