Let\'s say I have two locations represented by latitude and longitude.
Location 1 : 37.5613
, 126.978
Location 2 : 37.5776
, 126.973
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))