Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Manhattan Distance with haversine formula for geolocalizations is not accurate? [Python]

I want to compute the "MANHATTAN DISTANCE" also called "CITY BLOCK DISTANCE" among pairs of coordinates with LAT, LNG.

Following this post Manhattan Distance for two geolocations I had computed the distance using the haversine formula:

source = (45.070060, 7.663708)
target = (45.068250, 7.663492)

enter image description here

This is my computation:

    from math import radians, sin, asin, sqrt, atan2

    # convert decimal degrees to radians
    lat1, lon1, lat2, lon2 = map(radians, [source[0], source[1], target[0], target[1]])

    #haversine formula for delta_lat
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2
    c = 2 * atan2(sqrt(a), sqrt(1-a)))
    r = 6371
    lat_d = c * r

    # haversine formula for delta_lon
    dlon = lon2 - lon1
    a = sin(dlon / 2) ** 2
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    r = 6371
    lon_d = c * r


    print lat_d + lon_d

The problem is that my result is 225m, while Google Maps says 270m.

Trying again to compute the distance among

source = (45.070060, 7.663708)
target = (45.072800, 7.665540)

enter image description here

the result I obtained is 508m while Google Maps says 350m.

I will appreciate if someone can help me understanding what is wrong here and how to improve this solution which is far from being acceptable. Thank you!

like image 253
piezzoritro Avatar asked Oct 30 '22 08:10

piezzoritro


1 Answers

I probably got the point on myself, the answer is that if you look at these pictures, which are the same posted above in the original question, you can understand that the haversine method I have implemented, computes the distance as the RED LINE in the image. For this reason in the first case I obtain 225m instead of 270m (lower because the red line is the hypotenuse of the triangle) while in the second case I obtained 508m instead of 350 (higher because sum of legs of the triangle). Hence the way to solve this problem should be ROTATE THE CITY MAP COUNTERCLOCKWISE to align the BLUE DOTTED line with the Y-AXIS of the cartesian reference.

Any suggestion will be appreciated. Thank You enter image description hereenter image description here

like image 61
piezzoritro Avatar answered Nov 15 '22 07:11

piezzoritro