Skip to content

Commit 759c016

Browse files
authored
terrio-crs: place a Web Mercator WKT with its own TOWGS84 as EPSG:3857, as PROJ does (#1070)
* terrio-crs: place a Web Mercator WKT with its own TOWGS84 as EPSG:3857, as PROJ does * Check the registry's null grid only after the WKT's own TOWGS84 * Test the Web Mercator inverse through datum routing too
1 parent 8953346 commit 759c016

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎crates/terrio-crs/src/def.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,15 @@ impl Def {
460460
d.area = e.area;
461461
d.body = Some(399);
462462
// A WKT identified as this code but with its own TOWGS84: GDAL uses that shift (not
463-
// over a PROJ4 extension, which describes the whole CRS).
463+
// over a PROJ4 extension, which describes the whole CRS). Not over Web Mercator's
464+
// `+nadgrids=@null` (its registry WKT's extension), which wins over `+towgs84` as in a
465+
// PROJ string: PROJ 9.8.1 places ESRI's WGS_1984_Web_Mercator_Auxiliary_Sphere with a
466+
// TOWGS84, zero or not, as EPSG:3857 (a shift on the projection's sphere put 45°N 30 km
467+
// away).
464468
if s.proj4.is_none()
465469
&& let Some(w) = s.wkt.as_deref().filter(|w| *w != e.wkt && super::proj4_extension(w).is_none())
466470
&& let Some(t) = wkt_towgs84(w)
471+
&& !super::proj4_extension(&e.wkt).is_some_and(|p| p.split_whitespace().any(|t| t == "+nadgrids=@null"))
467472
{
468473
d.ops = vec![Arc::new(Op::world(datum::towgs84_steps(d.ell, &t)))];
469474
d.datum = None;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

‎docs/crs.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,9 @@ JavaScript `crsToWkt1Strict(crs)`. It is the WKT 1 of `to_wkt`, or `None`:
10641064
`custom_proj4` method for a projection WKT 1 has no name for.
10651065

10661066
Web Mercator (EPSG:3857) keeps GDAL's extension, as PROJ does: its `AUTHORITY` identifies it.
1067+
A Web Mercator WKT with a `TOWGS84` of its own (ESRI's WGS_1984_Web_Mercator_Auxiliary_Sphere
1068+
with one, zero or not) is placed as EPSG:3857, whose `+nadgrids=@null` wins over the shift, as
1069+
PROJ 9.8.1 places it; the shift is not applied on the projection's sphere.
10671070
A WKT 1 given is returned as it is when it needs no extension (or its root code is the CRS's
10681071
own), else `None`. A WKT 2 that terrio identifies as a code, without a datum
10691072
shift of its own, is written as the code's WKT 1; with the code's two horizontal axes swapped

0 commit comments

Comments
 (0)