Skip to content

Commit

Permalink
Merge pull request #55 from developmentseed/feat/proj-wkt2
Browse files Browse the repository at this point in the history
Add wkt2 proj info if available
  • Loading branch information
vincentsarago authored Dec 8, 2023
2 parents 4523242 + 9d76e06 commit c48beda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## 0.9.0 (2023-12-08)

* add `wkt2` representation of the dataset CRS if available (author @emileten, https://github.com/developmentseed/rio-stac/pull/55)

## 0.8.1 (2023-09-27)

* update `tests` requirements
Expand Down
34 changes: 28 additions & 6 deletions rio_stac/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,37 @@ def get_projection_info(
) -> Dict:
"""Get projection metadata.
see: https://github.com/stac-extensions/projection/#item-properties-or-asset-fields
The STAC projection extension allows for three different ways to describe the coordinate reference system
associated with a raster :
- EPSG code
- WKT2
- PROJJSON
All are optional, and they can be provided altogether as well. Therefore, as long as one can be obtained from
the data, we add it to the returned dictionary.
see: https://github.com/stac-extensions/projection
"""
projjson = None
wkt2 = None
epsg = None
if src_dst.crs is not None:
# EPSG
epsg = src_dst.crs.to_epsg() if src_dst.crs.is_epsg_code else None

# PROJJSON
try:
projjson = src_dst.crs.to_dict(projjson=True)
except (AttributeError, TypeError):
except (AttributeError, TypeError) as ex:
warnings.warn(f"Could not get PROJJSON from dataset : {ex}")
pass

# WKT2
try:
wkt2 = src_dst.crs.to_wkt()
except Exception as ex:
warnings.warn(f"Could not get WKT2 from dataset : {ex}")
pass

meta = {
Expand All @@ -106,12 +127,13 @@ def get_projection_info(
"shape": [src_dst.height, src_dst.width],
"transform": list(src_dst.transform),
}

if projjson is not None:
meta["projjson"] = projjson
# rasterio can't reproduce wkt2
# ref: https://github.com/mapbox/rasterio/issues/2044
# if epsg is None:
# meta["wkt2"] = src_dst.crs.to_wkt()

if wkt2 is not None:
meta["wkt2"] = wkt2

return meta


Expand Down
2 changes: 2 additions & 0 deletions tests/test_create_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def test_create_item_options():
]
assert "datetime" in item_dict["properties"]
assert "proj:epsg" in item_dict["properties"]
assert "proj:wkt2" in item_dict["properties"]
assert "proj:projjson" in item_dict["properties"]
assert "sci:citation" in item_dict["properties"]

# external assets
Expand Down

0 comments on commit c48beda

Please sign in to comment.