Joehat
Joehat

Reputation: 1129

Python: geopy.distance.geodesic in a for loop

I have the following data and I am trying to compute the geopy distance from one observation to another using their latitudes and longitudes.

trip3

>>
    trip_id                                     latitude    longitude
0   9367efd103c7314d70446927130c9b98778fbbd0    57.728867   11.949463
1   8a8449635c10cc4b8e7841e517f27e2652c57ea3    57.728867   11.949463
2   8a8449635c10cc4b8e7841e517f27e2652c57ea3    57.728954   11.949368
3   8a8449635c10cc4b8e7841e517f27e2652c57ea3    57.728867   11.949463
4   8a8449635c10cc4b8e7841e517f27e2652c57ea3    57.728954   11.949368
... ... ... ...
30473   019ebd48fe9c9ab20051e9de1d5ddfc6fd13c55b    57.691685   12.009715
30474   019ebd48fe9c9ab20051e9de1d5ddfc6fd13c55b    57.691452   12.010811
30475   019ebd48fe9c9ab20051e9de1d5ddfc6fd13c55b    57.690945   12.011210
30476   019ebd48fe9c9ab20051e9de1d5ddfc6fd13c55b    57.690468   12.011681
30477   d0fe1a916d47228c09911b487146731d75c7d728    57.690361   12.011670

To compute the distance I am trying:

# Empty string
current_trip_id = ""

# Creating a new column for the distance between observations of the same trip
# all rows have a default value of 0
trip3["geopy_distance"] = 0

# Loop
for index,row in trip3.iterrows():
    
    if row["trip_id"] == current_trip_id:
    
        lat_coor = row["latitude"]
        lon_coor = row["longitude"]

        final_pos = row["latitude"], row["longitude"]

        my_dist = geopy.distance.geodesic(initial_pos[0], initial_pos[1], final_pos[0], final_pos[1])
        trip3.loc[index, "geopy_distance"] = my_dist

    # Update loop
    current_trip_id = row["trip_id"]
    
    initial_pos = row["latitude"], row["longitude"]

However, I get the error:

A single number has been passed to the Point constructor. This is probably a mistake, because constructing a Point with just a latitude seems senseless. If this is exactly what was meant, then pass the zero longitude explicitly to get rid of this error.

This code worked with a defined haversine distance function but it's not working with geopy. I am interested in seeing how the two differ. So I'd only want to calculate the geopy distance as supposedly this tends to be more accurate than haversine.

How can my loop be improved to compute this distance?

Upvotes: 1

Views: 1456

Answers (1)

Ture Pålsson
Ture Pålsson

Reputation: 6786

According to the documentation, geopy.distance.geodesic takes its arguments as (lat, lon) tuples.

Upvotes: 1

Related Questions