2017-03-07 25 views
0

我正在使用Python和圖書館from geopy.geocoders import Nominatim和我解析通過一些緯度/經度座標,我想獲得座標的縣。如何從反向GeoLocation查找縣名?

這是我在做什麼:

def getCounty(data): 
    try: 
     #print(data["geo"]["coordinates"]) # Getting Lat/Long coordinates 
     geo_data = data["geo"]["coordinates"] 
     geo_detail = geolocator.reverse(geo_data) 

    except TypeError,e: 
     geo_data = "null" 
     geo_detail = "N/A" 

    ctr = 0 
    i = 0 
    j = 0 
    if(geo_detail != "N/A"): 
     while(geo_detail[i]!= ',' and ctr == 4): 
      j=j+1 
      if(ctr == 3): 
       i=j+1 
      if(geo_detail[j] == ','): 
       ctr = ctr+1 
     county = geo_detail 
    else: 
     county = "No Location Provided" 


    return county[i:j] 

這出於某種原因返回()

當我這樣做:return county,它只返回那些座標的所有信息。

好像我不能得到變量的子county

我怎樣才能獲得縣屬性還是什麼其他的方法我能得到縣?

+0

請您提供一個緯度/經度座標'data'要傳遞到'的一個例子getCounty(數據)'? – davedwards

+0

輸入:[43.03333333,-88.06666667] 輸出:美國威斯康星州密爾沃基縣Wauwatosa縣西里利大街12343號 –

+1

好吧,看起來你可以將逗號分隔輸出字符串,並獲得第四個索引(縣),對吧?''返回geo_detail.address.split(「,」)[3]' - >'密爾沃基縣' – davedwards

回答

1

拆分上逗號輸出字符串,並獲得第四屆指數(縣):

return geo_detail.address.split(",")[3] # Milwaukee County