【问题标题】:Geopandas contains working for one point but not manyGeopandas 包含为一个点工作,但不是很多
【发布时间】:2019-12-23 21:22:59
【问题描述】:

我需要在多面体中过滤数据框的行。我的多多边形存储在 gdf_polygon 中,我的点存储在 gdf ​​中。这是他们的外观的一些简历。

gdf_polygon
id          geometry
0           MULTIPOLYGON (((39.81239 21.43429, 39.81445 21...

gdf
id      geometry
0       POINT (50.05832 26.43992)
...     ...

问题是当我试图检查它里面是否有任何点时返回False,但我知道多边形里面有一些点。

基本上,如果我运行它,我有 False 作为输出。

gdf_polygon.geometry.contains(gdf.geometry).any()

否则,如果我运行它,我将有 True 作为输出,因为该点位于多边形内部。

gdf_polygon.geometry.contains(gdf.geometry[141828])

我知道我可以遍历 gdf 的所有行并为每一行运行包含,但由于我的数据集非常大(大约 30.000.000 行),效率非常低。所以我一直在寻找解释或可能的修复方法。

我的数据框创作是:

 crs = {'init': 'epsg:4326'}

 df = pd.read_csv(FOLDER+file, compression='gzip', escapechar='\\')

 geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]
 gdf = gpd.GeoDataFrame(df,crs=crs, geometry=geometry)

 inside = gdf.geometry.within(gdf_polygon.geometry)

【问题讨论】:

    标签: python geopandas


    【解决方案1】:

    当比较contains geopandas 中的两个 GeoSeries 时,geopandas 会对齐它们,请参阅https://gis.stackexchange.com/questions/345785/geopandas-intersect-function-gives-different-result-to-shapely/345822#345822 以了解说明。

    要使您的代码按预期工作,您需要将 GeoSeries 点与多多边形几何本身进行比较。反之亦然,使用within

    polygon = gdf_polygon.geometry.iloc[0]
    gdf.geometry.within(polygon)
    

    【讨论】:

    • 非常感谢!我很困扰,因为这些信息没有出现在 geopandas 文档中。
    猜你喜欢
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多