python计算两经纬度坐标距离和角度以及给定第一个坐标、距离和航向角计算第二个坐标

python计算两经纬度坐标距离和角度以及给定第一个坐标、距离和航向角计算第二个坐标,第1张

总结三个坐标相关公式

因为需要用到三角函数,导入math库

import math

两个坐标计算距离的函数

def distance(Alongitude, Alatitude, Blongitude, Blatitude):
    AEarth = 6378137.0
    DEG_TO_RAD = 1.7453292519943e-2
    RAD_to_DEG = 57.295779513082
    startLat = Alatitude * DEG_TO_RAD
    endLat = Blatitude * DEG_TO_RAD
    dLat = (Blatitude - Alatitude) * DEG_TO_RAD
    dLon = (Blongitude - Alongitude) * DEG_TO_RAD
    a = math.sin(dLat / 2) * (math.sin(dLat / 2)) + math.sin(dLon / 2) * math.sin(dLon / 2) * math.cos(
        startLat) * math.cos(endLat)
    c = 2 * math.asin(min(1.0, math.sqrt(a)))
    return AEarth * c

两坐标计算角度的函数

def degree(Alongitude, Alatitude, Blongitude, Blatitude):
    radLatA = math.radians(Alatitude)
    radLonA = math.radians(Alongitude)
    radLatB = math.radians(Blatitude)
    radLonB = math.radians(Blongitude)
    dLon = radLonB - radLonA
    y = math.sin(dLon) * math.cos(radLatB)
    x = math.cos(radLatA) * math.sin(radLatB) - math.sin(radLatA) * math.cos(radLatB) * math.cos(dLon)
    brng = math.degrees(math.atan2(y, x))
    brng = (brng + 360) % 360
    return brng

给定第一点经纬度、航向角和距离,计算第二点坐标函数

def CalculateCoordinates(lon, lat, brng, dist):
    lat1 = lat * math.pi / 180
    lon1 = lon * math.pi / 180
    brg = brng * math.pi / 180

    # 扁率
    flat = 298.257223563
    # 地球 半长轴
    a = 6378137.0
    # 地球 半短轴
    b = 6356752.314245

    f = 1 / flat
    sb = math.sin(brg)
    cb = math.cos(brg)
    tu1 = (1 - f) * math.tan(lat1)
    cu1 = 1 / math.sqrt((1 + tu1 * tu1))
    su1 = tu1 * cu1
    s2 = math.atan2(tu1, cb)
    sa = cu1 * sb
    csa = 1 - sa * sa
    us = csa * (a * a - b * b) / (b * b)
    A = 1 + us / 16384 * (4096 + us * (-768 + us * (320 - 175 * us)))
    B = us / 1024 * (256 + us * (-128 + us * (74 - 47 * us)))
    s1 = dist / (b * A)
    s1p = 2 * math.pi
    cs1m = 0.0
    ss1 = 0.0
    cs1 = 0.0
    ds1 = 0.0
    #
    while abs(s1 - s1p) > 1e-12:
        cs1m = math.cos(2 * s2 + s1)
        ss1 = math.sin(s1)
        cs1 = math.cos(s1)
        ds1 = B * ss1 * (cs1m + B / 4 * (cs1 * (-1 + 2 * cs1m * cs1m) - B / 6 * cs1m * (-3 + 4 * ss1 * ss1) * (-3 + 4 * cs1m * cs1m)))
        s1p = s1
        s1 = dist / (b * A) + ds1

    t = su1 * ss1 - cu1 * cs1 * cb
    lat2 = math.atan2(su1 * cs1 + cu1 * ss1 * cb, (1 - f) * math.sqrt(sa * sa + t * t))
    l2 = math.atan2(ss1 * sb, cu1 * cs1 - su1 * ss1 * cb)
    c = f / 16 * csa * (4 + f * (4 - 3 * csa))
    l = l2 - (1 - c) * f * sa * (s1 + c * ss1 * (cs1m + c * cs1 * (-1 + 2 * cs1m * cs1m)))
    lon2 = lon1 + l
    Longitude = lon2 * 180 / math.pi
    Latitude = lat2 * 180 / math.pi
    return Longitude, Latitude

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/727585.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-26
下一篇2022-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存