Python: - 给出一个元组坐标列表,找到最近的指定坐标的坐标(Google Maps Coords)) [英] Python:- Given a list of tuple coordinates, find the nearest coord to a specified coord (Google Maps Coords))
问题描述
我使用Python并且做了以下事情,给定一个元组坐标列表,找到最近的坐标到指定的坐标(Google Maps Coords))。
<与我的代码上的谷歌地图相比,并不准确的最近坐标。请帮助我。
这是我的代码
def find_coords (a,b)in l [1:]:
tmp_list = []
(x,y)= l [0]
if( xc [0])** 2 +(yc [1])** 2> (ac [0])** 2 +(bc [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)
如果您想查找最近的地理坐标,您应该使用特定的地理坐标结构(请参阅 geopy )。在这种情况下,我提出了以下解决方案:
import geopy
import geopy.distance
#your数据
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)
解决方案
pts = [geopy.Point(p [0],p [1])for c 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]#或者你可以用排序函数按距离排序
请注意,欧几里德度量(如你的例子)在地球上可能不正确。
I'm Using Python and done something with the following, Given a list of tuple coordinates, find the nearest coord to a specified coord (Google Maps Coords)).
But not accurate nearest coordinates compared with google maps on my code. Please help me.
Here is my code
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)
If you want to find the nearest geo coord you should use specific geo-coordinate structures (please see geopy ). In this case, I propose the following solution:
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
Please note that Euclidean metric (as in your example) may be incorrect on the globe.
这篇关于Python: - 给出一个元组坐标列表,找到最近的指定坐标的坐标(Google Maps Coords))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!