在同一GeodataFrame中的每个点的半径中查找点

发布于 2025-02-05 08:18:03 字数 650 浏览 2 评论 0原文

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

是否有可能在每个点的半径范围内找到点,并将其“值”和“几何”和“几何”添加到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.]

中正在检查是否在范围内,但我必须在半径中找到所有要点,并且不知道我应该使用什么工具。

I have 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'])

Is it possible to find points in a range of radius for example 10 for each point and add their "Value" and 'geometry' to GeoDataFrame so output would look like:

['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.]

Before i was finding nearest neighbor for each and after that was checking if is it in range but i must find all of the points in radius and don't know what tool should i use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱成瘾 2025-02-12 08:18:03

尽管在您的示例中,输出将在结果框架中具有可预测的列数,但总体上这并不正确。因此,我将在数据框架中创建一个列,该列由表示附近点的索引/值/几何形状的列表组成。

在像您提供的小型数据集中,Python中的简单算术足以满足。但是对于大型数据集,您将需要使用空间树来查询附近的点。我建议这样使用Scipy的kdtree:

import geopandas as gpd
import numpy as np
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(list(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)              [A, D]
2   2     C  POINT (100.00000 2.00000)                  []
3   3     D  POINT (20.00000 10.00000)                 [B]

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

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()

“

Although in your example the output will have a predictable amount of columns in the resulting dataframe, this not true in general. Therefore I would instead create a column in the dataframe that consists of a lists denoting the index/value/geometry of the nearby points.

In a small dataset like you provided, simple arithmetics in python will suffice. But for large datasets you will want to use a spatial tree to query the nearby points. I suggest to use scipy's KDTree like this:

import geopandas as gpd
import numpy as np
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(list(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])

This yields the following dataframe:

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

To verify the results, you may find plotting the result usefull:

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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文