Search code examples
pythonpandasgeopy

Is there a Geopy Python function that converts dataframe column (Latitude/Longitude) from "degree minute second" to "degree decimal"


I applied the following function for each row in my dataframe (df), and using a Lat0/Lon0, direction(azimuth) and distance(dist) I got the new Lat/Lon in "degree minute second":

def location(row):
    from geopy.distance import distance
    dist = row['dist']
    direction = row['azimuth']
    lat0 = 20
    lon0 = 20
    return distance(kilometers=dist).destination((lat0, lon0), direction)

df['coordinate'] = df.apply(lambda row: location(row), axis=1)

I wonder if there is a way to create new two columns in my dataframe with Lat and Lon (“degree decimal”) information based on df['coordinate'] output.

Out[]: 
                            time  zenith  azimuth      o3  uncertainty  flag  \
61 2019-01-24 15:02:57.983999999   66.90   121.72  241.85       4.9131     1   
62 2019-01-24 15:04:35.616000000   66.57   121.94  227.36       4.1773     1   
63 2019-01-24 15:06:13.248000000   66.25   122.16  232.97       3.4649     1   
64 2019-01-24 15:07:50.880000000   65.92   122.39  236.81       3.1841     1   
         dist                            coordinate  
61  51.578278   19 45m 16.3524s N, 20 25m 6.9961s E  
62  50.766056  19 45m 24.9176s N, 20 24m 39.7557s E  
63  49.998803   19 45m 32.885s N, 20 24m 13.9121s E  
64  49.227710  19 45m 40.8577s N, 20 23m 47.8847s E 

UPDATE Solved

def location(row, lat0, lon0):
    from geopy.distance import distance
    dist = row['dist']
    direction = row['azimuth']
    return distance(kilometers=dist).destination((lat0, lon0), direction).format_decimal()

df['coordinate'] = df.apply(lambda row: location(row, 20, 20), axis=1)

split_data = df.coordinate.str.split(', ')
df['lat'] = split_data.apply(lambda x: x[0])
df['long'] = split_data.apply(lambda x: x[1])

Current dataframe:

Out[]: 
                            time  zenith  azimuth      o3  uncertainty  flag  \
61 2019-01-24 15:02:57.983999999   66.90   121.72  241.85       4.9131     1   
62 2019-01-24 15:04:35.616000000   66.57   121.94  227.36       4.1773     1   
63 2019-01-24 15:06:13.248000000   66.25   122.16  232.97       3.4649     1   
64 2019-01-24 15:07:50.880000000   65.92   122.39  236.81       3.1841     1   
         dist                              coordinate                 lat  \
61  51.578278    19.75454233221212, 20.41861002686191   19.75454233221212   
62  50.766056  19.756921547635606, 20.411043240303318  19.756921547635606   
63  49.998803  19.759134724013204, 20.403864475793757  19.759134724013204   
64  49.227710  19.761349364643046, 20.396634631525913  19.761349364643046   
                  long  
61   20.41861002686191  
62  20.411043240303318  
63  20.403864475793757  
64  20.396634631525913 

Solution

  • If you remove .format_decimal(), your location function would return Point instances instead of strings (see https://geopy.readthedocs.io/en/stable/#geopy.point.Point), where decimal coordinates can easily be extracted as attributes:

    df['point'] = df.apply(lambda row: location(row, 20, 20), axis=1)
    
    df['lat'] = df['point'].apply(lambda point: point.latitude)
    df['long'] = df['point'].apply(lambda point: point.longitude)