Skip to content

Commit 9ac9c7b

Browse files
kylebarronclaude
andcommitted
feat: Expose GeogTOWGS84GeoKey and ProjRectifiedGridAngleGeoKey
GDAL writes GeogTOWGS84GeoKey (2062) for any CRS with +towgs84 and ProjRectifiedGridAngleGeoKey (3096) for every Hotine Oblique Mercator file, but GeoKeyDirectory skipped both as unknown keys, so a GeoKey to CRS builder could not recover the datum shift or the grid angle. Add geog_towgs84 (3 or 7 doubles) and proj_rectified_grid_angle to the Rust struct and the Python class, following the existing pattern. Closes #367 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 1867a7f commit 9ac9c7b

5 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎python/python/async_tiff/_geo.pyi‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class GeoKeyDirectory:
4545
@property
4646
def geog_prime_meridian_long(self) -> float | None: ...
4747
@property
48+
def geog_towgs84(self) -> list[float] | None:
49+
"""Datum shift parameters towards WGS 84 (GeogTOWGS84GeoKey, 2062).
50+
51+
Three translations, or seven values (translations, rotations, scale
52+
difference), as written by GDAL for `+towgs84=`.
53+
"""
54+
@property
4855
def projected_type(self) -> int | None: ...
4956
@property
5057
def proj_citation(self) -> str | None: ...
@@ -93,6 +100,13 @@ class GeoKeyDirectory:
93100
@property
94101
def proj_straight_vert_pole_long(self) -> float | None: ...
95102
@property
103+
def proj_rectified_grid_angle(self) -> float | None:
104+
"""Angle from the rectified grid to the skew grid (ProjRectifiedGridAngleGeoKey, 3096).
105+
106+
Used by the Hotine Oblique Mercator methods; libgeotiff defaults it to 90
107+
when absent.
108+
"""
109+
@property
96110
def vertical(self) -> int | None: ...
97111
@property
98112
def vertical_citation(self) -> str | None: ...

