用python 求两地的直线距离

通过百度,搜索到了球面直线距离计算公式

比如你要求point1(lng1,lat1) 和 point2(lng2,lat2)的距离Distance,那么

Distance = R*arccos(cos(lat1*pi/180 )*cos(lat2*pi/180)*cos(lng1*pi/180 -lng2*pi/180)+sin(lat1*pi/180 )*sin(lat2*pi/180))

其中,R=6370996.81 是地球半径,pi为圆周率π,(lng1,lat1),(lng2,lat2)分别是point1和point2的经纬度

R = 6371004 #The radius of the earth 
PI = 3.1415926 
MAX_NUM = 10000 
def get_distance(points):
    length = len(points)
    if length <= 1:
        raise PathException('The length of points must be greater than or equal to 2')
  
    distance =  [[0 for col in range(length)] for row in range(length)]
    for i in range(0,length-1):
        for j in range(i+1,length):  
            f1 = points[i].find('(') + 1
            f2 = points[i].find(',')
            f3 = points[i].find(')')
            lnga = float(points[i][f1:f2]) * PI /180
            lngb = float(points[j][f1:f2]) * PI /180
            lata = float(points[i][f2+1:f3]) * PI /180
            latb = float(points[j][f2+1:f3]) * PI /180  
            C1 = math.cos(lata)*math.cos(latb)*math.cos(lnga-lngb)
            C2 = C1 + math.sin(lata)*math.sin(latb)
            D = R * math.acos(C2)
            distance[i][j] = round(D,2)
            distance[j][i] = distance[i][j]  
        distance[i][i] = MAX_NUM   
    distance[length-1][length-1] = MAX_NUM  
    return distance
 
 

其中,输入的参数points,格式如下:

 points = ['Point(123.4201360000,41.7713180000)',#致用路 1
              'Point(123.4264960000,41.7752190000)',#北门 2
              'Point(123.4262980000,41.7736180000)',#信息科学与工程学院 3
              'Point(123.4262620000,41.7722200000)',#恩承图书馆 4
              'Point(123.4279150000,41.7706590000)',#材料与冶金学院 5
              'Point(123.4250770000,41.7693010000)',#求实路 6
              'Point(123.4289570000,41.7684940000)',#知行楼 7
              'Point(123.4234060000,41.7722200000)'#汉卿北路 左8
              'Point(123.4292630000,41.7719100000)' #汉卿北路 右8
              ]

你可能感兴趣的:(python,距离,经纬度)