0

I am trying to loop through many rows of lat/lon coordinates and create a new column of "distance" for each coordinate.

This is what it looks like: I used this formula:

def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
"""
slightly modified version: of http://stackoverflow.com/a/29546836/2901002

Calculate the great circle distance between two points
on the earth (specified in decimal degrees or in radians)

All (lat, lon) coordinates must have numeric dtypes and be of equal length.

"""
if to_radians:
    lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])

a = np.sin((lat2-lat1)/2.0)**2 + \
    np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2

return earth_radius * 2 * np.arcsin(np.sqrt(a))


df['dist'] = \
    haversine(df.lat.shift(), df.lon.shift(),
             df.loc[1:, 'lat'], df.loc[1:, 'lon'])

But I am getting this error:

AttributeError                            Traceback (most recent call last)
~\anaconda3\envs\geopandas\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
  5273                 return self[name]
-> 5274             return object.__getattribute__(self, name)
5275 

AttributeError: 'Series' object has no attribute 'radians'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-10-e19d56f45506> in <module>
   20 df['dist'] = \
  21     haversine(df.lat.shift(), df.lon.shift(),
 ---> 22                  df.loc[1:, 'lat'], df.loc[1:, 'lon'])

<ipython-input-10-e19d56f45506> in haversine(lat1, lon1, lat2, lon2, to_radians, earth_radius)
 10     """
 11     if to_radians:
 ---> 12         lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
 13 
 14     a = np.sin((lat2-lat1)/2.0)**2 + \

 TypeError: loop of ufunc does not support argument 0 of type Series which has no callable radians 
 method

Sorry for the wonky formatting - can anyone help me understand what's going on?

Thank you!

Statsugh
  • 21
  • 1
  • 3
  • Does this answer your question? [Haversine Formula in Python (Bearing and Distance between two GPS points)](https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points) – Trenton McKinney May 04 '20 at 18:16
  • Sorry to specify it's not just two static points I want it to loop through the row and where it's comparing it to the previous point in a loop to calculate distance for 500+ rows of lon/lat – Statsugh May 04 '20 at 18:26

1 Answers1

1

I figured it out, I was using this formula:

# vectorized haversine function
def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
"""
slightly modified version: of http://stackoverflow.com/a/29546836/2901002

Calculate the great circle distance between two points
on the earth (specified in decimal degrees or in radians)

All (lat, lon) coordinates must have numeric dtypes and be of equal length.

"""
if to_radians:
    lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])

a = np.sin((lat2-lat1)/2.0)**2 + \
    np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2

return earth_radius * 2 * np.arcsin(np.sqrt(a))


df['dist'] = \
haversine(df.LAT.shift(), df.LONG.shift(),
             df.loc[1:, 'LAT'], df.loc[1:, 'LONG'])

But changed np.radians to map(np.radians,[lat1,lon1,lat2,lon2] and it worked just fine

Statsugh
  • 21
  • 1
  • 3