‎python/src/geo.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub(crate) struct PyGeoKeyDirectory {
2222
geog_inv_flattening: Option<f64>,
2323
geog_azimuth_units: Option<u16>,
2424
geog_prime_meridian_long: Option<f64>,
25+
geog_towgs84: Option<Vec<f64>>,
2526

2627
projected_type: Option<u16>,
2728
proj_citation: Option<String>,
@@ -47,6 +48,7 @@ pub(crate) struct PyGeoKeyDirectory {
4748
proj_scale_at_center: Option<f64>,
4849
proj_azimuth_angle: Option<f64>,
4950
proj_straight_vert_pole_long: Option<f64>,
51+
proj_rectified_grid_angle: Option<f64>,
5052

5153
vertical: Option<u16>,
5254
vertical_citation: Option<String>,
@@ -110,6 +112,9 @@ impl PyGeoKeyDirectory {
110112
if self.geog_prime_meridian_long.is_some() {
111113
keys.push("geog_prime_meridian_long");
112114
}
115+
if self.geog_towgs84.is_some() {
116+
keys.push("geog_towgs84");
117+
}
113118
if self.projected_type.is_some() {
114119
keys.push("projected_type");
115120
}
@@ -182,6 +187,9 @@ impl PyGeoKeyDirectory {
182187
if self.proj_straight_vert_pole_long.is_some() {
183188
keys.push("proj_straight_vert_pole_long");
184189
}
190+
if self.proj_rectified_grid_angle.is_some() {
191+
keys.push("proj_rectified_grid_angle");
192+
}
185193
if self.vertical.is_some() {
186194
keys.push("vertical");
187195
}
@@ -224,6 +232,7 @@ impl PyGeoKeyDirectory {
224232
"geog_inv_flattening" => self.geog_inv_flattening.into_bound_py_any(py),
225233
"geog_azimuth_units" => self.geog_azimuth_units.into_bound_py_any(py),
226234
"geog_prime_meridian_long" => self.geog_prime_meridian_long.into_bound_py_any(py),
235+
"geog_towgs84" => self.geog_towgs84.clone().into_bound_py_any(py),
227236
"projected_type" => self.projected_type.into_bound_py_any(py),
228237
"proj_citation" => self.proj_citation.as_ref().into_bound_py_any(py),
229238
"projection" => self.projection.into_bound_py_any(py),
@@ -250,6 +259,7 @@ impl PyGeoKeyDirectory {
250259
"proj_straight_vert_pole_long" => {
251260
self.proj_straight_vert_pole_long.into_bound_py_any(py)
252261
}
262+
"proj_rectified_grid_angle" => self.proj_rectified_grid_angle.into_bound_py_any(py),
253263
"vertical" => self.vertical.into_bound_py_any(py),
254264
"vertical_citation" => self.vertical_citation.as_ref().into_bound_py_any(py),
255265
"vertical_datum" => self.vertical_datum.into_bound_py_any(py),
@@ -282,6 +292,7 @@ impl From<PyGeoKeyDirectory> for GeoKeyDirectory {
282292
geog_inv_flattening: value.geog_inv_flattening,
283293
geog_azimuth_units: value.geog_azimuth_units,
284294
geog_prime_meridian_long: value.geog_prime_meridian_long,
295+
geog_towgs84: value.geog_towgs84,
285296
projected_type: value.projected_type,
286297
proj_citation: value.proj_citation,
287298
projection: value.projection,
@@ -306,6 +317,7 @@ impl From<PyGeoKeyDirectory> for GeoKeyDirectory {
306317
proj_scale_at_center: value.proj_scale_at_center,
307318
proj_azimuth_angle: value.proj_azimuth_angle,
308319
proj_straight_vert_pole_long: value.proj_straight_vert_pole_long,
320+
proj_rectified_grid_angle: value.proj_rectified_grid_angle,
309321
vertical: value.vertical,
310322
vertical_citation: value.vertical_citation,
311323
vertical_datum: value.vertical_datum,
@@ -334,6 +346,7 @@ impl From<GeoKeyDirectory> for PyGeoKeyDirectory {
334346
geog_inv_flattening: value.geog_inv_flattening,
335347
geog_azimuth_units: value.geog_azimuth_units,
336348
geog_prime_meridian_long: value.geog_prime_meridian_long,
349+
geog_towgs84: value.geog_towgs84,
337350
projected_type: value.projected_type,
338351
proj_citation: value.proj_citation,
339352
projection: value.projection,
@@ -358,6 +371,7 @@ impl From<GeoKeyDirectory> for PyGeoKeyDirectory {
358371
proj_scale_at_center: value.proj_scale_at_center,
359372
proj_azimuth_angle: value.proj_azimuth_angle,
360373
proj_straight_vert_pole_long: value.proj_straight_vert_pole_long,
374+
proj_rectified_grid_angle: value.proj_rectified_grid_angle,
361375
vertical: value.vertical,
362376
vertical_citation: value.vertical_citation,
363377
vertical_datum: value.vertical_datum,

‎python/tests/test_ifd.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ async def test_ifd_dict():
4040
"geog_angular_units": 9102,
4141
"geog_semi_major_axis": 6378137.0,
4242
"geog_inv_flattening": 298.257222101004,
43+
"geog_towgs84": [0.0, 0.0, 0.0],
4344
}
4445
assert dict(gkd) == expected_gkd

‎src/geo/geo_key_directory.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub(crate) enum GeoKeyTag {
3232
GeogInvFlattening = 2059,
3333
GeogAzimuthUnits = 2060,
3434
GeogPrimeMeridianLong = 2061,
35+
GeogTOWGS84 = 2062,
3536

3637
// Projected CRS Parameter Keys
3738
ProjectedType = 3072,
@@ -58,6 +59,7 @@ pub(crate) enum GeoKeyTag {
5859
ProjScaleAtCenter = 3093,
5960
ProjAzimuthAngle = 3094,
6061
ProjStraightVertPoleLong = 3095,
62+
ProjRectifiedGridAngle = 3096,
6163

6264
// Vertical CRS Parameter Keys (4096-5119)
6365
Vertical = 4096,
@@ -102,6 +104,11 @@ pub struct GeoKeyDirectory {
102104
/// this is Greenwich).
103105
pub geog_prime_meridian_long: Option<f64>,
104106

107+
/// Datum shift parameters towards WGS 84, as written by GDAL for `+towgs84=`: three
108+
/// translations, or seven values (translations, rotations, scale difference).
109+
/// <https://docs.ogc.org/is/19-008r4/19-008r4.html#_requirements_class_geogtowgs84geokey>
110+
pub geog_towgs84: Option<Vec<f64>>,
111+
105112
pub projected_type: Option<u16>,
106113
pub proj_citation: Option<String>,
107114
pub projection: Option<u16>,
@@ -127,6 +134,10 @@ pub struct GeoKeyDirectory {
127134
pub proj_azimuth_angle: Option<f64>,
128135
pub proj_straight_vert_pole_long: Option<f64>,
129136

137+
/// Angle from the rectified grid to the skew (oblique) grid, used by the Hotine Oblique
138+
/// Mercator methods. libgeotiff defaults it to 90 when absent.
139+
pub proj_rectified_grid_angle: Option<f64>,
140+
130141
pub vertical: Option<u16>,
131142
pub vertical_citation: Option<String>,
132143
pub vertical_datum: Option<u16>,
@@ -154,6 +165,7 @@ impl GeoKeyDirectory {
154165
let mut geog_inv_flattening = None;
155166
let mut geog_azimuth_units = None;
156167
let mut geog_prime_meridian_long = None;
168+
let mut geog_towgs84 = None;
157169

158170
let mut projected_type = None;
159171
let mut proj_citation = None;
@@ -179,6 +191,7 @@ impl GeoKeyDirectory {
179191
let mut proj_scale_at_center = None;
180192
let mut proj_azimuth_angle = None;
181193
let mut proj_straight_vert_pole_long = None;
194+
let mut proj_rectified_grid_angle = None;
182195

183196
let mut vertical = None;
184197
let mut vertical_citation = None;
@@ -206,6 +219,7 @@ impl GeoKeyDirectory {
206219
GeoKeyTag::GeogPrimeMeridianLong => {
207220
geog_prime_meridian_long = Some(value.into_f64()?)
208221
}
222+
GeoKeyTag::GeogTOWGS84 => geog_towgs84 = Some(value.into_f64_vec()?),
209223
GeoKeyTag::ProjectedType => projected_type = Some(value.into_u16()?),
210224
GeoKeyTag::ProjCitation => proj_citation = Some(value.into_string()?),
211225
GeoKeyTag::Projection => projection = Some(value.into_u16()?),
@@ -238,6 +252,9 @@ impl GeoKeyDirectory {
238252
GeoKeyTag::ProjStraightVertPoleLong => {
239253
proj_straight_vert_pole_long = Some(value.into_f64()?)
240254
}
255+
GeoKeyTag::ProjRectifiedGridAngle => {
256+
proj_rectified_grid_angle = Some(value.into_f64()?)
257+
}
241258
GeoKeyTag::Vertical => vertical = Some(value.into_u16()?),
242259
GeoKeyTag::VerticalCitation => vertical_citation = Some(value.into_string()?),
243260
GeoKeyTag::VerticalDatum => vertical_datum = Some(value.into_u16()?),
@@ -265,6 +282,7 @@ impl GeoKeyDirectory {
265282
geog_inv_flattening,
266283
geog_azimuth_units,
267284
geog_prime_meridian_long,
285+
geog_towgs84,
268286

269287
projected_type,
270288
proj_citation,
@@ -290,6 +308,7 @@ impl GeoKeyDirectory {
290308
proj_scale_at_center,
291309
proj_azimuth_angle,
292310
proj_straight_vert_pole_long,
311+
proj_rectified_grid_angle,
293312

294313
vertical,
295314
vertical_citation,

‎src/test/geo.rs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
use std::collections::HashMap;
12
use std::path::PathBuf;
23
use std::sync::Arc;
34

45
use object_store::local::LocalFileSystem;
56

7+
use crate::geo::{GeoKeyDirectory, GeoKeyTag};
68
use crate::metadata::cache::ReadaheadMetadataCache;
79
use crate::metadata::TiffMetadataReader;
810
use crate::reader::{AsyncFileReader, ObjectReader};
11+
use crate::tag_value::TagValue;
12+
13+
#[test]
14+
fn test_towgs84_and_rectified_grid_angle_keys() {
15+
let towgs84 = vec![598.1, 73.7, 418.2, 0.202, 0.045, -2.455, 6.7];
16+
let mut tags = HashMap::new();
17+
tags.insert(GeoKeyTag::ModelType, TagValue::Short(1));
18+
tags.insert(
19+
GeoKeyTag::GeogTOWGS84,
20+
TagValue::List(towgs84.iter().copied().map(TagValue::Double).collect()),
21+
);
22+
tags.insert(
23+
GeoKeyTag::ProjRectifiedGridAngle,
24+
TagValue::Double(323.130102361111),
25+
);
26+
27+
let gkd = GeoKeyDirectory::from_tags(tags).unwrap();
28+
assert_eq!(gkd.geog_towgs84, Some(towgs84));
29+
assert_eq!(gkd.proj_rectified_grid_angle, Some(323.130102361111));
30+
}
931

1032
#[tokio::test]
1133
async fn test_parse_file_with_unknown_geokey() {
@@ -19,8 +41,12 @@ async fn test_parse_file_with_unknown_geokey() {
1941
let mut metadata_reader = TiffMetadataReader::try_open(&prefetch_reader)
2042
.await
2143
.unwrap();
22-
let _ = metadata_reader
44+
let ifds = metadata_reader
2345
.read_all_ifds(&prefetch_reader)
2446
.await
2547
.unwrap();
48+
49+
// This file carries a three-element GeogTOWGS84GeoKey (2062).
50+
let gkd = ifds[0].geo_key_directory().unwrap();
51+
assert_eq!(gkd.geog_towgs84, Some(vec![0.0, 0.0, 0.0]));
2652
}

0 commit comments

Comments
 (0)