通过纬度和经度计算给定半径内的点数

发布于 2025-01-09 08:21:29 字数 332 浏览 6 评论 0原文

我有一个包含 id 名称和纬度/经度的点数据框:

df = pd.DataFrame({'id':list('abcde'),'latitude': [38.470628, 37.994155, 38.66937, 34.119578, 36.292307],'longitude': [-121.404586, -121.802341, -121.295325, -117.413791, -119.804074]})  #sample

对于每个 id,我需要计算位于 2 英里半径范围内的(同一数据集的)点的数量。

问题:如何在Python中以最简单的方式做到这一点?

I have a dataframe of points with its id-name and latitude/longitude:

df = pd.DataFrame({'id':list('abcde'),'latitude': [38.470628, 37.994155, 38.66937, 34.119578, 36.292307],'longitude': [-121.404586, -121.802341, -121.295325, -117.413791, -119.804074]})  #sample

For each id I need to count the number of points (of the same dataset) that are located within a radius of 2 miles from it.

Question: how to do this in the simplest way in Python?

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

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

发布评论

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

评论(2

葬花如无物 2025-01-16 08:21:29
import numpy as np
import pandas as pd
from sklearn.neighbors import BallTree

示例数据

df = pd.DataFrame({'id':list('abcde'),'latitude': [38.470628, 37.994155, 38.66937, 34.119578, 36.292307],'longitude': [-121.404586, -121.802341, -121.295325, -117.413791, -119.804074]})  #sample

提取纬度、经度并转换为弧度。转换为单位球体后计算所需的半径。

coords = df[["latitude","longitude"]]

distance_in_miles = 50
earth_radius_in_miles = 3958.8

radius = distance_in_miles / earth_radius_in_miles
tree = BallTree( np.radians(coords), leaf_size=10, metric='haversine')

tree.query_radius( np.radians(coords), r=radius, count_only=True)

则给出 array([3, 2, 2, 1, 1])


如果你想返回 indici 并将它们用于聚合, 一种方法是

df = pd.DataFrame({'id':list('abcde'),'latitude': [38.470628, 37.994155, 38.66937, 34.119578, 36.292307],'longitude': [-121.404586, -121.802341, -121.295325, -117.413791, -119.804074], 'saleprice_usd_per_sqf': [200, 300, 700, 350, 50]})
coords = df[["latitude","longitude"]]

distance_in_miles = 50
earth_radius_in_miles = 3958.8

radius = distance_in_miles / earth_radius_in_miles

注意我们在这里使用 indici 而不仅仅是计数;

tree = BallTree( np.radians(coords), leaf_size=10, metric='haversine')
indici = tree.query_radius( np.radians(coords), r=radius, count_only=False)

并使用列表理解来获取每个半径的中值。请注意,点本身始终包含在其自身的半径内。

[np.median(df.saleprice_usd_per_sqf.values[idx]) for idx in indici]
import numpy as np
import pandas as pd
from sklearn.neighbors import BallTree

Sample Data

df = pd.DataFrame({'id':list('abcde'),'latitude': [38.470628, 37.994155, 38.66937, 34.119578, 36.292307],'longitude': [-121.404586, -121.802341, -121.295325, -117.413791, -119.804074]})  #sample

Extract lat,long and convert to radians. Calculate the needed radius when converted to unit sphere.

coords = df[["latitude","longitude"]]

distance_in_miles = 50
earth_radius_in_miles = 3958.8

radius = distance_in_miles / earth_radius_in_miles
tree = BallTree( np.radians(coords), leaf_size=10, metric='haversine')

tree.query_radius( np.radians(coords), r=radius, count_only=True)

Which gives array([3, 2, 2, 1, 1])


If you want to return the indici and use them for aggregates; one way is to

df = pd.DataFrame({'id':list('abcde'),'latitude': [38.470628, 37.994155, 38.66937, 34.119578, 36.292307],'longitude': [-121.404586, -121.802341, -121.295325, -117.413791, -119.804074], 'saleprice_usd_per_sqf': [200, 300, 700, 350, 50]})
coords = df[["latitude","longitude"]]

distance_in_miles = 50
earth_radius_in_miles = 3958.8

radius = distance_in_miles / earth_radius_in_miles

Note we use indici here and not only count;

tree = BallTree( np.radians(coords), leaf_size=10, metric='haversine')
indici = tree.query_radius( np.radians(coords), r=radius, count_only=False)

And use list comprehension to for instance get the median value for each radius. Be aware the the point itself is always included in its own radius.

[np.median(df.saleprice_usd_per_sqf.values[idx]) for idx in indici]
孤寂小茶 2025-01-16 08:21:29

这个问题有点模棱两可。您需要的第一个组件是计算两个坐标之间的距离的函数,这需要一些三角学,并且在 以下问题

获得该函数后,只需循环所有点并进行计算即可。可能有比两个嵌套循环更有效的方法,但这是最简单的。

The question is somewhat ambiguous. The first component you need is a function to calculate distance between two coordinates, this requires some trigonometry and has several implementations in the following questions.

After you have the function simply loop over all points and calculate. There might be more efficient ways than two nested loop but this is the simplest.

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