python3-经纬度的相关计算

来源:互联网 发布:php打印九九乘法表 编辑:程序博客网 时间:2024/05/16 10:28

一.计算两个点之间的距离(经纬度的计算)

#from math import radians, cos, sin, asin, sqrtfrom math import *def haversine(lon1, lat1, lon2, lat2): # 经度1,纬度1,经度2,纬度2 (十进制度数)    # 将十进制度数转化为弧度    # math.degrees(x):为弧度转换为角度    # math.radians(x):为角度转换为弧度    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])    # haversine公式    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 # 地球平均半径,单位为公里    return c * r*1000if __name__=='__main__':    a=haversine(103.85012,36.03653,103.84085,36.03842)    print(a)#注:该结果的数值存在问题

二.计算多个经纬度的中心

#通过经纬度,找出中心点from math import *def center_geolocation(geolocations):    x = 0    y = 0    z = 0    lenth = len(geolocations)    for lon, lat in geolocations:        lon = radians(float(lon))        lat = radians(float(lat))        x += cos(lat) * cos(lon)        y += cos(lat) * sin(lon)        z += sin(lat)    x = float(x / lenth)    y = float(y / lenth)    z = float(z / lenth)    return (degrees(atan2(y, x)), degrees(atan2(z, sqrt(x * x + y * y))))if __name__ == '__main__':    locations = [[116.568627, 39.994879], [116.564791, 39.990511], [116.575012, 39.984311]]    print(center_geolocation(locations))##结果(116.56947685224392, 39.98990040970147)
原创粉丝点击