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
61 changes: 60 additions & 1 deletion crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
Expand Down Expand Up @@ -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"]) {

Copy link
Copy Markdown
Contributor

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.import and layout.update. A frame centred on a pole stays unplaced when its map's projection changes later, and map.projection raises no notice for it. For example: a polar map with a frame at (0, -90), then map.projection set 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: have map.projection (or the layout render path) run the same unplaced_center check 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

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}));
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This notifies once per pole-centred frame. publish_notice doesn't coalesce, so a layout with several such frames gets several notices of the same kind, and none of them has a count. AGENTS.md asks for one notice per kind with a count. Suggested fix: collect the names and send one notice, for example "{layout}: {n} map frame(s) are centred where their map's projection can't show them, drawn at 85°: {first}…", and add a test with two frames that checks for a single notice.

Category: agents-md

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b21c0bb: layout.import now sends one notice for all such frames, counted, the first five named with where each is drawn. For example: "Polo: 2 map frame(s) are centred where their map's projection can't show the centre, and are drawn centred elsewhere: 'Mapa' at latitude -85, 'Norte' at latitude 85". layout.update touches one frame, so it uses the same wording with a count of 1.

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();
Expand Down
61 changes: 61 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14602,6 +14602,67 @@ 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().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: 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();
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.
#[test]
fn map_series_empty_names_use_row_numbers() {
Expand Down
60 changes: 58 additions & 2 deletions crates/map/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 Layout::clamp and reports only its repairs (crates/engine/src/project.rs:1498). The new "can't show" check runs only in layout.import and layout.update. Before this change, a centre of −88° on Web Mercator was repaired to −85° and reported on open. Now a centre of −90° on Web Mercator, which layout.update stores with a notice, reopens drawn at 85° with nothing said, and exports draw it the same way. AGENTS.md's "Never silent" rule covers a reader that approximates what it reads.

Suggested direction: run the same unplaced_center check for each layout during the project read, and feed its result to layout_notices as one counted notice. Add a test that reads a project with a pole-centred frame on Web Mercator and expects the notice, and one on EPSG:3031 that expects none. Confidence: high on the code path, medium on whether the old repair notice covered this case.

Category: bug

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes how a project is read: Layout::clamp runs on every project open (crates/engine/src/project.rs:1498), and the accepted centre latitude goes from ±85° to ±90°. AGENTS.md asks that a change to how a project is read adds or updates a fixture in schema/fixtures/. None is added here, and no existing schema/fixtures/*.rgis has a frame centre beyond ±85°, so nothing covers the new range. Add a fixture with a pole-centred frame, or say in the PR why the existing fixtures cover it. Confidence: medium, since the rule's trigger is arguable for a clamp-range change.

Category: agents-md

if let FrameShape::Polygon(p) = shape {
let points = p.len();
p.retain(|(u, v)| u.is_finite() && v.is_finite());
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Comment thread
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 {
Expand Down Expand Up @@ -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");
Expand Down
Loading