libgeos / geos

Geometry Engine, Open Source
https://libgeos.org
GNU Lesser General Public License v2.1
1.1k stars 339 forks source link

Unexpected result for intersection of Geometrycollection with polygon? #1059

Open theroggy opened 2 months ago

theroggy commented 2 months ago

The intersection of a geometrycollection with 4 polygons with a Polygon that overlaps with all four polygons in the collection results in one Polygon. Is this correct?

Issue created based on a slightly more complicated case posted on stackoverflow.

import shapely

poly = shapely.box(2, 2, 8, 8)
collection = shapely.GeometryCollection(
    [
        shapely.box(0, 0, 5, 5),
        shapely.box(5, 0, 10, 5),
        shapely.box(0, 5, 5, 10),
        shapely.box(5, 5, 10, 10),
    ]
)
intersection = shapely.intersection(poly, collection)
print(intersection)
dr-jts commented 2 months ago

Well, that depends on the semantics of handling overlay of GeometryCollections. At the moment it uses "union semantics" - the overlay result is the result of unioning the inputs first. Given this, the result makes sense.

The alternative is "collection semantics", where the result is the collection of the overlay applied to each collection element separately. If this behaviour is desired it's fairly easy to build externally.

Which one is preferable is likely a matter of preference (or use case).

It's worth noting that the in-progress support for GeometryCollections and spatial predicates will also use union semantics.

theroggy commented 2 months ago

OK, I expected the "collection semantics", but it will indeed be a matter of preference/use case you are thinking about.

At first sight, I think it is even easier to apply "union semantics" if "collection semantics" would be the default as you just need to call union() on the input collection(s) while for the other way around you need to write a loop and collect everything again?

For consistency, it does sound logical that the same semantics are used for overlays and spatial predicates, whatever is used.