Intersection of certain GeometryCollections segfaults
See original GitHub issueExpected behavior and actual behavior.
intersection()
method segfaults when intersecting a Polygon with some GeometryCollection. Converting the collection into a MultiPolygon fixes the problem.
I tried coming up with arbitrary GeometryCollections but I couldn’t reproduce the error except for the example below.
Steps to reproduce the problem.
from shapely.geometry import shape
from shapely.ops import unary_union
polygon = shape({
"type": "Polygon",
"coordinates": [[
[132.55004882812503, 34.2288353852272],
[132.55004882812503, 34.22997081127308],
[132.55142211914062, 34.22997081127308],
[132.55142211914062, 34.2288353852272],
[132.55004882812503, 34.2288353852272]
]]
})
collection = shape({
"type": "GeometryCollection",
"geometries": [{
"type": "MultiPolygon",
"coordinates": [
[[
[132.55004882812503, 34.22993173063148],
[132.55004882812503, 34.22997081127308],
[132.5500874053658, 34.22997081127308],
[132.55004882812503, 34.22993173063148]
]],
[[
[132.5501380995388, 34.22997081127308],
[132.55180084417634, 34.22997081127308],
[132.55180084417634, 34.228694170127646],
[132.5501380995388, 34.22997081127308]
]]
]
}]
})
# Intersecting this particular collection fails
polygon.intersection(collection)
# Converting the collection into a multipolygon circumvents the segfault and
# works as expected
multipolygon = unary_union(collection)
polygon.intersection(multipolygon)
Operating system
Reproduced on MacOS 10.15 Catalina and an up-to-date Manjaro Linux installation.
Shapely version and provenance
Fails on both 1.6.4.post2 and 1.7.0 installed via pip in a clean virtualenv.
Thanks for any information on the problem or a fix.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Intersection of certain GeometryCollections segfaults #860
Expected behavior and actual behavior. intersection() method segfaults when intersecting a Polygon with some GeometryCollection.
Read more >Segfault when testing the intersection with geos in parallel
When running the simulation with several threads the code crashes inside env_intersect with a segfault if I do not wrap GEOSPreparedIntersects_r ...
Read more >intersection between two multipolygons yielding anomalous ...
I recently had two geometries whose intersection produced a GeometryCollection of Polygon and tiny LineString .
Read more >Bug report #14494: geometry.intersection() does not work ...
When we try to intersect two linestring as shown in attached figure, we get a QGis.WKBUnknown geometry just because LString1 and LString2 do...
Read more >Intersection with distinct geometries crashes postgres
The OSM polygons are collected into a geometrycollection (group by) and intersected with the raster polygons. The following combination of geometries ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dr-jts thanks a lot for that detailed answer!
In that case, I think I would personally prefer to just pass inputs to GEOS. It is certainly true that the error from GEOS like “side location conflict at 1 2” is not at all informative (side question: is this something that could be improved in GEOS? Or is it too much down into the operation to have useful context to report about the error?). But checking in advance if polygons in collections are overlapping also doesn’t sound that an attractive option. Alteratively, we could still catch the error from GEOS and reraise it with some additional explanation about what might be causing the issue.
Yes, the comment about “strictly speaking GeometryCollections are not handled” still applies to OverlayNG.
OverlayNG will process some GeometryCollections, but with the following limitations:
IllegalArgumentException
)Yes, it should be safe to allow all but collections with overlapping polygons. Not guaranteed that semantics won’t change at some point in the future, though.
I’m pretty sure that inputs with overlapping polygons will always raise error, rather than crashing. The downside to just reporting that error back to the user is that it is pretty low-level, and doesn’t inform him of the real problem (overlapping polygon inputs). Also, in the past the same error could be caused by a robustness issue, which made it even harder for the user to determine what was wrong. However, that situation has (hopefully) been eliminated by OverlayNG, so it should be safe to assume that an error indicates overlapping polygon input.