Python Geopandas sjoin()错误:列表索引超出范围
我有两个框架。一个将形状点设置为.geometry,另一个将形状多边形和多多边形设置为.geometry。当我试图对它们使用Python Geopandas sjoin()错误:列表索引超出范围,python,join,geopandas,shapely,index-error,Python,Join,Geopandas,Shapely,Index Error,我有两个框架。一个将形状点设置为.geometry,另一个将形状多边形和多多边形设置为.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
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
所以在发布之后,我发现了一个错误:我的point_gdf中有一些空的shapely点。删除它们后,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')
只是一个问题,因为我无法复制这个:你拥有的shapely的版本是什么?(如果这是一个可复制的错误,我们应该在GeoPandas中修复它)我有shapely版本1.6.4。我将尝试上传一个可复制的代码。嗯,很奇怪,1.6.4也有类似于
Polygon([])的代码。bounds
适合我(返回空元组)我添加了复制错误的最小代码。谢谢!我尝试了一个空多边形,但实际上是一个空点,这会引起这个错误。这似乎是shapely本身的一个缺陷,我在这里提出了一个问题:
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')