1

Has anyone encountered the following problem? I have a pandas DataFrame containing the coordinates of renewable energy installations and I tried using reverse geocoding to get the related address data. I used geopy for this. The code worked fine on a test sample I used, but when I try to apply it to the complete data it throws and error where it says TypeError: 'address' must not be None.

So I have a pandas DataFrame called xml_data with a 'coordinates' column in which coordinates are put in the form of e.g.: 52.06192, 4.53075

The code is:

import pandas as pd
import geopy
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
import tqdm



#Create an extra column in the xml data to put the coordinates in as 'lat,lon'
xml_data['coordinates'] = xml_data['lat'].map(str) + ',' + xml_data['lon'].map(str)

#Create service provicder and pass timeout to be 10 minutes to prevent timeout errors
locator = Nominatim(user_agent='myGeocoder', timeout=10)
rgeocode = RateLimiter(locator.reverse, min_delay_seconds=0.001)

#Use the reverse code. Add tqdm to monitor progress.
tqdm.tqdm.pandas(desc="progress-bar")
xml_data['address'] = xml_data['coordinates'].progress_apply(rgeocode)

If the address for a specific coordinate doesn't exist, I thought, based on the docs, that a None value would be returned and not an error. Any thoughts on what is likely to be the issue?

ps: Apologies if my question wasn't formulated clearly enough, if there is anything I should add please let me know.

  • This might be a bug in geopy. TypeError definitely shouldn't be raised for a non-existing place. Could you please open an issue at https://github.com/geopy/geopy/issues with a specific query (i.e. the coordinates) reproducing the issue? – KostyaEsmukov Oct 31 '20 at 14:18
  • I'm a bit of a noob and I have never done anything with github, but I'll try! – Marc Marsidi Nov 06 '20 at 20:32

1 Answers1

0

The example you give for 52.06192, 4.53075 coordinates, works for me, I tried to change the latitude to 89.06192 to reproduce a similar error. First time I got "Adress must not be None" and when I added Try except, it works perfect.

import pandas as pd
import numpy as np
import geopy
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
df = pd.DataFrame({'latitude': [89.06192],
               'longitude': [4.53075]
               })

locator = Nominatim(user_agent="myGeocoder", timeout=10)
df["geom"] =  df["latitude"].map(str)  + ',' + df['longitude'].map(str)
def rg(x):
    try:
        rgeocode = RateLimiter(locator.reverse, min_delay_seconds=1, max_retries=2) 
    except:
        return None
df['address'] = df['geom'].progress_apply(rg)