Repository navigation
feat(layout): take a publication's extent in the map's coordinate system #3339
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
Merged
jgregoriorincon
merged 8 commits into
opengeos:main
from
jgregoriorincon:feat/3322-publication-map-crs
Nov 29, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
42eee94
feat(layout): take a publication's extent in the map's coordinate sys…
jgregoriorincon 7392ff3
fix(layout): list the publication's crs in the command surface (#3322)
jgregoriorincon ac47f5f
refactor(layout): clearer names and exact refusal messages for the pu…
jgregoriorincon e939c2e
test(layout): pin the graticule step of two rectangle sizes (#3322)
jgregoriorincon 5c1fc02
fix(layout): a publication's crs rectangle asks for a pole and a cent…
jgregoriorincon 302d803
Merge remote-tracking branch 'upstream/main' into feat/3322-publicati…
jgregoriorincon e115f20
fix(layout): refuse a publication rectangle too wide to measure, and …
jgregoriorincon 036cbd0
fix(layout): refuse a publication rectangle too large to frame and ta…
jgregoriorincon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| //! A publication's area given in the map's own coordinate system (#3322). | ||
|
|
||
| use super::*; | ||
| use rustgis_data::raster::Crs; | ||
|
|
||
| /// How finely a rectangle is sampled to find the places it covers: a grid of this many cells along | ||
| /// each side. Its inside is sampled as well as its edges, since a rectangle wider than its projection | ||
| /// has no edge on the Earth. | ||
| const GRID: u32 = 32; | ||
|
|
||
| /// The longest side a rectangle may have, in ground metres: two Earth circumferences, past any | ||
| /// projection's reach. A longer one would have the frame's scale cut and the rectangle missed. | ||
| const LONGEST_SIDE_M: f64 = 4.0 * std::f64::consts::PI * rustgis_geom::EARTH_RADIUS_M; | ||
|
|
||
| /// `extent` read in `crs`, which is the active map's own system: the area's size as a | ||
| /// longitude/latitude extent (for the graticule and the north arrow, which go by degrees), and the | ||
| /// rectangle itself in the map's coordinates, which is what the frame shows. | ||
| pub(super) fn in_map_system(s: &Session, extent: Option<&Value>, crs: Crs) -> Result<(Extent, Extent), String> { | ||
| let extent = extent.filter(|v| !v.is_null()).ok_or("crs goes with extent")?; | ||
| let map = s.map(); | ||
| // Web Mercator's own system is the Earth's: another body's map has none to name. | ||
| let own = map.own_system().filter(|_| map.body().is_earth()).ok_or( | ||
| "the map has no flat system of its own here (the globe, a world projection or another body's map): give extent in degrees without crs", | ||
| )?; | ||
| if crs != own { | ||
| return Err(format!( | ||
| "extent is in {}, but the map is in {}: give it in the map's system, or in degrees without crs", | ||
| crs.label(), | ||
| own.label() | ||
| )); | ||
| } | ||
| // Two finite sides can still be too far apart for their difference to be one. | ||
| let plane = match four_sides(extent) { | ||
| Some([w, so, e, n]) if w < e && so < n && (e - w).is_finite() && (n - so).is_finite() => Extent::new(w, so, e, n), | ||
| _ => return Err("extent is [west, south, east, north] in the map's coordinates with crs".into()), | ||
| }; | ||
| let places: Vec<Coord> = crate::cmd::workspace::places_in(map, plane, GRID).collect(); | ||
|
jgregoriorincon marked this conversation as resolved.
Resolved
jgregoriorincon marked this conversation as resolved.
Resolved
|
||
| if places.is_empty() { | ||
| return Err("the extent names no place on the map".into()); | ||
| } | ||
| // The frame keeps its centre in longitude and latitude, and one it can't be drawn at is moved. | ||
| let center = map | ||
| .to_lonlat(plane.center()) | ||
| .filter(|c| rustgis_map::layout::frame_center_placed(map, *c)) | ||
| .ok_or("the extent's centre has no place on the map")?; | ||
| // Measured as the frame's fit measures it. | ||
| let per_unit = (map.flat().ground_per_unit(plane.center()) * map.body().ratio()).max(1e-9); | ||
| if plane.width().max(plane.height()) * per_unit > LONGEST_SIDE_M { | ||
| return Err("the extent is too large to frame: more than twice the Earth's circumference across".into()); | ||
| } | ||
| Ok((size_in_degrees(map, plane, center, &places), plane)) | ||
| } | ||
|
|
||
| /// The four numbers of an `extent`, when it is a list of four finite ones. | ||
| pub(super) fn four_sides(extent: &Value) -> Option<[f64; 4]> { | ||
| let sides: Vec<f64> = extent.as_array()?.iter().map(|x| x.as_f64().filter(|x| x.is_finite())).collect::<Option<_>>()?; | ||
| sides.try_into().ok() | ||
| } | ||
|
|
||
| /// An extent as large as `places`, those of `plane` centred on `center` (the part of a rectangle off | ||
| /// the Earth counts for nothing, though the frame still shows it): continuous over ±180°; and | ||
| /// around a pole, where the width in longitude says nothing, a square twice the way from the pole to | ||
| /// the farthest of them. | ||
| fn size_in_degrees(map: &rustgis_map::Map, plane: Extent, center: Coord, places: &[Coord]) -> Extent { | ||
| let flat = map.flat(); | ||
| let holds = |c: Coord| c.x >= plane.min_x && c.x <= plane.max_x && c.y >= plane.min_y && c.y <= plane.max_y; | ||
| // Only a pole the projection places: Web Mercator puts its own at 85°. | ||
| let pole = [90.0, -90.0].into_iter().find(|lat| { | ||
| let at = Coord::new(center.x, *lat); | ||
| rustgis_map::layout::frame_center_placed(map, at) && flat.forward_checked(at).is_some_and(holds) | ||
| }); | ||
| if let Some(pole) = pole { | ||
| let far = places.iter().map(|c| (c.y - pole).abs()).fold(0.0, f64::max); | ||
| return Extent::new(-far, -far, far, far); | ||
| } | ||
| let mut size = Extent::EMPTY; | ||
| for c in places { | ||
| size.expand(Coord::new(center.x + (c.x - center.x + 180.0).rem_euclid(360.0) - 180.0, c.y)); | ||
| } | ||
| size | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.