Repository navigation
The pointer reads in the map's own coordinate system by default #2781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ use egui::Ui; | |
| use rustgis_engine::geom::Coord; | ||
| use serde_json::json; | ||
|
|
||
| use crate::coordreadout::MapSystem; | ||
| use crate::widgets::{menu_row, submenu_row}; | ||
| use crate::{CoordReadout, RustGisApp, i18n}; | ||
|
|
||
|
|
@@ -54,11 +55,18 @@ fn lat_lon(c: Coord) -> String { | |
| /// The coordinate row: the text it shows and copies, in the readout's format, and whether it fell | ||
| /// back from it. Decimal degrees are `lat, lon` ([`lat_lon`]); north of 84° N and south of 80° S | ||
| /// UTM and MGRS give UPS (`north …`, `Z …`); on another body's map (`earth` false) they fall back | ||
| /// to latitude and longitude (and the row's hover text says why). | ||
| fn coordinate_row(c: Coord, f: CoordReadout, earth: bool) -> (String, bool) { | ||
| match f { | ||
| CoordReadout::Decimal => (lat_lon(c), false), | ||
| _ => match f.format(c, earth) { | ||
| /// to latitude and longitude (and the row's hover text says why). Map coordinates are the map's | ||
| /// own system's (`map`), followed by its name so the copy says which system they are in | ||
| /// (`2600173 mE 1200272 mN EPSG:2056`); without one (the globe, another body) they are latitude | ||
| /// and longitude, and where the system doesn't reach they fall back to them. | ||
| fn coordinate_row(c: Coord, f: CoordReadout, earth: bool, map: Option<&MapSystem>) -> (String, bool) { | ||
| match (f, map) { | ||
| (CoordReadout::Decimal, _) | (CoordReadout::Map, None) => (lat_lon(c), false), | ||
| (CoordReadout::Map, Some(m)) => match f.format(c, earth, map) { | ||
| Some(text) => (format!("{text} {}", m.name()), false), | ||
| None => (lat_lon(c), true), | ||
| }, | ||
| _ => match f.format(c, earth, map) { | ||
| Some(text) => (text, false), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the map format, this arm copies text like Category: quality
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 00228ff. In map coordinates the row now copies the system's name after the numbers, Reading the copy back in the search box is a separate feature: the search box would need to parse a system and its coordinates. I've left that out of this PR. |
||
| None => (lat_lon(c), true), | ||
| }, | ||
|
|
@@ -124,8 +132,12 @@ pub fn show(app: &mut RustGisApp, ui: &mut Ui, c: Coord) { | |
| crate::shapeconvert::menu(app, ui); | ||
| let format = app.map.coord_format; | ||
| let earth = app.session.map().body().is_earth(); | ||
| let (coords, fell_back) = coordinate_row(c, format, earth); | ||
| let hover = if fell_back && !earth { | ||
| let system = MapSystem::of(app.session.map()); | ||
| let (coords, fell_back) = coordinate_row(c, format, earth, system.as_ref()); | ||
| let hover = if fell_back && format == CoordReadout::Map { | ||
| tl!("Copy coordinates to the clipboard. The map's coordinate system doesn't reach here, so latitude and longitude are copied") | ||
| .to_string() | ||
| } else if fell_back && !earth { | ||
| i18n::fmt( | ||
| "Copy coordinates to the clipboard. {format} is a grid of the Earth, so latitude and longitude are copied", | ||
| &[("format", format.label())], | ||
|
|
@@ -289,14 +301,14 @@ mod tests { | |
| #[test] | ||
| fn the_coordinate_row_copies_the_chosen_format() { | ||
| let p = Coord::new(44.4, 33.3); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Decimal, true), ("33.300000, 44.400000".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Dms, true), ("44°24′00.0″ E 33°18′00.0″ N".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Utm, true), ("38north 444141 3684706".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Mgrs, true), ("38S MB 44140 84706".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Decimal, true, None), ("33.300000, 44.400000".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Dms, true, None), ("44°24′00.0″ E 33°18′00.0″ N".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Utm, true, None), ("38north 444141 3684706".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Mgrs, true, None), ("38S MB 44140 84706".into(), false)); | ||
| // What is copied as UTM or MGRS is found again by the search box, in both hemispheres. | ||
| for q in [p, Coord::new(-58.4, -34.6), Coord::new(151.2, -33.87), Coord::new(3.0, -1e-7), Coord::new(-21.9, 64.15)] { | ||
| for f in [CoordReadout::Utm, CoordReadout::Mgrs] { | ||
| let (text, fell_back) = coordinate_row(q, f, true); | ||
| let (text, fell_back) = coordinate_row(q, f, true, None); | ||
| assert!(!fell_back, "{text}"); | ||
| let at = rustgis_engine::cmd::search::read_grid_reference(&text).unwrap_or_else(|e| panic!("{text}: {e}")); | ||
| assert!((at.x - q.x).abs() < 1e-4 && (at.y - q.y).abs() < 1e-4, "{text} {at:?}"); | ||
|
|
@@ -305,15 +317,29 @@ mod tests { | |
| // Where UTM has no zone, the row gives UPS (`north …`, `Z …`), which reads back too. | ||
| let pole = Coord::new(10.0, 86.0); | ||
| for f in [CoordReadout::Utm, CoordReadout::Mgrs] { | ||
| let (text, fell_back) = coordinate_row(pole, f, true); | ||
| let (text, fell_back) = coordinate_row(pole, f, true, None); | ||
| assert!(!fell_back && (text.starts_with("north ") || text.starts_with("Z ")), "{f:?}: {text}"); | ||
| let at = rustgis_engine::cmd::search::read_grid_reference(&text).unwrap_or_else(|e| panic!("{text}: {e}")); | ||
| assert!((at.x - pole.x).abs() < 1e-3 && (at.y - pole.y).abs() < 1e-4, "{text} {at:?}"); | ||
| // On another body's map the row falls back to latitude and longitude (and says so): | ||
| // UTM, MGRS and UPS are the Earth's grids. | ||
| assert_eq!(coordinate_row(p, f, false), ("33.300000, 44.400000".into(), true), "{f:?}"); | ||
| assert_eq!(coordinate_row(p, f, false, None), ("33.300000, 44.400000".into(), true), "{f:?}"); | ||
| } | ||
| assert!(!coordinate_row(pole, CoordReadout::Dms, true).1); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Dms, false), coordinate_row(p, CoordReadout::Dms, true)); | ||
| assert!(!coordinate_row(pole, CoordReadout::Dms, true, None).1); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Dms, false, None), coordinate_row(p, CoordReadout::Dms, true, None)); | ||
| // Map coordinates: the map's own system's, and latitude and longitude without one (the | ||
| // globe, another body) or where the system doesn't reach (said in the row's hover text). | ||
| let merc = MapSystem::new(rustgis_engine::map::projection::Flat::Mercator, 1000.0, "Web Mercator"); | ||
| assert_eq!(coordinate_row(Coord::new(0.0, 0.0), CoordReadout::Map, true, Some(&merc)), ("0 mE 0 mN Web Mercator".into(), false)); | ||
| assert_eq!(coordinate_row(p, CoordReadout::Map, true, None), ("33.300000, 44.400000".into(), false)); | ||
| let lv95 = MapSystem::new( | ||
| rustgis_engine::map::projection::Flat::of(&rustgis_engine::map::Projection::Custom("EPSG:2056".into())), | ||
| 1.0, | ||
| "EPSG:2056", | ||
| ); | ||
| let (bern, _) = coordinate_row(Coord::new(7.4386, 46.9511), CoordReadout::Map, true, Some(&lv95)); | ||
| assert!(bern.starts_with("2599") && bern.ends_with(" mN EPSG:2056"), "{bern}"); | ||
| let far = Coord::new(-150.0, -60.0); | ||
| assert_eq!(coordinate_row(far, CoordReadout::Map, true, Some(&lv95)), ("-60.000000, -150.000000".into(), true)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MapSystem::ofnow runs on every frame of the status bar, whatever the format. It takes a thread-local cache lookup that lowercases the projection code, andsystem_namecallsProjection::label()(alookup) and allocates a name string. Only the Map format uses the result, so the work is wasted in the other five formats. The added cost is small, since line 487 already callslabel()per frame for custom projections, but it is easy to avoid by computing the system only for Map:Category: performance