stactools-packages / sentinel2

stactools package for Sentinel-2
Other
16 stars 5 forks source link

Shapely make_valid() may return a GeometryCollection #166

Open StijnCaerts opened 4 months ago

StijnCaerts commented 4 months ago

shapely.validation.make_valid() may return a GeometryCollection, which is not compatible with antimeridian.bbox. This is for example the case when building the STAC metadata for S2A_MSIL1C_20210123T152641_N0500_R025_T18NWM_20230521T234122.SAFE.

Fixing the original geometry POLYGON ((-74.00787 6.104838, -74.00748 6.332093, -75.00018 6.333042, -75.00018 6.321833, -74.83688 6.289793, -74.836975 6.289361, -74.837006 6.28925, -74.83478 6.288812, -74.83481 6.288708, -74.83545 6.285831, -74.61459 6.239622, -74.61401 6.242212, -74.61401 6.242216, -74.61401 6.242214, -74.61398 6.242372, -74.416626 6.199568, -74.41672 6.199167, -74.41678 6.198875, -74.4147 6.198423, -74.41507 6.196748, -74.19534 6.147027, -74.19519 6.147696, -74.194885 6.149019, -74.194885 6.149018, -74.194885 6.149029, -74.194855 6.149247, -74.00787 6.104838)) results in this geometry collection: GEOMETRYCOLLECTION (POLYGON ((-74.61401 6.242212, -74.61459 6.239622, -74.83545 6.285831, -74.83481 6.288708, -74.83478 6.288812, -74.837006 6.28925, -74.836975 6.289361, -74.83688 6.289793, -75.00018 6.321833, -75.00018 6.333042, -74.00748 6.332093, -74.00787 6.104838, -74.194855 6.149247, -74.194885 6.149029, -74.194885 6.149019, -74.19519 6.147696, -74.19534 6.147027, -74.41507 6.196748, -74.4147 6.198423, -74.41678 6.198875, -74.41672 6.199167, -74.416626 6.199568, -74.61398 6.242372, -74.61401 6.242214, -74.61401 6.242212)), MULTILINESTRING ((-74.61401 6.242214, -74.61401 6.242216), (-74.194885 6.149019, -74.194885 6.149018)))

This causes the following error:

$ stac sentinel2 create-item S2A_MSIL1C_20210123T152641_N0500_R025_T18NWM_20230521T234122.SAFE /tmp/

./venv/lib/python3.10/site-packages/antimeridian/_implementation.py:391: FixWindingWarning: The exterior ring of this shape is wound clockwise. Since this is a common error in real-world geometries, this package is reversing the exterior coordinates of the input shape before running its algorithm. If you know that your input shape is correct (i.e. if your data encompasses both poles), pass `fix_winding=False`.
  FixWindingWarning.warn()
Traceback (most recent call last):
  File "./venv/bin/stac", line 8, in <module>
    sys.exit(run_cli())
  File "./venv/lib/python3.10/site-packages/stactools/cli/cli.py", line 37, in run_cli
    cli(prog_name="stac")
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "./venv/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "./venv/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "./venv/lib/python3.10/site-packages/stactools/sentinel2/commands.py", line 57, in create_item_command
    item = create_item(
  File "./venv/lib/python3.10/site-packages/stactools/sentinel2/stac.py", line 147, in create_item
    bbox = [round(v, COORD_ROUNDING) for v in antimeridian.bbox(geometry)]
  File "./venv/lib/python3.10/site-packages/antimeridian/_implementation.py", line 645, in bbox
    raise ValueError(
ValueError: unsupported geom_type for bbox calculation: GeometryCollection

A possible solution would be to extract the Polygon or MultiPolygon geometry object if the result of make_valid() is a GeometryCollection.