gpt4 book ai didi

python - Geopandas sjoin()错误: list index out of range

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:32 25 4
gpt4 key购买 nike

我有两个 GeoDataFrame。一种将 Shapely 点设置为 .geometry,另一种将 Shapely 多边形和多边形设置为 .geometry。当我尝试对它们使用 sjoin() 函数时,出现错误。

import pandas as pd
import geopandas as gpd

points_gdf = pd.read_pickle('points.pickle')
polys_gdf = pd.read_pickle('polys.pickle')

# points_gdf.geometry consists of shapely points
# polys_gdf.geometry consists of shapely polygons and multipolygons

# Now, I use the sjoin() function

return_gdf = gpd.sjoin(points_gdf, polys_gdf, how="inner", op='intersects')

然后我收到以下错误:

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-75-e5b042f3f0e2> in <module>
----> 1 return_gdf = gpd.sjoin(points_gdf, polys_gdf, how="inner", op='intersects')

~\AppData\Local\Continuum\anaconda3\lib\site-packages\geopandas\tools\sjoin.py in sjoin(left_df, right_df, how, op, lsuffix, rsuffix)
73 tree_idx = rtree.index.Index(stream)
74
---> 75 idxmatch = (left_df.geometry.apply(lambda x: x.bounds)
76 .apply(lambda x: list(tree_idx.intersection(x))))
77 idxmatch = idxmatch[idxmatch.apply(len) > 0]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\geopandas\tools\sjoin.py in <lambda>(x)
73 tree_idx = rtree.index.Index(stream)
74
---> 75 idxmatch = (left_df.geometry.apply(lambda x: x.bounds)
76 .apply(lambda x: list(tree_idx.intersection(x))))
77 idxmatch = idxmatch[idxmatch.apply(len) > 0]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\shapely\geometry\point.py in bounds(self)
120 @property
121 def bounds(self):
--> 122 xy = self.coords[0]
123 return (xy[0], xy[1], xy[0], xy[1])
124

IndexError: list index out of range

我尝试将polys_gdf分成两部分,一种仅包含多边形,另一种仅包含多重多边形。但我收到同样的错误。有人可以帮我吗?

重现错误的工作代码:

import geopandas as gpd
from shapely.geometry import Point, Polygon

point_list = [Point(),Point(0.5,0.5)]
poly_list = [Polygon([[0, 0], [1, 0], [1, 1], [0, 1]])]

points_gdf = gpd.GeoDataFrame(geometry=point_list)
polys_gdf = gpd.GeoDataFrame(geometry=poly_list)

return_df = gpd.sjoin(points_gdf, polys_gdf, how="inner", op='within')

最佳答案

所以发布后,我发现了错误:我的 point_gdf 中有一些空的形状点。删除它们后,sjoin() 的作用就像一个魅力。

import geopandas as gpd
from shapely.geometry import Point, Polygon

point_list = [Point(),Point(0.5,0.5)]
poly_list = [Polygon([[0, 0], [1, 0], [1, 1], [0, 1]])]

points_gdf = gpd.GeoDataFrame(geometry=point_list)
polys_gdf = gpd.GeoDataFrame(geometry=poly_list)

points_gdf = points_gdf[~points_gdf.geometry.is_empty] # delete empty points
return_df = gpd.sjoin(points_gdf, polys_gdf, how="inner", op='within')

关于python - Geopandas sjoin()错误: list index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com