Skip to content

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

@mrpgraae

Description

@mrpgraae

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.

Metadata

Metadata

Assignees

Labels

buggeosshapely-1.8Bugs only applicable to shapely 1.8.x or below

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions