Repository navigation
fix(layout): a map frame can be centred on a pole in a polar projection (#2684) #2711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cf88179
b21c0bb
879a4e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,14 @@ 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_notice(&l.name, &unplaced_center(s, l, id).into_iter().collect::<Vec<_>>())); | ||
| 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,13 +6377,63 @@ 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). | ||
| // One notice for them all, counted. | ||
| let unplaced: Vec<(String, Option<f64>)> = 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This notifies once per pole-centred frame. Category: agents-md
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in b21c0bb: How it was checked: the engine test now imports the issue's layout with a second frame on the North Pole, on Web Mercator. It asks for exactly one such notice, with "2 map frame(s)" and both frames named. The committed code fails the updated test. With the fix: 75 engine tests passed; clippy clean on both toolchains. |
||
| } | ||
| let name = l.name.clone(); | ||
| s.project.layouts.push(l); | ||
| let i = s.project.layouts.len() - 1; | ||
| s.active_layout = Some(i); | ||
| Ok(json!({"layout": i, "name": name})) | ||
| } | ||
|
|
||
| /// 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<f64>)> { | ||
| let e = l.element(id)?; | ||
| 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))?; | ||
| 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<f64>)]) -> Option<String> { | ||
| if frames.is_empty() { | ||
| return None; | ||
| } | ||
| let at = |lat: &Option<f64>| 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::<Vec<_>>() | ||
| .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. | ||
| fn frame_layer_name(input: &impl super::InputSource, l: &Layout, frame: u32, p: &Value) -> Result<String, String> { | ||
| let context = input.input_context(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opening a project now draws a pole-centred frame at 85° without saying so. The project read calls Suggested direction: run the same Category: bug
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes how a project is read: Category: agents-md |
||
| if let FrameShape::Polygon(p) = shape { | ||
| let points = p.len(); | ||
| p.retain(|(u, v)| u.is_finite() && v.is_finite()); | ||
|
|
@@ -2748,7 +2750,13 @@ 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). | ||
| // 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; | ||
| } | ||
| let per = map.ground_per_unit(cam.center).max(1e-9); | ||
|
|
@@ -2757,6 +2765,15 @@ 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, 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 { | ||
| map.to_map(center).and_then(|m| map.to_lonlat(m)).is_some_and(|back| (back.y - center.y).abs() < 1e-4) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /// 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,45 @@ 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))); | ||
| // 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:?}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn frame_camera_follows_the_frame_on_the_globe() { | ||
| let mut map = Map::new("Map"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "can't show" notice runs only in
layout.importandlayout.update. A frame centred on a pole stays unplaced when its map's projection changes later, andmap.projectionraises no notice for it. For example: a polar map with a frame at (0, -90), thenmap.projectionset to Web Mercator. The frame is drawn at 85° S from then on, and nothing says so. The centre itself stays -90, so the stored data is kept, but the drawing changes silently, and this rule is the one the PR's notice is meant to meet. Suggested direction: havemap.projection(or the layout render path) run the sameunplaced_centercheck for the layouts that use the map, and send one counted notice, with a test that switches a polar map to Mercator and expects it. Confidence: medium. I did not trace every path that changes a map's projection.Category: agents-md