Skip to content

Shapely 1.8 array interface deprecation for object arrays #1096

@mwtoews

Description

@mwtoews

I'm looking at some of the shapely 1.8 deprecation impacts in a niche package, and I'm stumbling over one issue that can be represented in a small example:

import numpy as np
from shapely.geometry import Point, MultiPoint
mp = MultiPoint([Point(1, 2), Point(3, 4)])
mp_list = [mp]
a = np.empty(len(mp_list), dtype=object)
a[:] = mp_list
print(a[0].wkt)

This example works for all current and development versions of shapely, printing MULTIPOINT (1 2, 3 4) from the object array.

Shapely 1.7 does not show any warnings.

Shapely 1.8dev shows two warnings:

/usr/bin/ipython3:1: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the geoms property instead to get the number of parts of a multi-part geometry.
#! /bin/sh
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:2882: ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0. Convert the '.coords' to a numpy array instead.
exec(code_obj, self.user_global_ns, self.user_ns)

Shapely 2.0dev does not show any warnings, and works the same as any other version.


It seems that the warnings from shapely 1.8dev are crying wolf. I understand these shenanigans are permitted with object arrays. Is there any way for shapely 1.8 to omit these warnings? Or should I just refactor to ether of these?

a = np.empty(len(mp_list), dtype=object)
for idx, obj in enumerate(mp_list):
    a[idx] = obj

# or

import warnings
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    a = np.empty(len(mp_list), dtype=object)
    a[:] = mp_list

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions