from geopy.geocoders import Nominatim
geolocator = Nominatim()
import numpy as np

"""
Explanation: Title: Geolocate A City Or Country 
Slug: geolocate_a_city_or_country
Summary: Geolocate a city or country.  
Date: 2016-09-21 12:00
Category: Python
Tags: Other
Authors: Chris Albon  
This tutorial creates a function that attempts to take a city and country and return its latitude and longitude. But when the city is unavailable (which is often be the case), the returns the latitude and longitude of the center of the country.
Preliminaries
End of explanation
"""


def geolocate(city=None, country=None):
    '''
    Inputs city and country, or just country. Returns the lat/long coordinates of 
    either the city if possible, if not, then returns lat/long of the center of the country.
    '''
    
    # If the city exists,
    if city != None:
        # Try
        try:
            # To geolocate the city and country
            loc = geolocator.geocode(str(city + ',' + country))
            # And return latitude and longitude
            return (loc.latitude, loc.longitude)
        # Otherwise
        except:
            # Return missing value
            return np.nan
    # If the city doesn't exist
    else:
        # Try
        try:
            # Geolocate the center of the country
            loc = geolocator.geocode(country)
            # And return latitude and longitude 
            return (loc.latitude, loc.longitude)
        # Otherwise
        except:
            # Return missing value
            return np.nan

"""
Explanation: Create Geolocation Function
End of explanation
"""


# Geolocate a city and country
geolocate(city='Austin', country='USA')

"""
Explanation: Geolocate A City And Country
End of explanation
"""


# Geolocate just a country
geolocate(country='USA')

"""
Explanation: Geolocate Just A Country
End of explanation
"""


