Skip to content

Commit

Permalink
produce an error on bad geometries that are larger than reasonable
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Varner committed Jan 26, 2024
1 parent 2b1a0e1 commit d05298e
Show file tree
Hide file tree
Showing 4 changed files with 5,767 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/stactools/sentinel2/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,23 @@ def create_item(

# 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))
geometry = make_valid(shapely_geometry)

# sometimes, antimeridian and/or polar crossing scenes on some platforms end up
# with geometries that cover the inverse area that they should, so nearly the
# entire globe. This has been seen to have different behaviors on different
# architectures and dependent library versions. To prevent these errors from
# resulting in a wildly-incorrect geometry, we fail here if the geometry
# is unreasonably large. Typical areas will no greater than 3, whereas an
# incorrect globe-covering geometry will have an area for 61110.
if (ga := geometry.area) > 100:
raise Exception(f"Area of geometry is {ga}, which is too large to be correct.")

bbox = [round(v, COORD_ROUNDING) for v in antimeridian.bbox(geometry)]

item = pystac.Item(
id=metadata.scene_id,
geometry=geometry,
geometry=shapely_mapping(geometry),
bbox=bbox,
datetime=metadata.datetime,
properties={"created": now_to_rfc3339_str()},
Expand Down
Loading

0 comments on commit d05298e

Please sign in to comment.