0

What would be the best way to loop through a list of addresses using geocoder.geocode if there are some locations that don't exist on the map? How could I skip them so that the loop continues without this exception? Exception: Nominatim geocoder returned no results for query "Beli manastir planina,Bakar,Croatia" Below is what I have tried.

L = [location1, location2, location3, ..., location n]
KO = []
for l in L:
    KO = list(ox.geocoder.geocode(l))
    if Exception:
        continue
    KO.append(KO)

Also I tried this:

try: 
    KO = []
    for l in L:
         KO = list(ox.geocoder.geocode(l))
         KO.append(KO)
except Exception:
    pass

Any help is appreciated.

Zinic
  • 27
  • 6
  • Nothing happens when I run the second snippet. I get no results and no errors. Here is and example of the L list `L = ['Pazinska,Zagreb,Croatia','Pulska, Pazin,Croatia','Zadarska,Zagreb,Croatia','Pazinska,Pula,Croatia']` The second location doesn't exist and should be skipped. Theoretically – Zinic Sep 10 '20 at 17:38
  • I think the result of that loop should be a list of lists. – Zinic Sep 10 '20 at 17:43

1 Answers1

1

Nest your for loop and try/except differently. Here's a minimal reproducible example:

import osmnx as ox
ox.config(log_console=True, use_cache=True)

locations= ['Pazinska,Zagreb,Croatia',
            'Pulska, Pazin,Croatia',
            'Zadarska,Zagreb,Croatia',
            'Pazinska,Pula,Croatia']

coords = []
for location in locations:
    try: 
        coords.append(ox.geocoder.geocode(location))
    except Exception as e:
        print(e)
gboeing
  • 5,691
  • 2
  • 15
  • 41