From 2e9a6052b62d7ca6af6897d1cfeb405a4a4cd67c Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 25 Nov 2026 17:59:56 +0100 Subject: [PATCH 1/2] fix(crs): 900913 WKT without a shift reads as PROJ does; a set-aside WGS 84 shift is said MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GDAL 1's EPSG:900913 text without its PROJ4 extension and without TOWGS84 read as Web Mercator, 21 km off at 45°N from PROJ 9.9.0's ellipsoidal Mercator: the Web Mercator comparison now runs without a shift too. A file's shift on a code without one (WGS 84's) is set aside with a note. Stale test comments and a PROJ 9.8.1 citation updated. Refs #2032 (terrio-crs release) --- crates/data/src/raster/wkt.rs | 38 +++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/crates/data/src/raster/wkt.rs b/crates/data/src/raster/wkt.rs index cf33df6122..c81efa39d2 100644 --- a/crates/data/src/raster/wkt.rs +++ b/crates/data/src/raster/wkt.rs @@ -497,8 +497,19 @@ fn read_root(root: &Node, code: Option, text: &str) -> Result<(Crs, Option< // transformation of its EPSG code, as in PROJ and GDAL. The code is used only when the two // place points alike. let files_shift = towgs84(geogcs(root).and_then(|g| g.child("DATUM"))); - if files_shift.is_none() || places_as(text, by_code) { - return Ok((by_code, projection_note(root, by_code).map(str::to_owned))); + // Without a shift, a code is read as itself, except Web Mercator: GDAL 1's EPSG:900913 without + // its `PROJ4` extension is Mercator on the WGS 84 ellipsoid, as PROJ reads it, not on Web + // Mercator's sphere. + if (files_shift.is_none() && by_code != Crs::WebMercator) || places_as(text, by_code) { + let note = projection_note(root, by_code).map(str::to_owned); + // A shift the file gives on a datum whose code has none (WGS 84's) is none for PROJ + // either, so the file's is set aside: said, as the file asks for it. + let set_aside = files_shift.is_some_and(|s| s.iter().any(|v| *v != 0.0)) && code_with_shift(by_code).is_none(); + if set_aside { + let said = format!("the file's own shift to WGS 84 is set aside, as EPSG:{code} has none, as in PROJ"); + return Ok((by_code, Some(note.map_or(said.clone(), |n| format!("{n}; {said}"))))); + } + return Ok((by_code, note)); } // A system that can't be read from its parameters is still read by its EPSG code, with the // code's shift in place of the file's, which is said. @@ -4883,17 +4894,19 @@ pub(crate) mod tests { let nad27 = r#"GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.978698213898],TOWGS84[0,0,0,0,0,0,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4267"]]"#; let def = crs_from_wkt(nad27).unwrap().custom_def().unwrap(); assert!(def.proj4.contains("+towgs84=0,0,0,0,0,0,0"), "{}", def.proj4); - // A WGS 84 code needs no shift: it is kept with a shift of zero, not with another. + // A WGS 84 code needs no shift: it is kept with a shift of zero, without a word. let utm = format!(r#"{},AUTHORITY["EPSG","32631"]]"#, ESRI_32631.strip_suffix(']').unwrap()); let towgs84 = |t: &str| utm.replace("298.257223563]]", &format!("298.257223563],{t}]")); - assert_eq!(crs_from_wkt(&towgs84("TOWGS84[0,0,0]")), Ok(Crs::Epsg(32631))); + assert_eq!(read_wkt(&towgs84("TOWGS84[0,0,0]")), Ok((Crs::Epsg(32631), None))); // Nor with another: a shift on WGS 84 itself is none. PROJ 9.9.0 reads this text as a // bound CRS and transforms it to EPSG:4326 without the shift (conda-forge PROJ 9.9.0 with // pyproj 3.8.0 built on it, network off: `Transformer.from_crs(CRS.from_wkt(text), // "EPSG:4326", always_xy=True).transform(500000, 5000000)` is 3°E 45.1534772°N), as // EPSG:32631 does. - let shifted = crs_from_wkt(&towgs84("TOWGS84[1,2,3]")); - assert_eq!(shifted, Ok(Crs::Epsg(32631))); + // The file's shift is set aside, which is said. + let (crs, note) = read_wkt(&towgs84("TOWGS84[1,2,3]")).unwrap(); + assert_eq!(crs, Crs::Epsg(32631)); + assert_eq!(note.as_deref(), Some("the file's own shift to WGS 84 is set aside, as EPSG:32631 has none, as in PROJ")); let ll = Crs::Epsg(32631).transformer().unwrap().to_lonlat(Coord::new(500_000.0, 5_000_000.0)).unwrap(); assert!((ll.x - 3.0).abs() < 1e-9 && (ll.y - 45.153_477_2).abs() < 1e-7, "{ll:?}"); } @@ -5124,8 +5137,8 @@ pub(crate) mod tests { assert!(metres_apart(ll, Coord::new(60.0, 45.0)) < 0.001, "{ll:?}"); } // Another shift: GDAL's `PROJ4` extension (`+nadgrids=@null` on Web Mercator's sphere) is - // what PROJ 9.8.1 reads in place of the WKT, so it stays Web Mercator (pyproj 3.8.0 places - // it as EPSG:3857); without the extension, the shift is the file's own. + // what PROJ 9.9.0 reads in place of the WKT, so it stays Web Mercator (it places it as + // EPSG:3857); without the extension, the shift is the file's own. let shifted = google.replace("TOWGS84[0,0,0,0,0,0,0]", "TOWGS84[1,2,3,0,0,0,0]"); assert_eq!(crs_from_wkt(&shifted), Ok(Crs::WebMercator)); let shifted = without_proj4_extension(&shifted).unwrap(); @@ -5133,9 +5146,12 @@ pub(crate) mod tests { // Without the extension it is Mercator on the WGS 84 ellipsoid, as PROJ 9.9.0 reads it // (conda-forge PROJ 9.9.0 with pyproj 3.8.0 built on it, network off: // `Transformer.from_crs(CRS.from_wkt(text), "EPSG:4326", always_xy=True)` puts - // 6 679 169.4476 m E, 5 621 521.4862 m N at 60°E 45.1923152°N, with or without the - // shift), not Web Mercator's sphere (45°N). - for text in [without_proj4_extension(google).unwrap(), shifted] { + // 6 679 169.4476 m E, 5 621 521.4862 m N at 60°E 45.1923152°N, with a shift, a shift of + // zero or none), not Web Mercator's sphere (45°N). + let unshifted = without_proj4_extension(google).unwrap().replace(",TOWGS84[0,0,0,0,0,0,0]", ""); + assert!(!unshifted.contains("TOWGS84") && !unshifted.contains("EXTENSION"), "{unshifted}"); + for text in [without_proj4_extension(google).unwrap(), shifted, unshifted] { + assert_ne!(crs_from_wkt(&text), Ok(Crs::WebMercator), "{text}"); let ll = crs_from_wkt(&text) .unwrap() .transformer() From 579342be77cb0e3f68f6912ac1c3c33a9dc9d051 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 25 Nov 2026 20:59:57 +0100 Subject: [PATCH 2/2] fix(crs): a code beside the text (GeoPackage, FileGDB, PDF) follows the same 900913 and set-aside rules read_wkt_over_code shares read_root's rule (by_code_note): GDAL 1's EPSG:900913 without a shift is compared with Web Mercator there too, and a shift set aside on a code without one is said. Only 900913 is compared without a shift: EPSG:3857's own text is Web Mercator for PROJ by its name, and a row coded 3857 is still read by its code. --- crates/data/src/raster/wkt.rs | 62 +++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/crates/data/src/raster/wkt.rs b/crates/data/src/raster/wkt.rs index c81efa39d2..8ea6b29de0 100644 --- a/crates/data/src/raster/wkt.rs +++ b/crates/data/src/raster/wkt.rs @@ -301,8 +301,13 @@ pub fn read_wkt_over_code(text: &str, code: u32) -> Option matches!(by_code_note(text, files_shift.as_deref(), code, by_code), Some(None)), + None => files_shift.is_none(), + }; + if !unusable_towgs84(&root) && as_code { return None; } Some(read_tree(&root, Some(code), text)) @@ -497,18 +502,12 @@ fn read_root(root: &Node, code: Option, text: &str) -> Result<(Crs, Option< // transformation of its EPSG code, as in PROJ and GDAL. The code is used only when the two // place points alike. let files_shift = towgs84(geogcs(root).and_then(|g| g.child("DATUM"))); - // Without a shift, a code is read as itself, except Web Mercator: GDAL 1's EPSG:900913 without - // its `PROJ4` extension is Mercator on the WGS 84 ellipsoid, as PROJ reads it, not on Web - // Mercator's sphere. - if (files_shift.is_none() && by_code != Crs::WebMercator) || places_as(text, by_code) { + if let Some(said) = by_code_note(text, files_shift.as_deref(), code, by_code) { let note = projection_note(root, by_code).map(str::to_owned); - // A shift the file gives on a datum whose code has none (WGS 84's) is none for PROJ - // either, so the file's is set aside: said, as the file asks for it. - let set_aside = files_shift.is_some_and(|s| s.iter().any(|v| *v != 0.0)) && code_with_shift(by_code).is_none(); - if set_aside { - let said = format!("the file's own shift to WGS 84 is set aside, as EPSG:{code} has none, as in PROJ"); - return Ok((by_code, Some(note.map_or(said.clone(), |n| format!("{n}; {said}"))))); - } + let note = match (note, said) { + (Some(n), Some(said)) => Some(format!("{n}; {said}")), + (n, said) => n.or(said), + }; return Ok((by_code, note)); } // A system that can't be read from its parameters is still read by its EPSG code, with the @@ -525,6 +524,29 @@ fn read_root(root: &Node, code: Option, text: &str) -> Result<(Crs, Option< } } +/// GDAL 1's code for Web Mercator, which its text gives otherwise ([`by_code_note`]). +const GDAL1_GOOGLE: u32 = 900_913; + +/// Whether a WKT1 system naming EPSG `code` (`by_code`), whose datum gives `files_shift`, is read +/// by that code: `None` when it is read from its own parameters instead, else what is said of +/// the file's shift (`Some(None)`: nothing). +/// +/// Without a shift, a code is read as itself, except GDAL 1's EPSG:900913: without its `PROJ4` +/// extension its text is Mercator on the WGS 84 ellipsoid, as PROJ reads it, not on Web +/// Mercator's sphere ([`places_as`] tells them apart). EPSG:3857 is read as itself: its own text +/// is Web Mercator for PROJ by its name, with or without the extension. A shift the file gives on a datum whose +/// code has none (WGS 84's) is none for PROJ either, so the file's is set aside, which is said. +fn by_code_note(text: &str, files_shift: Option<&[f64]>, code: u32, by_code: Crs) -> Option> { + if files_shift.is_none() && code != GDAL1_GOOGLE { + return Some(None); + } + if !places_as(text, by_code) { + return None; + } + let set_aside = files_shift.is_some_and(|s| s.iter().any(|v| *v != 0.0)) && code_with_shift(by_code).is_none(); + Some(set_aside.then(|| format!("the file's own shift to WGS 84 is set aside, as EPSG:{code} has none, as in PROJ"))) +} + /// Whether WKT `text`, as terrio-crs reads it, places points as `crs` does: within a centimetre /// (or the degrees of one) at the centre of the area of use of its EPSG code. A file's shift to /// WGS 84 that is its code's transformation then reads as the code. Only there: the text RustGIS @@ -4907,6 +4929,10 @@ pub(crate) mod tests { let (crs, note) = read_wkt(&towgs84("TOWGS84[1,2,3]")).unwrap(); assert_eq!(crs, Crs::Epsg(32631)); assert_eq!(note.as_deref(), Some("the file's own shift to WGS 84 is set aside, as EPSG:32631 has none, as in PROJ")); + // The same where the code comes beside the text (a GeoPackage's, say). + let beside = read_wkt_over_code(&towgs84("TOWGS84[1,2,3]"), 32631).unwrap().unwrap(); + assert_eq!(beside, (crs, note)); + assert_eq!(read_wkt_over_code(&towgs84("TOWGS84[0,0,0]"), 32631), None); let ll = Crs::Epsg(32631).transformer().unwrap().to_lonlat(Coord::new(500_000.0, 5_000_000.0)).unwrap(); assert!((ll.x - 3.0).abs() < 1e-9 && (ll.y - 45.153_477_2).abs() < 1e-7, "{ll:?}"); } @@ -5150,6 +5176,16 @@ pub(crate) mod tests { // zero or none), not Web Mercator's sphere (45°N). let unshifted = without_proj4_extension(google).unwrap().replace(",TOWGS84[0,0,0,0,0,0,0]", ""); assert!(!unshifted.contains("TOWGS84") && !unshifted.contains("EXTENSION"), "{unshifted}"); + // The same where the code comes beside the text. + let beside = read_wkt_over_code(&unshifted, 900913).unwrap().unwrap().0; + assert_eq!(Ok(beside), crs_from_wkt(&unshifted)); + // EPSG:3857's own text, named Pseudo-Mercator, stays Web Mercator without its extension + // too, as PROJ 9.9.0 reads it (`CRS.from_wkt(text).to_epsg()` is 3857, 60°E 45°N above). + let pseudo = r#"PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","3857"]]"#; + assert_eq!(read_wkt(pseudo), Ok((Crs::WebMercator, None))); + // Beside its code it is read either way as Web Mercator, without a note. + let beside = read_wkt_over_code(pseudo, 3857).unwrap_or(Ok((Crs::WebMercator, None))); + assert_eq!(beside, Ok((Crs::WebMercator, None))); for text in [without_proj4_extension(google).unwrap(), shifted, unshifted] { assert_ne!(crs_from_wkt(&text), Ok(Crs::WebMercator), "{text}"); let ll = crs_from_wkt(&text)