[英]Python code for finding shortest path between two GPS coordinates
I have some GPS coordinates.我有一些 GPS 坐标。 How can I find the shortest path between two gps points by using python code or other?如何使用python代码或其他找到两个gps点之间的最短路径?
If your points are closer together.如果你的观点更接近。 Then i would just convert from WGS84 to ECEF:然后我会从 WGS84 转换为 ECEF:
ny_pos = [40.7648, -73.9808] # GPS coords New York
la_pos = [34.0544, -118.2439] # GPS coords Los Angeles
def WGS84_to_ECEF(lat, lon, alt):
# convert to radians
rad_lat = lat * (np.pi / 180.0)
rad_lon = lon * (np.pi / 180.0)
a = 6378137.0
# f is the flattening factor
finv = 298.257223563
f = 1 / finv
# e is the eccentricity
e2 = 1 - (1 - f) * (1 - f)
# N is the radius of curvature in the prime vertical
N = a / np.sqrt(1 - e2 * np.sin(rad_lat) * np.sin(rad_lat))
x = (N + alt) * np.cos(rad_lat) * np.cos(rad_lon)
y = (N + alt) * np.cos(rad_lat) * np.sin(rad_lon)
z = (N * (1 - e2) + alt) * np.sin(rad_lat)
return np.array([x, y, z])
ny_xyz = WGS84_to_ECEF(ny_pos[0], ny_pos[1], 0)
la_xyz = WGS84_to_ECEF(la_pos[0], la_pos[1], 0)
# Vector from NY to LA
la_xyz - ny_xyz
# array([3838314.23415231, 10238.84453052, 591228.02180622])
You can also get the distance between two GPS points with the haversine formula.您还可以使用半正弦公式获得两个 GPS 点之间的距离。 This is to get the length for long distances on a sphere.:这是为了获得球体上长距离的长度。:
def calc_haversine(lat1, lon1, lat2, lon2):
RADIUS = 6_367_000
lat1, lon1, lat2, lon2 = map(np.radians, [lat1, lon1, lat2, lon2])
dlat = lat2 - lat1
dlon = lon2 - lon1
a = np.sin(dlat/2)**2 + \
np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2
dist = 2 * RADIUS * np.arcsin(a**0.5)
return dist
calc_haversine(ny_pos[0], ny_pos[1], la_pos[0], la_pos[1])
# 3934940.72281424 meters
Both functions return meters.这两个函数都返回仪表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.