Manhattan Distance for two geolocations

前端 未结 2 1794
有刺的猬
有刺的猬 2021-02-10 14:07

Let\'s say I have two locations represented by latitude and longitude. Location 1 : 37.5613 , 126.978 Location 2 : 37.5776 , 126.973

相关标签:
2条回答
  • 2021-02-10 14:38

    For example, calculating Manhattan Distance of Point1 and Point2. Simply apply LatLng distance function by projecting the "Point2" on to the same Lat or Lng of the "Point1".

    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))
    
    0 讨论(0)
  • 2021-02-10 14:59

    Given a plane with p1 at (x1, y1) and p2 at (x2, y2), it is, the formula to calculate the Manhattan Distance is |x1 - x2| + |y1 - y2|. (that is, the difference between the latitudes and the longitudes). So, in your case, it would be:

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

    EDIT: As you have said, that would give us the difference in latitude-longitude units. Basing on this webpage, this is what I think you must do to convert it to the metric system. I haven't tried it, so I don't know if it's correct:

    First, we get the latitude difference:

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

    Now, the longitude difference:

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

    Now, we will use the haversine formula. In the webpage it uses a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2), but that would give us a straight-line distance. So to do it with Manhattan distance, we will do the latitude and longitude distances sepparatedly.

    First, we get the latitude distance, as if longitude was 0 (that's why a big part of the formula got ommited):

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

    Now, the longitude distance, as if the latitude was 0:

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

    Finally, just add up |latitudeDistance| + |longitudeDistance|.

    0 讨论(0)
提交回复
热议问题