diff --git a/python/python/async_tiff/_geo.pyi b/python/python/async_tiff/_geo.pyi index 8124a4e6..02779da4 100644 --- a/python/python/async_tiff/_geo.pyi +++ b/python/python/async_tiff/_geo.pyi @@ -45,6 +45,13 @@ class GeoKeyDirectory: @property def geog_prime_meridian_long(self) -> float | None: ... @property + def geog_towgs84(self) -> list[float] | None: + """Datum shift parameters towards WGS 84 (GeogTOWGS84GeoKey, 2062). + + Three translations, or seven values (translations, rotations, scale + difference), as written by GDAL for `+towgs84=`. + """ + @property def projected_type(self) -> int | None: ... @property def proj_citation(self) -> str | None: ... @@ -93,6 +100,13 @@ class GeoKeyDirectory: @property def proj_straight_vert_pole_long(self) -> float | None: ... @property + def proj_rectified_grid_angle(self) -> float | None: + """Angle from the rectified grid to the skew grid (ProjRectifiedGridAngleGeoKey, 3096). + + Used by the Hotine Oblique Mercator methods; libgeotiff defaults it to 90 + when absent. + """ + @property def vertical(self) -> int | None: ... @property def vertical_citation(self) -> str | None: ... diff --git a/python/src/geo.rs b/python/src/geo.rs index cef3cdd0..82af0a0e 100644 --- a/python/src/geo.rs +++ b/python/src/geo.rs @@ -22,6 +22,7 @@ pub(crate) struct PyGeoKeyDirectory { geog_inv_flattening: Option, geog_azimuth_units: Option, geog_prime_meridian_long: Option, + geog_towgs84: Option>, projected_type: Option, proj_citation: Option, @@ -47,6 +48,7 @@ pub(crate) struct PyGeoKeyDirectory { proj_scale_at_center: Option, proj_azimuth_angle: Option, proj_straight_vert_pole_long: Option, + proj_rectified_grid_angle: Option, vertical: Option, vertical_citation: Option, @@ -110,6 +112,9 @@ impl PyGeoKeyDirectory { if self.geog_prime_meridian_long.is_some() { keys.push("geog_prime_meridian_long"); } + if self.geog_towgs84.is_some() { + keys.push("geog_towgs84"); + } if self.projected_type.is_some() { keys.push("projected_type"); } @@ -182,6 +187,9 @@ impl PyGeoKeyDirectory { if self.proj_straight_vert_pole_long.is_some() { keys.push("proj_straight_vert_pole_long"); } + if self.proj_rectified_grid_angle.is_some() { + keys.push("proj_rectified_grid_angle"); + } if self.vertical.is_some() { keys.push("vertical"); } @@ -224,6 +232,7 @@ impl PyGeoKeyDirectory { "geog_inv_flattening" => self.geog_inv_flattening.into_bound_py_any(py), "geog_azimuth_units" => self.geog_azimuth_units.into_bound_py_any(py), "geog_prime_meridian_long" => self.geog_prime_meridian_long.into_bound_py_any(py), + "geog_towgs84" => self.geog_towgs84.clone().into_bound_py_any(py), "projected_type" => self.projected_type.into_bound_py_any(py), "proj_citation" => self.proj_citation.as_ref().into_bound_py_any(py), "projection" => self.projection.into_bound_py_any(py), @@ -250,6 +259,7 @@ impl PyGeoKeyDirectory { "proj_straight_vert_pole_long" => { self.proj_straight_vert_pole_long.into_bound_py_any(py) } + "proj_rectified_grid_angle" => self.proj_rectified_grid_angle.into_bound_py_any(py), "vertical" => self.vertical.into_bound_py_any(py), "vertical_citation" => self.vertical_citation.as_ref().into_bound_py_any(py), "vertical_datum" => self.vertical_datum.into_bound_py_any(py), @@ -282,6 +292,7 @@ impl From for GeoKeyDirectory { geog_inv_flattening: value.geog_inv_flattening, geog_azimuth_units: value.geog_azimuth_units, geog_prime_meridian_long: value.geog_prime_meridian_long, + geog_towgs84: value.geog_towgs84, projected_type: value.projected_type, proj_citation: value.proj_citation, projection: value.projection, @@ -306,6 +317,7 @@ impl From for GeoKeyDirectory { proj_scale_at_center: value.proj_scale_at_center, proj_azimuth_angle: value.proj_azimuth_angle, proj_straight_vert_pole_long: value.proj_straight_vert_pole_long, + proj_rectified_grid_angle: value.proj_rectified_grid_angle, vertical: value.vertical, vertical_citation: value.vertical_citation, vertical_datum: value.vertical_datum, @@ -334,6 +346,7 @@ impl From for PyGeoKeyDirectory { geog_inv_flattening: value.geog_inv_flattening, geog_azimuth_units: value.geog_azimuth_units, geog_prime_meridian_long: value.geog_prime_meridian_long, + geog_towgs84: value.geog_towgs84, projected_type: value.projected_type, proj_citation: value.proj_citation, projection: value.projection, @@ -358,6 +371,7 @@ impl From for PyGeoKeyDirectory { proj_scale_at_center: value.proj_scale_at_center, proj_azimuth_angle: value.proj_azimuth_angle, proj_straight_vert_pole_long: value.proj_straight_vert_pole_long, + proj_rectified_grid_angle: value.proj_rectified_grid_angle, vertical: value.vertical, vertical_citation: value.vertical_citation, vertical_datum: value.vertical_datum, diff --git a/python/tests/test_ifd.py b/python/tests/test_ifd.py index b7f357a8..336e8085 100644 --- a/python/tests/test_ifd.py +++ b/python/tests/test_ifd.py @@ -40,5 +40,6 @@ async def test_ifd_dict(): "geog_angular_units": 9102, "geog_semi_major_axis": 6378137.0, "geog_inv_flattening": 298.257222101004, + "geog_towgs84": [0.0, 0.0, 0.0], } assert dict(gkd) == expected_gkd diff --git a/src/geo/geo_key_directory.rs b/src/geo/geo_key_directory.rs index 442530d1..409590da 100644 --- a/src/geo/geo_key_directory.rs +++ b/src/geo/geo_key_directory.rs @@ -32,6 +32,7 @@ pub(crate) enum GeoKeyTag { GeogInvFlattening = 2059, GeogAzimuthUnits = 2060, GeogPrimeMeridianLong = 2061, + GeogTOWGS84 = 2062, // Projected CRS Parameter Keys ProjectedType = 3072, @@ -58,6 +59,7 @@ pub(crate) enum GeoKeyTag { ProjScaleAtCenter = 3093, ProjAzimuthAngle = 3094, ProjStraightVertPoleLong = 3095, + ProjRectifiedGridAngle = 3096, // Vertical CRS Parameter Keys (4096-5119) Vertical = 4096, @@ -102,6 +104,11 @@ pub struct GeoKeyDirectory { /// this is Greenwich). pub geog_prime_meridian_long: Option, + /// Datum shift parameters towards WGS 84, as written by GDAL for `+towgs84=`: three + /// translations, or seven values (translations, rotations, scale difference). + /// + pub geog_towgs84: Option>, + pub projected_type: Option, pub proj_citation: Option, pub projection: Option, @@ -127,6 +134,10 @@ pub struct GeoKeyDirectory { pub proj_azimuth_angle: Option, pub proj_straight_vert_pole_long: Option, + /// Angle from the rectified grid to the skew (oblique) grid, used by the Hotine Oblique + /// Mercator methods. libgeotiff defaults it to 90 when absent. + pub proj_rectified_grid_angle: Option, + pub vertical: Option, pub vertical_citation: Option, pub vertical_datum: Option, @@ -154,6 +165,7 @@ impl GeoKeyDirectory { let mut geog_inv_flattening = None; let mut geog_azimuth_units = None; let mut geog_prime_meridian_long = None; + let mut geog_towgs84 = None; let mut projected_type = None; let mut proj_citation = None; @@ -179,6 +191,7 @@ impl GeoKeyDirectory { let mut proj_scale_at_center = None; let mut proj_azimuth_angle = None; let mut proj_straight_vert_pole_long = None; + let mut proj_rectified_grid_angle = None; let mut vertical = None; let mut vertical_citation = None; @@ -206,6 +219,7 @@ impl GeoKeyDirectory { GeoKeyTag::GeogPrimeMeridianLong => { geog_prime_meridian_long = Some(value.into_f64()?) } + GeoKeyTag::GeogTOWGS84 => geog_towgs84 = Some(value.into_f64_vec()?), GeoKeyTag::ProjectedType => projected_type = Some(value.into_u16()?), GeoKeyTag::ProjCitation => proj_citation = Some(value.into_string()?), GeoKeyTag::Projection => projection = Some(value.into_u16()?), @@ -238,6 +252,9 @@ impl GeoKeyDirectory { GeoKeyTag::ProjStraightVertPoleLong => { proj_straight_vert_pole_long = Some(value.into_f64()?) } + GeoKeyTag::ProjRectifiedGridAngle => { + proj_rectified_grid_angle = Some(value.into_f64()?) + } GeoKeyTag::Vertical => vertical = Some(value.into_u16()?), GeoKeyTag::VerticalCitation => vertical_citation = Some(value.into_string()?), GeoKeyTag::VerticalDatum => vertical_datum = Some(value.into_u16()?), @@ -265,6 +282,7 @@ impl GeoKeyDirectory { geog_inv_flattening, geog_azimuth_units, geog_prime_meridian_long, + geog_towgs84, projected_type, proj_citation, @@ -290,6 +308,7 @@ impl GeoKeyDirectory { proj_scale_at_center, proj_azimuth_angle, proj_straight_vert_pole_long, + proj_rectified_grid_angle, vertical, vertical_citation, diff --git a/src/test/geo.rs b/src/test/geo.rs index a3c357fe..0b88721b 100644 --- a/src/test/geo.rs +++ b/src/test/geo.rs @@ -1,11 +1,33 @@ +use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; use object_store::local::LocalFileSystem; +use crate::geo::{GeoKeyDirectory, GeoKeyTag}; use crate::metadata::cache::ReadaheadMetadataCache; use crate::metadata::TiffMetadataReader; use crate::reader::{AsyncFileReader, ObjectReader}; +use crate::tag_value::TagValue; + +#[test] +fn test_towgs84_and_rectified_grid_angle_keys() { + let towgs84 = vec![598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.7]; + let mut tags = HashMap::new(); + tags.insert(GeoKeyTag::ModelType, TagValue::Short(1)); + tags.insert( + GeoKeyTag::GeogTOWGS84, + TagValue::List(towgs84.iter().copied().map(TagValue::Double).collect()), + ); + tags.insert( + GeoKeyTag::ProjRectifiedGridAngle, + TagValue::Double(323.130102361111), + ); + + let gkd = GeoKeyDirectory::from_tags(tags).unwrap(); + assert_eq!(gkd.geog_towgs84, Some(towgs84)); + assert_eq!(gkd.proj_rectified_grid_angle, Some(323.130102361111)); +} #[tokio::test] async fn test_parse_file_with_unknown_geokey() { @@ -19,8 +41,12 @@ async fn test_parse_file_with_unknown_geokey() { let mut metadata_reader = TiffMetadataReader::try_open(&prefetch_reader) .await .unwrap(); - let _ = metadata_reader + let ifds = metadata_reader .read_all_ifds(&prefetch_reader) .await .unwrap(); + + // This file carries a three-element GeogTOWGS84GeoKey (2062). + let gkd = ifds[0].geo_key_directory().unwrap(); + assert_eq!(gkd.geog_towgs84, Some(vec![0.0, 0.0, 0.0])); }