python已知经纬度,求大圆弧距离以及两条大圆弧交点的经纬度(numpy实现)
python球面几何的几个实用的子函数(numpy实现)
如果不考虑地球的椭率,把地球近似为球体,下面的python程序可以快速计算出求球面上两点之间的大圆弧距离,以及两条大圆弧的交点。
原理:球坐标系转为笛卡尔坐标系,然后进行矢量叉乘
import numpy as np
def sph2car(lon,lat):
# input deg output 1
i = np.deg2rad( lat )
a = np.deg2rad( lon )
z = np.sin( lat )
x = np.cos( i ) * np.cos( a )
y = np.cos( i ) * np.sin( a )
return np.array([x,y,z])
def car2sph(x,y,z):
# input 1 output deg
l3 = np.sqrt( x*x+y*y+z*z )
l2 = np.sqrt( x*x+y*y )
lat = np.arccos( l2/l3 )
lon = np.arctan( y / x )
lat = np.rad2deg( lat )
lon = np.rad2deg( lon )
return np.array([lon,lat])
求两点之间的大圆弧长度
def points2arc(p1,p2):
# p1/2=(lon,lat), get their arc distance in deg
n1 = sph2car( *p1 )
n2 = sph2car( *p2 )
co = np.dot( n1 , n2 ) # |n1|=1 and |n2|=1
return np.rad2deg(np.arccos(co))
p1 = np.array([ 156.8, 39.0 ])
p2 = np.array([ -85.2, 34.3 ])
dis = points2arc(p1,p2)
print("The distance between ", p1, "and", p2, "is", dis, "deg")
求两条大圆弧之间的交点
def cross_greatcircles(p1,p2,p3,p4):
# get the cross point of the line p1-p2 and line p3-p4
# get the normal vector of the plane O-P1-P2
n1 = sph2car( *p1 ) #p1 vector
n2 = sph2car( *p2 ) #p2 vector
n12 = np.cross( n1, n2 )
# get the normal vector of the plane O-P3-P4
n3 = sph2car( *p3 ) #p3 vector
n4 = sph2car( *p4 ) #p4 vector
n34 = np.cross( n3, n4 )
# get their normal vector perpendicular to n12 and n34
n = np.cross( n12, n34 )
c = car2sph( *n )
return c
p1 = np.array([ 156.8, 39.0 ])
p2 = np.array([ -85.2, 34.3 ])
p3 = np.array([ -56.8, -9.0 ])
p4 = np.array([ 115.2, 14.3 ])
pn = cross_greatcircles(p1,p2,p3,p4)
print("Their cross point is", pn)
本文来自博客园,作者:Philbert,转载请注明原文链接:https://www.cnblogs.com/liangxuran/p/16088073.html