Home > Software engineering >  AttributeError: 'NoneType' object has no attribute 'longitude'
AttributeError: 'NoneType' object has no attribute 'longitude'

Time:11-07

I am trying to get coordinates for a 'city' column of a dataframe. That city column has a dtype of ('O'). Its a list of city names. I have made sure there are no digits or numbers within the city name. This is the function i run to get coordinates:

def get_coordinates(city_list):
    """Takes a list of cities and returns a dictionary of the cities and their corresponding coordinates."""
    geolocator = Nominatim(user_agent='location script')
    dicto = {}


    for city in city_list:
        


        try:
            location = geolocator.geocode(city)
        except:
            raise Exception("There was a problem with the getCoordinates function")
        coordinate_values = (location.longitude, location.latitude)  #in geopandas, the x value corresponds to the longitude while the y value, the latitude(Just in case you were wondering why it was *location.longitude, location.latitude* and not the other way round )
        dicto[city] = coordinate_values #adding the coordinate pair to the dictionary at the end of every loop
    return dicto #finally retruns the dict 

Then:

#getting coordinates for each city in the list
city_coords_dict = get_coordinates(city_list)
city_coords_dict

Final output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-63-dedf916e4307> in <module>
      1 #getting coordinates for each city in the list
----> 2 city_coords_dict = get_coordinates(city_list)
      3 city_coords_dict

<ipython-input-62-c391f86b76db> in get_coordinates(city_list)
     13         except:
     14             raise Exception("There was a problem with the getCoordinates function")
---> 15         coordinate_values = (location.longitude, location.latitude)  #in geopandas, the x value corresponds to the longitude while the y value, the latitude(Just in case you were wondering why it was *location.longitude, location.latitude* and not the other way round )
     16         dicto[city] = coordinate_values #adding the coordinate pair to the dictionary at the end of every loop
     17     return dicto #finally retruns the dict

AttributeError: 'NoneType' object has no attribute 'longitude'

Anyone with suggestions will greatly be appreciated. Thanks in advance.

CodePudding user response:

Your geolocator apparently does not have data for the specific city that it is trying to request. As not_speshal also said, geolocator.geocode(city) is returning None at some point, which is being assigned to location. So later, you try to get location.longitude, which, because location is now None, is equivalent to (None).longitude...

CodePudding user response:

Instead of throwing an exception, geocode returns a NoneType object. So Nominatim is not able to find one of the locations listed in "location script".

Try replacing the try catch block with assert location, city

  • Related