shapely / shapely/shapely

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

Open
#1,449 28 comments 0 reactions 1 assignee View on GitHub

@sgillies is already working on this.

Since Aug 15, 2022.

bug geos shapely-1.8
Dominant language
Python
Stars
4.5k
Forks
631
Avg merge
1d 11h
Merged PRs (30d)
10

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.