geos
geos copied to clipboard
Union has incorrect results for some GeometryCollection inputs
Taking the union (using Geometry::Union) of POLYGON ((0 0, 0 1, 1 1, 0 0)) and MULTIPOINT (0 0, 99 99) produces GEOMETRYCOLLECTION (POLYGON ((0 1, 1 1, 0 0, 0 1)), POINT (99 99)), which is correct (albeit with the polygon in the result starting at a different vertex).
However, taking the union of POLYGON ((0 0, 0 1, 1 1, 0 0)) and GEOMETRYCOLLECTION (MULTIPOINT (0 0, 99 99)) produces POLYGON ((0 1, 1 1, 0 0, 0 1)), which has lost the point (99 99).
Yap the GEOMETRYCOLLECTION part is tripping it up.
I see the same issue on GEOS main and my production GEOS 3.10.2
See also GEOS Trac issue 755.
This behaviour is due to the fact that GEOS overlay now uses OverlayNG. OverlayNG does not support GeometryCollections as input. This case should cause an error, but a coding bug actually has the effect that no error is reported, just an incorrect result.
A: In the short term this could be fixed by ensuring an error is thrown. In GEOS this should end up delegating to the old overlay code, which handles this case correctly. (@pramsey @dbaston thoughts ?)
B: Medium term upgrade is to handle "simple" GCs. Simple GCs are ones which are equivalent to valid Multi geometries (i.e. are homogeneous and which contain no overlapping Polygons).
C: Longer term full support for GC inputs could be added to OverlayNG.
Note that this particular case was handled by the old overlay code. However, the old overlay code does not appear to handle GCs in general. It may just handle Simple GCs - more research required.
JTS has added OverlayNG support for simple GeometryCollections in https://github.com/locationtech/jts/pull/915.
This handles the homogeneous collection cases in this issue.
Interesting that these cases were supported by GEOS but removing that support apparently produced no test failures in GEOS or PostGIS?
I'd have to go back and dig, but any tests we may have had (and maybe we had none) would have come out / been turned off at OverlayNG switchover. I think it's possible we just didn't have any, I have no recollection of taking them out.
The support in old GEOS overlay may have been just accidental. As far as I can see it was not documented.