Home > Net >  Python generate lat/long points from address
Python generate lat/long points from address

Time:11-06

How do I take addresses and generate lat, long coordinates from them in python? I have a few addresses that I would like get lat, long points but seems it doesn't work.

I used geopandas but it returns me nothing. I am also a bit confused about what to use for the user_agent. Here is my code,

import pandas as pd
from geopy.geocoders import Nominatim

df2['location_lat'] = ""
df2['location_long'] = ""

geolocator = Nominatim(user_agent="myApp")

for i in df2.index:
    try:
        #tries fetch address from geopy
        location = geolocator.geocode(df2['Location'][i])
        
        #append lat/long to column using dataframe location
        df2.loc[i,'location_lat'] = location.latitude
        df2.loc[i,'location_long'] = location.longitude
  
    except:
        #catches exception for the case where no value is returned
        #appends null value to column
        df2.loc[i,'location_lat'] = ""
        df2.loc[i,'location_long'] = ""
   

Any help is appreciated. Thanks.

CodePudding user response:

i made without pandas, but i think that you can use it

Code:

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent='your_app_name')

addresses = [
            '20, Willmannstraße, Fechenheim, Ost, Frankfurt am Main, Hessen, 60386, Deutschland',
            '20, Willmannstraße, Fechenheim, Ost, Frankfurt am Main, Hessen, 60386, Deutschland',
            'Pöhlen, Schönberg, Sandesneben-Nusse, Herzogtum Lauenburg, Schleswig-Holstein, 22929, Deutschland',
            'Pöhlen, Schönberg, Sandesneben-Nusse, Herzogtum Lauenburg, Schleswig-Holstein, 22929, Deutschland',
            'Pöhlen, Schönberg, Sandesneben-Nusse, Herzogtum Lauenburg, Schleswig-Holstein, 22929, Deutschland'
]

coordinates = []

for address in addresses:
    location = geolocator.geocode(address)

    lat, lon = location.latitude, location.longitude

    coordinates.append((lat, lon))

print(coordinates)

# output:
[(50.1262402, 8.767057053362574),
 (50.1262402, 8.767057053362574),
 (53.6805214, 10.4282207),
 (53.6805214, 10.4282207),
 (53.6805214, 10.4282207)]

I hope that helps you =D

CodePudding user response:

You can use apply directly on a DataFrame column:

import pandas as pd
from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="myApp")

df2 = pd.DataFrame({'Location':
            ['2094 Valentine Avenue,Bronx,NY,10457',
             '1123 East Tremont Avenue,Bronx,NY,10460',
             '412 Macon Street,Brooklyn,NY,11233']})

df2[['location_lat', 'location_long']] = df2['Location'].apply(
    geolocator.geocode).apply(lambda x: pd.Series(
        [x.latitude, x.longitude], index=['location_lat', 'location_long']))

It should give:

                                  Location  location_lat  location_long
0     2094 Valentine Avenue,Bronx,NY,10457     40.852905     -73.899665
1  1123 East Tremont Avenue,Bronx,NY,10460     40.840130     -73.876245
2       412 Macon Street,Brooklyn,NY,11233     40.682651     -73.934353
  • Related