Skip to content

Commit

Permalink
sqme: Include un-parsed geotiff tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill888 committed Aug 25, 2023
1 parent 2c768e3 commit 235ae4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 11 additions & 2 deletions odc/geo/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from .geom import Geometry, point
from .types import XY, xy_

GEOTIFF_TAGS = {34735, 34737, 33920, 33550, 33922, 34264, 50844}


def from_geopandas(series) -> List[Geometry]:
"""
Expand Down Expand Up @@ -99,7 +101,9 @@ def rio_geobox(rdr: Any) -> Union[GeoBox, GCPGeoBox]:
return GeoBox.from_rio(rdr)


def geotiff_metadata(geobox: GeoBox) -> Dict[str, Any]:
def geotiff_metadata(
geobox: GeoBox,
) -> Tuple[List[Tuple[int, int, int, Any]], Dict[str, Any]]:
"""
Convert GeoBox to geotiff metadata for :py:mod:`tifffile`.
Expand All @@ -119,5 +123,10 @@ def geotiff_metadata(geobox: GeoBox) -> Dict[str, Any]:
buf = to_cog(xr_zeros(geobox[:2, :2]), compress=None, overview_levels=[])
tf = TiffFile(BytesIO(buf), mode="r")
assert tf.geotiff_metadata is not None
geo_tags = [
(t.code, t.dtype.value, t.count, t.value)
for t in tf.pages.first.tags.values()
if t.code in GEOTIFF_TAGS
]

return tf.geotiff_metadata
return geo_tags, tf.geotiff_metadata
17 changes: 15 additions & 2 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
gpd = pytest.importorskip("geopandas")
gpd_datasets = pytest.importorskip("geopandas.datasets")

import odc.geo.converters
from odc.geo._interop import have
from odc.geo.converters import (
GEOTIFF_TAGS,
extract_gcps,
from_geopandas,
geotiff_metadata,
Expand Down Expand Up @@ -112,8 +112,21 @@ def test_map_crs():
def test_geotiff_metadata(gbox: GeoBox):
assert gbox.crs is not None

md = geotiff_metadata(gbox)
geo_tags, md = geotiff_metadata(gbox)
assert isinstance(md, dict)
assert isinstance(geo_tags, list)
assert len(geo_tags) >= 2
for code, dtype, count, val in geo_tags:
assert code in GEOTIFF_TAGS
assert isinstance(dtype, int)
assert isinstance(count, int)
if count > 0:
assert isinstance(val, (tuple, str))
if isinstance(val, str):
assert len(val) + 1 == count
else:
assert len(val) == count

if gbox.axis_aligned:
assert "ModelPixelScale" in md
else:
Expand Down

0 comments on commit 235ae4c

Please sign in to comment.