如何从反向地理位置查找县名?

问题描述 投票:0回答:3
,我正在通过一些LAT/长坐标来解析,我想获得坐标县。

这就是我在做的事情:

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

python geolocation
3个回答
1
投票

我只是想补充一点,这并不总是适用于所有地址。有些回报不包括街道号码(即使在查找中提供),然后将返回状态。 try:

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

0
投票

由于位置并非全部以相同的格式使用:

[x.strip() for x in location.address.split(',') if 'County' in x ][0]
    

这对我有用:

0
投票
location = geolocator.reverse("47.173056,-114.553611") line = location.address line = line.replace(',','') words = line.lower().split() print(words) ind = (words.index('county')-1) print(words[ind])


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.