csv文件中的Python距离矩阵

时间:2016-08-04 09:25:47

标签: python-2.7 matrix distance

我目前有一个csv文件,其名称,纬度和经度为2000个不同的位置。我需要使用Python为每个位置创建一个距离矩阵到另一个位置。我非常新,不知道从哪里开始。请帮忙

`from math import radians, cos, sin, asin, sqrt
import csv

with open('DBN.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter = ',')
for row in readCSV:
    for i in readCSV:
        distances =[]
        lon1 = float(row[1])
        lat1= float(row[2])
        lon2= float(i[1])
        lat2= float(i[2])
        lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
        dlon = lon2 - lon1 
        dlat = lat2 - lat1 
        a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
        c = 2 * asin(sqrt(a)) 
        r = 6371 # Radius of earth in kilometers. Use 3956 for miles
        dist = c * r
        distances.append(dist)
        print distances`

0 个答案:

没有答案