I have dataset contains Latitude and Longitude. Now i want to determine distance between these 2 points. Could you help me in solving this? In my dataset i have only 2 variables Longitude = -73.953918 Latitude = 40.778873
Asked
Active
Viewed 54 times
0
-
Does this answer your question? [Calculating distance between two points using latitude longitude and altitude (elevation)](https://stackoverflow.com/questions/33111529/calculating-distance-between-two-points-using-latitude-longitude-and-altitude-e) – Grzegorz Skibinski Jan 01 '21 at 11:35
-
Thanks for prompt reply. In my dataset i have only 2 variables like Longitude = -73.953918 Latitude = 40.778873. – ramreddy bolla Jan 01 '21 at 11:37
-
2Please read: https://en.m.wikipedia.org/wiki/Longitude and https://en.m.wikipedia.org/wiki/Latitude - to answer your question if you want to calculate distance between longitude and latitude it's always 0 (because these 2 values refer to 2 intersecting lines) – Grzegorz Skibinski Jan 01 '21 at 11:49
-
can you provide a sample of your data set and create expected output as example ? so we can help you? – adir abargil Jan 01 '21 at 12:53
-
sklearn has got [haversine distance metric](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html) implemented. it gives you angular distance, which you can multiply by the radius of earth and get the result. (according to the docs, upper bound error 1% exists due to small eccentricity) – Alireza Jan 01 '21 at 14:08
1 Answers
0
if you have pandas dataframe then convert each column to a np array, and this function can work for either scalars or numpy array, just be consistent with lat and lng :
def haversine_np(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
All args must be of equal length.
"""
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat / 2.0) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2.0) ** 2
kms= (2 * 6367) * np.arcsin(np.sqrt(a))
return kms
example:
np.random.seed(42)
df = pd.DataFrame(dict(lat=np.random.uniform(32,33,(100,)),lng=np.random.uniform(33,34,(100,))))
point = dict(lat=32.5,lng=33.5)
df['distance_km'] = haversine_np(df['lng'].values,df['lat'].values,point['lng'],point['lat'])
df
>>>
lat lng distance_km
0 32.374540 33.031429 46.104413
1 32.950714 33.636410 51.683653
2 32.731994 33.314356 31.089653
3 32.598658 33.508571 10.992790
4 32.156019 33.907566 54.090586
... ... ... ...
95 32.493796 33.349210 14.149670
96 32.522733 33.725956 21.324490
97 32.427541 33.897110 38.093632
98 32.025419 33.887086 64.065001
99 32.107891 33.779876 50.888535

adir abargil
- 5,495
- 3
- 19
- 29