2017-03-07 45 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