我编写了一个python函数,它将使用google api计算两点之间的距离。功能如下所示:
导入urllib 将json导入为simplejson
def calculateDistance(sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude):
sourceCoordinates = sourceLatitude, sourceLongitude
destinationCoordinates = destinationLatitude, destinationLongitude
url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(str(sourceCoordinates),str(destinationCoordinates))
result= simplejson.load(urllib.urlopen(url))
resulting_distance = result['rows'][0]['elements'][0]['distance']['text']
print "The distance is: " + resulting_distance
#calculateDistance(5.341612, -3.913602,5.342044, -4.143452)
#calculateDistance(5.342044, -4.143452,5.341612, -3.913602)
我有一个tsv文件,如下所示:
-4.143452 5.342044
-3.913602 5.341612
-3.967045 5.263331
....
....
第一列代表纬度,第二列代表纬度。
现在,我想将这些值的每个组合传递给我的函数以获得点之间的距离。有人能告诉我一个有效的方法吗? tsv文件非常庞大。
答案 0 :(得分:0)
Points=[]
with open('file.tsv','r') as f:
for line in f:
Points.append(line.strip().split('\t'))
for combo in itertools.combinations(Points, 2):
calculateDistance(combo[0][0], combo[0][1], combo[1][0], combo[1][1])