[英]How to get County Name from reverse GeoLocation look up?
我正在使用 python 和from geopy.geocoders import Nominatim
的库,我正在解析一些纬度/经度坐标,我想获得坐标的县。
这就是我正在做的:
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
的子字符串
我如何获得县属性或我可以通过哪些其他方式获得县?
在逗号上拆分该输出字符串,并获得第 4 个索引(县):
return geo_detail.address.split(",")[3] # Milwaukee County
我只是想补充一点,这并不总是适用于所有地址。 某些返回不包括街道号码(即使在查找中提供),然后这将返回状态。
尝试:
[x.strip() for x in location.address.split(',') if 'County' in x ][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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.