python使用欧式距离_如何使用python比较大圆距和两个球体点的欧式距离?

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

So or two points:

p1=[39.8616,-75.0748], p2=[-7.30933,112.76]

the

gcd = 78.8433004543197 klm

using the great_circle(p1,p2).kilometers function from geopy

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)

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]

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 includes two functions in the math package; radians converts degrees to radians, and degrees converts radians to degrees.

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值