def geocode(query, IGN_API=''): from geopy.geocoders import IGNFrance geolocator = IGNFrance(api_key=IGN_API, referer='localhost', user_agent='Agent_tests') location = geolocator.geocode(query,maximum_responses=1,exactly_one=True) return (location.longitude, location.latitude)
def setUpClass(cls): cls.geocoder = IGNFrance(api_key=env.get('IGNFRANCE_KEY'), username=env.get('IGNFRANCE_USERNAME'), password=env.get('IGNFRANCE_PASSWORD'), referer=env.get('IGNFRANCE_REFERER'), timeout=20) cls.delta_exact = 0.2
def test_user_agent_custom(self): geocoder = IGNFrance( api_key='DUMMYKEY1234', username='******', password='******', user_agent='my_user_agent/1.0' ) self.assertEqual(geocoder.headers['User-Agent'], 'my_user_agent/1.0')
def geotag_Compte(df, prefix='', IGN_API=''): """ This function corrects Phenix original geo coordinates (longitude and latitude). This function uses IGN geocoding API to convert adresses into Longitude and Latitude coordinates. Args: df (DataFrame): 'Comptes' DataFrame prefix (str): Prefix used in the 'Comptes' DataFrame IGN_API (str): IGN API key needed to access to IGN geocoding API Returns: df_matrix (DataFrame): Simplified version of 'Comptes' DataFrame with the following columns Nom = Account name as in PhenixDB AdresseLigne3 = Account address as in PhenixDB Ville = Account city as in PhenixDB CodePostal = Account postal code as in PhenixDB old_lon, old_lat = Account original geo coordinate as in PhenixDB new_lon, new_lat = Account recalculated geo coordinate from IGN delta = Distance (km) between old and new geo coordinates """ from geopy.geocoders import IGNFrance from geopy.distance import lonlat, distance geolocator = IGNFrance(api_key=IGN_API, referer='localhost', user_agent='Agent_tests') matrix = [] for index, value in df.iterrows(): i = value[prefix + 'Id'] adresse1 = '' if str(value[prefix + 'Adresse_AdresseLigne1']) == 'nan' else str( value[prefix + 'Adresse_AdresseLigne1']) adresse2 = '' if str(value[prefix + 'Adresse_AdresseLigne2']) == 'nan' else str( value[prefix + 'Adresse_AdresseLigne2']) adresse3 = '' if str(value[prefix + 'Adresse_AdresseLigne3']) == 'nan' else str( value[prefix + 'Adresse_AdresseLigne3']) ville = '' if str(value[prefix + 'Adresse_Ville']) == 'nan' else str( value[prefix + 'Adresse_Ville']) CP = '' if str(value[prefix + 'Adresse_CodePostal']) == 'nan' else str( value[prefix + 'Adresse_CodePostal']) query = adresse3 + ',' + ville + ',' + CP # check if geolocation is found try: location = geolocator.geocode(query, maximum_responses=1, exactly_one=True) except: print('(%s) %s --> location not found' % (i, query)) element = dict(Id=value[prefix + 'Id'], delta=None, Nom=value[prefix + 'Nom'], adresse1=value[prefix + 'Adresse_AdresseLigne1'], adresse2=value[prefix + 'Adresse_AdresseLigne2'], adresse3=value[prefix + 'Adresse_AdresseLigne3'], ville=value[prefix + 'Adresse_Ville'], CP=value[prefix + 'Adresse_CodePostal'], old_lon=value[prefix + 'Longitude'], old_lat=value[prefix + 'Latitude'], new_lon=None, new_lat=None) matrix.append(element) continue # calculate distance old vs new geolocation old_lonlat = (value[prefix + 'Longitude'], value[prefix + 'Latitude']) new_lonlat = (location.longitude, location.latitude) delta = calc_geodistance(lonlat1=old_lonlat, lonlat2=new_lonlat) #### delta = distance(lonlat(*old_lonlat), lonlat(*new_lonlat)).km element = dict(Id=value[prefix + 'Id'], delta=delta, Nom=value[prefix + 'Nom'], adresse1=value[prefix + 'Adresse_AdresseLigne1'], adresse2=value[prefix + 'Adresse_AdresseLigne2'], adresse3=value[prefix + 'Adresse_AdresseLigne3'], ville=value[prefix + 'Adresse_Ville'], CP=value[prefix + 'Adresse_CodePostal'], old_lon=value[prefix + 'Longitude'], old_lat=value[prefix + 'Latitude'], new_lon=location.longitude, new_lat=location.latitude) matrix.append(element) df_matrix = pd.DataFrame.from_dict(matrix) return df_matrix
def test_invalid_auth_3(self): """ IGNFrance username without password is ConfgurationError """ with self.assertRaises(ConfigurationError): IGNFrance(api_key="a", username="******")
def test_invalid_auth_2(self): """ IGNFrance api_key, username, and referer is ConfigurationError """ with self.assertRaises(ConfigurationError): IGNFrance(api_key="a", username="******", referer="c")
def test_invalid_auth_1(self): """ IGNFrance api_key without referer is ConfigurationError """ with self.assertRaises(ConfigurationError): IGNFrance(api_key="a")
def make_geocoder(cls, **kwargs): return IGNFrance(api_key=env['IGNFRANCE_USERNAME_KEY'], username=env['IGNFRANCE_USERNAME'], password=env['IGNFRANCE_PASSWORD'], timeout=10, **kwargs)
def make_geocoder(cls, **kwargs): return IGNFrance(api_key=env['IGNFRANCE_KEY'], referer=env['IGNFRANCE_REFERER'], timeout=10)
def test_invalid_auth_3(self): with pytest.raises(ConfigurationError): IGNFrance(api_key="a", username="******")
def test_invalid_auth_1(self): with pytest.raises(ConfigurationError): IGNFrance(api_key="a")