Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise exception for missing tileDataGeometry #153

Merged
merged 3 commits into from
Jan 4, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased] - TBD

### Fixed

- if tileinfo metadata is missing tileDataGeometry field, throw a ValueError with a meaningful
message instead of an unintentional KeyError

### Changed

- use reproject_shape instead of reproject_geom (deprecated)
Expand Down
12 changes: 8 additions & 4 deletions src/stactools/sentinel2/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def create_item(
read_href_modifier: Optional[ReadHrefModifier] = None,
asset_href_prefix: Optional[str] = None,
) -> pystac.Item:
"""Create a STC Item from a Sentinel 2 granule.
"""Create a STAC Item from a Sentinel 2 granule.

Arguments:
granule_href: The HREF to the granule. This is expected to be a path
Expand All @@ -129,8 +129,6 @@ def create_item(
granule_href, read_href_modifier, tolerance
)

created = now_to_rfc3339_str()

# ensure that we have a valid geometry, fixing any antimeridian issues
shapely_geometry = shapely_shape(antimeridian.fix_shape(metadata.geometry))
geometry = shapely_mapping(make_valid(shapely_geometry))
Expand All @@ -141,7 +139,7 @@ def create_item(
geometry=geometry,
bbox=bbox,
datetime=metadata.datetime,
properties={"created": created},
properties={"created": now_to_rfc3339_str()},
)

# --Common metadata--
Expand Down Expand Up @@ -578,6 +576,12 @@ def metadata_from_granule_metadata(
)
product_metadata = ProductMetadata(f, read_href_modifier)

if not tileinfo_metadata.geometry:
raise ValueError(
f"Metadata does not contain geometry for {granule_metadata_href}. "
"Perhaps there is no data in the scene?"
)

geometry = reproject_shape(
f"epsg:{granule_metadata.epsg}", "epsg:4326", tileinfo_metadata.geometry
).simplify(tolerance)
Expand Down
8 changes: 2 additions & 6 deletions src/stactools/sentinel2/tileinfo_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@
from stactools.sentinel2.constants import TILEINFO_METADATA_ASSET_KEY


class TileInfoMetadataError(Exception):
pass


class TileInfoMetadata:
def __init__(self, href, read_href_modifier: Optional[ReadHrefModifier] = None):
self.href = href
self.tileinfo = json.loads(read_text(self.href, read_href_modifier))

self._datetime = str_to_datetime(self.tileinfo["timestamp"])
self._geometry = self.tileinfo["tileDataGeometry"]
self._bbox = shape(self._geometry).bounds
self._geometry = self.tileinfo.get("tileDataGeometry")
self._bbox = shape(self._geometry).bounds if self._geometry else None
self._product_path = self.tileinfo["productPath"]

@property
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"path" : "tiles/34/L/BP/2022/4/1/0",
"timestamp" : "2022-04-01T09:03:19.283Z",
"utmZone" : 34,
"latitudeBand" : "L",
"gridSquare" : "BP",
"datastrip" : {
"id" : "S2A_OPER_MSI_L2A_DS_VGS1_20220401T110010_S20220401T090142_N04.00",
"path" : "products/2022/4/1/S2A_MSIL2A_20220401T083601_N0400_R064_T34LBQ_20220401T110010/datastrip/0"
},
"tileGeometry" : {
"type" : "Polygon",
"crs" : {
"type" : "name",
"properties" : {
"name" : "urn:ogc:def:crs:EPSG:8.8.1:32734"
}
},
"coordinates" : [ [ [ 199980.0, 8900020.0 ], [ 309780.0, 8900020.0 ], [ 309780.0, 8790220.0 ], [ 199980.0, 8790220.0 ], [ 199980.0, 8900020.0 ] ] ]
},
"tileOrigin" : {
"type" : "Point",
"crs" : {
"type" : "name",
"properties" : {
"name" : "urn:ogc:def:crs:EPSG:8.8.1:32734"
}
},
"coordinates" : [ 199980.0, 8900020.0 ]
},
"dataCoveragePercentage" : 2.85,
"cloudyPixelPercentage" : 0.0,
"productName" : "S2A_MSIL2A_20220401T083601_N0400_R064_T34LBQ_20220401T110010",
"productPath" : "products/2022/4/1/S2A_MSIL2A_20220401T083601_N0400_R064_T34LBQ_20220401T110010"
}
10 changes: 10 additions & 0 deletions tests/test_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def test_product_metadata_asset() -> None:
assert "product_metadata" in item.assets


def test_raises_for_missing_tileDataGeometry() -> None:
file_name = (
"S2A_OPER_MSI_L2A_TL_VGS1_20220401T110010_A035382_T34LBQ-no-tileDataGeometry"
)
path = test_data.get_path(f"data-files/{file_name}")
with pytest.raises(ValueError):
with pytest.warns(FixWindingWarning):
stac.create_item(path)


def test_antimeridian() -> None:
path = test_data.get_path(
"data-files/S2A_MSIL2A_20230821T221941_N0509_R029_T01KAB_20230822T021825.SAFE"
Expand Down