如何使用python比较大圆距与两个球体点的欧几里得距离? [英] How to compare great circle distance with euclidean distance of two sphere points using python?
问题描述
我正在尝试检查您用欧几里德距离而不是大圆距离(gcd)计算地球上两点的距离时引入的误差.我有两个要点,分别是它们的纬度和经度. 我将python geopy框架用于大圆距离.这是gcd的代码:
I am trying to check the error that is introduced when you compute the distance of two points on earth with the euclidean distance instead of using the great circle distance (gcd). I have two points that are defined by their lattitude and longtitude. I used the python geopy framework for the great circle distance. Here the code for the gcd:
def measure(self, a, b):
a, b = Point(a), Point(b)
lat1, lng1 = radians(degrees=a.latitude), radians(degrees=a.longitude)
lat2, lng2 = radians(degrees=b.latitude), radians(degrees=b.longitude)
sin_lat1, cos_lat1 = sin(lat1), cos(lat1)
sin_lat2, cos_lat2 = sin(lat2), cos(lat2)
delta_lng = lng2 - lng1
cos_delta_lng, sin_delta_lng = cos(delta_lng), sin(delta_lng)
d = atan2(sqrt((cos_lat2 * sin_delta_lng) ** 2 +
(cos_lat1 * sin_lat2 -
sin_lat1 * cos_lat2 * cos_delta_lng) ** 2),
sin_lat1 * sin_lat2 + cos_lat1 * cos_lat2 * cos_delta_lng)
return self.RADIUS * d
所以还是两点:
p1 = [39.8616,-75.0748],p2 = [-7.30933,112.76]
p1=[39.8616,-75.0748], p2=[-7.30933,112.76]
gcd = 78.8433004543197 klm
使用geopy的great_circle(p1,p2).kilometers
函数
然后我使用以下公式将这两个点转换为笛卡尔坐标:
I then transformed these two points in cartesian coordinates using this formula:
def spherical_to_cartesian(r,la,lo):
x=r*np.sin(90-la)*np.cos(lo)
y=r*np.sin(90-la)*np.sin(lo)
z=r*np.cos(90-la)
return (x,y,z)
其中r=6372.795
,这将导致以下笛卡尔坐标
where r=6372.795
, which results in the following cartesians coordinates
p1=[ -765.81579368, -256.69640558, 6321.40405587],
p2=[480.8302149,-168.64726394,-6352.39140142]
然后通过键入:np.linalg.norm(p2-p1)
我得到了1103.4963114787836
作为其欧几里得范数,与来自gcd的〜78klm相比,这似乎并不合理.我是说错了吗?
Then by typing: np.linalg.norm(p2-p1)
i am getting 1103.4963114787836
as their euclidean norm which doesn't seem reasonable compared with ~78klm from the gcd. Am i inffering sth wrong?
推荐答案
Python在math软件包中包含两个函数;弧度将度转换为弧度,度将弧度转换为度.
Python includes two functions in the math package; radians converts degrees to radians, and degrees converts radians to degrees.
sin()方法以弧度返回x的正弦.
The method sin() returns the sine of x, in radians.
import math
def spherical_to_cartesian(r,la,lo):
rlo = math.radians(lo)
rla = math.radians(90-la)
x=r*np.sin(rla)*np.cos(rlo)
y=r*np.sin(rla)*np.sin(rlo)
z=r*np.cos(rla)
return (x,y,z)
这篇关于如何使用python比较大圆距与两个球体点的欧几里得距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!