简体   繁体   English

用于查找两个 GPS 坐标之间的最短路径的 Python 代码

[英]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.

相关问题 查找 GPS 坐标集之间的最短距离 - Python - Find the Shortest Distance between sets of GPS Coordinates - Python 在图中找到两个短语之间的最短路径 - finding shortest path between two phrases in a graph 找到两点之间的最短距离 Python - Finding the shortest distance between two points Python 在Python中查找两个gps点之间的距离 - Finding distance between two gps points in Python 如何找到二维数组中两个坐标之间的最短路径? - How to find the shortest path between two coordinates in a 2-dimensional array? 在负权重的加权DAG中找到两个节点之间的最短路径 - Finding shortest path between two nodes in a weighted DAG with negative weights 与父母一起寻找两个顶点之间的最短路径 object - Finding shortest Path between two verticies with Parents object 在 Python 中找到坐标系中某些点之间的最短路径 - Finding the Shortest Path Between Certain Points in the Coordinate System in Python Python 距离(以英里为单位)到两个 gps 坐标之间的欧几里得距离 - Python distance in miles to euclidean distance between two gps coordinates 如何计算 Python 中两个 Z8​​C578DE37278ADA488D763EA86C5CF20Z 坐标(纬度和经度)之间的 RMSE - How to compute RMSE between two GPS coordinates (latitude and longitude) in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM