通过纬度和经度计算给定半径内的点数
我有一个包含 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
示例数据
提取纬度、经度并转换为弧度。转换为单位球体后计算所需的半径。
则给出 array([3, 2, 2, 1, 1])
如果你想返回 indici 并将它们用于聚合, 一种方法是
注意我们在这里使用 indici 而不仅仅是计数;
并使用列表理解来获取每个半径的中值。请注意,点本身始终包含在其自身的半径内。
Sample Data
Extract lat,long and convert to radians. Calculate the needed radius when converted to unit sphere.
Which gives
array([3, 2, 2, 1, 1])
If you want to return the indici and use them for aggregates; one way is to
Note we use indici here and not only count;
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.
这个问题有点模棱两可。您需要的第一个组件是计算两个坐标之间的距离的函数,这需要一些三角学,并且在 以下问题。
获得该函数后,只需循环所有点并进行计算即可。可能有比两个嵌套循环更有效的方法,但这是最简单的。
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.