【问题标题】:Finding points in radius of each point in same GeoDataFrame在同一 GeoDataFrame 中的每个点的半径中查找点
【发布时间】:2022-06-12 18:56:09
【问题描述】:

我有 geoDataFrame:

df = gpd.GeoDataFrame([[0, 'A', Point(10,12)], 
                       [1, 'B', Point(14,8)],
                       [2, 'C', Point(100,2)],
                       [3, 'D' ,Point(20,10)]], 
                      columns=['ID','Value','geometry'])

是否可以在半径范围内找到点,例如每个点为 10,并将它们的“值”和“几何”添加到 GeoDataFrame,这样输出看起来像:

['ID','Value','geometry','value_of_point_in_range_1','geometry_of_point_in_range_1','value_of_point_in_range_2','geometry_of_point_in_range_2' etc.]

在我为每个寻找最近的邻居之前,然后检查它是否在范围内,但我必须找到半径中的所有点并且不知道我应该使用什么工具。

【问题讨论】:

    标签: python-3.x pandas geopandas shapely


    【解决方案1】:

    尽管在您的示例中,输出将在结果数据框中具有可预测的列数,但通常情况并非如此。因此,我将改为在数据框中创建一列,该列由表示附近点的索引/值/几何的列表组成。

    在您提供的小型数据集中,python 中的简单算法就足够了。但是对于大型数据集,您将需要使用空间树来查询附近的点。我建议像这样使用 scipy 的 KDTree:

    import geopandas as gpd
    import numpy as np
    import pandas as pd
    from shapely.geometry import Point
    from scipy.spatial import KDTree
    
    df = gpd.GeoDataFrame([[0, 'A', Point(10,12)],
                           [1, 'B', Point(14,8)],
                           [2, 'C', Point(100,2)],
                           [3, 'D' ,Point(20,10)]],
                          columns=['ID','Value','geometry'])
    
    tree = KDTree(pd.DataFrame(zip(df.geometry.x, df.geometry.y)))
    pairs = tree.query_pairs(10)
    
    df['ValueOfNearbyPoints'] = np.empty((len(df), 0)).tolist()
    
    n = df.columns.get_loc("ValueOfNearbyPoints")
    m = df.columns.get_loc("Value")
    for (i, j) in pairs:
        df.iloc[i, n].append(df.iloc[j, m])
        df.iloc[j, n].append(df.iloc[i, m])
    

    这会产生以下数据框:

       ID Value                   geometry ValueOfNearbyPoints
    0   0     A  POINT (10.00000 12.00000)                 [B]
    1   1     B   POINT (14.00000 8.00000)                 [D]
    2   2     C  POINT (100.00000 2.00000)                  []
    3   3     D  POINT (20.00000 10.00000)                  []
    

    要验证结果,您可能会发现绘制结果很有用:

    import matplotlib.pyplot as plt
    ax = plt.subplot()
    df.plot(ax=ax)
    for (i, j) in pairs:
        plt.plot([df.iloc[i].geometry.x, df.iloc[j].geometry.x],
                 [df.iloc[i].geometry.y, df.iloc[j].geometry.y], "-r")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 2014-03-07
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多