Python:- 给定元组坐标列表,找到最接近指定坐标的坐标(Google 地图坐标))

Python:- Given a list of tuple coordinates, find the nearest coord to a specified coord (Google Maps Coords))

我正在使用 Python 并完成了以下操作,给定元组坐标列表,找到最接近指定坐标(Google 地图坐标)的坐标)。

但与我的代码中的 google 地图相比,最近的坐标不准确。请帮助我。

这是我的代码

def find_coords(c, l):
    tmp_list = []
    (x,y) = l[0]
    for (a,b) in l[1:]:
            if (x-c[0])**2 + (y-c[1])**2 > (a-c[0])**2 + (b-c[1])**2:
                    (x,y) = (a,b)
                    tmp_list.append((x,y))                  
    return tmp_list 

ccoordinate_list = [(11.6702634, 72.313323), (11.6723698, 78.114523), (31.67342698, 78.465323), (12.6702634, 72.313323), (12.67342698, 75.465323)]

coordinate = (11.6723698, 78.114523)

while coordinate_list[1:]:      
     coordinate_list = find_coords(coordinate, coordinate_list)

您的结果与 Google 不同的原因是您使用的是平面的距离公式(毕达哥拉斯定理),但地球不是平面,纬度和经度也不是Cartesian coordinate system.

对于不太靠近两极的区域中的小距离,该平坦公式是可行的。为了获得更好的结果,您可以使用 great circle distance 公式。

为了获得更高的准确性,您需要考虑到地球并不是一个完美的球体这一事实:它的两极有些扁平,使其成为一个椭圆体(几乎)。要计算椭圆体上的距离,您可以使用 Vincenty's formulae。即使那些公式并不完全准确,但误差很小。

更新

现代不推荐使用 Vincenty 的公式。它们的历史可追溯至 1970 年代,旨在用于当时的台式计算器。我们现在可以获得更好的计算技术,并且有更好的方法来计算椭圆体上的距离。我推荐 C. F. F. Karney 的 geographiclib. Dr Karney is a major contributor to the Wikipedia articles on geodesics, in particular Geodesics on an ellipsoid, as I mentioned in ,它有一个使用 geographiclib 计算 WGS84 参考椭球体上两点之间距离的简短演示,以及 link 更广泛的示例。

如果您想找到最近的地理坐标,您应该使用特定的地理坐标结构(请参阅 geopy)。针对这种情况,我提出以下解决方案:

import geopy
import geopy.distance
# your data    
ccoordinate_list = [(11.6702634, 72.313323), (11.6723698, 78.114523), (31.67342698, 78.465323), (12.6702634, 72.313323), (12.67342698, 75.465323)]
coordinate = (11.6723698, 78.114523)
# the solution
pts = [ geopy.Point(p[0],p[1]) for p in ccoordinate_list ]
onept = geopy.Point(coordinate[0],coordinate[1])
alldist = [ (p,geopy.distance.distance(p, onept).km) for p in pts ]
nearest_point = min(alldist, key=lambda x: (x[1]))[0] # or you can sort in by distance with sorted function

请注意,地球上的欧几里得度量(如您的示例)可能不正确。