def get_lat_long(city, state, address): from geopy.geocoders import GeocoderDotUS f_string = "%s {0} {1}".format(city, state) geolocator = GeocoderDotUS(format_string=f_string) address, (latitude, longitude) = geolocator.geocode(address) return (address, latitude, longitude)
def geocoderdotus(query): us = GeocoderDotUS() try: place, (lat, lng) = us.geocode(query) return (lat, lng) except: return None, None
def test_dot_us_auth(self): """ GeocoderDotUS Authorization header """ geocoder = GeocoderDotUS(username='******', password='******') with patch.object( geocoder, '_call_geocoder', side_effect=NotImplementedError()) as mock_call_geocoder: with self.assertRaises(NotImplementedError): geocoder.geocode("1 5th Ave NYC") args, kwargs = mock_call_geocoder.call_args request = args[0] self.assertEqual(request.get_header('Authorization'), 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=')
def _geocode(request, context): api_selection = None coordinateList = [] # Iterate over bundled rows for request_rows in request: # Iterate over rows for row in request_rows.rows: # The first strData is the coordinate data = [d.strData for d in row.duals][0] # Grab selected API # Possible choices are: Google, Open Street, GeocoderDotUS if not api_selection: api_selection = [d.strData for d in row.duals][1] if 'google' in api_selection.lower(): geolocator = GoogleV3( api_key="AIzaSyDlLSgsXiYG-zKYTuLxsbj1BNr7XoGpRMk") elif 'open street' in api_selection.lower(): geolocator = Nominatim(scheme='http') elif 'geocoderdotus' in api_selection.lower(): geolocator = GeocoderDotUS() else: geolocator = Nominatim() # geocode for i in range(2): try: location = geolocator.geocode(data, timeout=2) break except geopy.exc.GeocoderTimedOut: pass except geopy.exc.GeocoderQueryError: pass except urllib.error.HTTPError: pass try: latitude = location.latitude except AttributeError: latitude = 'Unavailable' try: longitude = location.longitude except AttributeError: longitude = 'Unavailable' coordinates = '[' + str(longitude) + ', ' + str(latitude) + ']' coordinateList.append(coordinates) # Create an iterable of dual with the result duals = iter([[SSE.Dual(strData=c)] for c in coordinateList]) # Yield the row data as bundled rows yield SSE.BundledRows(rows=[SSE.Row(duals=d) for d in duals])
def test_dot_us_auth(self): """ GeocoderDotUS Authorization header """ geocoder = GeocoderDotUS(username='******', password='******') def _print_call_geocoder(query, timeout, raw): """ We want to abort at call time and just get the request object. """ raise Exception(query) geocoder._call_geocoder = _print_call_geocoder exc_raised = False try: geocoder.geocode("1 5th Ave NYC") except Exception as err: exc_raised = True request = err.message if not py3k else err.args[0] self.assertEqual(request.get_header('Authorization'), 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=') self.assertTrue(exc_raised)
def test_dot_us_auth(self): """ GeocoderDotUS Authorization header """ geocoder = GeocoderDotUS(username='******', password='******') def _print_call_geocoder(query, timeout, raw): """ We want to abort at call time and just get the request object. """ raise Exception(query) geocoder._call_geocoder = _print_call_geocoder exc_raised = False try: geocoder.geocode("1 5th Ave NYC") except Exception as err: exc_raised = True request = err.message if not py3k else err.args[0] self.assertEqual( request.get_header('Authorization'), 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=' ) self.assertTrue(exc_raised)
def test_get_headers(self): geocoder = GeocoderDotUS() self.assertDictEqual({}, geocoder._get_headers()) username = '******' password = '******' # echo -n testuser:testpassword | base64 b64 = 'dGVzdHVzZXI6dGVzdHBhc3N3b3Jk' geocoder = GeocoderDotUS(username=username, password=password) self.assertDictEqual({'Authorization': 'Basic %s' % b64}, geocoder._get_headers())
def setUpClass(cls): cls.geocoder = GeocoderDotUS(username=env['GEOCODERDOTUS_USERNAME'], password=env['GEOCODERDOTUS_PASSWORD'], timeout=3)
def test_user_agent_custom(self): geocoder = GeocoderDotUS(user_agent='my_user_agent/1.0') self.assertEqual(geocoder.headers['User-Agent'], 'my_user_agent/1.0')
########################################################################## ## Module Constants ########################################################################## FEATURES = ["Address", "Latitude", "Longitude"] ########################################################################## ## Program converts addresses to latitude and longitude (Starbucks, McDonald's) to CSV file. ########################################################################## if __name__== "__main__": #Import the data frame df = pd.read_excel(DATAPATH,header=None, names=FEATURES) #Loop through addresses for each coordinate geolocator = GeocoderDotUS(format_string="%s, Washington DC") for row in range (0,48): address, coordinates = geolocator.geocode(df["Address"][row]) df.loc[row, "Address"] = address df.loc[row, "Latitude"] = coordinates[0] df.loc[row, "Longitude"] = coordinates[1] #Write the results to a CSV file df.to_csv(OUTPATH, index=False)
def _geocodeScript(request, context): api_selection = None resultList = [] idNumList = [] # counter z = 1 # Iterate over bundled rows for request_rows in request: # Iterate over rows for row in request_rows.rows: logging.info('Geocoding address # ' + str(z)) z += 1 # The first strData is the coordinate data = [d.strData for d in row.duals][0] # grab the id idNum = [d.numData for d in row.duals][1] idNumList.append(idNum) # Grab selected API # Possible choices are: Google, Open Street, GeocoderDotUS if not api_selection: api_selection = [d.strData for d in row.duals][2] if 'google' in api_selection.lower(): geolocator = GoogleV3() elif 'open street' in api_selection.lower(): geolocator = Nominatim(scheme='http') elif 'geocoderdotus' in api_selection.lower(): geolocator = GeocoderDotUS() else: geolocator = Nominatim() # geocode for i in range(2): try: location = geolocator.geocode(data, timeout=2) break except geopy.exc.GeocoderTimedOut: pass except geopy.exc.GeocoderQueryError: pass except urllib.error.HTTPError: pass try: latitude = location.latitude except AttributeError: latitude = 'Unavailable' try: longitude = location.longitude except AttributeError: longitude = 'Unavailable' coordinates = '[' + str(longitude) + ', ' + str(latitude) + ']' resultList.append(coordinates) # Create an iterable of dual with the result dualsList = [] dualsList.append([SSE.Dual(numData=d) for d in idNumList]) dualsList.append([SSE.Dual(strData=d) for d in resultList]) response_rows = [] for i in range(len(idNumList)): duals = [dualsList[z][i] for z in range(len(dualsList))] response_rows.append(SSE.Row(duals=iter(duals))) # Set and send Table header table = SSE.TableDescription(name='Geocodes') table.fields.add(dataType=SSE.NUMERIC) table.fields.add(dataType=SSE.STRING) md = (('qlik-tabledescription-bin', table.SerializeToString()), ) context.send_initial_metadata(md) yield SSE.BundledRows(rows=response_rows)
#https://gist.github.com/rgdonohue/c4beedd3ca47d29aef01 #pip install geopy from geopy.geocoders import ArcGIS, Bing, Nominatim, OpenCage, GeocoderDotUS, GoogleV3, OpenMapQuest import csv, sys #define geocoders arcgis = ArcGIS(timeout=100) nominatim = Nominatim(timeout=100) geocoderDotUS = GeocoderDotUS(timeout=100) googlev3 = GoogleV3(timeout=100) openmapquest = OpenMapQuest(timeout=100) #preference order for geocoders geocoders = [googlev3, nominatim, geocoderDotUS, openmapquest] def geocode(address): i = 0 try: while i < len(geocoders): # try to geocode using a service location = geocoders[i].geocode(address) # if it returns a location if location != None: # return those values return [location.latitude, location.longitude] else: # otherwise try the next geocoder in the list i += 1