-1

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.

komskisp
  • 67
  • 1
  • 5

2 Answers2

0

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...

  • I can understand all that, that is why im asking for a suggestion or alternative – komskisp Nov 06 '21 at 16:41
  • The please add more information to your question, such as what value is failing. –  Nov 06 '21 at 16:45
  • I have added: ` for city in city_list: if 'None' not in city_list:` When i run it with this 'IF' statement, then the output actually, gives me the first city coordinate in a dictionary. However, just one, the first one. What is stopping from getting the rest? – komskisp Nov 06 '21 at 16:50
0

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

Jia Geng
  • 11
  • 3
  • when i try doing that, the output: Exception: There was a problem with the getCoordinates function – komskisp Nov 06 '21 at 16:40
  • Hi, I think the try catch block is still there. It is printing exactly the output of the catch statement. If it was unclear, what I meant was removing the lines 4 lines inclusive and after `try: ...` with two lines: 1: `location = geolocator.geocode(city)` and 2: `assert location,city`. – Jia Geng Nov 14 '21 at 15:32