Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 65 additions & 13 deletions crates/data/src/raster/wkt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,13 @@ pub fn read_wkt_over_code(text: &str, code: u32) -> Option<Result<(Crs, Option<S
}
};
let files_shift = towgs84(geogcs(&root).and_then(|g| g.child("DATUM")));
// A shift that can't be read is said, so the text is read (by the code) to say it.
if !unusable_towgs84(&root) && (files_shift.is_none() || Crs::from_epsg(code).is_some_and(|by_code| places_as(text, by_code))) {
// A shift that can't be read is said, so the text is read (by the code) to say it; so is one
// set aside ([`by_code_note`]), and a system read from its parameters is read from them.
let as_code = match Crs::from_epsg(code) {
Some(by_code) => 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))
Expand Down Expand Up @@ -497,8 +502,13 @@ fn read_root(root: &Node, code: Option<u32>, 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)));
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);
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
// code's shift in place of the file's, which is said.
Expand All @@ -514,6 +524,29 @@ fn read_root(root: &Node, code: Option<u32>, 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<Option<String>> {
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
Expand Down Expand Up @@ -4883,17 +4916,23 @@ 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"));
// 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:?}");
}
Expand Down Expand Up @@ -5124,18 +5163,31 @@ 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();
assert!(matches!(crs_from_wkt(&shifted), Ok(Crs::Custom(_))), "{:?}", crs_from_wkt(&shifted));
// 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}");
// 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)));
Comment on lines +5187 to +5188

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion can't fail on the None case. unwrap_or(Ok((Crs::WebMercator, None))) turns None into the expected value, so the test passes whether read_wkt_over_code reads the text (Some(Ok(..))) or defers to the code (None). That is the intended outcome, but the test should say it. Asserting it directly also makes a regression to Some(Err(..)) show up with its message.

Suggested change
let beside = read_wkt_over_code(pseudo, 3857).unwrap_or(Ok((Crs::WebMercator, None)));
assert_eq!(beside, Ok((Crs::WebMercator, None)));
let beside = read_wkt_over_code(pseudo, 3857);
assert!(beside.clone().is_none_or(|r| r == Ok((Crs::WebMercator, None))), "{beside:?}");

Category: quality

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged already, so no follow-up for this: None is meant to pass. It means the caller uses the code, and 3857 is Web Mercator, so either outcome reads the text as Web Mercator with no note, which is what the test checks. A wrong reading (Some(Ok(custom)) or a note) still fails it.

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()
Expand Down
Loading