计算距坐标最近的位置

发布于 2025-01-09 00:16:22 字数 105 浏览 0 评论 0原文

假设我有一个位置列表(坐标)和一个位置(同样,坐标),我可以根据位置与该位置的距离对位置列表进行排序吗?

我想知道 Python 和 OpenStreetMap 是否可以实现这一点。

Let's say I had a list of locations (the coordinates) and one location (again, coordinates), can I sort the list of locations depending on how close they are to the location?

I'm wondering if this is possible with Python and OpenStreetMap.

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

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

发布评论

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

评论(1

仄言 2025-01-16 00:16:22

好吧,假设每个位置都是一个具有纬度和经度的点,如 (2, 3)。因此,您的列表将是:

x = [(1, 2), (5, 6), (7, -3)]

您的位置将是:

y = (4, 5)

那么您的工作是计算从列表 x 到您的位置 y 的位置之间的距离:

import math
def distance(a, b):
    return math.hypot(a[1] - b[1], a[0] - b[0])

distances = []
for i in x:
    distances.append((i, distance(i, y)))
# sort coordinate by distance
distances = sorted(distances, key=lambda i: x[1])

Well, let's say each location is a point with lat and long like (2, 3). So your list would be:

x = [(1, 2), (5, 6), (7, -3)]

Your location would be:

y = (4, 5)

Then your job is to calculate the distance between locations from the list x to your location y:

import math
def distance(a, b):
    return math.hypot(a[1] - b[1], a[0] - b[0])

distances = []
for i in x:
    distances.append((i, distance(i, y)))
# sort coordinate by distance
distances = sorted(distances, key=lambda i: x[1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文