簡體   English   中英

用於查找兩個 GPS 坐標之間的最短路徑的 Python 代碼

[英]Python code for finding shortest path between two GPS coordinates

我有一些 GPS 坐標。 如何使用python代碼或其他找到兩個gps點之間的最短路徑?

如果你的觀點更接近。 然后我會從 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])

您還可以使用半正弦公式獲得兩個 GPS 點之間的距離。 這是為了獲得球體上長距離的長度。:

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

這兩個函數都返回儀表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM