Segmentation fault when calling geometry.MultiPolygon with shapes from mask array
See original GitHub issueExpected behavior and actual behavior.
Segmentation fault when trying to create a MultiPolygon, from shapes generated from rasterio.features.shapes
.
Steps to reproduce the problem.
The problem was initially encountered in terracotta, an XYZ tile server that uses Shapely as a dependency.
The specific line of code that triggers the segfault is this one https://github.com/DHI-GRAS/terracotta/blob/b7c67c3c2736401295644c1e8882b3f0f013bb5c/terracotta/raster.py#L157
Below is a minimal example that triggers the segfault.
Python 3.9.11 (main, Apr 7 2022, 11:47:24)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
In [2]: from rasterio import features
In [3]: from shapely import geometry
In [4]: mask = np.random.randint(0, 2, (2000, 2000), dtype=int)
In [5]: mask = mask.astype("uint8")
In [6]: hull_shapes = (geometry.shape(s) for s, _ in features.shapes(np.ones(mask.shape, 'uint8'), mask=ma
...: sk))
In [7]: geometry.MultiPolygon(hull_shapes)
[1] 44843 segmentation fault ipython
Operating system
arch: macOS/arm64
macOS Monterey 12.4, Apple Silicon M1 chip.
Shapely version and provenance
The issue is present in Shapely 1.8.2 (newest as of the time of writing) and Shapely 1.8.1. Shapely 1.8.0 is the latest version that is not affected by the issue as of the time of writing.
Issue Analytics
- State:
- Created a year ago
- Comments:28 (15 by maintainers)
Top Results From Across the Web
Segmentation fault with simple geometry operations using ...
Segmentation fault when calling geometry.MultiPolygon with shapes from mask array shapely/shapely#1449. Open.
Read more >Why does this R code that maps points into polygons cause a ...
Valid R code shouldn't cause a seg fault, even if it is wrong. ... latitudes st_transform silently converts the points to an empty...
Read more >python - How can I select the pixels that fall within a contour in ...
First it computes the bounding box of the polygon, then it creates an array points with the coordinates of all pixels that fall...
Read more >ee.Geometry.Point.buffer - Earth Engine - Google Developers
The distance of the buffering, which may be negative. If no projection is specified, the unit is meters. Otherwise the unit is in...
Read more >Masks - L3HarrisGeospatial.com
Masked pixels will be excluded from any processing and will be displayed as transparent in the resulting image (called a masked raster).
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
Yes, rasterio is built from source (as is the default via
pip install rasterio
).The problem is fixed if I also build shapely from source, as you probably suspected:
I ran the same as dionhaefner, upgrading too to 1.8.4 (I was in 1.8.2) and the problem dissapeared!