如何解决Python距离以英里为单位到两个gps坐标之间的欧式距离
我正在尝试提出一个功能,...
输入:测地距离,以英里或公里为单位
输出:任意两个gps点之间的欧式距离,即输入距离
我觉得我有一些组成部分
import numpy as np
from numpy import linalg as LA
from geopy.distance import geodesic
loc1 = np.array([40.099993,-83.166000])
loc2 = np.array([40.148652,-82.903962])
这是这两点之间的欧式距离
LA.norm(loc1-loc2)
#0.2665175636332336
这是这两个点之间的测地距离(以英里为单位)
geodesic(loc1,loc2).miles
#14.27909749425243
我的大脑现在快没水了,任何人对我如何实现这样的功能都有任何想法:
geodesic_to_euclidean(14.27909749425243)
#0.2665175636332336
解决方法
如评论中所述,如果您对圆弧距离没事,那么应该可以。这是正弦距离:
def haversine(origin,destination,units='mi'):
# Radian deltas
origin_lat = radians(float(origin[0]))
origin_lon = radians(float(origin[1]))
destination_lat = radians(float(destination[0]))
destination_lon = radians(float(destination[1]))
lat_delta = destination_lat - origin_lat
lon_delta = destination_lon - origin_lon
# Radius of earth in meters
r = 6378127
# Haversine formula
a = sin(lat_delta / 2) ** 2 + cos(origin_lat) * \
cos(destination_lat) * sin(lon_delta / 2) ** 2
c = 2 * asin(sqrt(a))
meters_traveled = c * r
scaling_factors = {
"m:": 1,"km": 1 / 1000,"ft": 3.2808,# meters to feet
"mi:": 0.000621371 # meters to miles
}
return meters_traveled * scaling_factors[units]
如果您已经有了以米为单位的测地线(大圆)距离,并且想要弦长,那么可以执行以下操作
def chord(geodesic_distance):
"""
Chord length
C = 2 * r * sin(theta/2)
Arc length; which is geodesic distance in this case
AL = R * theta
therefore
C = 2 * R * sin(AL/(2*R))
"""
r = 6378127 # Radius of earth in meters
return 2 * r * sin(geodesic_distance / (2 * r))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。