NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

Hi!

I am using geopy Nominatim in python to retrieve city names, province and regions of some postcodes. I have been able to get accurate city names using coutry_codes, but now I am having problems with provinces.

The problem is that I don't have the province in my raw['address'] field. I learned that I should use admin_level = 6 to get the province in my output but I don't know how to use it.

This is the code:

from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
geolocator = Nominatim()
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)

Locations = pd.read_csv("geoloc1.csv", delimiter = ';')
for index, row in Locations.iterrows():
time.sleep(1)
country = np.where(countries == row['NAZIONE'])[0][0] # Finding the index of the corresponding country
country = switch_country(country) # Finding the correct code for the country
#print(nazione)

if country == 'it':
    loca = geocode(query = row['CAP'], country_codes = country, addressdetails = True, language = 'it,en')
    try:
        row['PROVINCIA'] = loca.raw['address']['county']
    except:
        row['PROVINCIA'] = '_'
    try:
        row['REGIONE'] = loca.raw['address']['state']
    except:
        row['REGIONE'] = '_'
loca = geocode(query = row['CAP'], country_codes = country, addressdetails = True, language = 'it,en')
try:
    row['LOCATION'] = loca.raw['address']['city']
    Cities.append(loca.raw['address'])
except:
    try:
        row['LOCATION'] = loca.raw['address']['hamlet']
        Cities.append(loca.raw['address'])
    except:
        try:
            row['LOCATION'] = loca.raw['address']['village']
            Cities.append(loca.raw['address'])
        except:
            row['LOCATION'] = '_'
            Cities.append(loca)

Output = Output.append(row)

How can I use admin_level = 6 to get the province name in my raw['address']?

Thanks, Carlotta.

asked 06 Apr '20, 08:03

Carlotta's gravatar image

Carlotta
21449
accept rate: 0%

edited 07 Apr '20, 14:57

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×689
×129
×107
×78
×26

question asked: 06 Apr '20, 08:03

question was seen: 1,575 times

last updated: 07 Apr '20, 14:57

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum