python 计算两个点之间的距离和方向角,经纬度

代码借自:https://blog.csdn.net/zhuqiuhui/article/details/53180395

夹角示意图说明:

import math# 两个经纬度之间的距离
def LatLng2Dist(LatZero,LngZero,Lat,Lng):ra = 6378140  # radius of equator: meterrb = 6356755  # radius of polar: meterflatten = (ra - rb) / ra  # Partial rate of the earth# change angle to radiansradLatA = math.radians(LatZero)radLonA = math.radians(LngZero)radLatB = math.radians(Lat)radLonB = math.radians(Lng)pA = math.atan(rb / ra * math.tan(radLatA))pB = math.atan(rb / ra * math.tan(radLatB))x = math.acos(math.sin(pA) * math.sin(pB) + math.cos(pA) * math.cos(pB) * math.cos(radLonA - radLonB))c1 = (math.sin(x) - x) * (math.sin(pA) + math.sin(pB))**2 / math.cos(x / 2)**2c2 = (math.sin(x) + x) * (math.sin(pA) - math.sin(pB))**2 / math.sin(x / 2)**2dr = flatten / 8 * (c1 - c2)distance = ra * (x + dr)return distance# 点1到点2方向沿逆时针方向转到正北方向的夹角
def LatLng2Degree(LatZero,LngZero,Lat,Lng):"""Args:point p1(latA, lonA)point p2(latB, lonB)Returns:bearing between the two GPS points,default: the basis of heading direction is north"""radLatA = math.radians(LatZero)radLonA = math.radians(LngZero)radLatB = math.radians(Lat)radLonB = math.radians(Lng)dLon = radLonB - radLonAy = 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) % 360return brngif __name__ == "__main__":LatLng2Degree(40,116,40,117)

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部