From e260e4b36eda84e506368f73e6fc0865e51e145a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 21:22:34 -0500 Subject: [PATCH 1/7] feat(layout): type the wizard's coordinates in the map's system --- crates/map/src/projection.rs | 6 + crates/ui-egui/src/coordreadout.rs | 24 +- crates/ui-egui/src/publicationwizard/map.rs | 565 ++++++++++++++++-- crates/ui-egui/src/publicationwizard/mod.rs | 13 +- .../ui-egui/src/publicationwizard/review.rs | 2 +- docs/user-guide/layouts.md | 16 +- 6 files changed, 542 insertions(+), 84 deletions(-) diff --git a/crates/map/src/projection.rs b/crates/map/src/projection.rs index ee5977a47a..ed0382b7e4 100644 --- a/crates/map/src/projection.rs +++ b/crates/map/src/projection.rs @@ -734,6 +734,12 @@ impl Projector { self.engine.to_lonlat(m).filter(|c| c.y.abs() <= 90.0 + 1e-9) } + /// Which way its map coordinates point + /// ([`rustgis_data::raster::Transformer::axis_directions`]): east, then north, for most. + pub fn axis_directions(&self) -> [(f64, f64); 2] { + self.engine.axis_directions() + } + pub fn to_meter(&self) -> f64 { self.to_meter } diff --git a/crates/ui-egui/src/coordreadout.rs b/crates/ui-egui/src/coordreadout.rs index b5ed92b0b1..a056c87ea6 100644 --- a/crates/ui-egui/src/coordreadout.rs +++ b/crates/ui-egui/src/coordreadout.rs @@ -162,6 +162,20 @@ fn layer_conversion(crs: rustgis_engine::data::raster::Crs) -> Option &'static str { + if (to_meter - 1.0).abs() < 1e-9 { + "m" + } else if (to_meter - 0.3048).abs() < 1e-9 { + "ft" + } else if (to_meter - 1200.0 / 3937.0).abs() < 1e-9 { + "ftUS" + } else { + "" + } +} + /// How a system gives a place's coordinates from its longitude and latitude. enum Plane { /// A map's flat projection, as the map draws it (Web Mercator's too, for a layer in it). @@ -257,15 +271,7 @@ impl MapSystem { Plane::Flat(Flat::Custom(p)) => p.to_meter(), Plane::Data(_, to_meter) => *to_meter, }; - let unit = if (to_meter - 1.0).abs() < 1e-9 { - "m" - } else if (to_meter - 0.3048).abs() < 1e-9 { - "ft" - } else if (to_meter - 1200.0 / 3937.0).abs() < 1e-9 { - "ftUS" - } else { - "" - }; + let unit = unit_of(to_meter); // Rounded first, then + 0.0: a coordinate that rounds to zero reads 0, not -0. let scale = 10f64.powi(decimals as i32); let shown = |v: f64| (v * scale).round() / scale + 0.0; diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs index e0710155fe..b16a90b90e 100644 --- a/crates/ui-egui/src/publicationwizard/map.rs +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -2,7 +2,9 @@ use super::*; use crate::dialogs::MapOwner; use rustgis_engine::cmd::workspace::view_extent; -use rustgis_engine::map::Layer; +use rustgis_engine::geom::Coord; +use rustgis_engine::map::projection::Flat; +use rustgis_engine::map::{Layer, Projection}; /// Which area the map frame shows. #[derive(Clone, Debug, PartialEq, Eq)] @@ -15,11 +17,27 @@ pub enum Area { Layer { map: MapOwner, layer: u64 }, /// The selected features of the map it was picked in. Selection { map: MapOwner }, - /// The coordinates typed ([`Draft::coords`]), whichever flat map is active; a map on the globe - /// takes none. + /// The coordinates typed ([`Draft::coords`]), whichever flat map is active: the map step + /// converts them to its system. A map on the globe takes none. Coordinates, } +/// An area as `layout.publication` takes it. +#[derive(Clone, Debug, PartialEq)] +pub struct Coords { + /// West, south, east and north. + pub sides: [f64; 4], + /// The code of the map's system the sides are in; none for longitude and latitude. + pub crs: Option, +} + +impl From<[f64; 4]> for Coords { + /// Longitude and latitude. + fn from(sides: [f64; 4]) -> Self { + Coords { sides, crs: None } + } +} + /// The name `layout.publication` gives when none is: data, not translated. const DEFAULT_NAME: &str = "Publication"; @@ -27,18 +45,45 @@ const DEFAULT_NAME: &str = "Publication"; /// ±90 edge half of it is cut off again. const MIN_SIDE: f64 = 0.01; +/// The cells a side of the grid that takes coordinates from one system to another: a rectangle's +/// sides, and what lies inside it, may reach past its corners there. +const GRID: u32 = 8; + +/// The system a map's coordinates are typed in: its code and the metres in one of its units; none +/// where they are longitude and latitude. That is a map of the Earth in a system of its own with no +/// world edge ([`Flat::direct_system`]) whose axes point east, then north, as the pointer readout +/// asks of an easting and northing. On Web Mercator and the world projections the box in degrees is +/// the area framed, or no one has an area in their units. +fn typed_in(map: &rustgis_engine::map::Map) -> Option<(String, f64)> { + if !map.body().is_earth() { + return None; + } + let flat = map.flat(); + let crs = flat.direct_system()?; + let Flat::Custom(p) = flat else { return None }; + (p.axis_directions() == [(1.0, 0.0), (0.0, 1.0)]).then(|| (crs.label(), p.to_meter())) +} + +/// `sides` rounded outward to what a field starts on: five decimals of a degree, whole units of a +/// map's system. +fn outward([w, s, e, n]: [f64; 4], projected: bool) -> [f64; 4] { + let per = if projected { 1.0 } else { 1e5 }; + let down = |v: f64| (v * per).floor() / per; + let up = |v: f64| (v * per).ceil() / per; + [down(w), down(s), up(e), up(n)] +} + /// What a pick names in the session now. enum Held<'a> { View, Layer(&'a Layer), Selection, - /// West, south, east and north. - Coordinates([f64; 4]), + Coordinates(Coords), } /// What `area` names now, or why it no longer holds: the one check the step, the review and Create /// share, so a stale pick reads the same in the three. It reads no places, as it runs every frame. -fn held<'a>(s: &'a Session, area: &Area, coords: Option<[f64; 4]>) -> Result, &'static str> { +fn held<'a>(s: &'a Session, area: &Area, coords: Option<&Coords>) -> Result, &'static str> { let map = s.map(); match area { Area::View => Ok(Held::View), @@ -54,9 +99,16 @@ fn held<'a>(s: &'a Session, area: &Area, coords: Option<[f64; 4]>) -> Result Err(tl!("Nothing is selected: pick the map extent again.")), Area::Selection { .. } => Ok(Held::Selection), Area::Coordinates if map.projection.is_globe() => Err(on_globe()), - // What `layout.publication` asks of an `extent`; the fields keep it to the Earth. + Area::Coordinates if coords.is_some_and(|c| c.crs != typed_in(map).map(|s| s.0)) => { + Err(tl!("The map's coordinate system changed since the coordinates were typed: go back to the Map step")) + } + // What `layout.publication` asks of an `extent`. Area::Coordinates => coords - .filter(|[w, s, e, n]| w < e && s < n) + .filter(|c| { + let [w, s, e, n] = c.sides; + w < e && s < n + }) + .cloned() .map(Held::Coordinates) .ok_or_else(|| tl!("West must be less than East, and South less than North.")), } @@ -66,34 +118,102 @@ fn on_globe() -> &'static str { tl!("A map on the globe takes no coordinates: pick another map extent.") } -/// What the coordinate fields start on: the view, rounded outward to the five decimals they show; -/// the whole Earth where the view has nothing to frame. -fn from_view(s: &Session) -> [f64; 4] { - let [w, so, e, n] = framed(view_extent(s)).unwrap_or([-180.0, -90.0, 180.0, 90.0]); - let down = |v: f64| (v * 1e5).floor() / 1e5; - let up = |v: f64| (v * 1e5).ceil() / 1e5; - [down(w).max(-180.0), down(so).max(-90.0), up(e).min(180.0), up(n).min(90.0)] +/// What the coordinate fields start on: the view, rounded outward. In a map's system, its +/// rectangle there; in degrees, the box around it, or the whole Earth where it has nothing to +/// frame. +fn from_view(s: &Session) -> Coords { + if let Some((crs, _)) = typed_in(s.map()) { + let (w, h) = s.view_size; + let e = s.map().camera.extent(w, h); + return Coords { sides: outward([e.min_x, e.min_y, e.max_x, e.max_y], true), crs: Some(crs) }; + } + in_degrees(framed(view_extent(s)).unwrap_or([-180.0, -90.0, 180.0, 90.0])).into() +} + +/// `sides` in degrees as the fields start on them, kept to the Earth. +fn in_degrees(sides: [f64; 4]) -> [f64; 4] { + let [w, s, e, n] = outward(sides, false); + [w.max(-180.0), s.max(-90.0), e.min(180.0), n.min(90.0)] +} + +/// `held` in the system the coordinates of `map` are typed in now: the box around the places of a +/// grid over it. None when none of them has a place there. +fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option { + let from = match &held.crs { + Some(code) => Some(Flat::of(&Projection::from_code(code).ok()?)), + None => None, + }; + let crs = typed_in(map).map(|s| s.0); + let to = crs.is_some().then(|| map.flat()); + let [w, s, e, n] = held.sides; + let at = |min: f64, max: f64, i: u32| min + (max - min) * f64::from(i) / f64::from(GRID); + let mut got = Extent::EMPTY; + for (i, j) in (0..=GRID).flat_map(|i| (0..=GRID).map(move |j| (i, j))) { + let c = Coord::new(at(w, e, i), at(s, n, j)); + let place = match &from { + Some(f) => f.inverse(c), + None => Some(c), + }; + let there = match &to { + Some(f) => place.and_then(|c| f.forward_checked(c)), + None => place, + }; + if let Some(c) = there { + got.expand(c); + } + } + let sides = if to.is_none() { + in_degrees(framed(got)?) + } else if got.is_empty() { + return None; + } else { + outward([got.min_x, got.min_y, got.max_x, got.max_y], true) + }; + Some(Coords { sides, crs }) +} + +/// A value typed or pasted in a map's units: with the unit and axis the pointer readout writes +/// after it, and a decimal comma too, but for one with exactly three digits after it, which may +/// part the thousands. +fn in_map_units(text: &str) -> Option { + let text = text.trim(); + let text = ["ftUSE", "ftUSN", "ftUS", "ftE", "ftN", "mE", "mN", "E", "N"] + .iter() + .find_map(|unit| text.strip_suffix(unit)) + .map_or(text, str::trim_end); + if text.rsplit_once(',').is_some_and(|(_, after)| after.len() == 3 && after.bytes().all(|b| b.is_ascii_digit())) { + return None; + } + number(text) +} + +/// A finite number, with a decimal comma or point. +fn number(text: &str) -> Option { + text.replace(',', ".").replace('\u{2212}', "-").parse::().ok().filter(|v| v.is_finite()) } /// The four fields of the coordinates, written to the draft as they change: no command runs, so a /// value holds even when Next is pressed while it is being typed. -fn coordinate_fields(ui: &mut egui::Ui, coords: &mut [f64; 4]) { +fn coordinate_fields(ui: &mut egui::Ui, coords: &mut Coords, res: f64) { + let projected = coords.crs.is_some(); egui::Grid::new("publication_wizard_coordinates").num_columns(2).spacing(egui::vec2(8.0, 4.0)).show(ui, |ui| { for (text, at, most) in [(tl!("West"), 0, 180.0), (tl!("East"), 2, 180.0), (tl!("South"), 1, 90.0), (tl!("North"), 3, 90.0)] { let label = ui.label(text); - if let Some(v) = coords.get_mut(at) { - // A decimal comma too, which egui's own parser reads as no number, and a degree sign - // pasted with the value. Finite numbers only: the range would take NaN as a limit. - let field = egui::DragValue::new(v).range(-most..=most).speed(0.01).max_decimals(5).custom_parser(|t| { - t.trim() - .trim_end_matches('°') - .trim_end() - .replace(',', ".") - .replace('\u{2212}', "-") - .parse::() - .ok() - .filter(|v| v.is_finite()) - }); + if let Some(v) = coords.sides.get_mut(at) { + // A decimal comma too, which egui's own parser reads as no number. Finite numbers + // only: the range would take NaN as a limit. + let field = if projected { + // A drag moves by the view's pixels. Read once typed, not key by key: "512,34" + // on the way to the ambiguous "512,345" would otherwise stay. + egui::DragValue::new(v).speed(res).max_decimals(3).update_while_editing(false).custom_parser(in_map_units) + } else { + // A degree sign pasted with the value too. + egui::DragValue::new(v) + .range(-most..=most) + .speed(0.01) + .max_decimals(5) + .custom_parser(|t| number(t.trim().trim_end_matches('°').trim_end())) + }; ui.add(field).labelled_by(label.id); } ui.end_row(); @@ -117,7 +237,7 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { ui.end_row(); ui.label(""); // A pick that no longer holds marks nothing. - let marked = held(s, &d.area, d.coords); + let marked = held(s, &d.area, d.coords.as_ref()); ui.horizontal(|ui| { let picked = match marked { Ok(Held::Layer(l)) => Some(l), @@ -162,7 +282,10 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { let coordinates = egui::RadioButton::new(d.area == Area::Coordinates, tl!("Coordinates")); if ui .add_enabled(!map.projection.is_globe(), coordinates) - .on_hover_text(tl!("An area typed as longitude and latitude (WGS 84), in decimal degrees.")) + .on_hover_text(match typed_in(map) { + Some(_) => tl!("An area typed as easting and northing in the map's coordinate system."), + None => tl!("An area typed as longitude and latitude (WGS 84), in decimal degrees."), + }) .on_disabled_hover_text(on_globe()) .clicked() { @@ -172,9 +295,23 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { if d.area == Area::Coordinates { ui.label(""); ui.vertical(|ui| { - coordinate_fields(ui, d.coords.get_or_insert_with(|| from_view(s))); + let coords = d.coords.get_or_insert_with(|| from_view(s)); + let system = typed_in(map); + // Typed in another system: converted as the step shows. The globe takes none. + if !map.projection.is_globe() && coords.crs.as_ref() != system.as_ref().map(|s| &s.0) { + *coords = converted(map, coords).unwrap_or_else(|| from_view(s)); + } + coordinate_fields(ui, coords, map.camera.res); + if let Some((crs, to_meter)) = &system { + let line = match crate::coordreadout::unit_of(*to_meter) { + "" => i18n::fmt("Easting and northing in {crs}", &[("crs", crs)]), + unit => i18n::fmt("Easting and northing in {crs} ({unit})", &[("crs", crs), ("unit", unit)]), + }; + ui.set_max_width(field_width(ui)); + ui.add(egui::Label::new(line).wrap()); + } // Read after the fields: this frame's pick and values, not the ones it began with. - if let Err(why) = held(s, &d.area, d.coords) { + if let Err(why) = held(s, &d.area, d.coords.as_ref()) { ui.set_max_width(field_width(ui)); ui.add(egui::Label::new(egui::RichText::new(why).color(ui.visuals().warn_fg_color)).wrap()); } @@ -199,17 +336,21 @@ pub(super) fn name_label(s: &Session, name: &str) -> String { } /// How the review names the area. -pub(super) fn area_label(s: &Session, area: &Area, coords: Option<[f64; 4]>) -> String { +pub(super) fn area_label(s: &Session, area: &Area, coords: Option<&Coords>) -> String { match held(s, area, coords) { Ok(Held::View) => tl!("The view").to_string(), Ok(Held::Layer(l)) => i18n::fmt("Layer: {layer}", &[("layer", &l.name)]), Ok(Held::Selection) => tl!("Selected features").to_string(), - Ok(Held::Coordinates([w, so, e, n])) => { - let [w, so, e, n] = [w, so, e, n].map(|v| v.to_string()); - i18n::fmt( - "West {west}, South {south}, East {east}, North {north}", - &[("west", &w), ("south", &so), ("east", &e), ("north", &n)], - ) + Ok(Held::Coordinates(c)) => { + let [w, so, e, n] = c.sides.map(|v| v.to_string()); + let sides = [("west", w.as_str()), ("south", &so), ("east", &e), ("north", &n)]; + match &c.crs { + None => i18n::fmt("West {west}, South {south}, East {east}, North {north}", &sides), + Some(crs) => { + let [w, so, e, n] = sides; + i18n::fmt("West {west}, South {south}, East {east}, North {north} ({crs})", &[w, so, e, n, ("crs", crs)]) + } + } } Err(_) => tl!("(none)").to_string(), } @@ -233,7 +374,7 @@ fn framed(e: Extent) -> Option<[f64; 4]> { /// The `extent` to send for `area`, none for the view; a pick that no longer holds is an error /// rather than another area framed. Read once, at Create, on the UI thread where /// `layout.publication` runs next: it may walk the whole layer, or the whole selection. -pub(super) fn area_extent(s: &Session, area: &Area, coords: Option<[f64; 4]>) -> Result, String> { +pub(super) fn area_extent(s: &Session, area: &Area, coords: Option<&Coords>) -> Result, String> { let extent = match held(s, area, coords)? { Held::View => return Ok(None), // As typed: checked by `held`, so neither widened nor cut. @@ -241,7 +382,9 @@ pub(super) fn area_extent(s: &Session, area: &Area, coords: Option<[f64; 4]>) -> Held::Layer(l) => l.shown_extent(), Held::Selection => s.map().selection_extent(), }; - framed(extent).map(Some).ok_or_else(|| tl!("There is nothing to frame there: pick another map extent.").to_string()) + framed(extent) + .map(|sides| Some(sides.into())) + .ok_or_else(|| tl!("There is nothing to frame there: pick another map extent.").to_string()) } #[cfg(test)] @@ -252,6 +395,8 @@ mod tests { use egui_kittest::kittest::{NodeT, Queryable}; use rustgis_engine::data::{Dataset, GeometryType}; use rustgis_engine::geom::{Coord, lonlat_to_merc}; + use rustgis_engine::map::Projection; + use rustgis_engine::map::projection::Flat; fn close(got: Option<[f64; 4]>, want: [f64; 4]) { let got = got.expect("an extent"); @@ -284,12 +429,12 @@ mod tests { (l.instance_id(), l.shown_extent(), l.extent()) }; assert_ne!(shown, all, "the query narrows it"); - assert_eq!(area_extent(&app.session, &Area::Layer { map: map.clone(), layer: id }, None), Ok(framed(shown))); + assert_eq!(area_extent(&app.session, &Area::Layer { map: map.clone(), layer: id }, None), Ok(framed(shown).map(Coords::from))); app.run("select.byAttributes", json!({"layer": "Cities", "where": "name = 'Tokyo'"})).unwrap(); let selected = app.session.map().selection_extent(); let got = area_extent(&app.session, &Area::Selection { map }, None).unwrap(); - assert_eq!(got, framed(selected)); - assert!(got.is_some_and(|[w, s, e, n]| w < e && s < n), "one point, framed: {got:?}"); + assert_eq!(got, framed(selected).map(Coords::from)); + assert!(got.as_ref().is_some_and(|c| c.sides[0] < c.sides[2] && c.sides[1] < c.sides[3]), "one point, framed: {got:?}"); } /// The step starts on the view; Layer picks one from the list; Selection is off with nothing @@ -524,8 +669,12 @@ mod tests { h.state().dialogs.publication.as_ref().expect("the wizard is open") } - fn sent(h: &Wizard) -> Result, String> { - area_extent(&h.state().session, &draft(h).area, draft(h).coords) + fn sides(h: &Wizard) -> Option<[f64; 4]> { + draft(h).coords.as_ref().map(|c| c.sides) + } + + fn sent(h: &Wizard) -> Result, String> { + area_extent(&h.state().session, &draft(h).area, draft(h).coords.as_ref()) } fn type_into(h: &mut Wizard, field: &str, text: &str) { @@ -544,22 +693,22 @@ mod tests { let view = framed(view_extent(&h.state().session)).unwrap(); pick(&mut h, "Coordinates"); assert_eq!(draft(&h).area, Area::Coordinates); - let held = draft(&h).coords.expect("filled from the view"); + let held = sides(&h).expect("filled from the view"); assert!(held[0] <= view[0] && held[1] <= view[1] && held[2] >= view[2] && held[3] >= view[3], "{held:?} {view:?}"); assert!(held.iter().zip(view).all(|(a, b)| (a - b).abs() <= 1e-5), "{held:?} {view:?}"); assert!(held.iter().all(|v| ((v * 1e5).round() - v * 1e5).abs() < 1e-6), "five decimals: {held:?}"); for field in ["West", "East", "South", "North"] { h.get_by_role_and_label(Role::SpinButton, field); } - assert_eq!(sent(&h), Ok(Some(held))); + assert_eq!(sent(&h), Ok(Some(held.into()))); h.state_mut().session.map_mut().camera.center.x += 1_000_000.0; h.run_steps(4); - assert_eq!(draft(&h).coords, Some(held), "the view moved"); + assert_eq!(sides(&h), Some(held), "the view moved"); pick(&mut h, "View"); assert!(h.query_by_role(Role::SpinButton).is_none()); assert_eq!(sent(&h), Ok(None)); pick(&mut h, "Coordinates"); - assert_eq!(draft(&h).coords, Some(held)); + assert_eq!(sides(&h), Some(held)); } /// A view over ±180° is cut there, as `layout.publication` cuts the view. @@ -574,7 +723,7 @@ mod tests { ); let view = framed(view_extent(&h.state().session)).unwrap(); pick(&mut h, "Coordinates"); - let [w, _, e, _] = draft(&h).coords.unwrap(); + let [w, _, e, _] = sides(&h).unwrap(); assert_eq!(e, 180.0); assert!(w > 0.0 && (w - view[0]).abs() <= 1e-5, "{w} {view:?}"); } @@ -589,7 +738,7 @@ mod tests { pick(&mut h, "Coordinates"); type_into(&mut h, field, text); click(&mut h, "Next"); - assert_eq!(draft(&h).coords.unwrap()[at], want, "{field}: {text}"); + assert_eq!(sides(&h).unwrap()[at], want, "{field}: {text}"); } } @@ -599,7 +748,7 @@ mod tests { for text in ["NaN", "inf", "-infinity"] { let mut h = step_harness(); pick(&mut h, "Coordinates"); - let before = draft(&h).coords; + let before = draft(&h).coords.clone(); type_into(&mut h, "West", text); click(&mut h, "Next"); assert_eq!(draft(&h).coords, before, "{text}"); @@ -611,7 +760,7 @@ mod tests { fn a_short_drag_moves_a_field_by_hundredths() { let mut h = step_harness(); pick(&mut h, "Coordinates"); - let before = draft(&h).coords.unwrap()[0]; + let before = sides(&h).unwrap()[0]; let at = h.get_by_role_and_label(Role::SpinButton, "West").rect().center(); let to = at + egui::vec2(10.0, 0.0); h.drag_at(at); @@ -620,7 +769,7 @@ mod tests { h.run_steps(2); h.drop_at(to); h.run_steps(4); - let moved = (draft(&h).coords.unwrap()[0] - before).abs(); + let moved = (sides(&h).unwrap()[0] - before).abs(); assert!(moved > 0.0 && moved <= 0.1 + 1e-9, "moved {moved}°"); } @@ -632,7 +781,7 @@ mod tests { let mut h = map_wizard( |a| { let d = a.dialogs.publication.as_mut().unwrap(); - (d.area, d.coords) = (Area::Coordinates, Some(bad)); + (d.area, d.coords) = (Area::Coordinates, Some(bad.into())); }, egui::vec2(1400.0, 1000.0), ); @@ -640,7 +789,7 @@ mod tests { assert_eq!(h.query_all_by_role(Role::SpinButton).count(), 4, "{bad:?}"); let marked = h.get_by_role_and_label(Role::RadioButton, "Coordinates").accesskit_node().toggled(); assert_eq!(marked, Some(egui::accesskit::Toggled::True), "{bad:?}"); - assert_eq!(area_label(&h.state().session, &draft(&h).area, draft(&h).coords), "(none)"); + assert_eq!(area_label(&h.state().session, &draft(&h).area, draft(&h).coords.as_ref()), "(none)"); assert_eq!(sent(&h), Err(ORDER.to_string())); h.state_mut().dialogs.publication.as_mut().unwrap().step = Step::Review; h.run_steps(4); @@ -672,7 +821,7 @@ mod tests { #[test] fn coordinates_hold_whatever_changes_in_the_session() { let mut app = app(); - let coords = Some([-10.5, -20.0, 30.0, 40.25]); + let coords = Some(Coords::from([-10.5, -20.0, 30.0, 40.25])); for (command, params) in [ ("map.rename", json!({"name": "Renamed"})), ("select.byAttributes", json!({"layer": "Cities", "where": "name = 'Tokyo'"})), @@ -681,9 +830,9 @@ mod tests { ("map.new", json!({})), ] { app.run(command, params).unwrap(); - assert_eq!(area_extent(&app.session, &Area::Coordinates, coords), Ok(coords), "{command}"); + assert_eq!(area_extent(&app.session, &Area::Coordinates, coords.as_ref()), Ok(coords.clone()), "{command}"); } - assert_eq!(area_label(&app.session, &Area::Coordinates, coords), "West -10.5, South -20, East 30, North 40.25"); + assert_eq!(area_label(&app.session, &Area::Coordinates, coords.as_ref()), "West -10.5, South -20, East 30, North 40.25"); } /// On the globe the choice is off; one picked before no longer holds, and is said. @@ -696,13 +845,13 @@ mod tests { |a| { globe(a); let d = a.dialogs.publication.as_mut().unwrap(); - (d.area, d.coords) = (Area::Coordinates, Some([0.0, 0.0, 1.0, 1.0])); + (d.area, d.coords) = (Area::Coordinates, Some([0.0, 0.0, 1.0, 1.0].into())); }, egui::vec2(1400.0, 1000.0), ); assert!(h.query_by_label(ON_GLOBE).is_some()); assert_eq!(sent(&h), Err(ON_GLOBE.to_string())); - assert_eq!(area_label(&h.state().session, &draft(&h).area, draft(&h).coords), "(none)"); + assert_eq!(area_label(&h.state().session, &draft(&h).area, draft(&h).coords.as_ref()), "(none)"); } /// Create frames the typed area, for a sheet and a figure, the whole world on Mercator too. @@ -715,7 +864,7 @@ mod tests { open(&mut app); let d = app.dialogs.publication.as_mut().unwrap(); d.size = size.into(); - (d.area, d.coords, d.step) = (Area::Coordinates, Some(coords), Step::Review); + (d.area, d.coords, d.step) = (Area::Coordinates, Some(coords.into()), Step::Review); let before = app.session.project.layouts.len(); let mut h = harness(app); click(&mut h, "Create"); @@ -752,4 +901,292 @@ mod tests { let d = draft(&h); assert!(d.area == Area::View && d.coords.is_none()); } + + const UTM: &str = "EPSG:32633"; + const UTM_LINE: &str = "Easting and northing in EPSG:32633 (m)"; + const SYSTEM_CHANGED: &str = "The map's coordinate system changed since the coordinates were typed: go back to the Map step"; + const AREA: [f64; 4] = [500_000.0, 5_000_000.0, 600_000.0, 5_100_000.0]; + + /// The map in `code`, its view 200 m a pixel around 550 km east, 5 050 km north. + fn in_system(a: &mut RustGisApp, code: &str) { + a.run("map.projection", json!({"type": code})).unwrap(); + let camera = &mut a.session.map_mut().camera; + camera.center = Coord::new(550_000.25, 5_050_000.75); + camera.res = 200.0; + } + + fn utm_harness() -> Wizard { + map_wizard(|a| in_system(a, UTM), egui::vec2(1400.0, 1000.0)) + } + + fn typed(sides: [f64; 4], crs: &str) -> Coords { + Coords { sides, crs: Some(crs.to_string()) } + } + + /// The wizard on the map step in the map's `code`, holding `coords`. + fn holding(code: &str, coords: Coords) -> Wizard { + map_wizard( + |a| { + in_system(a, code); + let d = a.dialogs.publication.as_mut().unwrap(); + (d.area, d.coords) = (Area::Coordinates, Some(coords)); + }, + egui::vec2(1400.0, 1000.0), + ) + } + + fn change(h: &mut Wizard, code: &str) { + h.state_mut().run("map.projection", json!({"type": code})).unwrap(); + h.run_steps(4); + } + + /// `got` holds the longitude/latitude places of the rectangle `old`, given in `from`, in the + /// system `to`. + fn holds_places(got: [f64; 4], old: [f64; 4], from: &str, to: &str) { + let flat = |code: &str| Flat::of(&Projection::from_code(code).unwrap()); + let (from, to) = (flat(from), flat(to)); + let [w, s, e, n] = old; + for c in [Coord::new(w, s), Coord::new(w, n), Coord::new(e, s), Coord::new(e, n)] { + let at = to.forward(from.inverse(c).unwrap()).unwrap(); + assert!(got[0] <= at.x && at.x <= got[2] && got[1] <= at.y && at.y <= got[3], "{at:?} outside {got:?}"); + } + } + + /// Web Mercator, a world preset, its ESRI code and a local view keep longitude and latitude. + #[test] + fn a_map_with_no_projected_system_of_its_own_keeps_degrees() { + for code in ["mercator", "robinson", "ESRI:54030", "local:7.4386,46.9511"] { + let mut h = map_wizard(|a| drop(a.run("map.projection", json!({"type": code})).unwrap()), egui::vec2(1400.0, 1000.0)); + pick(&mut h, "Coordinates"); + let c = draft(&h).coords.clone().unwrap(); + assert_eq!(c.crs, None, "{code}"); + assert!(c.sides.iter().all(|v| v.abs() <= 180.0), "{code}: {c:?}"); + assert!(h.query_by_label_contains("Easting and northing").is_none(), "{code}"); + } + } + + /// A system whose axes don't point east, then north, keeps degrees, as the pointer readout + /// refuses it an easting and northing. + #[test] + fn a_system_with_other_axes_keeps_degrees() { + for code in ["EPSG:5513", "EPSG:22285"] { + let mut h = map_wizard(|a| drop(a.run("map.projection", json!({"type": code})).unwrap()), egui::vec2(1400.0, 1000.0)); + pick(&mut h, "Coordinates"); + assert_eq!(draft(&h).coords.as_ref().unwrap().crs, None, "{code}"); + } + } + + /// On a map in a projected system the fields start on the view's rectangle there, rounded + /// outward to whole units, and the step names the system and its unit. + #[test] + fn a_projected_map_starts_on_the_view_in_its_units() { + let mut h = utm_harness(); + pick(&mut h, "Coordinates"); + let c = draft(&h).coords.clone().unwrap(); + assert_eq!(c.crs.as_deref(), Some(UTM)); + let (w, height) = h.state().session.view_size; + let view = h.state().session.map().camera.extent(w, height); + let want = [view.min_x.floor(), view.min_y.floor(), view.max_x.ceil(), view.max_y.ceil()]; + assert_eq!(c.sides, want); + assert!(h.query_by_label(UTM_LINE).is_some()); + let feet = holding("EPSG:2263", typed([900_000.0, 100_000.0, 1_000_000.0, 200_000.0], "EPSG:2263")); + assert!(feet.query_by_label("Easting and northing in EPSG:2263 (ftUS)").is_some()); + } + + /// Typed in the map's units: a decimal comma or point, no cut at ±180, and the unit and axis + /// the pointer readout writes pasted with it. + #[test] + fn a_value_in_map_units_is_read_whole() { + for (text, want) in [ + ("512345,5", 512_345.5), + ("512345.5", 512_345.5), + ("-512345", -512_345.0), + ("2600000 mE", 2_600_000.0), + ("1200000 mN", 1_200_000.0), + ("512345 ftUS", 512_345.0), + ("512345 ftUSE", 512_345.0), + ("512345 ftUSN", 512_345.0), + ("512345 ftE", 512_345.0), + ("512345 ftN", 512_345.0), + ("512345 E", 512_345.0), + ("512345 N", 512_345.0), + ] { + let mut h = utm_harness(); + pick(&mut h, "Coordinates"); + type_into(&mut h, "West", text); + click(&mut h, "Next"); + assert_eq!(sides(&h).unwrap()[0], want, "{text}"); + } + } + + /// A comma with exactly three digits after it may part the thousands, and what is no finite + /// number names no place: both leave the field as it was, typed key by key too. + #[test] + fn an_ambiguous_or_unreadable_value_leaves_a_field_in_map_units() { + for text in ["512,345", "1,234,567", "512,345 mE", "NaN", "inf"] { + for key_by_key in [false, true] { + let mut h = utm_harness(); + pick(&mut h, "Coordinates"); + let before = sides(&h); + h.get_by_role_and_label(Role::SpinButton, "West").click(); + h.run_steps(4); + let parts: Vec = if key_by_key { text.chars().map(String::from).collect() } else { vec![text.to_string()] }; + for part in parts { + h.event(egui::Event::Text(part)); + h.run_steps(2); + } + click(&mut h, "Next"); + assert_eq!(sides(&h), before, "{text}, key by key: {key_by_key}"); + } + } + // In degrees the comma is the decimal one, whatever follows it. + let mut h = step_harness(); + pick(&mut h, "Coordinates"); + type_into(&mut h, "West", "40,125"); + click(&mut h, "Next"); + assert_eq!(sides(&h).unwrap()[0], 40.125); + } + + /// Create sends the rectangle in the map's system, so the frame is centred inside it; the + /// review names the system. + #[test] + fn create_frames_the_rectangle_in_the_maps_system() { + let mut h = holding(UTM, typed(AREA, UTM)); + assert_eq!(sent(&h), Ok(Some(typed(AREA, UTM)))); + h.state_mut().dialogs.publication.as_mut().unwrap().step = Step::Review; + h.run_steps(4); + assert!(h.query_by_label("West 500000, South 5000000, East 600000, North 5100000 (EPSG:32633)").is_some()); + let before = h.state().session.project.layouts.len(); + click(&mut h, "Create"); + let layouts = &h.state().session.project.layouts; + assert_eq!(layouts.len(), before + 1); + let center = layouts.last().unwrap().elements.iter().find_map(|el| match &el.kind { + rustgis_engine::map::layout::ElementKind::MapFrame { center, .. } if el.name == "Map Frame" => Some(*center), + _ => None, + }); + let at = h.state().session.map().flat().forward(center.expect("the map frame")).unwrap(); + assert!((at.x - 550_000.0).abs() < 1_000.0 && (at.y - 5_050_000.0).abs() < 1_000.0, "{at:?}"); + } + + /// Sides out of order are said in map units as they are in degrees. + #[test] + fn sides_out_of_order_in_map_units_are_said() { + let h = holding(UTM, typed([600_000.0, 5_000_000.0, 500_000.0, 5_100_000.0], UTM)); + assert!(h.query_by_label(ORDER).is_some()); + assert_eq!(sent(&h), Err(ORDER.to_string())); + } + + /// The step converts the coordinates held to the map's system as it is now: to another zone, + /// to degrees and back. + #[test] + fn the_step_converts_the_coordinates_to_the_maps_new_system() { + let mut h = holding(UTM, typed(AREA, UTM)); + change(&mut h, "EPSG:32634"); + let zone = draft(&h).coords.clone().unwrap(); + assert_eq!(zone.crs.as_deref(), Some("EPSG:32634")); + assert!(zone.sides.iter().all(|v| v.fract() == 0.0), "{zone:?}"); + holds_places(zone.sides, AREA, UTM, "EPSG:32634"); + change(&mut h, "mercator"); + let degrees = draft(&h).coords.clone().unwrap(); + assert_eq!(degrees.crs, None); + let [w, s, e, n] = degrees.sides; + assert!(w > 10.0 && e < 22.0 && s > 44.0 && n < 47.0 && w < e && s < n, "{degrees:?}"); + change(&mut h, UTM); + let back = draft(&h).coords.clone().unwrap(); + assert_eq!(back.crs.as_deref(), Some(UTM)); + holds_places(back.sides, AREA, UTM, UTM); + } + + /// Where nothing of the area held has a place in the new system, the fields start on the view. + #[test] + fn an_area_with_no_place_in_the_new_system_starts_again_on_the_view() { + let mut h = map_wizard( + |a| { + let d = a.dialogs.publication.as_mut().unwrap(); + (d.area, d.coords) = (Area::Coordinates, Some([170.0, -45.0, 175.0, -40.0].into())); + }, + egui::vec2(1400.0, 1000.0), + ); + change(&mut h, "EPSG:2056"); + let got = draft(&h).coords.clone().unwrap(); + assert_eq!(got.crs.as_deref(), Some("EPSG:2056")); + let (w, height) = h.state().session.view_size; + let view = h.state().session.map().camera.extent(w, height); + assert_eq!(got.sides, [view.min_x.floor(), view.min_y.floor(), view.max_x.ceil(), view.max_y.ceil()]); + } + + /// Changed while on the review, the coordinates read "(none)", and Create says to go back to + /// the Map step and makes nothing. + #[test] + fn a_system_changed_on_the_review_makes_nothing() { + let mut h = holding(UTM, typed(AREA, UTM)); + h.state_mut().dialogs.publication.as_mut().unwrap().step = Step::Review; + h.run_steps(4); + change(&mut h, "EPSG:32634"); + assert_eq!(draft(&h).coords, Some(typed(AREA, UTM)), "converted only as the step shows"); + assert_eq!(area_label(&h.state().session, &draft(&h).area, draft(&h).coords.as_ref()), "(none)"); + assert_eq!(sent(&h), Err(SYSTEM_CHANGED.to_string())); + let before = h.state().session.project.layouts.len(); + click(&mut h, "Create"); + assert_eq!(h.state().session.project.layouts.len(), before); + assert_eq!(h.state().dialogs.open, Some("publicationWizard")); + } + + /// Another map in the same system, and a rename, leave the coordinates as they are. + #[test] + fn coordinates_hold_across_maps_of_the_same_system() { + let mut h = holding(UTM, typed(AREA, UTM)); + h.state_mut().run("map.rename", json!({"name": "Renamed"})).unwrap(); + h.run_steps(2); + h.state_mut().run("map.new", json!({})).unwrap(); + change(&mut h, UTM); + assert_eq!(draft(&h).coords, Some(typed(AREA, UTM))); + assert_eq!(sent(&h), Ok(Some(typed(AREA, UTM)))); + } + + /// Undoing the change of system converts them back by the same rule. + #[test] + fn undoing_a_change_of_system_converts_them_again() { + let mut h = holding(UTM, typed(AREA, UTM)); + change(&mut h, "EPSG:32634"); + h.state_mut().run("edit.undo", Value::Null).unwrap(); + h.run_steps(4); + let back = draft(&h).coords.clone().unwrap(); + assert_eq!(back.crs.as_deref(), Some(UTM)); + holds_places(back.sides, AREA, UTM, UTM); + } + + /// A short drag moves a field in map units by about the view's pixels. + #[test] + fn a_short_drag_moves_a_field_in_map_units_by_pixels() { + let mut h = holding(UTM, typed(AREA, UTM)); + let at = h.get_by_role_and_label(Role::SpinButton, "West").rect().center(); + let to = at + egui::vec2(10.0, 0.0); + h.drag_at(at); + h.run_steps(2); + h.hover_at(to); + h.run_steps(2); + h.drop_at(to); + h.run_steps(4); + let moved = sides(&h).unwrap()[0] - AREA[0]; + assert!(moved >= 200.0 && moved <= 10.0 * 200.0 + 1.0, "moved {moved} m"); + } + + /// Fields holding seven digits stay inside the window at its smallest. + #[test] + fn the_fields_in_map_units_stay_in_the_window() { + let h = map_wizard( + |a| { + in_system(a, UTM); + let d = a.dialogs.publication.as_mut().unwrap(); + (d.area, d.coords) = (Area::Coordinates, Some(typed([-9_999_999.125, 1_000_000.0, 9_999_999.125, 9_999_999.0], UTM))); + }, + egui::vec2(420.0, 600.0), + ); + for field in ["West", "East", "South", "North"] { + let right = h.get_by_role_and_label(Role::SpinButton, field).rect().right(); + assert!(right <= 420.0, "{field} runs to {right}"); + } + assert!(h.get_by_label(UTM_LINE).rect().right() <= 420.0); + } } diff --git a/crates/ui-egui/src/publicationwizard/mod.rs b/crates/ui-egui/src/publicationwizard/mod.rs index 58f9b85bff..5284d7c6d7 100644 --- a/crates/ui-egui/src/publicationwizard/mod.rs +++ b/crates/ui-egui/src/publicationwizard/mod.rs @@ -16,7 +16,7 @@ use crate::{RustGisApp, i18n, theme::Tokens}; use accent::accent_step; use arrange::{Arrangement, arrange_step}; use elements::{Elements, elements_step}; -use map::{Area, area_extent, map_step}; +use map::{Area, Coords, area_extent, map_step}; use review::review_step; use rustgis_engine::Session; use rustgis_engine::cmd::layout::{ @@ -112,7 +112,7 @@ pub struct Draft { pub area: Area, /// West, south, east and north as typed for [`Area::Coordinates`], filled from the view when /// it is first picked and kept while another choice is. - pub coords: Option<[f64; 4]>, + pub coords: Option, /// The layout's name; empty leaves it to the command. pub name: String, pub style: String, @@ -630,11 +630,14 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { // A layout the command refuses is said (a map with no extent), and the wizard stays. if create { let held = d.arrangement.held(&app.session).map_err(String::from); - match held.and_then(|_| area_extent(&app.session, &d.area, d.coords)) { + match held.and_then(|_| area_extent(&app.session, &d.area, d.coords.as_ref())) { Ok(extent) => { let mut params = d.sent(&app.session); - if let Some(extent) = extent { - params["extent"] = json!(extent); + if let Some(Coords { sides, crs }) = extent { + params["extent"] = json!(sides); + if let Some(crs) = crs { + params["crs"] = json!(crs); + } } if crate::ribbon::make_publication(app, params) { close = true; diff --git a/crates/ui-egui/src/publicationwizard/review.rs b/crates/ui-egui/src/publicationwizard/review.rs index 9609f3671c..b6b667bca5 100644 --- a/crates/ui-egui/src/publicationwizard/review.rs +++ b/crates/ui-egui/src/publicationwizard/review.rs @@ -15,7 +15,7 @@ pub(super) fn review_step(ui: &mut egui::Ui, s: &Session, d: &Draft) { ui.end_row(); }; row(tl!("Size"), size_label(&d.size)); - row(tl!("Map extent"), area_label(s, &d.area, d.coords)); + row(tl!("Map extent"), area_label(s, &d.area, d.coords.as_ref())); row(tl!("Name"), name_label(s, &d.name)); if d.figure() { row(tl!("Elements"), elements_value(s, d)); diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 9f07157711..9cfac2a8fa 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -59,12 +59,18 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu with nothing to frame, one gone by Create, or one picked before the map or the project was switched or the map renamed, is said and nothing is made); without a name the layout is `Publication`; the review says when a layout has that name, so the new one is numbered. It can - also be **Coordinates**: West, East, South and North as longitude and latitude (WGS 84) - in decimal degrees, whatever the map's projection, filled from the view - when first picked, West less than East and South less than North. They are asked as the sheet + also be **Coordinates**: West, East, South and North, filled from the view when first picked, + West less than East and South less than North. On a map of the Earth in a projected system of + its own with an EPSG or ESRI code (UTM, a national grid; not Web Mercator, a world + projection or a local view, nor one whose axes don't point east and north) they are easting + and northing in its units, the step naming the system, and the sheet frames that rectangle; + a comma followed by exactly three digits isn't read there, as it may part the thousands. + On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After + the map's projection changes, the Map step converts them (to the box around the area, which + can grow a little each time) and Create says to go back to it first. They are asked as the sheet is made (the frame's properties don't take them afterwards, and what the sheet derives from - the area stays as made); an area crossing ±180° isn't taken (a view that crosses it is cut - there, as with View), and the choice is off for a map on the globe. The + the area stays as made); in degrees an area crossing ±180° isn't taken (a view that crosses it + is cut there, as with View), and the choice is off for a map on the globe. The **elements** are what is drawn around the map: the locator (the default, named as what it will be, a globe, an inset, both or none), the north arrow (auto, always or none) and its style, the legend or not and its title, the colorbar, the scale bar with its units and style, and the frame From 12bb45776c7a266941e5b072451487223497ae95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 21:29:44 -0500 Subject: [PATCH 2/7] fix(layout): cut a converted area at 180 degrees --- crates/ui-egui/src/publicationwizard/map.rs | 28 ++++++++++++++++++++- docs/user-guide/layouts.md | 4 +-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs index b16a90b90e..f801339fe0 100644 --- a/crates/ui-egui/src/publicationwizard/map.rs +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -147,7 +147,9 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option { let to = crs.is_some().then(|| map.flat()); let [w, s, e, n] = held.sides; let at = |min: f64, max: f64, i: u32| min + (max - min) * f64::from(i) / f64::from(GRID); - let mut got = Extent::EMPTY; + // Into degrees, the places too as longitudes continuous around the rectangle's centre. + let middle = from.as_ref().filter(|_| to.is_none()).and_then(|f| f.inverse(Coord::new((w + e) / 2.0, (s + n) / 2.0))); + let (mut got, mut around) = (Extent::EMPTY, Extent::EMPTY); for (i, j) in (0..=GRID).flat_map(|i| (0..=GRID).map(move |j| (i, j))) { let c = Coord::new(at(w, e, i), at(s, n, j)); let place = match &from { @@ -160,8 +162,16 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option { }; if let Some(c) = there { got.expand(c); + if let Some(m) = middle { + around.expand(Coord::new(m.x + (c.x - m.x + 180.0).rem_euclid(360.0) - 180.0, c.y)); + } } } + // An area over ±180° is narrow around its centre, and cut there as a view over it is. One + // around a pole is wide both ways: it keeps every longitude. + if !around.is_empty() && around.width() < 180.0 { + got = around; + } let sides = if to.is_none() { in_degrees(framed(got)?) } else if got.is_empty() { @@ -1097,6 +1107,22 @@ mod tests { holds_places(back.sides, AREA, UTM, UTM); } + /// An area over ±180° is cut there in degrees, as a view over it is, not widened to the other + /// way round the world. + #[test] + fn an_area_over_the_antimeridian_is_cut_at_180_in_degrees() { + const ZONE: &str = "EPSG:32660"; + for (sides, side) in + [([600_000.0, 5_500_000.0, 800_000.0, 5_600_000.0], 2), ([650_000.0, 5_500_000.0, 1_200_000.0, 5_600_000.0], 0)] + { + let mut h = holding(ZONE, typed(sides, ZONE)); + change(&mut h, "mercator"); + let [w, s, e, n] = draft(&h).coords.as_ref().unwrap().sides; + assert!(e - w < 20.0 && w < e && s < n, "{w} {e}"); + assert_eq!([w, s, e, n][side].abs(), 180.0, "{w} {e}"); + } + } + /// Where nothing of the area held has a place in the new system, the fields start on the view. #[test] fn an_area_with_no_place_in_the_new_system_starts_again_on_the_view() { diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 9cfac2a8fa..dc4efb2db8 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -67,8 +67,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu a comma followed by exactly three digits isn't read there, as it may part the thousands. On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After the map's projection changes, the Map step converts them (to the box around the area, which - can grow a little each time) and Create says to go back to it first. They are asked as the sheet - is made (the frame's properties don't take them afterwards, and what the sheet derives from + can grow each time; an area over ±180° is cut there in degrees) and Create says to go back to + it first. They are asked as the sheet is made (the frame's properties don't take them afterwards, and what the sheet derives from the area stays as made); in degrees an area crossing ±180° isn't taken (a view that crosses it is cut there, as with View), and the choice is off for a map on the globe. The **elements** are what is drawn around the map: the locator (the default, named as what it will From 8319898b7f1b283233337ab38f00e0e1d58dbe10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 21:56:57 -0500 Subject: [PATCH 3/7] fix(layout): refuse a map coordinate of another unit or axis --- crates/ui-egui/src/publicationwizard/map.rs | 108 ++++++++++++++------ docs/user-guide/layouts.md | 3 +- 2 files changed, 77 insertions(+), 34 deletions(-) diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs index f801339fe0..4a30a07f14 100644 --- a/crates/ui-egui/src/publicationwizard/map.rs +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -182,15 +182,30 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option { Some(Coords { sides, crs }) } -/// A value typed or pasted in a map's units: with the unit and axis the pointer readout writes -/// after it, and a decimal comma too, but for one with exactly three digits after it, which may -/// part the thousands. -fn in_map_units(text: &str) -> Option { - let text = text.trim(); - let text = ["ftUSE", "ftUSN", "ftUS", "ftE", "ftN", "mE", "mN", "E", "N"] - .iter() - .find_map(|unit| text.strip_suffix(unit)) - .map_or(text, str::trim_end); +/// A value typed or pasted in a map's `unit` into a field along `axis` (`E` or `N`): with the +/// unit and axis the pointer readout writes after it, where they are these ones, and a decimal +/// comma too, but for one with exactly three digits after it, which may part the thousands. +fn in_map_units(text: &str, unit: &str, axis: char) -> Option { + let mut text = text.trim(); + // Each suffix with the unit and the axis it names, a longer one before its own ending. + let suffixes = [ + ("ftUSE", "ftUS", Some('E')), + ("ftUSN", "ftUS", Some('N')), + ("ftUS", "ftUS", None), + ("ftE", "ft", Some('E')), + ("ftN", "ft", Some('N')), + ("mE", "m", Some('E')), + ("mN", "m", Some('N')), + ("E", "", Some('E')), + ("N", "", Some('N')), + ]; + if let Some((rest, named, along)) = suffixes.iter().find_map(|(s, named, along)| Some((text.strip_suffix(s)?, *named, *along))) { + // Another unit's or the other axis's number names another place. + if (!named.is_empty() && named != unit) || along.is_some_and(|a| a != axis) { + return None; + } + text = rest.trim_end(); + } if text.rsplit_once(',').is_some_and(|(_, after)| after.len() == 3 && after.bytes().all(|b| b.is_ascii_digit())) { return None; } @@ -204,7 +219,7 @@ fn number(text: &str) -> Option { /// The four fields of the coordinates, written to the draft as they change: no command runs, so a /// value holds even when Next is pressed while it is being typed. -fn coordinate_fields(ui: &mut egui::Ui, coords: &mut Coords, res: f64) { +fn coordinate_fields(ui: &mut egui::Ui, coords: &mut Coords, unit: &'static str, res: f64) { let projected = coords.crs.is_some(); egui::Grid::new("publication_wizard_coordinates").num_columns(2).spacing(egui::vec2(8.0, 4.0)).show(ui, |ui| { for (text, at, most) in [(tl!("West"), 0, 180.0), (tl!("East"), 2, 180.0), (tl!("South"), 1, 90.0), (tl!("North"), 3, 90.0)] { @@ -215,7 +230,12 @@ fn coordinate_fields(ui: &mut egui::Ui, coords: &mut Coords, res: f64) { let field = if projected { // A drag moves by the view's pixels. Read once typed, not key by key: "512,34" // on the way to the ambiguous "512,345" would otherwise stay. - egui::DragValue::new(v).speed(res).max_decimals(3).update_while_editing(false).custom_parser(in_map_units) + let axis = if at % 2 == 0 { 'E' } else { 'N' }; + egui::DragValue::new(v) + .speed(res) + .max_decimals(3) + .update_while_editing(false) + .custom_parser(move |t| in_map_units(t, unit, axis)) } else { // A degree sign pasted with the value too. egui::DragValue::new(v) @@ -311,9 +331,10 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { if !map.projection.is_globe() && coords.crs.as_ref() != system.as_ref().map(|s| &s.0) { *coords = converted(map, coords).unwrap_or_else(|| from_view(s)); } - coordinate_fields(ui, coords, map.camera.res); - if let Some((crs, to_meter)) = &system { - let line = match crate::coordreadout::unit_of(*to_meter) { + let unit = system.as_ref().map_or("", |s| crate::coordreadout::unit_of(s.1)); + coordinate_fields(ui, coords, unit, map.camera.res); + if let Some((crs, _)) = &system { + let line = match unit { "" => i18n::fmt("Easting and northing in {crs}", &[("crs", crs)]), unit => i18n::fmt("Easting and northing in {crs} ({unit})", &[("crs", crs), ("unit", unit)]), }; @@ -405,8 +426,6 @@ mod tests { use egui_kittest::kittest::{NodeT, Queryable}; use rustgis_engine::data::{Dataset, GeometryType}; use rustgis_engine::geom::{Coord, lonlat_to_merc}; - use rustgis_engine::map::Projection; - use rustgis_engine::map::projection::Flat; fn close(got: Option<[f64; 4]>, want: [f64; 4]) { let got = got.expect("an extent"); @@ -1004,28 +1023,51 @@ mod tests { } /// Typed in the map's units: a decimal comma or point, no cut at ±180, and the unit and axis - /// the pointer readout writes pasted with it. + /// the pointer readout writes pasted with it, each in its own system and field. #[test] fn a_value_in_map_units_is_read_whole() { - for (text, want) in [ - ("512345,5", 512_345.5), - ("512345.5", 512_345.5), - ("-512345", -512_345.0), - ("2600000 mE", 2_600_000.0), - ("1200000 mN", 1_200_000.0), - ("512345 ftUS", 512_345.0), - ("512345 ftUSE", 512_345.0), - ("512345 ftUSN", 512_345.0), - ("512345 ftE", 512_345.0), - ("512345 ftN", 512_345.0), - ("512345 E", 512_345.0), - ("512345 N", 512_345.0), + const FEET: &str = "EPSG:2263"; + for (code, field, at, text, want) in [ + (UTM, "West", 0, "512345,5", 512_345.5), + (UTM, "West", 0, "512345.5", 512_345.5), + (UTM, "West", 0, "-512345", -512_345.0), + (UTM, "West", 0, "2600000 mE", 2_600_000.0), + (UTM, "South", 1, "1200000 mN", 1_200_000.0), + (UTM, "East", 2, "512345 E", 512_345.0), + (UTM, "North", 3, "512345 N", 512_345.0), + (FEET, "West", 0, "512345 ftUS", 512_345.0), + (FEET, "West", 0, "512345 ftUSE", 512_345.0), + (FEET, "North", 3, "512345 ftUSN", 512_345.0), + ("EPSG:2222", "West", 0, "512345 ftE", 512_345.0), + ("EPSG:2222", "South", 1, "512345 ftN", 512_345.0), + ] { + let mut h = map_wizard(|a| in_system(a, code), egui::vec2(1400.0, 1000.0)); + pick(&mut h, "Coordinates"); + type_into(&mut h, field, text); + click(&mut h, "Next"); + assert_eq!(sides(&h).unwrap()[at], want, "{code} {field}: {text}"); + } + } + + /// A value in another unit than the map's, or of the other axis than the field's, would name + /// another place: it leaves the field as it was. + #[test] + fn a_value_of_another_unit_or_axis_leaves_the_field() { + for (field, text) in [ + ("West", "512345 ftUS"), + ("West", "512345 ftE"), + ("South", "512345 ftUSN"), + ("West", "1200000 mN"), + ("East", "1200000 N"), + ("South", "512345 mE"), + ("North", "512345 E"), ] { let mut h = utm_harness(); pick(&mut h, "Coordinates"); - type_into(&mut h, "West", text); + let before = sides(&h); + type_into(&mut h, field, text); click(&mut h, "Next"); - assert_eq!(sides(&h).unwrap()[0], want, "{text}"); + assert_eq!(sides(&h), before, "{field}: {text}"); } } @@ -1195,7 +1237,7 @@ mod tests { h.drop_at(to); h.run_steps(4); let moved = sides(&h).unwrap()[0] - AREA[0]; - assert!(moved >= 200.0 && moved <= 10.0 * 200.0 + 1.0, "moved {moved} m"); + assert!((200.0..=10.0 * 200.0 + 1.0).contains(&moved), "moved {moved} m"); } /// Fields holding seven digits stay inside the window at its smallest. diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index dc4efb2db8..9a31afdb05 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -64,7 +64,8 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu its own with an EPSG or ESRI code (UTM, a national grid; not Web Mercator, a world projection or a local view, nor one whose axes don't point east and north) they are easting and northing in its units, the step naming the system, and the sheet frames that rectangle; - a comma followed by exactly three digits isn't read there, as it may part the thousands. + a comma followed by exactly three digits isn't read there, as it may part the thousands, nor a + value written with another unit or the other axis (`ftUS` on a map in metres, `mN` in West). On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After the map's projection changes, the Map step converts them (to the box around the area, which can grow each time; an area over ±180° is cut there in degrees) and Create says to go back to From 2bfb323ba0ba9002381f50a0752b3cff3fddc7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 22:17:58 -0500 Subject: [PATCH 4/7] fix(layout): say what converting the typed coordinates loses --- crates/ui-egui/src/publicationwizard/map.rs | 71 ++++++++++++++++++--- crates/ui-egui/src/publicationwizard/mod.rs | 12 +++- docs/user-guide/layouts.md | 11 ++-- 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs index 4a30a07f14..f81fe9a951 100644 --- a/crates/ui-egui/src/publicationwizard/map.rs +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -45,8 +45,9 @@ const DEFAULT_NAME: &str = "Publication"; /// ±90 edge half of it is cut off again. const MIN_SIDE: f64 = 0.01; -/// The cells a side of the grid that takes coordinates from one system to another: a rectangle's -/// sides, and what lies inside it, may reach past its corners there. +/// The cells along each side of the grid of points that takes a rectangle from one system to +/// another, where its sides, and what lies inside it, may reach past its corners: the box around +/// those points is what the area becomes. const GRID: u32 = 8; /// The system a map's coordinates are typed in: its code and the metres in one of its units; none @@ -137,8 +138,8 @@ fn in_degrees(sides: [f64; 4]) -> [f64; 4] { } /// `held` in the system the coordinates of `map` are typed in now: the box around the places of a -/// grid over it. None when none of them has a place there. -fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option { +/// grid over it, and whether it was cut at ±180°. None when none of them has a place there. +fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, bool)> { let from = match &held.crs { Some(code) => Some(Flat::of(&Projection::from_code(code).ok()?)), None => None, @@ -172,6 +173,8 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option { if !around.is_empty() && around.width() < 180.0 { got = around; } + // Only a box built around the rectangle's centre reaches past ±180°. + let cut = to.is_none() && (got.min_x < -180.0 || got.max_x > 180.0); let sides = if to.is_none() { in_degrees(framed(got)?) } else if got.is_empty() { @@ -179,7 +182,7 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option { } else { outward([got.min_x, got.min_y, got.max_x, got.max_y], true) }; - Some(Coords { sides, crs }) + Some((Coords { sides, crs }, cut)) } /// A value typed or pasted in a map's `unit` into a field along `axis` (`E` or `N`): with the @@ -251,8 +254,11 @@ fn coordinate_fields(ui: &mut egui::Ui, coords: &mut Coords, unit: &'static str, }); } -/// The map step: the area the map frame shows and the layout's name. -pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { +/// The map step: the area the map frame shows and the layout's name. Answers what a conversion of +/// the coordinates to the map's new system lost of the area, in the one frame it is made, for the +/// caller to say. +pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option<&'static str> { + let mut lost = None; let map = s.map(); // In the map's layer order, top first, by what costs nothing a frame: whether it has rows, a // raster or a source. Its places are read at Create ([`Layer::extent`] walks every @@ -329,7 +335,18 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { let system = typed_in(map); // Typed in another system: converted as the step shows. The globe takes none. if !map.projection.is_globe() && coords.crs.as_ref() != system.as_ref().map(|s| &s.0) { - *coords = converted(map, coords).unwrap_or_else(|| from_view(s)); + *coords = match converted(map, coords) { + Some((new, cut)) => { + lost = cut.then(|| tl!("The area typed crosses ±180°: it is cut there.")); + new + } + None => { + lost = Some(tl!( + "No part of the area typed has a place in the map's new system: the coordinates start again on the view." + )); + from_view(s) + } + }; } let unit = system.as_ref().map_or("", |s| crate::coordreadout::unit_of(s.1)); coordinate_fields(ui, coords, unit, map.camera.res); @@ -353,6 +370,7 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { ui.add(egui::TextEdit::singleline(&mut d.name).hint_text(DEFAULT_NAME).desired_width(field_width(ui))).labelled_by(label.id); ui.end_row(); }); + lost } /// How the review names the layout: as typed, or as the command names one given no name, saying @@ -1183,6 +1201,43 @@ mod tests { assert_eq!(got.sides, [view.min_x.floor(), view.min_y.floor(), view.max_x.ceil(), view.max_y.ceil()]); } + /// What a conversion loses of the area typed is said, once: all of it, where no place of it is + /// in the new system, and what lies past ±180° in degrees. A conversion that keeps the area + /// says nothing. + #[test] + fn what_a_conversion_loses_of_the_area_is_said_once() { + const ZONE: &str = "EPSG:32660"; + for (code, held, to, said) in [ + ( + "mercator", + Coords::from([170.0, -45.0, 175.0, -40.0]), + "EPSG:2056", + Some("No part of the area typed has a place in the map's new system: the coordinates start again on the view."), + ), + ( + ZONE, + typed([600_000.0, 5_500_000.0, 800_000.0, 5_600_000.0], ZONE), + "mercator", + Some("The area typed crosses ±180°: it is cut there."), + ), + (UTM, typed(AREA, UTM), "mercator", None), + ] { + let mut h = holding(code, held); + let before = h.state().session.notice_mark(); + change(&mut h, to); + h.run_steps(8); + let session = &h.state().session; + match said { + Some(said) => { + assert_eq!(session.notice_mark(), before + 1, "{to}"); + assert!(h.query_by_label(said).is_none(), "a notice, not a line of the step"); + assert_eq!(session.notices_since(before).first().map(|n| n.text.as_str()), Some(said), "{to}"); + } + None => assert_eq!(session.notice_mark(), before, "{to}"), + } + } + } + /// Changed while on the review, the coordinates read "(none)", and Create says to go back to /// the Map step and makes nothing. #[test] diff --git a/crates/ui-egui/src/publicationwizard/mod.rs b/crates/ui-egui/src/publicationwizard/mod.rs index 5284d7c6d7..a76121cdee 100644 --- a/crates/ui-egui/src/publicationwizard/mod.rs +++ b/crates/ui-egui/src/publicationwizard/mod.rs @@ -110,8 +110,9 @@ pub struct Draft { pub size: String, pub orientation: Orientation, pub area: Area, - /// West, south, east and north as typed for [`Area::Coordinates`], filled from the view when - /// it is first picked and kept while another choice is. + /// West, south, east and north for [`Area::Coordinates`], with the code of the map's system + /// they are in (none: degrees): filled from the view when it is first picked, kept while + /// another choice is, and converted by the map step when the map's system changes. pub coords: Option, /// The layout's name; empty leaves it to the command. pub name: String, @@ -545,6 +546,8 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { } let t = Tokens::get(ctx); let (mut open, mut close, mut create) = (true, false, false); + // What the map step's conversion of the coordinates lost, said once. + let mut lost = None; // Narrower on a screen too narrow for it, as the other dialogs are. let frame = egui::Frame::window(&ctx.global_style()); let screen = ctx.content_rect(); @@ -594,7 +597,7 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { .auto_shrink([false, false]) .show(ui, |ui| match d.step { Step::Size => size_step(ui, &mut d), - Step::Map => map_step(ui, &app.session, &mut d), + Step::Map => lost = map_step(ui, &app.session, &mut d), Step::Style => style_step(ui, &mut d, t.accent), Step::Elements => elements_step(ui, &app.session, &mut d), Step::Placement => arrange_step(ui, &app.session, &mut d), @@ -627,6 +630,9 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { }); }); }); + if let Some(lost) = lost { + app.session.warn(lost); + } // A layout the command refuses is said (a map with no extent), and the wizard stays. if create { let held = d.arrangement.held(&app.session).map_err(String::from); diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 9a31afdb05..d4323a2b59 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -68,11 +68,12 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu value written with another unit or the other axis (`ftUS` on a map in metres, `mN` in West). On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After the map's projection changes, the Map step converts them (to the box around the area, which - can grow each time; an area over ±180° is cut there in degrees) and Create says to go back to - it first. They are asked as the sheet is made (the frame's properties don't take them afterwards, and what the sheet derives from - the area stays as made); in degrees an area crossing ±180° isn't taken (a view that crosses it - is cut there, as with View), and the choice is off for a map on the globe. The - **elements** are what is drawn around the map: the locator (the default, named as what it will + can grow each time), saying when it cuts an area over ±180° there in degrees, or starts again + on the view because no part of it has a place; Create says to go back to the step first. They + are asked as the sheet is made (the frame's properties don't take them afterwards, and what the + sheet derives from the area stays as made); in degrees an area crossing ±180° isn't taken (a + view that crosses it is cut there, as with View), and the choice is off for a map on the globe. + The **elements** are what is drawn around the map: the locator (the default, named as what it will be, a globe, an inset, both or none), the north arrow (auto, always or none) and its style, the legend or not and its title, the colorbar, the scale bar with its units and style, and the frame grid or not (without it the default reference leaves the graticule out; the map's own graticule From 5ac957c914fa42eb776813e12b57a771875cee9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 22:25:01 -0500 Subject: [PATCH 5/7] fix(layout): keep a typed coordinate and report a partial conversion --- crates/ui-egui/src/publicationwizard/map.rs | 68 ++++++++++++++++----- crates/ui-egui/src/publicationwizard/mod.rs | 19 ++++-- docs/user-guide/layouts.md | 7 ++- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs index f81fe9a951..edd77908f8 100644 --- a/crates/ui-egui/src/publicationwizard/map.rs +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -138,8 +138,9 @@ fn in_degrees(sides: [f64; 4]) -> [f64; 4] { } /// `held` in the system the coordinates of `map` are typed in now: the box around the places of a -/// grid over it, and whether it was cut at ±180°. None when none of them has a place there. -fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, bool)> { +/// grid over it, and what of the area that box leaves out, to be said. None when none of them has +/// a place there. +fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, Vec<&'static str>)> { let from = match &held.crs { Some(code) => Some(Flat::of(&Projection::from_code(code).ok()?)), None => None, @@ -151,6 +152,7 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, b // Into degrees, the places too as longitudes continuous around the rectangle's centre. let middle = from.as_ref().filter(|_| to.is_none()).and_then(|f| f.inverse(Coord::new((w + e) / 2.0, (s + n) / 2.0))); let (mut got, mut around) = (Extent::EMPTY, Extent::EMPTY); + let mut left_out = false; for (i, j) in (0..=GRID).flat_map(|i| (0..=GRID).map(move |j| (i, j))) { let c = Coord::new(at(w, e, i), at(s, n, j)); let place = match &from { @@ -161,6 +163,7 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, b Some(f) => place.and_then(|c| f.forward_checked(c)), None => place, }; + left_out |= there.is_none(); if let Some(c) = there { got.expand(c); if let Some(m) = middle { @@ -173,8 +176,14 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, b if !around.is_empty() && around.width() < 180.0 { got = around; } + let mut lost = Vec::new(); + if left_out { + lost.push(tl!("Part of the area typed has no place in the map's new system: it is left out of the box.")); + } // Only a box built around the rectangle's centre reaches past ±180°. - let cut = to.is_none() && (got.min_x < -180.0 || got.max_x > 180.0); + if to.is_none() && (got.min_x < -180.0 || got.max_x > 180.0) { + lost.push(tl!("The area typed crosses ±180°: it is cut there.")); + } let sides = if to.is_none() { in_degrees(framed(got)?) } else if got.is_empty() { @@ -182,7 +191,7 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, b } else { outward([got.min_x, got.min_y, got.max_x, got.max_y], true) }; - Some((Coords { sides, crs }, cut)) + Some((Coords { sides, crs }, lost)) } /// A value typed or pasted in a map's `unit` into a field along `axis` (`E` or `N`): with the @@ -257,8 +266,11 @@ fn coordinate_fields(ui: &mut egui::Ui, coords: &mut Coords, unit: &'static str, /// The map step: the area the map frame shows and the layout's name. Answers what a conversion of /// the coordinates to the map's new system lost of the area, in the one frame it is made, for the /// caller to say. -pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option<&'static str> { - let mut lost = None; +pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Vec<&'static str> { + let mut lost = Vec::new(); + // Taken once the step is drawn: a value being typed in its fields is read in the click's + // frame, which a choice that hides them would skip. + let mut picked_area = None; let map = s.map(); // In the map's layer order, top first, by what costs nothing a frame: whether it has rows, a // raster or a source. Its places are read at Create ([`Layer::extent`] walks every @@ -268,7 +280,7 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option< // A row for each choice, so the label sits beside the first one. ui.label(tl!("Map extent")); if ui.radio(d.area == Area::View, tl!("View")).on_hover_text(tl!("What the map view shows.")).clicked() { - d.area = Area::View; + picked_area = Some(Area::View); } ui.end_row(); ui.label(""); @@ -284,7 +296,7 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option< if radio.clicked() && let Some(layer) = picked.map(Layer::instance_id).or(top) { - d.area = Area::Layer { map: MapOwner::of(s), layer }; + picked_area = Some(Area::Layer { map: MapOwner::of(s), layer }); } if let Some(picked) = picked { egui::ComboBox::from_id_salt("publication_wizard_map_layer") @@ -294,7 +306,7 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option< .show_ui(ui, |ui| { for l in framable() { if ui.selectable_label(l.instance_id() == picked.instance_id(), l.name.as_str()).clicked() { - d.area = Area::Layer { map: MapOwner::of(s), layer: l.instance_id() }; + picked_area = Some(Area::Layer { map: MapOwner::of(s), layer: l.instance_id() }); } } }) @@ -311,7 +323,7 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option< .on_disabled_hover_text(tl!("Nothing is selected in the map.")) .clicked() { - d.area = Area::Selection { map: MapOwner::of(s) }; + picked_area = Some(Area::Selection { map: MapOwner::of(s) }); } ui.end_row(); ui.label(""); @@ -325,7 +337,7 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option< .on_disabled_hover_text(on_globe()) .clicked() { - d.area = Area::Coordinates; + picked_area = Some(Area::Coordinates); } ui.end_row(); if d.area == Area::Coordinates { @@ -336,12 +348,12 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option< // Typed in another system: converted as the step shows. The globe takes none. if !map.projection.is_globe() && coords.crs.as_ref() != system.as_ref().map(|s| &s.0) { *coords = match converted(map, coords) { - Some((new, cut)) => { - lost = cut.then(|| tl!("The area typed crosses ±180°: it is cut there.")); + Some((new, said)) => { + lost = said; new } None => { - lost = Some(tl!( + lost.push(tl!( "No part of the area typed has a place in the map's new system: the coordinates start again on the view." )); from_view(s) @@ -370,6 +382,9 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Option< ui.add(egui::TextEdit::singleline(&mut d.name).hint_text(DEFAULT_NAME).desired_width(field_width(ui))).labelled_by(label.id); ui.end_row(); }); + if let Some(area) = picked_area { + d.area = area; + } lost } @@ -1067,6 +1082,21 @@ mod tests { } } + /// A value typed in a field in map units, read once typed, holds when the click that ends it + /// takes the field away: a step of the list, or another map extent. + #[test] + fn a_value_typed_in_map_units_holds_when_the_click_takes_the_field_away() { + for (role, away) in [(Role::Button, "Review"), (Role::RadioButton, "View"), (Role::RadioButton, "Layer")] { + let mut h = utm_harness(); + pick(&mut h, "Coordinates"); + type_into(&mut h, "West", "512345"); + h.get_by_role_and_label(role, away).click(); + h.run_steps(4); + assert!(h.query_by_role(Role::SpinButton).is_none(), "{away}"); + assert_eq!(sides(&h).unwrap()[0], 512_345.0, "{away}"); + } + } + /// A value in another unit than the map's, or of the other axis than the field's, would name /// another place: it leaves the field as it was. #[test] @@ -1202,8 +1232,8 @@ mod tests { } /// What a conversion loses of the area typed is said, once: all of it, where no place of it is - /// in the new system, and what lies past ±180° in degrees. A conversion that keeps the area - /// says nothing. + /// in the new system, part of it, and what lies past ±180° in degrees. A conversion that keeps + /// the area says nothing. #[test] fn what_a_conversion_loses_of_the_area_is_said_once() { const ZONE: &str = "EPSG:32660"; @@ -1220,6 +1250,12 @@ mod tests { "mercator", Some("The area typed crosses ±180°: it is cut there."), ), + ( + "mercator", + Coords::from([0.0, 30.0, 100.0, 50.0]), + "EPSG:2056", + Some("Part of the area typed has no place in the map's new system: it is left out of the box."), + ), (UTM, typed(AREA, UTM), "mercator", None), ] { let mut h = holding(code, held); diff --git a/crates/ui-egui/src/publicationwizard/mod.rs b/crates/ui-egui/src/publicationwizard/mod.rs index a76121cdee..e46b5ab7eb 100644 --- a/crates/ui-egui/src/publicationwizard/mod.rs +++ b/crates/ui-egui/src/publicationwizard/mod.rs @@ -512,8 +512,10 @@ const LIST_WIDTH: f32 = 110.0; /// The steps of this draft, each clickable to go to it, in `height`: a shorter window scrolls /// them, bringing the current one into view once when it changes, not each frame, so the list can -/// be scrolled by hand. -fn step_list(ui: &mut egui::Ui, d: &mut Draft, height: f32) { +/// be scrolled by hand. Answers the step clicked, for the caller to go to once the step shown is +/// drawn: a value being typed in it is read in the click's frame. +fn step_list(ui: &mut egui::Ui, d: &mut Draft, height: f32) -> Option { + let mut picked = None; ui.vertical(|ui| { ui.set_width(LIST_WIDTH); egui::ScrollArea::vertical().id_salt("publication_wizard_steps").max_height(height).auto_shrink([false, false]).show(ui, |ui| { @@ -526,11 +528,12 @@ fn step_list(ui: &mut egui::Ui, d: &mut Draft, height: f32) { item.scroll_to_me(None); } if item.clicked() { - d.go(step); + picked = Some(step); } } }); }); + picked } /// Shows the wizard while it is open; Create runs `layout.publication` as the menu does, and the @@ -547,7 +550,7 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { let t = Tokens::get(ctx); let (mut open, mut close, mut create) = (true, false, false); // What the map step's conversion of the coordinates lost, said once. - let mut lost = None; + let mut lost = Vec::new(); // Narrower on a screen too narrow for it, as the other dialogs are. let frame = egui::Frame::window(&ctx.global_style()); let screen = ctx.content_rect(); @@ -586,8 +589,9 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { let body = egui::vec2(ui.available_width(), (ui.available_height() - footer).max(0.0)); ui.allocate_ui_with_layout(body, egui::Layout::left_to_right(egui::Align::Min), |ui| { ui.set_min_size(body); + let mut picked = None; if list { - step_list(ui, &mut d, body.y); + picked = step_list(ui, &mut d, body.y); ui.separator(); } ui.vertical(|ui| { @@ -608,6 +612,9 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { Step::Review => review_step(ui, &app.session, &d), }); }); + if let Some(step) = picked { + d.go(step); + } }); ui.separator(); ui.horizontal(|ui| { @@ -630,7 +637,7 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { }); }); }); - if let Some(lost) = lost { + for lost in lost { app.session.warn(lost); } // A layout the command refuses is said (a map with no extent), and the wizard stays. diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index d4323a2b59..2d7d9b2d87 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -68,9 +68,10 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu value written with another unit or the other axis (`ftUS` on a map in metres, `mN` in West). On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After the map's projection changes, the Map step converts them (to the box around the area, which - can grow each time), saying when it cuts an area over ±180° there in degrees, or starts again - on the view because no part of it has a place; Create says to go back to the step first. They - are asked as the sheet is made (the frame's properties don't take them afterwards, and what the + can grow each time), saying when part of the area has no place there and is left out, when it + cuts an area over ±180° there in degrees, or when it starts again on the view because no part + of it has a place; Create says to go back to the step first. + They are asked as the sheet is made (the frame's properties don't take them afterwards, and what the sheet derives from the area stays as made); in degrees an area crossing ±180° isn't taken (a view that crosses it is cut there, as with View), and the choice is off for a map on the globe. The **elements** are what is drawn around the map: the locator (the default, named as what it will From ca1e6c464e0a9fde38e3c894978d9adb10cdd652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Sun, 29 Nov 2026 22:45:37 -0500 Subject: [PATCH 6/7] fix(layout): keep the pole when converting an area to degrees --- crates/ui-egui/src/publicationwizard/map.rs | 34 ++++++++++++++++++--- docs/user-guide/layouts.md | 21 +++++++------ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs index edd77908f8..77c7ab28a6 100644 --- a/crates/ui-egui/src/publicationwizard/map.rs +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -171,10 +171,20 @@ fn converted(map: &rustgis_engine::map::Map, held: &Coords) -> Option<(Coords, V } } } - // An area over ±180° is narrow around its centre, and cut there as a view over it is. One - // around a pole is wide both ways: it keeps every longitude. - if !around.is_empty() && around.width() < 180.0 { - got = around; + // A pole the rectangle holds, as `layout.publication` tells it (the engine's `size_in_degrees`). + let holds = |c: Coord| c.x >= w && c.x <= e && c.y >= s && c.y <= n; + let pole = middle.and_then(|m| { + [90.0, -90.0].into_iter().find(|lat| from.as_ref().and_then(|f| f.forward_checked(Coord::new(m.x, *lat))).is_some_and(holds)) + }); + match pole { + // Around a pole it takes every longitude and the pole, whichever of them the grid lands on. + Some(lat) => { + got.expand(Coord::new(-180.0, lat)); + got.expand(Coord::new(180.0, lat)); + } + // Else it is the box around its centre, cut at ±180° where it crosses, as a view over it is. + None if !around.is_empty() => got = around, + None => {} } let mut lost = Vec::new(); if left_out { @@ -1231,6 +1241,22 @@ mod tests { assert_eq!(got.sides, [view.min_x.floor(), view.min_y.floor(), view.max_x.ceil(), view.max_y.ceil()]); } + /// An area around a pole keeps every longitude and the pole itself in degrees, whether the + /// grid over it lands on them or not, and loses nothing to be said. + #[test] + fn an_area_around_a_pole_keeps_the_pole_in_degrees() { + const SOUTH: &str = "EPSG:3031"; + for sides in [[-1_000_000.0, -1_000_000.0, 1_000_000.0, 1_000_000.0], [-300_000.0, -310_000.0, 1_700_000.0, 1_690_000.0]] { + let mut h = holding(SOUTH, typed(sides, SOUTH)); + let before = h.state().session.notice_mark(); + change(&mut h, "mercator"); + let [w, s, e, n] = draft(&h).coords.as_ref().unwrap().sides; + assert_eq!([w, s, e], [-180.0, -90.0, 180.0], "{sides:?}"); + assert!(n > -90.0 && n < -60.0, "{sides:?}: {n}"); + assert_eq!(h.state().session.notice_mark(), before, "{sides:?}"); + } + } + /// What a conversion loses of the area typed is said, once: all of it, where no place of it is /// in the new system, part of it, and what lies past ±180° in degrees. A conversion that keeps /// the area says nothing. diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 2d7d9b2d87..5d97b988e7 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -66,16 +66,17 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu and northing in its units, the step naming the system, and the sheet frames that rectangle; a comma followed by exactly three digits isn't read there, as it may part the thousands, nor a value written with another unit or the other axis (`ftUS` on a map in metres, `mN` in West). - On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After - the map's projection changes, the Map step converts them (to the box around the area, which - can grow each time), saying when part of the area has no place there and is left out, when it - cuts an area over ±180° there in degrees, or when it starts again on the view because no part - of it has a place; Create says to go back to the step first. - They are asked as the sheet is made (the frame's properties don't take them afterwards, and what the - sheet derives from the area stays as made); in degrees an area crossing ±180° isn't taken (a - view that crosses it is cut there, as with View), and the choice is off for a map on the globe. - The **elements** are what is drawn around the map: the locator (the default, named as what it will - be, a globe, an inset, both or none), the north arrow (auto, always or none) and its style, the + On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After the + map's projection changes, the Map step converts them (to the box around the area, which can + grow each time), saying when part of the area has no place there and is left out, when it cuts + an area over ±180° there in degrees, or when it starts again on the view because no part of it + has a place; Create says to go back to the step first. They are asked as the sheet is made (the + frame's properties don't take them afterwards, and what the sheet derives from the area stays + as made); in degrees an area crossing ±180° isn't taken (a view that crosses it is cut there, + as with View), and the choice is off for a map on the globe. + The **elements** are what is drawn around the map: the locator (the default, named as what it + will be, a globe, an inset, both or none), the north arrow (auto, always or none) and its style, + the legend or not and its title, the colorbar, the scale bar with its units and style, and the frame grid or not (without it the default reference leaves the graticule out; the map's own graticule is still drawn when on). **Advanced**, closed at first, holds the finer settings, its header From 42844ab02e7ddff764bece45f3a7b7050706e4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 05:06:46 -0500 Subject: [PATCH 7/7] fix(layout): don't convert reversed sides, read a bare unit suffix --- crates/ui-egui/src/publicationwizard/map.rs | 58 +++++++++++++++++---- docs/user-guide/layouts.md | 14 ++--- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs index 03e7ad1553..dc47b485d3 100644 --- a/crates/ui-egui/src/publicationwizard/map.rs +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -103,16 +103,18 @@ fn held<'a>(s: &'a Session, area: &Area, coords: Option<&Coords>) -> Result coords - .filter(|c| { - let [w, s, e, n] = c.sides; - w < e && s < n - }) + .filter(|c| ordered(c.sides)) .cloned() .map(Held::Coordinates) .ok_or_else(|| tl!("West must be less than East, and South less than North.")), } } +/// Whether sides name an area: West less than East, and South less than North. +fn ordered([w, s, e, n]: [f64; 4]) -> bool { + w < e && s < n +} + fn on_globe() -> &'static str { tl!("A map on the globe takes no coordinates: pick another map extent.") } @@ -216,6 +218,8 @@ fn in_map_units(text: &str, unit: &str, axis: char) -> Option { ("ftN", "ft", Some('N')), ("mE", "m", Some('E')), ("mN", "m", Some('N')), + ("ft", "ft", None), + ("m", "m", None), ("E", "", Some('E')), ("N", "", Some('N')), ]; @@ -355,15 +359,21 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Vec<&'s let system = typed_in(map); // Typed in another system: converted as the step shows. The globe takes none. if !map.projection.is_globe() && coords.crs.as_ref() != system.as_ref().map(|s| &s.0) { - *coords = match converted(map, coords) { - Some((new, said)) => { + // The box around the places of sides out of order would be an area nobody typed. + let new = if ordered(coords.sides) { + converted(map, coords).ok_or_else(|| { + tl!("No part of the area typed has a place in the map's new system: the coordinates start again on the view.") + }) + } else { + Err(tl!("The coordinates typed were out of order and can't be converted: they start again on the view.")) + }; + *coords = match new { + Ok((new, said)) => { lost = said; new } - None => { - lost.push(tl!( - "No part of the area typed has a place in the map's new system: the coordinates start again on the view." - )); + Err(why) => { + lost.push(why); from_view(s) } }; @@ -378,7 +388,8 @@ pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) -> Vec<&'s ui.set_max_width(field_width(ui)); ui.add(egui::Label::new(line).wrap()); } - // Read after the fields: this frame's pick and values, not the ones it began with. + // Read after the fields: this frame's values, not the ones it began with. The area is + // the one drawn: a pick made this frame is taken once the step is. if let Err(why) = held(s, &d.area, d.coords.as_ref()) { ui.set_max_width(field_width(ui)); ui.add(egui::Label::new(egui::RichText::new(why).color(ui.visuals().warn_fg_color)).wrap()); @@ -1072,6 +1083,8 @@ mod tests { (UTM, "West", 0, "512345,5", 512_345.5), (UTM, "West", 0, "512345.5", 512_345.5), (UTM, "West", 0, "-512345", -512_345.0), + (UTM, "West", 0, "512345 m", 512_345.0), + ("EPSG:2222", "North", 3, "512345 ft", 512_345.0), (UTM, "West", 0, "2600000 mE", 2_600_000.0), (UTM, "South", 1, "1200000 mN", 1_200_000.0), (UTM, "East", 2, "512345 E", 512_345.0), @@ -1112,6 +1125,7 @@ mod tests { for (field, text) in [ ("West", "512345 ftUS"), ("West", "512345 ftE"), + ("West", "512345 ft"), ("South", "512345 ftUSN"), ("West", "1200000 mN"), ("East", "1200000 N"), @@ -1298,6 +1312,28 @@ mod tests { } } + /// Sides out of order name no area to convert: after a change of system the fields start on the + /// view again, which is said, instead of becoming an ordered box nobody typed. + #[test] + fn sides_out_of_order_are_not_converted_into_an_area_nobody_typed() { + for sides in [[600_000.0, 5_000_000.0, 500_000.0, 5_100_000.0], [500_000.0, 5_100_000.0, 600_000.0, 5_100_000.0]] { + let mut h = holding(UTM, typed(sides, UTM)); + let before = h.state().session.notice_mark(); + change(&mut h, "EPSG:32634"); + let got = draft(&h).coords.clone().unwrap(); + assert_eq!(got.crs.as_deref(), Some("EPSG:32634")); + let (w, height) = h.state().session.view_size; + let view = h.state().session.map().camera.extent(w, height); + assert_eq!(got.sides, [view.min_x.floor(), view.min_y.floor(), view.max_x.ceil(), view.max_y.ceil()], "{sides:?}"); + let said = h.state().session.notices_since(before); + assert_eq!( + said.iter().map(|n| n.text.as_str()).collect::>(), + ["The coordinates typed were out of order and can't be converted: they start again on the view."], + "{sides:?}" + ); + } + } + /// Changed while on the review, the coordinates read "(none)", and Create says to go back to /// the Map step and makes nothing. #[test] diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 94d24b49ef..baf5bd289e 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -67,13 +67,13 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu a comma followed by exactly three digits isn't read there, as it may part the thousands, nor a value written with another unit or the other axis (`ftUS` on a map in metres, `mN` in West). On every other flat map they are longitude and latitude (WGS 84) in decimal degrees. After the - map's projection changes, the Map step converts them (to the box around the area, which can - grow each time), saying when part of the area has no place there and is left out, when it cuts - an area over ±180° there in degrees, or when it starts again on the view because no part of it - has a place; Create says to go back to the step first. They are asked as the sheet is made (the - frame's properties don't take them afterwards, and what the sheet derives from the area stays - as made); in degrees an area crossing ±180° isn't taken (a view that crosses it is cut there, - as with View), and the choice is off for a map on the globe. + map's projection changes, the Map step converts them (to the box around the area, which can grow + each time), saying when part of the area has no place there and is left out, when it cuts an + area over ±180° there in degrees, or when it starts again on the view because no part of it has + a place or its sides were out of order; Create says to go back to the step first. They are asked + as the sheet is made (the frame's properties don't take them afterwards, and what the sheet + derives from the area stays as made); in degrees an area crossing ±180° isn't taken (a view that + crosses it is cut there, as with View), and the choice is off for a map on the globe. The **elements** are what is drawn around the map: the locator (the default, named as what it will be, a globe, an inset, an inset and a globe, or none), the north arrow (auto, always or none) and its style, the legend or not and its title, the colorbar, the scale bar with its units