question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Segmentation fault when calling geometry.MultiPolygon with shapes from mask array

See original GitHub issue

Expected 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:open
  • Created a year ago
  • Comments:28 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
dionhaefnercommented, Aug 18, 2022

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:

$ pip install shapely --no-binary shapely
1reaction
Scolymuscommented, Aug 24, 2022

I ran the same as dionhaefner, upgrading too to 1.8.4 (I was in 1.8.2) and the problem dissapeared!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found