Skip to content

fix: restore Polygon cutline schema for Shapely 2#2044

Merged
MJohnson459 merged 3 commits into
OpenDroneMap:masterfrom
MJohnson459:fix-multipolygon
Jul 3, 2026
Merged

fix: restore Polygon cutline schema for Shapely 2#2044
MJohnson459 merged 3 commits into
OpenDroneMap:masterfrom
MJohnson459:fix-multipolygon

Conversation

@MJohnson459

Copy link
Copy Markdown
Contributor

Fix split-merge cutline GPKG writes failing under Shapely 2 + Fiona 1.9+.

This came up with running OATS brighton_split_merge test:

Traceback (most recent call last):
  File "/code/run.py", line 65, in <module>
    retcode = app.execute()
              ^^^^^^^^^^^^^
  File "/code/stages/odm_app.py", line 118, in execute
    raise e
  File "/code/stages/odm_app.py", line 82, in execute
    self.first_stage.run()
  File "/code/opendm/types.py", line 465, in run
    self.next_stage.run(outputs)
  File "/code/opendm/types.py", line 465, in run
    self.next_stage.run(outputs)
  File "/code/opendm/types.py", line 465, in run
    self.next_stage.run(outputs)
  [Previous line repeated 7 more times]
  File "/code/opendm/types.py", line 444, in run
    self.process(self.args, outputs)
  File "/code/stages/odm_orthophoto.py", line 122, in process
    compute_cutline(tree.odm_orthophoto_tif, 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/opendm/cutline.py", line 170, in compute_cutline
    sink.write({
  File "/code/.pixi/envs/prod/lib/python3.12/site-packages/fiona/collection.py", line 551, in write
    self.writerecords([record])
  File "/code/.pixi/envs/prod/lib/python3.12/site-packages/fiona/collection.py", line 541, in writerecords
    self.session.writerecs(records, self)
  File "fiona/ogrext.pyx", line 1660, in fiona.ogrext.WritingSession.writerecs
fiona.errors.GeometryTypeValidationError: Record's geometry type does not match collection schema's geometry type: 'Polygon' != 'MultiPolygon'

The root cause is related to two dependency bumps we did in #1958 - Shapely 2 no longer has __getitem__ for MultiPolygon and so unary_union results were written whole instead of picking the largest polygon. Commit 75a1e1f only fixed one half of that mismatch by changing the schema to MultiPolygon but Fiona 1.9+ still rejects Polygon records against a MultiPolygon schema.

Note #1958 testing originally hit the opposite mismatch (MultiPolygon record vs Polygon schema) before 75a1e1f; this PR fixes both directions by fixing Shapely unpacking and restoring the Polygon schema.

Shapely 2 no longer has `__getitem__` for MultiPolygon, so unary_union
results were written whole instead of picking the largest polygon.
Commit 75a1e1f only fixed one half of that mismatch by changing the
schema to MultiPolygon; Fiona 1.9+ still rejects Polygon records against
a MultiPolygon schema. Use .geoms to select the largest polygon and
declare the GPKG layer as Polygon again.
Comment thread tests/test_cutline.py Outdated

class TestCutline(unittest.TestCase):
def test_largest_polygon_from_single_polygon(self):
p = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, I would add some comments and/or "ASCII" art to visualise what is going on.

Right now the tests are not super clear to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few more comments to explain what was going on 👍

@MJohnson459
MJohnson459 merged commit a7faa11 into OpenDroneMap:master Jul 3, 2026
2 checks passed
@MJohnson459
MJohnson459 deleted the fix-multipolygon branch July 3, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants