【问题标题】:Geopandas sjoin() error: list index out of rangeGeopandas sjoin() 错误:列表索引超出范围
【发布时间】:2019-05-31 18:04:55
【问题描述】:

我有两个 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')

【问题讨论】:

  • 只是一个问题,因为我无法重现此问题:您拥有的 shapely 版本是什么? (如果是可重现的错误,我们应该在 GeoPandas 中修复它)
  • 我有 1.6.4 版。我会尝试上传可重现的代码。
  • 嗯,奇怪,还有 1.6.4,像 Polygon([]).bounds 这样的东西对我有用(返回空元组)
  • 我添加了重现错误的最少代码。
  • 谢谢!我尝试了一个空的多边形,但实际上是一个空的点,这会引发这个错误。这似乎是身材本身的一个错误,我在这里打开了一个问题:github.com/Toblerity/Shapely/issues/716

标签: python join geopandas shapely index-error


【解决方案1】:

所以在发布之后,我发现了错误:我的 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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2016-01-06
    相关资源
    最近更新 更多