shapely: NumPy 2.0: minimum_rotated_rectangle / affine_transform failing on MacOS 14

Expected behavior and actual behavior.

The following currently fails with Numpy 2.0 (released yesterday) and shapely 2.0.4:

In [18]: from shapely.geometry import MultiPoint

In [19]: import numpy as np

In [20]: xp = np.frombuffer(b'\xb3\x03b\xffN!3\xc0\xcb\x92\x87\x89\xfe\xde4@\x04d\x85\xa0|\x1a5@&ww\x99E\xe52\xc0')

In [21]: yp = np.frombuffer(b'\xe1y\x97b\x82\xb6+\xc0\x92\xce\xf0W\\L,\xc0\x14\x14h\xaap\xac/@\xfa\x8e\xbaY\xf7*0@')

In [22]: mp = MultiPoint(list(zip(xp, yp, strict=True)))

In [23]: mp.minimum_rotated_rectangle
---------------------------------------------------------------------------
GEOSException                             Traceback (most recent call last)
Cell In[23], line 1
----> 1 mp.minimum_rotated_rectangle

File ~/python/dev/lib/python3.11/site-packages/shapely/geometry/base.py:428, in BaseGeometry.minimum_rotated_rectangle(self)
    416 @property
    417 def minimum_rotated_rectangle(self):
    418     """
    419     Returns the oriented envelope (minimum rotated rectangle) that
    420     encloses the geometry.
   (...)
    426     Alias of `oriented_envelope`.
    427     """
--> 428     return shapely.oriented_envelope(self)

File ~/python/dev/lib/python3.11/site-packages/shapely/constructive.py:1034, in oriented_envelope(geometry, **kwargs)
   1032 else:
   1033     f = _oriented_envelope_geos
-> 1034 return f(geometry, **kwargs)

File ~/python/dev/lib/python3.11/site-packages/shapely/algorithms/_oriented_envelope.py:52, in _oriented_envelope_min_area(geometry, **kwargs)
     49         yield (transf_rect, (ux, vx, uy, vy, 0, 0))
     51 # check for the minimum area rectangle and return it
---> 52 transf_rect, inv_matrix = min(_transformed_rects(), key=lambda r: r[0].area)
     53 return affine_transform(transf_rect, inv_matrix)

File ~/python/dev/lib/python3.11/site-packages/shapely/algorithms/_oriented_envelope.py:46, in _oriented_envelope_min_area.<locals>._transformed_rects()
     42 vx, vy = -uy, ux
     43 # transform hull from the original coordinate system to
     44 # the coordinate system defined by the edge and compute
     45 # the axes-parallel bounding rectangle.
---> 46 transf_rect = affine_transform(hull, (ux, uy, vx, vy, 0, 0)).envelope
     47 # yield the transformed rectangle and a matrix to
     48 # transform it back to the original coordinate system.
     49 yield (transf_rect, (ux, vx, uy, vy, 0, 0))

File ~/python/dev/lib/python3.11/site-packages/shapely/affinity.py:74, in affine_transform(geom, matrix)
     71 def _affine_coords(coords):
     72     return np.matmul(A, coords.T).T + off
---> 74 return shapely.transform(geom, _affine_coords, include_z=ndim == 3)

File ~/python/dev/lib/python3.11/site-packages/shapely/coordinates.py:65, in transform(geometry, transformation, include_z)
     59 if new_coordinates.shape != coordinates.shape:
     60     # if the shape is too small we will get a segfault
     61     raise ValueError(
     62         "The provided transformation returned an array with an unexpected "
     63         f"shape ({new_coordinates.shape})"
     64     )
---> 65 geometry_arr = lib.set_coordinates(geometry_arr, new_coordinates)
     66 if geometry_arr.ndim == 0 and not isinstance(geometry, np.ndarray):
     67     return geometry_arr.item()

GEOSException: IllegalArgumentException: Points of LinearRing do not form a closed linestring

I have to use those particular array values for the error to happen, hence the frombuffer calls. For convenience:

In [24]: xp
Out[24]: array([-19.13011166,  20.87107143,  21.10346416, -18.89559325])

In [25]: yp
Out[25]: array([-13.85646351, -14.14914203,  15.83679707,  16.16783677])

If I downgrade to Numpy 1.26.4 I don’t get any error:

In [6]: mp.minimum_rotated_rectangle
Out[6]: <POLYGON ((20.871 -14.169, 21.105 15.855, -18.896 16.168, -19.13 -13.856, 20...>

What could be causing this?

Operating system

MacOS 14.1.1

Shapely version and provenance

Shapely was installed from PyPI using pip.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 30 (24 by maintainers)

Most upvoted comments

Can confirm that the assertion fails when installed using the wheel from PyPI. But passes when installed from conda-forge, same as the original issue.

@Mec-iS that also seems related to numpy 2.0 indeed, but is a separate issue from the minimum_rotated_rectangle / affine_transform one. Could you open a new issue?

This only happens on MacOS with Silicone chips on my side by the way, I also don’t see the issue on Linux or Windows.