Anya Pilipentseva
Anya Pilipentseva

Reputation: 187

convert polygon recorded as object to shapely polygon gives 'str' object has no attribute '__array_interface__'

Initially, I have 2 datasets. One is dataset with 45 polygons defined in Excel and another one is geometric coordinates of points. I need to know for each geometric point in which of 45 polygons it locates.

For file with polygons, I have a csv file which recorded POLYGON(......) as objects. I want to later check whether polygon contains point with shapely. I thought that it already was polygon type, but when I import it from csv, it imports just as a string. I tried to convert this data to Polygon()

Each raw in df looks smth like (shortened on purpose)

POLYGON ((37.667289733886719 55.700740814208984,37.670955657958984 55.70050048828125)

As suggest, I also printed the first 5 raws of this dataset:

print(io.head(5))

                                                 WKT  IO_ID  Unnamed: 2
0  POLYGON ((37.667289733886719 55.70074081420898...     28         NaN
1  POLYGON ((37.671272277832031 55.62009048461914...     29         NaN
2  POLYGON ((37.713523864746094 55.77525711059570...     24         NaN
3  POLYGON ((37.700267791748047 55.72071075439453...     25         NaN
4  POLYGON ((37.783447265625 55.648544311523438,3...     26         NaN

And if I check datatypes of columns with polygon - it is an object format

df.dtype

WKT            object
IO_ID           int64
Unnamed: 2    float64
dtype: object




for polygon in df.WKT:
  polygon = Polygon(polygon)

And it give me the error: 'str' object has no attribute 'array_interface'

I can't get why this happens and what can be done (I confess I am completely new to geodata). My understanding that instead of object format I need to have the data in polygon format, but somehow i can't change it to it.

Upvotes: 1

Views: 2123

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15432

To use the spatial features of geopandas, your shapes need to be geometry type, not strings. You can see what type the objects are using the dtype attribute - you should see something like the following:

In [6]: df.geometry.dtype
Out[6]: <geopandas.array.GeometryDtype at 0x17a0934c0>

If instead the output says something like dtype('O'), then you just have strings and need to convert them to a GeometryArray.

It looks like your shapes are in the "well known text" (aka wkt) format. You can convert a wkt column to a geometry column with geopandas.GeoSeries.from_wkt:

# replace string geometry representations with shapely geometries
df['geometry'] = gpd.GeoSeries.from_wkt(df['WKT'])

# initialize GeoDataFrame with the result
# ('geometry' is the default geometry column name)
gdf = gpd.GeoDataFrame(df)

At this point your GeoDataFrame gdf should have all the spatial features of geopandas, and could be used to join to a GeometryArray of points using geopandas.sjoin. Note that a regular DataFrame of points will need to first be converted into a GeoDataFrame using geopandas.points_from_xy - see e.g. this question for an example.

Upvotes: 3

Related Questions