简体   繁体   English

如何计算 python 中 n 个坐标之间的距离

[英]How to calculate distance between n coordinates in python

I am working on a python project where from a function I am getting coordinate values x, y in a dict like below:我正在研究python项目,其中从dict我得到坐标值x, y如下所示:

centroid_dict = {0: (333, 125), 1: (288, 52), 2: (351, 41)}

where 0, 1, 2 are the objectId and (333, 125), (288, 52), (351, 41) are their (x, y) coordinate values respectively.其中0, 1, 2objectId(333, 125), (288, 52), (351, 41)分别是它们的(x, y)坐标值。 I need to calculate the distance between each coordinate which means:我需要计算每个坐标之间的距离,这意味着:

0 - 1 -> ((333, 125) - (288, 52))

0 - 2 -> ((333, 125) - (351, 41))

1 - 0 -> ((288, 52) - (333, 125))

1 - 2 -> ((288, 52) - (351, 41))

2 - 0 -> ((351, 41) - (333, 125))

2 - 1 -> ((351, 41) - (288, 52))

To calculate the distance, I can use:要计算距离,我可以使用:

def calculateDistance(x1, y1, x2, y2):
    dist = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    return dist

but I am not able to think of any logic which can calculate distance between each points as the length of dict might increase in future.但我想不出任何可以计算每个点之间距离的逻辑,因为 dict 的长度将来可能会增加。 As of now, it is 3 but it can be 10 .截至目前,它是3但它可以是10 Can anyone please help me give some ideas on it.任何人都可以帮我提供一些想法。 Thanks谢谢

You can use combinations from itertools to form a new dictionary with every pair of objects as key:您可以使用来自 itertools 的组合来形成一个以每对对象为键的新字典:

from itertools import combinations:
distances = dict()
for (id1,p1),(id2,p2) in combinations(centroid_dict.items(),2):
    dx,dy = p1[0]-p2[0], p1[1]-p2[1]
    distances[id1,id2] = distances[id2,id1] = math.sqrt(dx*dx+dy*dy)

The drawback of this approach is that it will systematically calculate all distances and your program may not need to access all of those values.这种方法的缺点是它会系统地计算所有距离,您的程序可能不需要访问所有这些值。 A more efficient approach could be to use a dictionary as a cache of distance and obtain them "on demand" using a function:一种更有效的方法是使用字典作为距离缓存,并使用 function“按需”获取它们:

distanceCache = dict()
def getDist(id1,id2):
    if id1 > id2: return getDist(id2,id1) # only store each pair once
    if (id1,id1) in distanceCache: return distanceCache[id1,id2]
    x1,y1 = centroid_dict[id1]
    x2,y2 = centroid_dict[id2]
    dx,dy = x1-x2,y1-y2 
    return distanceCache.setDefault((id1,id2),math.sqrt(dx*dx+dy*dy))

This will allow you to simply clear the cache when object locations are changed without incurring an immediate delay of O(n^2) time这将允许您在 object 位置更改时简单地清除缓存,而不会立即延迟 O(n^2) 时间

Note that you could also use the points (positions) themselves as key to the cache and also use an LRU cache (from functools)请注意,您也可以使用点(位置)本身作为缓存的键,也可以使用 LRU 缓存(来自 functools)

from functools import lru_cache
import math

@lru_cache(1024)
def getDist(p1,p2):
    dx,dy = p1[0]-p2[0],p1[1]-p2[1]
    return math.sqrt(dx*dx+dy*dy)

def getObjectDist(id1,id2):
    return getDist(centroid_dict[id1],centroid_dict[id2])

using solution from here , it is kd tree graph problem使用here的解决方案,这是kd树图问题

Find the Euclidean distances between four 2-D coordinates:求四个二维坐标之间的欧几里得距离:

from scipy.spatial import distance
coords = [(35.0456, -85.2672),
          (35.1174, -89.9711),
          (35.9728, -83.9422),
          (36.1667, -86.7833)]
distance.cdist(coords, coords, 'euclidean')

array([[ 0.    ,  4.7044,  1.6172,  1.8856],
   [ 4.7044,  0.    ,  6.0893,  3.3561],
   [ 1.6172,  6.0893,  0.    ,  2.8477],
   [ 1.8856,  3.3561,  2.8477,  0.    ]])

You can do something like this.你可以做这样的事情。 No import required.无需导入。

def dist(key1,key2):
    return calculateDistance(*centroid_dict[key1],*centroid_dict[key2])

all_dist = []
for key1 in centroid_dict:
    all_dist.extend([dist(key1,key2) for key2 in centroid_dict if not key1==key2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历坐标列表并在 Python 中计算它们之间的距离 - How to iterate through a list of coordinates and calculate distance between them in Python 如何计算坐标之间的成对harsine距离 - How to calculate the pairwise haversine distance between coordinates 如何计算存储在列表中的坐标之间的距离? - How to calculate distance between coordinates stored in a list? 如何遍历坐标列表并计算它们之间的距离? - How to iterate through a list of coordinates and calculate distance between them? 计算地球上两个坐标之间的距离 - Calculate distance between two coordinates on a globe 如何使用python获取两个地理坐标之间的行驶距离? - How to get the driving distance between two geographical coordinates using python? 如何使用OSM和python查找两个坐标之间的路径和距离? - How to find path and distance between two coordinates using OSM and python? 如何将坐标捕捉到道路并计算距离 - How to snap coordinates to road and calculate distance 如何使用python中的返回方法计算两点之间的距离? - How to calculate the distance between two points using return methods in python? 如何计算Python中两个累积样本分布之间的最大距离? - How to calculate the largest distance between two cumulative sample distributions in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM