user3447653
user3447653

Reputation: 4148

Retrieve the country from the geographical locations in Python

I am trying to get the country name from the latitude and longitude points from my pandas dataframe. Currently I have used geolocator.reverse(latitude,longitude) to get the full address of the geographic location. But there is no option to retrieve the country name from the full address as it returns a list.

Method used:

 def get_country(row):
     pos = str(row['StartLat']) + ', ' + str(row['StartLong'])
     locations = geolocator.reverse(pos)
     return locations

Call to get_country by passing the dataframe:

df4['country'] = df4.apply(lambda row: get_country(row), axis = 1)

Current output:

StartLat                  StartLong           Address          
52.509669              13.376294          Potsdamer Platz, Mitte, Berlin, Deutschland, Europe

Just wondering whether there is some Python library to retrieve the country when we pass the geographic points.

Any help would be appreciated.

Upvotes: 2

Views: 7370

Answers (3)

Shalini Baranwal
Shalini Baranwal

Reputation: 2998

My code,hopefully that helps:

from geopy.geocoders import Nominatim 
nm = Nominatim()

place, (lat, lng) = nm.geocode("3995 23rd st, San Francisco,CA 94114")
print('Country' + ": " + place.split()[-1])

Upvotes: 2

Thomas Schultz
Thomas Schultz

Reputation: 2467

I'm not sure what service you're using with geopy, but as a small plug which I'm probably biased towards, this I think could be a simpler solution for you.

https://github.com/Ziptastic/ziptastic-python

from ziptastic import Ziptastic

# Set API key.
api = Ziptastic('<your api key>')
result = api.get_from_coordinates('42.9934', '-84.1595')

Which will return a list of dictionaries like so:

[
    {
        "city": "Owosso",
        "geohash": "dpshsfsytw8k",
        "country": "US",
        "county": "Shiawassee",
        "state": "Michigan",
        "state_short": "MI",
        "postal_code": "48867",
        "latitude": 42.9934,
        "longitude": -84.1595,
        "timezone": "America/Detroit"
    }
]

Upvotes: 0

user1541105
user1541105

Reputation:

In your get_country function, your return value location will have an attribute raw, which is a dict that looks like this:

{
  'address': {
     'attraction': 'Potsdamer Platz',
     'city': 'Berlin',
     'city_district': 'Mitte',
     'country': 'Deutschland',
     'country_code': 'de',
     'postcode': '10117',
     'road': 'Potsdamer Platz',
     'state': 'Berlin'
   },
   'boundingbox': ['52.5093982', '52.5095982', '13.3764983', '13.3766983'],
   'display_name': 'Potsdamer Platz, Mitte, Berlin, 10117, Deutschland',
   ... and so one ...
}

so location.raw['address']['country'] gives 'Deutschland'

If I read your question correctly, a possible solution could be:

def get_country(row):
    pos = str(row['StartLat']) + ', ' + str(row['StartLong'])
    locations = geolocator.reverse(pos)
    return location.raw['address']['country']

EDIT: The format of the location.raw object will differ depending on which geolocator service you are using. My example uses geopy.geocoders.Nominatim, from the example on geopy's documentation site, so your results might differ.

Upvotes: 3

Related Questions