假设我有两个以纬度和经度表示的位置。
位置1:37.5613
,126.978
位置2:37.5776
,126.973
如何使用曼哈顿距离计算距离?
编辑:我知道计算曼哈顿距离的公式,如Emd4600
所述,答案为|x1-x2| - |y1-y2|
,但我认为这是笛卡儿的。如果可以直接应用|37.5613-37.5776| + |126.978-126.973|
那么结果的距离单位是什么?
答案 0 :(得分:6)
如果p1
的{{1}}和(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|
。
答案 1 :(得分:2)
例如,计算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))