将多边形记录为对象的多边形转换为外形多边形给出' str'对象没有属性' __ array_interface __'

发布于 2025-02-01 08:23:57 字数 1220 浏览 1 评论 0原文

最初,我有2个数据集。一个是在Excel中定义的45个多边形的数据集,另一个是点的几何坐标。我需要知道每个几何点的45个多边形中的哪个。

对于带有多边形的文件,我有一个CSV文件,该文件将多边形记录为对象。我想稍后检查多边形是否包含点。我认为它已经是多边形类型,但是当我从CSV导入它时,它只是作为字符串导入。我尝试将这些数据转换为polygon()

df中的每个raw看起来像SMTH(目的是故意缩短)

POLYGON ((37.667289733886719 55.700740814208984,37.670955657958984 55.70050048828125)

,我还打印了此数据集的前5个原始方法:

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

如果我用Polygon检查列的列数据类型 - 它是一个对象格式

df.dtype

WKT            object
IO_ID           int64
Unnamed: 2    float64
dtype: object




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

,它给我一个错误:'str'对象没有属性' array_interface '

我无法明白为什么会发生这种情况以及可以做什么(我承认我是Geodata的新手)。我的理解是,我需要以多边形格式使用对象格式,但是我无法将其更改为。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2025-02-08 08:23:57

要使用地质的空间特征,您的形状需要是几何类型,而不是字符串。您可以看到使用dtype属性的对象类型 - 您应该看到以下内容:

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

如果输出表示dtype('o')之类的内容,则您只有字符串,需要将它们转换为几何阶层。

看来您的形状在“ nofollow noreferrer”>“ nofollow noreferrer”>“ nofollow noreferrer”>“ nofollow noreferrer”>“已知文本”(aka wkt)格式 。您可以使用 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)

此时,您的geodataframe GDF应该具有geopandas的所有空间特征,并且可以使用 geopandas.sjoin 。请注意,需要首先使用 geopandas.points_from_xy - 请参阅eg 这个问题

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文