From cf88179b3739658d397015b835c2cb4f5d5423e6 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 22 Nov 2026 10:25:25 -0700 Subject: [PATCH 1/3] fix(layout): a map frame can be centred on a pole in a polar projection (#2684) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The layout sanitizer clamped a frame's centre latitude to ±85° whatever the map's projection, and layout.update's centre did the same. A frame centred on the South Pole of a polar stereographic map was moved to 85° S without a word, the pole off the frame's centre. The centre's latitude is now kept to ±90°, and the map's projection decides where the frame is drawn: frame_camera centres it where the projection places the centre, and at 85° where it can't (a pole on Web Mercator, at infinity), as frames always were. frame_center_placed tells the two apart by a round trip. layout.import and layout.update say when a frame's centre lies where its map's projection can't show it. --- crates/engine/src/cmd/layout.rs | 33 ++++++++++++++++++++- crates/engine/src/tests.rs | 36 +++++++++++++++++++++++ crates/map/src/layout.rs | 52 +++++++++++++++++++++++++++++++-- 3 files changed, 118 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 3fede227fe..31ca33ec53 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -4996,7 +4996,8 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { Some(c) => { let a = c.as_array().ok_or("center is [lon, lat]")?; match (a.first().and_then(Value::as_f64), a.get(1).and_then(Value::as_f64)) { - (Some(x), Some(y)) if x.is_finite() && y.is_finite() => Some(Coord::new(x.clamp(-540.0, 540.0), y.clamp(-85.0, 85.0))), + // A pole too: the frame's map decides where it's drawn (#2684). + (Some(x), Some(y)) if x.is_finite() && y.is_finite() => Some(Coord::new(x.clamp(-540.0, 540.0), y.clamp(-90.0, 90.0))), _ => return Err("center is [lon, lat]".into()), } } @@ -5535,6 +5536,13 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { if shown || TEXT_FIT_KEYS.iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) || BAR_FIT_KEYS.iter().any(|k| p.get(*k).is_some()) { say_text_fit(s, li, id); } + // A centre the frame's map can't place (a pole on Web Mercator) is said (#2684). + if new_center.is_some() || given(&["map"]) { + let said = s.project.layouts.get(li).and_then(|l| unplaced_center(s, l, id)); + if let Some(said) = said { + s.notify(said, false); + } + } say_held(s, "Element Properties", held); if let Some(w) = font_told { return Ok(json!({"warning": w})); @@ -6368,6 +6376,11 @@ pub(crate) fn import(s: &mut Session, text: &str) -> CmdResult { false, ); } + // A frame centred where its map can't place the point (a pole on Web Mercator) is said (#2684). + let unplaced: Vec = l.elements.iter().filter_map(|e| unplaced_center(s, &l, e.id)).collect(); + for said in unplaced { + s.notify(said, false); + } let name = l.name.clone(); s.project.layouts.push(l); let i = s.project.layouts.len() - 1; @@ -6375,6 +6388,24 @@ pub(crate) fn import(s: &mut Session, text: &str) -> CmdResult { Ok(json!({"layout": i, "name": name})) } +/// What is said of map frame `id` in `l` when its centre lies where its map's projection can't +/// place it (a pole on Web Mercator, whose poles are at infinity): it's drawn at 85° instead +/// (#2684). `None` when it's drawn where it's centred, or `id` isn't a frame of a map here. +fn unplaced_center(s: &Session, l: &Layout, id: u32) -> Option { + let e = l.element(id)?; + let ElementKind::MapFrame { map, center, .. } = &e.kind else { return None }; + let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map))?; + (!rustgis_map::layout::frame_center_placed(m, *center)).then(|| { + format!( + "{}: the frame '{}' is centred at latitude {}, which the map's projection can't show: it's drawn at {}", + crate::project::short_name(&l.name), + crate::project::short_name(&e.name), + rustgis_map::symbology::fmt_num(center.y), + if center.y < 0.0 { "85° S" } else { "85° N" } + ) + }) +} + /// Selects the explicit or first feature layer in the stored frame's map. fn frame_layer_name(input: &impl super::InputSource, l: &Layout, frame: u32, p: &Value) -> Result { let context = input.input_context(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index abfdd7b115..495a6ae43d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -14602,6 +14602,42 @@ fn a_publications_colorbar_title_stays_in_the_legends_box() { let _ = std::fs::remove_dir_all(&dir); } +/// A layout frame centred on a pole keeps that centre through `layout.import` and +/// `layout.update`, and says nothing on a polar map, where it's drawn there; on Web Mercator, +/// which can't show a pole, it says the frame is drawn at 85° (#2684). +#[test] +fn a_layout_frame_centred_on_a_pole_is_kept_and_said_where_it_cant_be_shown() { + use rustgis_map::layout::ElementKind; + // The issue's layout: one round frame on the South Pole. + let layout = r#"{"name":"Polo","width":150,"height":150,"elements":[{"id":1,"name":"Mapa","x":5,"y":5,"w":140,"h":140,"visible":true, + "kind":{"MapFrame":{"map":"Mapa","center":{"x":0.0,"y":-90.0},"scale":60000000.0,"shape":"Circle", + "grid":{"step":10.0,"color":[0,0,0,255],"labels":false,"kind":"graticule","short":false},"extent_of":null}}}], + "series":null,"next_id":2}"#; + for (projection, said) in [(Some("EPSG:3031"), false), (None, true)] { + let mut s = Session::new(Project::default()); + s.run("map.new", &json!({"name": "Mapa"})).unwrap(); + if let Some(p) = projection { + s.run("map.projection", &json!({"type": p})).unwrap(); + } + let center = |s: &Session| match s.project.layouts.last().map(|l| &l.elements[0].kind) { + Some(ElementKind::MapFrame { center, .. }) => center.y, + _ => panic!("no frame"), + }; + let told = |s: &Session, mark| s.notices_since(mark).iter().any(|n| n.text.contains("can't show") && n.text.contains("85° S")); + let mark = s.notice_mark(); + s.run("layout.import", &json!({"text": layout})).unwrap(); + assert_eq!(center(&s), -90.0, "{projection:?}: imported"); + assert_eq!(told(&s, mark), said, "{projection:?}: {:?}", s.notices_since(mark)); + // Moved away, then back onto the pole by its properties. + s.run("layout.update", &json!({"element": 1, "center": [0, -60]})).unwrap(); + let mark = s.notice_mark(); + assert!(!told(&s, mark)); + s.run("layout.update", &json!({"element": 1, "center": [0, -90]})).unwrap(); + assert_eq!(center(&s), -90.0, "{projection:?}: updated"); + assert_eq!(told(&s, mark), said, "{projection:?}: {:?}", s.notices_since(mark)); + } +} + /// Series page labels and dynamic text share the row-number fallback for null and blank names. #[test] fn map_series_empty_names_use_row_numbers() { diff --git a/crates/map/src/layout.rs b/crates/map/src/layout.rs index 7c7f1ba3d3..e2f006b591 100644 --- a/crates/map/src/layout.rs +++ b/crates/map/src/layout.rs @@ -2527,7 +2527,9 @@ impl Layout { *center = Coord::new(0.0, 0.0); } center.x = center.x.clamp(-540.0, 540.0); - center.y = center.y.clamp(-85.0, 85.0); + // A pole is a centre like any other on a polar map: the map's projection + // decides where the frame is drawn ([`frame_camera`], #2684). + center.y = center.y.clamp(-90.0, 90.0); if let FrameShape::Polygon(p) = shape { let points = p.len(); p.retain(|(u, v)| u.is_finite() && v.is_finite()); @@ -2748,7 +2750,10 @@ fn is_zero(v: &f64) -> bool { /// turned or tilted. pub fn frame_camera(map: &Map, center: Coord, scale: f64, bearing: f64, pitch: f64) -> Camera { let mut cam = Camera { bearing: crate::camera::normalize_bearing(bearing), pitch: crate::camera::normalize_pitch(pitch), ..map.camera }; - if let Some(c) = map.to_map(center) { + // The centre where the map's projection places it: a pole itself on a polar map. Where it + // can't (a pole on Web Mercator, at infinity), 85° of latitude, as frames always were (#2684). + let at = if frame_center_placed(map, center) { center } else { Coord::new(center.x, center.y.clamp(-85.0, 85.0)) }; + if let Some(c) = map.to_map(at) { cam.center = c; } let per = map.ground_per_unit(cam.center).max(1e-9); @@ -2757,6 +2762,18 @@ pub fn frame_camera(map: &Map, center: Coord, scale: f64, bearing: f64, pitch: f cam } +/// Whether a frame centred on `center` (lon/lat) is drawn centred there on `map`: its projection +/// places the point within a view's reach. A pole on Web Mercator isn't (it lies at infinity; +/// [`frame_camera`] draws the frame at 85° instead); on a polar map it is (#2684). +pub fn frame_center_placed(map: &Map, center: Coord) -> bool { + let lim = std::f64::consts::PI * rustgis_geom::MERCATOR_R; + // Placed, and placed there: Web Mercator's projection takes a pole to its edge at 85.05°, which + // reads back as that latitude, not the pole. + map.to_map(center).is_some_and(|m| { + m.x.abs() <= lim * 1.5 && m.y.abs() <= lim && map.to_lonlat(m).is_some_and(|back| (back.y - center.y).abs() < 1e-4) + }) +} + /// The scale that fits a lon/lat extent into a frame `w_mm`×`h_mm` with a margin, and its centre. pub fn fit_scale(map: &Map, e: &rustgis_geom::Extent, w_mm: f64, h_mm: f64) -> Option<(Coord, f64)> { if e.is_empty() || w_mm <= 0.0 || h_mm <= 0.0 { @@ -3800,6 +3817,37 @@ mod tests { assert_eq!(ScaleUnits::Meters.resolve(0.005), (1.0, "m"), "a chosen unit stays"); } + /// A frame centred on a pole keeps that centre, and is drawn centred there on a polar map; + /// Web Mercator, whose poles are at infinity, draws it at 85° as frames always were, and + /// [`frame_center_placed`] tells it can't place it (#2684). + #[test] + fn a_frame_centred_on_a_pole_is_drawn_there_on_a_polar_map() { + let mut l = Layout::new("Polo", 150.0, 150.0); + let frame: ElementKind = + serde_json::from_value(serde_json::json!({"MapFrame":{"map":"Mapa", "center":{"x":0,"y":-90}, "scale":60000000.0}})).unwrap(); + let id = l.add("Mapa", (5.0, 5.0, 140.0, 140.0), frame); + l.clamp(); + let center = |l: &Layout| match l.element(id).map(|e| &e.kind) { + Some(ElementKind::MapFrame { center, .. }) => *center, + _ => panic!("no frame"), + }; + assert_eq!(center(&l).y, -90.0, "the pole kept"); + // On a south polar stereographic map, the pole is the frame's centre. + let mut polar = Map::new("Mapa"); + polar.set_projection(crate::Projection::from_code("EPSG:3031").unwrap()); + let south = Coord::new(0.0, -90.0); + assert!(frame_center_placed(&polar, south)); + let cam = frame_camera(&polar, south, 60_000_000.0, 0.0, 0.0); + let pole = polar.to_map(south).unwrap(); + assert!((cam.center.x - pole.x).abs() < 1e-3 && (cam.center.y - pole.y).abs() < 1e-3, "{:?} {pole:?}", cam.center); + // On Web Mercator it can't be: drawn at 85° S. + let merc = Map::new("Mapa"); + assert!(!frame_center_placed(&merc, south) && frame_center_placed(&merc, Coord::new(0.0, -85.0))); + let cam = frame_camera(&merc, south, 60_000_000.0, 0.0, 0.0); + let at = merc.to_lonlat(cam.center).unwrap(); + assert!((at.y + 85.0).abs() < 1e-6, "{at:?}"); + } + #[test] fn frame_camera_follows_the_frame_on_the_globe() { let mut map = Map::new("Map"); From b21c0bb269600511c591dccb5f346ba5b2ea5a9c Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 22 Nov 2026 11:38:46 -0700 Subject: [PATCH 2/3] fix(layout): say where a frame the projection can't centre is drawn, in one counted notice (#2684) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round on #2711. - frame_center_placed bounded the map coordinates too, which rejected a valid centre past 270° of longitude on Web Mercator (the sanitizer keeps up to ±540°) and said a false "drawn at 85°". It now asks only that the projection places the centre and reads it back at the same latitude. - The notice named 85° whatever happened. It now says where each frame is drawn, as frame_camera draws it, and the map's view centre when that can't be read back. - An import with several such frames gave one notice each. It now gives one, counted, the first five named. --- crates/engine/src/cmd/layout.rs | 62 ++++++++++++++++++++++++--------- crates/engine/src/tests.rs | 33 +++++++++++++++--- crates/map/src/layout.rs | 15 ++++---- 3 files changed, 81 insertions(+), 29 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 31ca33ec53..8a848e4554 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -5538,7 +5538,8 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { } // A centre the frame's map can't place (a pole on Web Mercator) is said (#2684). if new_center.is_some() || given(&["map"]) { - let said = s.project.layouts.get(li).and_then(|l| unplaced_center(s, l, id)); + let said = + s.project.layouts.get(li).and_then(|l| unplaced_notice(&l.name, &unplaced_center(s, l, id).into_iter().collect::>())); if let Some(said) = said { s.notify(said, false); } @@ -6377,8 +6378,9 @@ pub(crate) fn import(s: &mut Session, text: &str) -> CmdResult { ); } // A frame centred where its map can't place the point (a pole on Web Mercator) is said (#2684). - let unplaced: Vec = l.elements.iter().filter_map(|e| unplaced_center(s, &l, e.id)).collect(); - for said in unplaced { + // One notice for them all, counted. + let unplaced: Vec<(String, Option)> = l.elements.iter().filter_map(|e| unplaced_center(s, &l, e.id)).collect(); + if let Some(said) = unplaced_notice(&l.name, &unplaced) { s.notify(said, false); } let name = l.name.clone(); @@ -6388,22 +6390,48 @@ pub(crate) fn import(s: &mut Session, text: &str) -> CmdResult { Ok(json!({"layout": i, "name": name})) } -/// What is said of map frame `id` in `l` when its centre lies where its map's projection can't -/// place it (a pole on Web Mercator, whose poles are at infinity): it's drawn at 85° instead -/// (#2684). `None` when it's drawn where it's centred, or `id` isn't a frame of a map here. -fn unplaced_center(s: &Session, l: &Layout, id: u32) -> Option { +/// Map frame `id` in `l` when its centre lies where its map's projection can't place it (a pole +/// on Web Mercator, whose poles are at infinity): its name and the latitude it's drawn centred +/// at instead, as [`rustgis_map::layout::frame_camera`] draws it (85° for a Mercator pole; +/// `None` when that can't be read back) (#2684). `None` when it's drawn where it's centred, or +/// `id` isn't a frame of a map here. +fn unplaced_center(s: &Session, l: &Layout, id: u32) -> Option<(String, Option)> { let e = l.element(id)?; - let ElementKind::MapFrame { map, center, .. } = &e.kind else { return None }; + let ElementKind::MapFrame { map, center, scale, .. } = &e.kind else { return None }; let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map))?; - (!rustgis_map::layout::frame_center_placed(m, *center)).then(|| { - format!( - "{}: the frame '{}' is centred at latitude {}, which the map's projection can't show: it's drawn at {}", - crate::project::short_name(&l.name), - crate::project::short_name(&e.name), - rustgis_map::symbology::fmt_num(center.y), - if center.y < 0.0 { "85° S" } else { "85° N" } - ) - }) + if rustgis_map::layout::frame_center_placed(m, *center) { + return None; + } + let cam = rustgis_map::layout::frame_camera(m, *center, *scale, 0.0, 0.0); + let drawn = m.to_lonlat(cam.center).map(|c| (c.y * 100.0).round() / 100.0).filter(|y| y.is_finite()); + Some((e.name.clone(), drawn)) +} + +/// One notice for the map frames of layout `layout` centred where their maps' projections can't +/// show them, with where each is drawn instead ([`unplaced_center`]): counted, the first five +/// named. `None` for none (#2684). +fn unplaced_notice(layout: &str, frames: &[(String, Option)]) -> Option { + if frames.is_empty() { + return None; + } + let at = |lat: &Option| match lat { + Some(y) => format!("latitude {}", rustgis_map::symbology::fmt_num(*y)), + None => "the map's view centre".to_string(), + }; + let mut named = frames + .iter() + .take(5) + .map(|(name, lat)| format!("'{}' at {}", crate::project::short_name(name), at(lat))) + .collect::>() + .join(", "); + if frames.len() > 5 { + named.push_str(&format!(" and {} more", frames.len() - 5)); + } + Some(format!( + "{}: {} map frame(s) are centred where their map's projection can't show the centre, and are drawn centred elsewhere: {named}", + crate::project::short_name(layout), + frames.len() + )) } /// Selects the explicit or first feature layer in the stored frame's map. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 495a6ae43d..25e55d1c16 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -14623,19 +14623,44 @@ fn a_layout_frame_centred_on_a_pole_is_kept_and_said_where_it_cant_be_shown() { Some(ElementKind::MapFrame { center, .. }) => center.y, _ => panic!("no frame"), }; - let told = |s: &Session, mark| s.notices_since(mark).iter().any(|n| n.text.contains("can't show") && n.text.contains("85° S")); + let told = |s: &Session, mark| { + s.notices_since(mark).iter().filter(|n| n.text.contains("can't show") && n.text.contains("'Mapa' at latitude -85")).count() == 1 + }; let mark = s.notice_mark(); s.run("layout.import", &json!({"text": layout})).unwrap(); assert_eq!(center(&s), -90.0, "{projection:?}: imported"); assert_eq!(told(&s, mark), said, "{projection:?}: {:?}", s.notices_since(mark)); - // Moved away, then back onto the pole by its properties. + // Moved away, then back onto the pole by its properties: each command says only its own. + let mark = s.notice_mark(); s.run("layout.update", &json!({"element": 1, "center": [0, -60]})).unwrap(); + assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("can't show")), "{:?}", s.notices_since(mark)); let mark = s.notice_mark(); - assert!(!told(&s, mark)); s.run("layout.update", &json!({"element": 1, "center": [0, -90]})).unwrap(); assert_eq!(center(&s), -90.0, "{projection:?}: updated"); assert_eq!(told(&s, mark), said, "{projection:?}: {:?}", s.notices_since(mark)); - } + // A longitude past ±180° isn't a centre the projection can't show. + let mark = s.notice_mark(); + s.run("layout.update", &json!({"element": 1, "center": [300, -60]})).unwrap(); + assert!(!s.notices_since(mark).iter().any(|n| n.text.contains("can't show")), "{:?}", s.notices_since(mark)); + } + // Two pole frames in one layout on Web Mercator: one notice, counting both. + let mut s = Session::new(Project::default()); + s.run("map.new", &json!({"name": "Mapa"})).unwrap(); + let mut two: Value = serde_json::from_str(layout).unwrap(); + let mut second = two["elements"][0].clone(); + second["id"] = json!(2); + second["name"] = json!("Norte"); + second["kind"]["MapFrame"]["center"]["y"] = json!(90.0); + two["elements"].as_array_mut().unwrap().push(second); + two["next_id"] = json!(3); + let mark = s.notice_mark(); + s.run("layout.import", &json!({"text": two.to_string()})).unwrap(); + let said: Vec<_> = s.notices_since(mark).iter().filter(|n| n.text.contains("can't show")).map(|n| n.text.clone()).collect(); + assert_eq!(said.len(), 1, "{said:?}"); + assert!( + said[0].contains("2 map frame(s)") && said[0].contains("'Mapa' at latitude -85") && said[0].contains("'Norte' at latitude 85"), + "{said:?}" + ); } /// Series page labels and dynamic text share the row-number fallback for null and blank names. diff --git a/crates/map/src/layout.rs b/crates/map/src/layout.rs index e2f006b591..cfc4124b0b 100644 --- a/crates/map/src/layout.rs +++ b/crates/map/src/layout.rs @@ -2763,15 +2763,12 @@ pub fn frame_camera(map: &Map, center: Coord, scale: f64, bearing: f64, pitch: f } /// Whether a frame centred on `center` (lon/lat) is drawn centred there on `map`: its projection -/// places the point within a view's reach. A pole on Web Mercator isn't (it lies at infinity; -/// [`frame_camera`] draws the frame at 85° instead); on a polar map it is (#2684). +/// places the point, and reads it back at the same latitude. A pole on Web Mercator isn't: its +/// projection takes a pole to its edge at 85.05°, which reads back as that ([`frame_camera`] +/// draws the frame at 85° instead); on a polar map it is (#2684). The longitude isn't compared: +/// it may run past ±180°, and at a pole every longitude is the same point. pub fn frame_center_placed(map: &Map, center: Coord) -> bool { - let lim = std::f64::consts::PI * rustgis_geom::MERCATOR_R; - // Placed, and placed there: Web Mercator's projection takes a pole to its edge at 85.05°, which - // reads back as that latitude, not the pole. - map.to_map(center).is_some_and(|m| { - m.x.abs() <= lim * 1.5 && m.y.abs() <= lim && map.to_lonlat(m).is_some_and(|back| (back.y - center.y).abs() < 1e-4) - }) + map.to_map(center).and_then(|m| map.to_lonlat(m)).is_some_and(|back| (back.y - center.y).abs() < 1e-4) } /// The scale that fits a lon/lat extent into a frame `w_mm`×`h_mm` with a margin, and its centre. @@ -3843,6 +3840,8 @@ mod tests { // On Web Mercator it can't be: drawn at 85° S. let merc = Map::new("Mapa"); assert!(!frame_center_placed(&merc, south) && frame_center_placed(&merc, Coord::new(0.0, -85.0))); + // A longitude past ±180° (the sanitizer keeps up to ±540°) is placed: only the latitude counts. + assert!(frame_center_placed(&merc, Coord::new(300.0, 40.0)) && frame_center_placed(&merc, Coord::new(-500.0, -60.0))); let cam = frame_camera(&merc, south, 60_000_000.0, 0.0, 0.0); let at = merc.to_lonlat(cam.center).unwrap(); assert!((at.y + 85.0).abs() < 1e-6, "{at:?}"); From 879a4e4c7a90eb3d68dd68e3b18ce29caad4a1df Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 22 Nov 2026 12:21:02 -0700 Subject: [PATCH 3/3] =?UTF-8?q?fix(layout):=20a=20frame=20centred=20past?= =?UTF-8?q?=20=C2=B1270=C2=B0=20of=20longitude=20shows=20that=20place,=20n?= =?UTF-8?q?ot=20the=20view's=20edge=20(#2684)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review on #2711: on Web Mercator the camera keeps a view within ±270° of longitude, so a frame centred at 300° (the sanitizer keeps up to ±540°) was drawn at 270°, another place, with nothing said. frame_camera now takes a longitude past ±270° as the same meridian a turn of the globe nearer, so the frame shows the place it's centred on. --- crates/map/src/layout.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/map/src/layout.rs b/crates/map/src/layout.rs index cfc4124b0b..ef0f7921bc 100644 --- a/crates/map/src/layout.rs +++ b/crates/map/src/layout.rs @@ -2752,6 +2752,9 @@ pub fn frame_camera(map: &Map, center: Coord, scale: f64, bearing: f64, pitch: f let mut cam = Camera { bearing: crate::camera::normalize_bearing(bearing), pitch: crate::camera::normalize_pitch(pitch), ..map.camera }; // The centre where the map's projection places it: a pole itself on a polar map. Where it // can't (a pole on Web Mercator, at infinity), 85° of latitude, as frames always were (#2684). + // A longitude past a view's reach (±270° on Web Mercator) is the same meridian a turn of the + // globe nearer: the frame shows the same place, not one moved to the view's edge (#2711). + let center = if center.x.abs() > 270.0 { Coord::new((center.x + 180.0).rem_euclid(360.0) - 180.0, center.y) } else { center }; let at = if frame_center_placed(map, center) { center } else { Coord::new(center.x, center.y.clamp(-85.0, 85.0)) }; if let Some(c) = map.to_map(at) { cam.center = c; @@ -3842,6 +3845,12 @@ mod tests { assert!(!frame_center_placed(&merc, south) && frame_center_placed(&merc, Coord::new(0.0, -85.0))); // A longitude past ±180° (the sanitizer keeps up to ±540°) is placed: only the latitude counts. assert!(frame_center_placed(&merc, Coord::new(300.0, 40.0)) && frame_center_placed(&merc, Coord::new(-500.0, -60.0))); + // And drawn there: the same meridian a turn of the globe nearer, not the view's edge at 270°. + for (lon, lat) in [(300.0, 40.0), (-500.0, -60.0)] { + let at = merc.to_lonlat(frame_camera(&merc, Coord::new(lon, lat), 60_000_000.0, 0.0, 0.0).center).unwrap(); + let turns = (at.x - lon).rem_euclid(360.0); + assert!((turns < 1e-6 || turns > 360.0 - 1e-6) && (at.y - lat).abs() < 1e-6, "({lon}, {lat}) drawn at {at:?}"); + } let cam = frame_camera(&merc, south, 60_000_000.0, 0.0, 0.0); let at = merc.to_lonlat(cam.center).unwrap(); assert!((at.y + 85.0).abs() < 1e-6, "{at:?}");