|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 Qiusheng Wu and Terrio contributors |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +//! A Web Mercator WKT with a `TOWGS84` of its own is placed as EPSG:3857, whose |
| 4 | +//! `+nadgrids=@null` wins over the shift, as PROJ 9.8.1 places it (opengeos/terrio#1012, item 63). |
| 5 | +
|
| 6 | +use terrio_crs::{SpatialRef, Transformer}; |
| 7 | + |
| 8 | +/// ESRI's WGS_1984_Web_Mercator_Auxiliary_Sphere with `TOWGS84[0,0,0,0,0,0,0]` in its datum. |
| 9 | +const ESRI: &str = r#"PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563],TOWGS84[0,0,0,0,0,0,0]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]"#; |
| 10 | + |
| 11 | +/// PROJ 9.8.1 (pyproj 3.8.0, network off) identifies ESRI's Web Mercator with a zero or a 10 m |
| 12 | +/// `TOWGS84`, and GDAL's WKT 1 of EPSG:3857 with one added, as EPSG:3857 and places (0°, 45°) and |
| 13 | +/// (10°, 60°) from WGS 84 at (0, 5621521.486192066) and (1113194.9079327357, 8399737.889818357) |
| 14 | +/// (`Transformer.from_crs("EPSG:4326", CRS.from_wkt(wkt), always_xy=True)`), as EPSG:3857 itself. |
| 15 | +/// terrio applied the shift through geocentric coordinates on the projection's sphere: 30.2 km and |
| 16 | +/// 37.1 km north. Within 1 mm, and back within 1e-9°, both with EPSG:4326, whose Web Mercator |
| 17 | +/// shortcut skips datum routing, and with `+proj=longlat +datum=WGS84`, which goes through it. |
| 18 | +#[test] |
| 19 | +fn a_web_mercator_shift_is_not_applied() { |
| 20 | + terrio_crs::set_grid_search_paths(Some(vec![])); |
| 21 | + terrio_crs::set_network_enabled(Some(false)); |
| 22 | + let geographic = [SpatialRef::from_epsg(4326).unwrap(), SpatialRef::from_proj4("+proj=longlat +datum=WGS84").unwrap()]; |
| 23 | + let gdal = terrio_crs::epsg_wkt(3857).unwrap().replacen( |
| 24 | + r#"AUTHORITY["EPSG","7030"]]"#, |
| 25 | + r#"AUTHORITY["EPSG","7030"]],TOWGS84[10,0,0,0,0,0,0]"#, |
| 26 | + 1, |
| 27 | + ); |
| 28 | + assert!(gdal.contains("TOWGS84")); |
| 29 | + for wkt in [ESRI.to_string(), ESRI.replace("TOWGS84[0,0,0", "TOWGS84[10,0,0"), gdal] { |
| 30 | + let crs = SpatialRef::from_wkt(&wkt); |
| 31 | + assert_eq!(crs.epsg, Some(3857), "{wkt}"); |
| 32 | + for wgs84 in &geographic { |
| 33 | + let forward = Transformer::new(wgs84, &crs).unwrap(); |
| 34 | + let inverse = Transformer::new(&crs, wgs84).unwrap(); |
| 35 | + for ((lon, lat), (x, y)) in |
| 36 | + [((0.0, 45.0), (0.0, 5_621_521.486_192_066)), ((10.0, 60.0), (1_113_194.907_932_735_7, 8_399_737.889_818_357))] |
| 37 | + { |
| 38 | + let (a, b) = forward.transform(lon, lat).unwrap(); |
| 39 | + assert!((a - x).abs() < 1e-3 && (b - y).abs() < 1e-3, "{wkt}: ({a}, {b}), PROJ ({x}, {y})"); |
| 40 | + let (a, b) = inverse.transform(x, y).unwrap(); |
| 41 | + assert!((a - lon).abs() < 1e-9 && (b - lat).abs() < 1e-9, "{wkt}: ({a}, {b}), ({lon}, {lat})"); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments