Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions python/python/async_tiff/_geo.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down Expand Up @@ -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: ...
Expand Down
14 changes: 14 additions & 0 deletions python/src/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct PyGeoKeyDirectory {
geog_inv_flattening: Option<f64>,
geog_azimuth_units: Option<u16>,
geog_prime_meridian_long: Option<f64>,
geog_towgs84: Option<Vec<f64>>,

projected_type: Option<u16>,
proj_citation: Option<String>,
Expand All @@ -47,6 +48,7 @@ pub(crate) struct PyGeoKeyDirectory {
proj_scale_at_center: Option<f64>,
proj_azimuth_angle: Option<f64>,
proj_straight_vert_pole_long: Option<f64>,
proj_rectified_grid_angle: Option<f64>,

vertical: Option<u16>,
vertical_citation: Option<String>,
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -282,6 +292,7 @@ impl From<PyGeoKeyDirectory> 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,
Expand All @@ -306,6 +317,7 @@ impl From<PyGeoKeyDirectory> 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,
Expand Down Expand Up @@ -334,6 +346,7 @@ impl From<GeoKeyDirectory> 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,
Expand All @@ -358,6 +371,7 @@ impl From<GeoKeyDirectory> 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,
Expand Down
1 change: 1 addition & 0 deletions python/tests/test_ifd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions src/geo/geo_key_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub(crate) enum GeoKeyTag {
GeogInvFlattening = 2059,
GeogAzimuthUnits = 2060,
GeogPrimeMeridianLong = 2061,
GeogTOWGS84 = 2062,

// Projected CRS Parameter Keys
ProjectedType = 3072,
Expand All @@ -58,6 +59,7 @@ pub(crate) enum GeoKeyTag {
ProjScaleAtCenter = 3093,
ProjAzimuthAngle = 3094,
ProjStraightVertPoleLong = 3095,
ProjRectifiedGridAngle = 3096,

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

/// Datum shift parameters towards WGS 84, as written by GDAL for `+towgs84=`: three
/// translations, or seven values (translations, rotations, scale difference).
/// <https://docs.ogc.org/is/19-008r4/19-008r4.html#_requirements_class_geogtowgs84geokey>
pub geog_towgs84: Option<Vec<f64>>,

pub projected_type: Option<u16>,
pub proj_citation: Option<String>,
pub projection: Option<u16>,
Expand All @@ -127,6 +134,10 @@ pub struct GeoKeyDirectory {
pub proj_azimuth_angle: Option<f64>,
pub proj_straight_vert_pole_long: Option<f64>,

/// 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<f64>,

pub vertical: Option<u16>,
pub vertical_citation: Option<String>,
pub vertical_datum: Option<u16>,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()?),
Expand Down Expand Up @@ -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()?),
Expand Down Expand Up @@ -265,6 +282,7 @@ impl GeoKeyDirectory {
geog_inv_flattening,
geog_azimuth_units,
geog_prime_meridian_long,
geog_towgs84,

projected_type,
proj_citation,
Expand All @@ -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,
Expand Down
28 changes: 27 additions & 1 deletion src/test/geo.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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]));
}
Loading