简体   繁体   中英

How to get County Name from reverse GeoLocation look up?

I'm using python and the library from geopy.geocoders import Nominatim and I'm parsing through some Lat/Long coordinates and I want to get the county of the coordinates.

This is what I'm doing:

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]

This for some reason returns ()

when I do: return county , It does return all the information from those coordinates.

It seems like I'm not able to get the substring of the variable county

How can I get the county attribute or what are other ways I could get the county?

在逗号上拆分该输出字符串,并获得第 4 个索引(县):

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

I just wanted to add that this won't always work for all addresses. Some returns for this don't include the street number (even if provided in the lookup) and then this will then return the state.

Try:

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

This will always return the county if one exists.

Since locations are not all in the same format you should use:

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM