两个地理位置的曼哈顿距离

Manhattan Distance for two geolocations

假设我有两个由纬度和经度表示的位置。 位置 1:37.5613126.978 位置 2:37.5776126.973

如何使用曼哈顿距离计算距离?

编辑:我知道计算曼哈顿距离的公式,如 Emd4600 在答案 |x1-x2| - |y1-y2| 中所述,但我认为它适用于笛卡尔。如果可以直接应用 |37.5613-37.5776| + |126.978-126.973| 结果的距离单位是多少?

给定一个p1(x1, y1)p2(x2, y2)的平面,计算曼哈顿距离的公式是|x1 - x2| + |y1 - y2|。 (即纬度和经度之间的差异)。因此,在您的情况下,它将是:

|126.978 - 126.973| + |37.5613 - 37.5776| = 0.0213

编辑:正如您所说,这会给我们带来纬度-经度单位的差异。基于 this webpage,我认为这是将其转换为公制必须执行的操作。没试过,不知道对不对:

首先,我们得到纬度差:

Δφ = |Δ2 - Δ1|
Δφ = |37.5613 - 37.5776| = 0.0163

现在,经度差:

Δλ = |λ2 - λ1|
Δλ = |126.978 - 126.973| = 0.005

现在,我们将使用 haversine 公式。在网页中它使用 a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2),但这会给我们一个直线距离。所以要用曼哈顿距离来做,我们将分开做纬度和经度距离。

首先,我们得到纬度距离,就好像经度是 0(这就是为什么公式的很大一部分被省略的原因):

a = sin²(Δφ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
latitudeDistance = R ⋅ c // R is the Earth's radius, 6,371km

现在,经度距离,好像纬度是0:

a = sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
longitudeDistance = R ⋅ c // R is the Earth's radius, 6,371km

最后就是加起来|latitudeDistance| + |longitudeDistance|.

例如计算Point1和Point2的曼哈顿距离。 只需将 "Point2" 投影到 "Point1".

的相同纬度或经度即可应用 LatLng 距离函数
def distance(lat1, lng1, lat2, lng2, coordinates):

    lat1 = radians(lat1)
    lat2 = radians(lat2)
    lon1 = radians(lng1)
    lon2 = radians(lng2)
    d_lon = lon2 - lon1
    d_lat = lat2 - lat1

    if coordinates['LatLong']:
        r = 6373.0
        a = (np.sin(d_lat/2.0))**2 + np.cos(lat1) * \
            np.cos(lat2) * (np.sin(d_lon/2.0))**2
        c = 2 * np.arcsin(np.sqrt(a))
        total_distance = r * c

    if coordinates['XY']:
        total_distance = math.sqrt(d_lon * d_lon + d_lat * d_lat)
    return total_distance

def latlng2manhattan(lat1, lng1, lat2, lng2):
    coordinates = {"LatLong": True, "XY": False}
    # direction = 1
    if lat1 == 0:
        lat1 = lat2
        # if lng1 < lng2:
            # direction = -1
    if lng1 == 0:
        lng1 = lng2
        # if lat1 < lat2:
            # direction = -1
    # mh_dist = direction * distance(lat1, lng1, lat2, lng2, coordinates) * 3280.84 # km to ft
    mh_dist = distance(lat1, lng1, lat2, lng2, coordinates) * 3280.84
    return mh_dist

df["y_mh"] = df["y_lat"].apply(lambda x: latlng2manhattan(0, x, center_long, center_lat))
df["x_mh"] = df["x_long"].apply(lambda x: latlng2manhattan(x, 0, center_long, center_lat))