我正在嘗試獲取資料框“城市”列的坐標。該城市列的資料型別為 ('O')。它是一個城市名稱串列。我已確保城市名稱中沒有數字或數字。這是我運行以獲取坐標的函式:
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
然后:
#getting coordinates for each city in the list
city_coords_dict = get_coordinates(city_list)
city_coords_dict
最終輸出:
---------------------------------------------------------------------------
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'
任何有建議的人將不勝感激。提前致謝。
uj5u.com熱心網友回復:
您的地理定位器顯然沒有它試圖請求的特定城市的資料。正如not_speshal 還說的,在某個時候geolocator.geocode(city)回傳None,它被分配給location. 所以稍后,您嘗試 get location.longitude,因為location是 now None,所以等效于(None).longitude...
uj5u.com熱心網友回復:
地理編碼不會拋出例外,而是回傳一個 NoneType 物件。因此 Nominatim 無法找到“位置腳本”中列出的位置之一。
嘗試將 try catch 塊替換為 assert location, city
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350782.html
標籤:Python 熊猫 jupyter-笔记本 坐标 地理
上一篇:多處理矩陣的不同行
