经纬度计算距离与方位角

距离

根据经纬度计算距离是有固定公式的,按照公式写个函数很简单。

import math
def cal_dis(lat1, lon1,lat2, lon2):
    latitude1 = (math.pi/180)*lat1
    latitude2 = (math.pi/180)*lat2
    longitude1 = (math.pi/180)*lon1
    longitude2= (math.pi/180)*lon2
    #因此AB两点的球面距离为:{arccos[sinb*siny+cosb*cosy*cos(a-x)]}*R
    #地球半径
    R = 6378.137
    d = math.acos(math.sin(latitude1)*math.sin(latitude2)+ math.cos(latitude1)*math.cos(latitude2)*math.cos(longitude2-longitude1))*R
    return d


if __name__ == '__main__':
    print cal_dis(23.0,101.1,23.06,113.34)

注意,经纬度是角度,而三角函数的输入是弧度,角度与弧度的关系  180=pi

 

有一次我在一个项目中发现 matlab 的计算距离和上面这个函数有些出入,虽然相差不大,但是确实不一样,当时也折腾了好久,后来发现 python 有个自带的函数计算结果和 matlab 相同。

from geopy.distance import geodesic

geodesic((30.28708,120.12802999999997), (28.7427,115.86572000000001)).m

 

方位角

def calc_azimuth(lat1, lon1, lat2, lon2):
    lat1_rad = lat1 * math.pi / 180
    lon1_rad = lon1 * math.pi / 180
    lat2_rad = lat2 * math.pi / 180
    lon2_rad = lon2 * math.pi / 180

    y = math.sin(lon2_rad - lon1_rad) * math.cos(lat2_rad)
    x = math.cos(lat1_rad) * math.sin(lat2_rad) - \
        math.sin(lat1_rad) * math.cos(lat2_rad) * math.cos(lon2_rad - lon1_rad)

    brng = math.atan2(y, x) * 180 / math.pi

    return float((brng + 360.0) % 360.0)

所谓方位角是与正北方向、顺时针之间的夹角

转载于:https://www.cnblogs.com/yanshw/p/11382501.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值