zoukankan      html  css  js  c++  java
  • 经纬度计算距离与方位角

    距离

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

    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)

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

  • 相关阅读:
    Python即时网络爬虫项目启动说明
    Spring Cloud Consul入门
    Selenium用法示例
    云中漫步,做个公众号方便生活、取悦自己
    PhantomJS用法示例
    Python3环境安装PySpider爬虫框架过程
    Python3环境安装Scrapy爬虫框架过程及常见错误
    爬虫扒下 bilibili 视频信息
    Sql优化(一) Merge Join vs. Hash Join vs. Nested Loop
    email.py
  • 原文地址:https://www.cnblogs.com/yanshw/p/11382501.html
Copyright © 2011-2022 走看看