Python距离(以英里为单位)到两个gps坐标之间的欧几里得距离

2024-03-29 08:41:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试提出一个函数,其中…
输入:以英里或公里为单位的测地距离
输出:任意两个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

Tags: 函数fromimportnumpy距离asnp组件
1条回答
网友
1楼 · 发布于 2024-03-29 08:41:58

如评论中所述,如果你对一个大的圆距离感到满意,那么这应该是可行的。这是哈弗森距离:

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))

相关问题 更多 >