Geopandas sjoin() error: list index out of range
Geopandas sjoin() error: list index out of range
我有两个 GeoDataFrame。一个将 Shapely points 设置为 .geometry,另一个将 Shapely polygons 和 multipolygons 设置为 .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')
我有两个 GeoDataFrame。一个将 Shapely points 设置为 .geometry,另一个将 Shapely polygons 和 multipolygons 设置为 .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')