Repository navigation
feat(geom): UTM and MGRS coordinate readouts, copies and search #1429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,285
−40
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3b1e6c4
feat(geom): UTM and MGRS coordinate readouts, copies and search
PawelKra 88c0f84
fix(geom): MGRS squares must reach their band, two-digit zones, grid …
PawelKra ca6d99a
Merge upstream main into feat/utm-mgrs-readout
PawelKra 83a8dcd
fix(ui): grid refusal reasons in logical order; finish the copy hover…
PawelKra f0f09b8
fix(ui): search list notes keep right-to-left rows in reading order
PawelKra a2ae0ab
fix(geom): the UTM readout states the hemisphere, not the latitude band
PawelKra 9d7f0c5
fix(search): a number and a word get no grid-reference reason
PawelKra 9075b0b
Merge remote-tracking branch 'upstream/main' into feat/utm-mgrs-readout
PawelKra c4d7c03
fix(ui): keep an unknown saved coordinate format until the user picks…
PawelKra b88c924
Merge remote-tracking branch 'origin/main' into pr-1429
giswqs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| //! The UTM series in `rustgis-geom` (Karney's Krüger series) against proj4rs, which reprojects | ||
| //! rasters and layers: the same places in every zone and band, both hemispheres, the Norway | ||
| //! and Svalbard zones and the zones' edges, within a millimetre. | ||
|
|
||
| use rustgis_data::raster::Crs; | ||
| use rustgis_geom::{Coord, utm}; | ||
|
|
||
| /// A small deterministic generator (no dependency). | ||
| struct Lcg(u64); | ||
| impl Lcg { | ||
| fn next(&mut self) -> f64 { | ||
| self.0 = self.0.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407); | ||
| (self.0 >> 11) as f64 / (1u64 << 53) as f64 | ||
| } | ||
| } | ||
|
|
||
| fn proj(zone: u8, north: bool) -> rustgis_data::raster::Transformer { | ||
| let south = if north { "" } else { " +south" }; | ||
| Crs::parse(&format!("+proj=utm +zone={zone}{south} +datum=WGS84 +units=m +no_defs")).unwrap().transformer().unwrap() | ||
| } | ||
|
|
||
| /// Compares one place both ways; returns the largest difference in metres and degrees. | ||
| fn compare(t: &rustgis_data::raster::Transformer, p: Coord, zone: u8, north: bool) -> (f64, f64) { | ||
| let (e, n) = utm::to_utm_in_zone(p, zone, north).unwrap(); | ||
| let want = t.from_lonlat(p).unwrap(); | ||
| let dm = (e - want.x).abs().max((n - want.y).abs()); | ||
| assert!(dm < 1e-3, "zone {zone} north {north} at {p:?}: ours {e} {n}, proj4rs {} {}", want.x, want.y); | ||
| let back = utm::from_utm(zone, north, want.x, want.y).unwrap(); | ||
| let theirs = t.to_lonlat(want).unwrap(); | ||
| let dd = (back.x - theirs.x).abs().max((back.y - theirs.y).abs()); | ||
| // A millimetre is about 9e-9° of latitude. | ||
| assert!(dd < 1e-8, "zone {zone} north {north} at {p:?}: ours {back:?}, proj4rs {theirs:?}"); | ||
| (dm, dd) | ||
| } | ||
|
|
||
| #[test] | ||
| fn random_places_in_every_zone_agree_with_proj4rs() { | ||
| let mut r = Lcg(2026); | ||
| let (mut worst_m, mut worst_deg) = (0.0f64, 0.0f64); | ||
| let mut count = 0; | ||
| for zone in 1u8..=60 { | ||
| for north in [true, false] { | ||
| let t = proj(zone, north); | ||
| for _ in 0..40 { | ||
| let lat = if north { r.next() * utm::UTM_MAX_LAT } else { -r.next() * -utm::UTM_MIN_LAT }; | ||
| // Across the zone and a little past its edges, as Norway's and Svalbard's do. | ||
| let lon = utm::central_meridian(zone) + (r.next() - 0.5) * 9.0; | ||
| let p = Coord::new(lon, lat); | ||
| let (m, d) = compare(&t, p, zone, north); | ||
| worst_m = worst_m.max(m); | ||
| worst_deg = worst_deg.max(d); | ||
| count += 1; | ||
| } | ||
| } | ||
| } | ||
| eprintln!("{count} places: largest difference {worst_m:.2e} m, {worst_deg:.2e}°"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn standard_zones_bands_and_exceptions_agree_with_proj4rs() { | ||
| let places = [ | ||
| // GeographicLib's GeoConvert example (Baghdad): 38n 444140.54 3684706.36. | ||
| (44.4, 33.3), | ||
| // Norway's 32V west of 6° E, and Bergen. | ||
| (3.5, 56.5), | ||
| (5.32, 60.39), | ||
| (11.99, 63.9), | ||
| // Svalbard's wide zones: their edges, 6° from the central meridian. | ||
| (0.01, 79.0), | ||
| (8.99, 72.1), | ||
| (9.01, 83.9), | ||
| (20.99, 75.0), | ||
| (21.01, 80.0), | ||
| (32.99, 77.0), | ||
| (33.01, 78.0), | ||
| (41.99, 83.99), | ||
| // The antimeridian, the equator, the band edges and the UTM limits. | ||
| (-180.0, 0.0), | ||
| (179.999, -0.001), | ||
| (-174.0, 8.0), | ||
| (0.0, -80.0), | ||
| (-0.001, 83.999), | ||
| (151.21, -33.87), | ||
| (-74.006, 40.7128), | ||
| ]; | ||
| for (lon, lat) in places { | ||
| let p = Coord::new(lon, lat); | ||
| let u = utm::to_utm(p).unwrap(); | ||
| let t = proj(u.zone, u.north()); | ||
| compare(&t, p, u.zone, u.north()); | ||
| let want = t.from_lonlat(p).unwrap(); | ||
| assert!((u.easting - want.x).abs() < 1e-3 && (u.northing - want.y).abs() < 1e-3, "{p:?} {u:?} {want:?}"); | ||
| // The EPSG codes the app reprojects with name the same systems. | ||
| let epsg = if u.north() { 32600 } else { 32700 } + u32::from(u.zone); | ||
| let by_code = Crs::from_epsg(epsg).unwrap().transformer().unwrap().from_lonlat(p).unwrap(); | ||
| assert!((by_code.x - want.x).abs() < 1e-6 && (by_code.y - want.y).abs() < 1e-6, "EPSG:{epsg}"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.