0

Please see attached snapshot as I ask my question. I have a data frame with a pair of Latitude / Longitudes that have been geocoded from different program and I am trying to generate a distance matrix between (Latitude1/Longitude1) and (Latitude2/Longitude2) and so on to find distance between locations.

My program below doesn't seem to read all the rows.

   import pandas as pd
   import googlemaps
   import requests, json

   gmaps = googlemaps.Client(key='123)

   source = pd.DataFrame({'Latitude': df['Latitude1'] ,'Longitude': df['Longitude1']})
   destination = pd.DataFrame({'Latitude': df['Latitude2'] ,'Longitude': df['Longitude2']})

   source = source.reset_index(drop=True)
   destination = destination.reset_index(drop=True)

   for i in range(len(source)):
   result = gmaps.distance_matrix(source, destination)
   print(result)

Expected Output

   Distance
   12 Miles
   10 Miles
   5 Miles
   1 Mile

DataFrame

 Key Latitude1  Longitude1   Latitude2    Longitude#2
 1    42             -91          40           -92
 2    39             -94.35       38           -94
 3    37             -120         36           -120
 4    28.7           -90          35           -90
 5    40             -94          38           -90
 6    30             -90          25           -90

1 Answers1

1

I haven't used gmaps, but this is a simple formula for calculating distance.
This is just maths, so I won't explain it here. Just know you need 2 locations in the format (lat, lon) as the arguments and need to import math

def distance(origin, destination):
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 3959 # mi

    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
        * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    d = radius * c

    return d

Now we need to merge the 2 dataframes More detail here

 maindf = pd.merge(source, destination , left_index=True, right_index=True)

Next you need to apply that to each row

maindf['Distance'] = maindf.apply(lambda row: distance((row.Latitude1,row.Longditude1),(row.Latitude2,row.Longditude2)), axis=1)

Apply loops over the dataframe and applies the function.
In this case it is applies 'distance' to every row based on the 2 lat/long pairs in each row.
This adds a new column 'Distance' with the distance in miles between the 2 locations.

I would also add, if that is your full code, you don't actually add any data to the dataframes.

  • 1
    Although this is correct, it doesn't take into account real-world variables, such as roads, buildings and what not. I think using gmaps api would be better. – Umar.H Jul 05 '19 at 22:00