Search code examples
pythongeopandaspointshapelymultilinestring

ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0


I have the following dataframe:

    ID_from  ID_to  geometry_to                      geometry_from
0   DE111    DE111  POINT (4260741.157 2851183.129)  POINT (4260741.157 2851183.129)
1   DE111    DE112  POINT (4243568.080 2841580.049)  POINT (4260741.157 2851183.129)
2   DE111    DE113  POINT (4274165.690 2838336.792)  POINT (4260741.157 2851183.129)

I'm trying to convert 2 points to a linestring with the following code:

df_line['Line'] = df_line.apply(lambda row: LineString([row['geometry_from'], row['geometry_to']]), axis=1)

Running the code, I get the desired outcome:

    ID_from  ID_to  geometry_to                      geometry_from                      Line
0   DE111    DE111  POINT (4260741.157 2851183.129)  POINT (4260741.157 2851183.129)    LINESTRING (4260741.157299999 2851183.1295, 42...
1   DE111    DE112  POINT (4243568.080 2841580.049)  POINT (4260741.157 2851183.129)    LINESTRING (4260741.157299999 2851183.1295, 42...
2   DE111    DE113  POINT (4274165.690 2838336.792)  POINT (4260741.157 2851183.129)    LINESTRING (4260741.157299999 2851183.1295, 42...

Nevertheless, I receive the following warning:

ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0. Convert the '.coords' to a numpy array instead.
  arr = construct_1d_object_array_from_listlike(values)

I've found the respective documentation (https://shapely.readthedocs.io/en/stable/migration.html) but I can't adjust the code on my own. How should I change my code to avoid this warning?


Solution

  • This will prevent the warning from being displayed when I run my code.

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", ShapelyDeprecationWarning)
        df_line['Line'] = df_line.apply(lambda row: LineString([row['geometry_from'], row['geometry_to']]), axis=1)
    

    Another option is to upgrade to the latest version of Shapely, which does not have this issue.