def get_coordinates(addresses): ''' (list) -> list Get coords list from Bing maps from addresses list. ''' print('in') print(len(addresses)) coords = [] num_slices_and_rest = divmod(len(addresses), 50) for i in range(num_slices_and_rest[0]): print('in2') coords.extend([ venue.latlng for venue in geocoder.bing(addresses[50 * i:50 * (i + 1)], method='batch', key=MAPS_API_KEY) ]) coords.extend([ venue.latlng for venue in geocoder.bing( addresses[len(addresses) - num_slices_and_rest[1]:len(addresses)], method='batch', key=MAPS_API_KEY) ]) return coords
def test_bing_details(): details = { 'adminDistrict': 'Ontario', 'locality': 'Ottawa' } g = geocoder.bing(None, method='details', **details) assert g.ok assert g.city == city osm_count, fields_count = g.debug()[0] assert osm_count >= 3 assert fields_count >= 12 details = { 'addressLine': '6912 Route 8', 'adminDistrict': 'Northumberland', 'countryRegion': 'CA', 'locality': 'Ludlow' } g = geocoder.bing(None, method='details', **details) assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count >= 3 assert fields_count >= 12
def convert_to_coord_data(self, base_street, start_street, end_street): location1 = "{} and {}, Hoboken, NJ, 07030".format( base_street, start_street) lat_long = geocoder.bing(location1, key=BING_MAPS_API).latlng location2 = "{} and {}, Hoboken, NJ, 07030".format( base_street, end_street) late_long_2 = geocoder.bing(location2, key=BING_MAPS_API).latlng return (lat_long, late_long_2)
def bing_api(df): bing_key = BING_KEY addresses = [] for i, row in df.iterrows(): if (row['street'] and row['city'] and row['state']): address = row['street'] + ' ' + row['city'] + ', ' + row['state'] g = geocoder.bing(address, key=bing_key) addresses.append([row['Id'], row['account'], g.json]) addy_list = [] for add in addresses: add_id = add[0] add_acct = add[1] add_data = add[2] print(add_data) street = add_data['raw']['address']['addressLine'] city = add_data['raw']['address']['locality'] state = add_data['raw']['address']['adminDistrict'] county = add_data['raw']['address']['adminDistrict2'] zipcode = add_data['raw']['address']['postalCode'] country = add_data['raw']['address']['countryRegion'] lat = add_data['raw']['point']['coordinates'][0] lon = add_data['raw']['point']['coordinates'][1] confidence = add_data['confidence'] add_row = [ add_id, add_acct, street, city, state, county, zipcode, country, lat, lon, confidence ] addy_list.append(add_row) full_address = pd.DataFrame(addy_list, columns=[ 'id', 'acct_name', 'shipping_street', 'city', 'state', 'county', 'zipcode', 'country', 'lat', 'lon', 'confidence' ]) return full_address
def place_finder(file_name, save_file): """Find the latidude and longitude of places on the input file. Args: file_name: the file containing all places to be found save_file: txt file contains original info and latitude & longitude info Returns: ndarray containing latitude and longitude info of all locations """ fw = open(save_file, 'w') ll_coords = [] for line in open(file_name, 'r').readlines(): line = line.strip() strs = line.split('\t') addr = strs[1] + ', ' + strs[2] try: g = geocoder.bing(addr, key=API_KEY.bing_api_key) ll_coord = g.latlng except: print('error fetching') fw.write('{}\t{}\t{}\n'.format(line, ll_coord[0], ll_coord[1])) ll_coords.append(ll_coord) sleep(1) fw.close() return np.array(ll_coords)
def gcode( row ): g = geocoder.bing( row[ 'City' ] + ", " + row[ 'State' ], key=os.environ[ 'BING_API_KEY' ] ) if 'adminDistrict2' in g.json[ 'raw' ][ 'address' ]: county = g.json[ 'raw' ][ 'address' ][ 'adminDistrict2' ] return( county ) else: return( 'Sumpter County' )
def test_bing(): g = geocoder.bing(location) assert g.ok assert g.city == city osm_count, fields_count = g.debug()[0] assert osm_count >= 3 assert fields_count >= 12
def GeoCode(GeoCoder, strAddr): strBingMapKey = cfg.getConfigValue(r"Geocoder/BingKey") #strBingMapKey = 'AjlU0VglpeaGSVjfdrvFNEEZKSRWLtUYbDGGBbkVq1SsFK6Vz724WpqxqRi2m8SJ' try: if GeoCoder == 'google': g = geocoder.google(strAddr) return (g.lat, g.lng, g.address, GeoCoder, g.neighborhood, g.quality, g.accuracy, None) elif GeoCoder == 'bing': g = geocoder.bing(strAddr, key=strBingMapKey) return (g.lat, g.lng, g.address, GeoCoder, g.neighborhood, g.quality, g.accuracy, g.confidence) elif GeoCoder == 'census': cg = CensusGeocode() j = cg.onelineaddress(strAddr) try: return (j[0]['coordinates']['y'], j[0]['coordinates']['x'], j[0]['matchedAddress'], GeoCoder, None, None, None, None) except: return (None, None, None, GeoCoder, None, None, None, None) else: g = geocoder.yahoo(strAddr) return (g.lat, g.lng, g.json['address'], GeoCoder, g.neighborhood, g.quality, g.accuracy, None) except: print('error encountered when geocoding address: {0}'.format(strAddr)) traceback.print_exc() return (None, None, None, GeoCoder, None, None, None, None)
def test_bing_batch_forward(): """ Data subnitted would be the following: Bing Spatial Data Services, 2.0 Id,GeocodeRequest/Query,GeocodeResponse/Point/Latitude,GeocodeResponse/Point/Longitude 0,"Denver,CO",, 1,"Boulder,CO",, """ url_submission = 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode?input=csv&key=test' url_check = 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/3bf1b729dddd498e9df45515cdb36130' url_result = 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/3bf1b729dddd498e9df45515cdb36130/output/succeeded' submission_file = 'tests/results/bing_batch_submission.json' confirmation_file = 'tests/results/bing_batch_confirmation.json' data_file = 'tests/results/bing_batch.csv' with requests_mock.Mocker() as mocker, \ open(submission_file, 'rb') as submission_result, \ open(confirmation_file, 'rb') as confirmation_result, \ open(data_file, 'rb') as batch_result: mocker.post(url_submission, text=str(submission_result.read(), 'utf8')) mocker.get(url_check, text=str(confirmation_result.read(), 'utf8')) mocker.get(url_result, text=str(batch_result.read(), 'utf8')) g = geocoder.bing(locations_forward, key='test', method='batch') assert g.ok assert len(g) == 2 expected_results = [[39.7400093078613, -104.99201965332], [40.015739440918, -105.279243469238]] assert [result.latlng for result in g] == expected_results
def test_bing_batch_forward(): """ Data subnitted would be the following: Bing Spatial Data Services, 2.0 Id,GeocodeRequest/Query,GeocodeResponse/Point/Latitude,GeocodeResponse/Point/Longitude 0,"Denver,CO",, 1,"Boulder,CO",, """ url_submission = 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode?input=csv&key=test' url_check = 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/3bf1b729dddd498e9df45515cdb36130' url_result = 'http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/3bf1b729dddd498e9df45515cdb36130/output/succeeded' submission_file = 'tests/results/bing_batch_submission.json' confirmation_file = 'tests/results/bing_batch_confirmation.json' data_file = 'tests/results/bing_batch.csv' with requests_mock.Mocker() as mocker, \ open(submission_file, 'rb') as submission_result, \ open(confirmation_file, 'rb') as confirmation_result, \ open(data_file, 'rb') as batch_result: mocker.post(url_submission, text=str(submission_result.read(), 'utf8')) mocker.get(url_check, text=str(confirmation_result.read(), 'utf8')) mocker.get(url_result, text=str(batch_result.read(), 'utf8')) g = geocoder.bing(locations_forward, key='test', method='batch') assert g.ok assert len(g) == 2 expected_results = [ [39.7400093078613, -104.99201965332], [40.015739440918, -105.279243469238] ] assert [result.latlng for result in g] == expected_results
def geolocate(address, try_all=True): try: # see https://astral.readthedocs.io/en/latest/#cities a = lookup(address, database()) return a.latitude, a.longitude except: pass # use online geocoder location_data = geocoder.geonames(address, method='details', key='jarbas') if not location_data.ok: location_data = geocoder.geocodefarm(address) if not location_data.ok: location_data = geocoder.osm(address) if try_all: # more are just making it slow if not location_data.ok: location_data = geocoder.google(address) if not location_data.ok: location_data = geocoder.arcgis(address) if not location_data.ok: location_data = geocoder.bing(address) if not location_data.ok: location_data = geocoder.canadapost(address) if not location_data.ok: location_data = geocoder.yandex(address) if not location_data.ok: location_data = geocoder.tgos(address) if location_data.ok: location_data = location_data.json lat = location_data.get("lat") lon = location_data.get("lng") return lat, lon raise ValueError
def address_to_latlng(self, address): print(address) g = geocoder.bing(address, key=os.environ.get('key_bingmaps')) print(g) print(g.ok) print(g.json) return (g.lat, g.lng)
def geocode(df, address_col, zone_col='ZONE'): # for row in df[column_name].iteritems(): for row in df.iterrows(): # idx, address = row idx, data = row address = data[address_col] if not np.isnan(data['Lat']): continue if data[zone_col] == 'JOHOR BAHRU': address = f'{address}, Malaysia' else: address = f'{address}, Singapore' print(f'{idx}: "{address}"', end='') if BING_API_KEY is None: g = geocoder.osm(address) else: g = geocoder.bing(address, key=BING_API_KEY) try: lat, lon = g.latlng df.loc[idx, 'Lat'] = lat df.loc[idx, 'Lon'] = lon print(f'... ({lat}, {lon})') if 'features' in g.geojson.keys(): geo = g.geojson['features'] if len(geo) > 0: geo = geo[0] df.loc[idx, 'geo'] = str(geo) except Exception as e: print(f'... (FAILED)') time.sleep(2) return df
def reverse_geocode_place(lat, lon): l = [lat, lon] place = geocoder.bing(l, method='reverse', key=KEY) return place.city + ", " + place.state + ", " + place.country #p = reverse_geocode_place(44.9778, 93.2650) #print(p)
def test_bing_batch_forward(): g = geocoder.bing(locations_forward, method='batch') assert g.ok assert len(g) == 2 expected_results = [[39.7400093078613, -104.99201965332], [40.015739440918, -105.279243469238]] assert [result.latlng for result in g] == expected_results
def bing_point(self, location_str): g = geocoder.bing(location_str, key=self.bing_key) if g.ok: self.geojson_point = g.geojson else: self.geojson_point = { 'status': 'no_match', 'location_str': location_str }
def transform(lat, lon): list = [] geocod = geocoder.bing([lat, lon], method='reverse') #print "Cidade " + str(lat) + " " + str(lon) + " " + str(geocod) list.append(geocod.city) list.append(geocod.state) list.append(geocod.country) return list
def transform(lat,lon): list = [] geocod = geocoder.bing([lat,lon], method='reverse') #print "Cidade " + str(lat) + " " + str(lon) + " " + str(geocod) list.append(geocod.city) list.append(geocod.state) list.append(geocod.country) return list
def geocode(text): try: latlng = geocoder.bing(regexWrapper(text)).latlng except: latlng = [] if not latlng: return [38.9071923, -77.0368707] else: return latlng
def test_multi_results(): g = geocoder.bing(location, maxRows=3) assert len(g) == 2 assert g.city == city expected_results = [[45.4217796325684, -75.6911926269531], [45.2931327819824, -75.7756805419922]] assert [result.latlng for result in g] == expected_results
def main(): print ("in main function") g = '' init_global_variables() db_select = init_db() db_update = init_db() cur_select = create_cursor(db_select) cur_insert = create_cursor(db_update) #todo: set the cursor here? #inputFile = open('C:\\Users\\cathleen\\2015 Voter File\\voterhistory.txt',"r") # stuff to finish cur_select.execute(sql_stmt) row = cur_select.fetchone() lineCount = 0 nolatlng = 0 while row is not None: lineCount +=1 fieldsToUpdate = [] gaddr = '' gaddr = row[1] + " " + row[2] + " " + row[4] + " RI" g = geocoder.bing(gaddr, key='AhZjbRIwW3C8eSJ0j05OlZMqWQjhsnpC3X-q9eUY4TqNjIR4RhxgG2oXL7qA3LXu') #g = geocoder.google(gaddr) # only set the fields for the vstat id, the lat and the lng # after processing each column and each c is set up for the insert # - push (append) g.latlng[0] as lat to fieldsToUpdate # - push (append) g.latlng[1] as lng to fieldsToUpdate if (len(g.latlng) == 0): # what is the latlng for the center of ri?? fieldsToUpdate.append(0.0) fieldsToUpdate.append(0.0) nolatlng +=1 e = 'had an error with ' + gaddr print(e, file = outputErrorFile) print(e) else: fieldsToUpdate = [g.latlng[0], g.latlng[1], row[0] ] update_rows(db_update, cur_insert, fieldsToUpdate) # print(row) # and get the next row row = cur_select.fetchone() if ((lineCount % 100) == 0): print ("processing line number ", lineCount) commit_changes(db_update) #think about...???? do a commit after each insert? instsead of once at the end.... commit_changes(db_update) #fetch_rows(cur) print('number of addresses with no lat lng = ', nolatlng , file = outputErrorFile) print('number of addresses with no lat lng = ', nolatlng) print('record count = ', lineCount , file = outputErrorFile) print('record count = ', lineCount) db_update.close() return
def geolocate(address, service="geocodefarm"): data = {} if service == "geocodefarm": location_data = geocoder.geocodefarm(address) elif service == "osm": location_data = geocoder.osm(address) elif service == "google": location_data = geocoder.google(address) elif service == "arcis": location_data = geocoder.arcgis(address) elif service == "bing": location_data = geocoder.bing(address) elif service == "canadapost": location_data = geocoder.canadapost(address) elif service == "yandex": location_data = geocoder.yandex(address) elif service == "tgos": location_data = geocoder.tgos(address) else: raise ValueError("Unknown geocoder service") if location_data.ok: location_data = location_data.json data["country"] = location_data.get("country") data["country_code"] = location_data.get("country_code") data["region"] = location_data.get("region") data["address"] = location_data.get("address") data["state"] = location_data.get("state") data["confidence"] = location_data.get("confidence") data["lat"] = location_data.get("lat") data["lon"] = location_data.get("lng") data["city"] = location_data.get("city") data["postal"] = location_data.get("postal") data["timezone"] = location_data.get("timezone_short") else: return None location = { "city": { "name": data["city"], "state": { "name": data["state"], "country": { "code": data["country_code"], "name": data["country"] } } }, "coordinate": { "latitude": data["lat"], "longitude": data["lon"] }, "timezone": { "name": data["timezone"] } } return location
def bing_geocode(self, lugar): try: geocod = geocoder.bing(lugar, key=config_twitter.BING_API_KEY) coordenadas = geocod.geojson return coordenadas except ValueError as varr: self.log.escribir( u'Error de geocoding. Lugar: {0}. {1}'.format( lugar, varr.message), self.log.ERROR) return None
def address2latlon(addr): """ Return the coordinates of the corresponding address :param addr: (string) address :return (list) list of coordinates (float) [lat, lon] """ key = "AjBnbJXTfnqbk1fgDACBIfrnhHs6SMQGGi6XGzaqCw2lyQ_RjtnCSQaCGrFlXS_L" g = geocoder.bing(addr, key=key) # g = geocoder.google(addr) gjson = g.json timeout = time.time() + 7 while gjson is None: # Redo until we have a response g = geocoder.bing(addr, key=key) # g = geocoder.google(addr) gjson = g.json if time.time( ) > timeout: # if google can't find the address after a certain amount of time sys.exit("Google ne trouve pas cette adresse, veuillez réessayer") return g.latlng
def test_multi_results(): g = geocoder.bing(location, maxRows=3) assert len(g) == 2 assert g.city == city expected_results = [ [45.4217796325684, -75.6911926269531], [45.2931327819824, -75.7756805419922] ] assert [result.latlng for result in g] == expected_results
def geocodeLocation(index, row): try: #html = requests.get(url, stream=True) #open(f'{file_name}.json', 'wb').write(html.content) #print("row:", row, "printed#######################") geocodedLoc.loc[index, 'latLong'] = geocoder.bing(geocodedLoc.loc[index, 'Block']+', '+geocodedLoc.loc[index, 'District']+', '+geocodedLoc.loc[index, 'State']+', India', key='AvXEL2IXYy89DtKTFqDlt5eujV_JyPT9lQ564nEPJgmETY-ye9Yj59DvYn02p-GK').latlng #geocodedLoc.loc[(geocodedLoc['State'] == row[0]) & (geocodedLoc['District'] == row[1]) & (geocodedLoc['Block'] == row[2])] = row print("row:", row, "printed#######################") return row except requests.exceptions.RequestException as e: return e
def test_multi_results(): g = geocoder.bing(location, maxRows=3) assert len(g) == 3 assert g.city == city expected_results = [ [45.4217796325684, -75.6911926269531], [45.2931327819824, -75.7756805419922], [36.9871711730957, -94.7606735229492], ] assert [result.latlng for result in g] == expected_results
def new_crime(address, date): # Get address from Bing Maps API g_address = geocoder.bing(address, key=BING_KEY) if not g_address: return 'invalid address' geocode_address = g_address.latlng crime = Crime(incident_date=date, latitude=geocode_address[0], longitude=geocode_address[1]) db.session.add(crime) db.session.commit() return str(crime.id)
def test_bing_details(): details = {'adminDistrict': 'Ontario', 'locality': 'Ottawa'} g = geocoder.bing(None, method='details', **details) assert g.ok assert g.city == city osm_count, fields_count = g.debug()[0] assert osm_count >= 3 assert fields_count >= 12 details = { 'addressLine': '6912 Route 8', 'adminDistrict': 'Northumberland', 'countryRegion': 'CA', 'locality': 'Ludlow' } g = geocoder.bing(None, method='details', **details) assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count >= 3 assert fields_count >= 12
def geocode(self, address, session): # url = 'http://localhost/nominatim/' if self.offline: url = 'localhost' self.g = geocoder.osm(address, url=url, session=session) if self.kind == 'arcgis': self.g = geocoder.arcgis(address, session=session) if self.kind == 'here': self.g = geocoder.here(address, session=session, app_id=here_app_id, app_code=here_app_key) if self.kind == 'bing': self.g = geocoder.bing(address, session=session, key=bing_key) res = processs_geojson(self.g) return res
def zipcode(): if request.method == 'GET': return render_template('zipcoding.html') else: # request was a POST #app.vars['pandas'] = request.form['keyword'] file = request.files.get("data_file") if file.filename.endswith('.csv'): df = pd.read_csv(file) else: df = pd.read_excel(file) g = Bing( 'AoGg6nfFsORF7WrNxkvEQpj2r-O7WTS5hoOYXg6fDynZIo4JkGcFZ-UPjPJ7HQda', timeout=5) df2 = df.drop_duplicates(subset=['latitude', 'longitude'], keep='first') reverse = df2[['latitude', 'longitude']] reverse_list = reverse.values.tolist() zips = [] import geocoder # pip install geocoder for i in reverse_list: g = geocoder.bing( i, method='reverse', key= 'AoGg6nfFsORF7WrNxkvEQpj2r-O7WTS5hoOYXg6fDynZIo4JkGcFZ-UPjPJ7HQda' ) try: zips.append(g.postal) except: zips.append('N/A') reverse['zipcode'] = zips reverse = reverse.reset_index() #reverse = reverse.drop('action_timestamp', 1) result = pd.merge(df, reverse, how='left', on=['latitude', 'longitude']) result resp = make_response(result.to_csv()) resp.headers[ "Content-Disposition"] = "attachment; filename=export_zip.csv" resp.headers["Content-Type"] = "text/csv" return resp
def bruteforceGeocoding(dict_address, apiKey, waitTime): #la funziona tenta un approccio di georeferenziazione a forza bruta prendendo #prima tutti i dati a disposizione e poi andando a scalare se questi #non danno risultati time.sleep(waitTime) if ('ADDRESS' in dict_address.keys()) & ('CITY' in dict_address.keys()) & ( 'ZIPCODE' in dict_address.keys()): g = geocoder.bing(location=None, addressLine=dict_address['ADDRESS'], locality=dict_address['CITY'], postalCode=dict_address['ZIPCODE'], method='details', key=apiKey) a = g.json if a is not None: return time.sleep(waitTime) if ('ADDRESS' in dict_address.keys()) & ('CITY' in dict_address.keys()): g = geocoder.bing(location=None, addressLine=dict_address['ADDRESS'], locality=dict_address['CITY'], method='details', key=apiKey) a = g.json if a is not None: return a time.sleep(waitTime) if ('CITY' in dict_address.keys()): g = geocoder.bing(location=None, locality=dict_address['CITY'], method='details', key=apiKey) a = g.json if a is not None: return a #se sono arrivato qui non ho trovato nulla return None
def gcode(row): logger.info("Inside gcode...City: %s :: State: %s", row['City'], row['State']) g = geocoder.bing(row['City'] + ", " + row['State'], key=os.environ['BING_API_KEY']) if 'adminDistrict2' in g.json['raw']['address']: county = g.json['raw']['address']['adminDistrict2'] logger.info("City: %s :: State: %s :: Country: %s", row['City'], row['State'], county) return (county) else: logger.warning("City: %s :: State: %s . . . No county found", row['City'], row['State']) return (np.nan)
def geo_reverse(latitude, longitude, my_bing_map): """ A function using the Reverse Geocoding. Using the the Bing map API key converts the given latitude and longitude to a phyiscal address or location. Parameters: latitude and longitude. Return: a zipcode corresponding to the latitude and longitude pair. """ api_key = my_bing_map g = geocoder.bing([latitude, longitude], method="reverse", key=api_key) zipcode = g.postal return zipcode
def latlon2address(lat, lon): """ Reverse geocoding from coordinates to addresses :param lat: (float) latitude :param lon: (float) longitude :return n: (tuple) address of coordinate (address, street, city, state, postal, country) """ key = "AjBnbJXTfnqbk1fgDACBIfrnhHs6SMQGGi6XGzaqCw2lyQ_RjtnCSQaCGrFlXS_L" # quota de 125 000 requêtes/année b = geocoder.bing([lat, lon], method="reverse", key=key) timeout = time.time() + 10 while b.city is None: b = geocoder.bing([lat, lon], method="reverse", key=key) if b.city is None and time.time( ) > timeout: # if google can't find the address after a certain amount of time return "no info", "0", "no info", "no info", "no info", "no info", "no info" # sys.exit("Bing ne trouve pas d'adresse, veuillez réessayer") no_st = b.street print(no_st) # for bug detection # no info if b.street is None: no, st = "0", "no info" # no house number elif not no_st[0].isnumeric(): no = "0" st = no_st # no house number and street name starting with a number (ex: 4th street) elif (no_st[0].isnumeric() and no_st[1].isalpha()) or \ (no_st[0].isnumeric() and no_st[1].isnumeric() and no_st[2].isalpha()) or \ (no_st[0].isnumeric() and no_st[1].isnumeric() and no_st[2].isnumeric() and no_st[3].isalpha()): no = "0" st = no_st else: match = re.match(r'(\d+)(?:-\d+(?=\s))?\s(.*)', no_st).groups() no = match[0] st = match[1] return b.address, no, st, b.city, b.state, b.postal, b.country
def geo_forward(address, my_bing_map): """ A function using the Geocoding. Using the the Bing map API key converts the given address or location into latitude and longitude. Parameter: address Return: latitude and longitude corresponding to this address """ api_key = my_bing_map g = geocoder.bing(address, key=api_key) latitude = g.lat longitude = g.lng return latitude, longitude
def geocode(geo_info): """ Get GeoJSON coordinates from an address List of availables geocoders : * geocoder.geolytica * geocoder.mapquest * geocoder.arcgis * geocoder.geonames * geocoder.nokia * geocoder.bing * geocoder.google * geocoder.yahoo * geocoder.tomtom Just change the geocoder below when you hit the limit """ geo = geocoder.bing(geo_info) return geo.geojson
def buscarTweets(contenido, direccion, radio): insertados = 0; if(geocoder.google(direccion).lat > 0.0): location = geocoder.google(direccion) geocode = str(location.lat) + "," + str(location.lng) + "," + str(radio) + "km" result = api.search(q=str(contenido), geocode=geocode, rpp=1000, show_user=1) for tweet in result: userCity = str(tweet.user.time_zone) if (tweet.coordinates != None): tweetCoordinates = tweet.coordinates['coordinates'] g = geocoder.bing(tweetCoordinates, method='reverse') tweetLocation = g.locality if (g.locality != None) else g.country else: tweetCoordinates = None tweetLocation = None if (geocoder.google(tweet.user.location).locality): userCity = geocoder.google(tweet.user.location).locality else: if (geocoder.google(tweet.user.location).country) : userCity = geocoder.google(tweet.user.location).country tweetNuevo = { 'contenido' : contenido, 'direccion' : direccion, 'radio': radio, 'texto': tweet.text, 'ciudadUsuario': str(userCity), 'coordenadasCiudadUsuario': [geocoder.google(tweet.user.location).lat, geocoder.google(tweet.user.location).lng], 'CoordenadasTweet':tweetCoordinates, 'ciudadTweet':tweetLocation, 'nombreUsuario':tweet.user.screen_name } print tweetNuevo insertados += 1 tweetCol.insert(tweetNuevo) print 'FIN' return insertados
def geocode(location, provider='google', display_map=False): # Try to find the location 10 times before raising the warning and returning coordinates (0.0, 0.0) i = 0 while True: if provider == 'google': geo = geocoder.google(location) elif provider == 'nokia': geo = geocoder.nokia(location) elif provider == 'osm': geo = geocoder.osm(location) elif provider == 'bing': geo = geocoder.bing(location) elif provider == 'tomtom': geo = geocoder.tomtom(location) if geo.json['status'] == 'OK' or i == 10: break i+=1 #print geo.json if display_map == True: #Check if address is a coordinate pair if location.replace(',', '').replace('.', '').replace('-', '').isdigit(): url = "http://maps.google.com/?q=%s" % geo.address else: url = "http://www.google.com/maps/place/%s,%s" % (geo.lat, geo.lng) webbrowser.open(url) if geo.json['status'] == 'OK': #time.sleep(0.5) # Try to avoid the rate limit return geo.json else: warn = 'WARNING: %s was not found! Coordinates (0.0, 0.0) was returned instead.' % location warnings.warn(warn) #time.sleep(0.5) # Try to avoid the rate limit return {'lat': 0.0, 'lng': 0.0}
def main(): np_concat = np.vectorize(concat) # Tratamos los ficheros de Autonomos #for file in glob.glob(PATH_DATA + '/Autonomos*.csv'): #print('Parsing file %s' % file) frame = pd.read_csv('empresas_asturias/Empresas1(Asturias).csv', sep=';') frame['latitude'] = np.nan frame['longitude'] = np.nan frame['approximation'] = np.nan frame['concatenated'] = np_concat(frame['Direccion'], frame['CP'], frame['Poblacion'], frame['Provincia'], frame['Comunidad Autonoma'], 'España') #for i in range(0,100): for idx, row in frame.iterrows(): #address = frame.ix[i, 'concatenated'] address = row['concatenated'] #print address ############################################################################### # Codigo geopy # ############################################################################### #geolocator = Bing() #exception = False #try: # location = geolocator.geocode(address, exactly_one=True, timeout=20) #except geopy.exc.GeocoderTimedOut: # exception = True #except geopy.exc.GeocoderQueryError: # exception = True #print location.address #print location.latitude #print location.longitude #print "" #frame.loc[i, 'latitude'] = location.latitude if exception == False and location.latitude else np.nan #frame.loc[i, 'longitude'] = location.longitude if exception == False and location.longitude else np.nan ############################################################################### # Codigo geocoder # ############################################################################### location = geocoder.bing(address) #print location.address #print location.lat #print location.lng #print "" #frame.loc[i, 'latitude'] = str(location.lat).replace(".",",") if location and location.lat else str(np.nan) #frame.loc[i, 'longitude'] = str(location.lng).replace(".",",") if location and location.lng else str(np.nan) #if location: # if location.status != "OK": # frame.loc[i, 'approximation'] = "2" # elif str(frame.loc[i, 'Direccion']) == "nan": # frame.loc[i, 'approximation'] = "2" # else: # frame.loc[i, 'approximation'] = "1" #else: # frame.loc[i, 'approximation'] = np.nan frame.loc[idx, 'latitude'] = str(location.lat).replace(".",",") if location and location.lat else str(np.nan) frame.loc[idx, 'longitude'] = str(location.lng).replace(".",",") if location and location.lng else str(np.nan) if location: if location.status != "OK": frame.loc[idx, 'approximation'] = "3" elif str(frame.loc[idx, 'Direccion']) == "nan": frame.loc[idx, 'approximation'] = "2" else: frame.loc[idx, 'approximation'] = "1" else: frame.loc[idx, 'approximation'] = np.nan frame.__delitem__('concatenated') frame.to_csv('data_results.csv', sep=';')
address2 = address[0:2] text2 = text[0:2] address1000 = address[0:1000] text1000 = text[0:1000] time = df.created_at[0:1000] results = [] lat = [] lon = [] pcode = [] coord = [] attempts = 0 for addr in address1000: g = geocoder.bing(addr, key = bing_key, url = bing_url) attempts += 1 time.sleep(1) #Probably should report g.ok here results.append(g) lat.append(g.lat) lon.append(g.lng) pcode.append(g.postal) coord.append(",".join([str(g.lat), str(g.lng)])) print attempts df_1000 = pd.DataFrame() df_1000["address"] = address1000 df_1000["text"] = text1000 df_1000["lat"] = lat
def geocode_df_row(row): try: g1 = geocoder.bing(row.full_address, key=bkey) return pd.Series([g1.latlng[0], g1.latlng[1]], index=['latitude','longitude']) except: pass
# Find Existing in City search = list() for item in db_city.find().skip(22000).limit(50000): if not item['location'] in existing: search.append(item) print 'Remaining:', len(search) # Scan Database for item in search: location = item['location'] x, y = item['geometry']['coordinates'] # Geocode Address if provider == 'bing': g = geocoder.bing(location) elif provider == 'osm': time.sleep(1) g = geocoder.osm(location) # Calculate Distance with Haversine formula distance = haversine([y, x], [g.lat, g.lng]) * 1000 # Set Results results = g.json results['city'] = city results['distance'] = distance # Save in Mongo DB try: db_geocoder.insert(results)
final.columns = ['cities', 'raw','address','zip'] waffles = data.append(final) return(waffles) listOfStates = ['al','ar','az','ca','co','de','fl','ga', 'il','in','ks','ky','la','md','mi','mo', 'ms','nc','nm','ny','oh','ok','pa','sc', 'tn','tx','va','wi','wv'] wafflesdf = pd.DataFrame() for item in listOfStates: print "processing " + item tempdf = waffles(item) wafflesdf = wafflesdf.append(tempdf) print "finished with " + item data = pd.read_csv('~/waffles.csv') data['full'] = data['address'] + ", " + data['cities'] + " " + data['zip'] coordinates = [] for a in data['full']: print a coordinates.append(geocoder.bing(a, key = keyString).latlng) coordinates = pd.DataFrame(coordinates) coordinates.columns = ['lats','lon'] data = data.loc[:, "full"] data = pd.concat([data, coordinates], axis=1) data.to_csv('~/geoWaffles.csv')
import geocoder import csv with open('fall_2013.csv', 'w') as csvfile_w: writer = csv.writer(csvfile_w) with open('Fallecidos_2013.csv', 'r') as csvfile: reader = csv.reader(csvfile) for row in reader: direccion = row[10] +", " + row[1] + ", Uruguay" g = geocoder.google(direccion) if g.lat == 0.0: b = geocoder.bing(direccion) row.append(b.lat) row.append(b.lng) else: row.append(g.lat) row.append(g.lng) print row writer.writerow(row)
def test_bing_unicode(): g = geocoder.bing('1 rue bergere, paris') assert g
def get_location(self, address): key_bing = 'AlGO9QXKhTKI8tTIqpEcWNVenBn7x1yqeWYhs1lVgSwDSUVHM1y7QQ2yo97zculq' g = geocoder.bing(address ,key=key_bing) return g
def test_bing_reverse(): g = geocoder.bing(ottawa, method='reverse') assert g.ok assert g.city == city
def test_bing_reverse(): g = geocoder.bing(ottawa, reverse=True) assert g.ok
def test_bing(): g = geocoder.bing(location) assert g.ok
from geopy.distance import vincenty newport_ri = (41.49008, -71.312796) cleveland_oh = (41.499498, -81.695391) print(vincenty(newport_ri, cleveland_oh).miles) ## great-circle distance from geopy.distance import great_circle newport_ri = (41.49008, -71.312796) cleveland_oh = (41.499498, -81.695391) print(great_circle(newport_ri, cleveland_oh).miles) ## 查询坐标 import geocoder as geo # bing g = geo.bing('中国人民大学',key='AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') place = geo.bing(g.latlng,method='reverse',key='AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') # baidu g = geo.baidu('中国人民大学',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') #place = geo.baidu(g.latlng,method='reverse',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') from geopy.geocoders import Baidu,Bing geoBaidu = Baidu(r'DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') location = geoBaidu.geocode("中国人民大学") place= geoBaidu.reverse((location.latitude,location.longitude))
for i in data.Hundred_Block: if i == 'Intersection': test.append('') else: test.append(i.replace("#", "0")) data['h_block'] = test data['city'] = " Vancouver, BC, Canada" data['full_address'] = data['h_block'] +" "+ data['Street_Name'] +","+data['city'] print data['full_address'] lat =[] lon = [] for i in data['full_address']: # temp = geocoder.osm(i) temp = geocoder.bing(i, key='AjIweCKDtll_jAgD6sJeJJzco_aEzO2mwZ5SgpCLLmC1WIvvJXGVEkeWQtsehwc8') print(str(temp.lat) +",", str(temp.lng)) lat.append(temp.lat) lon.append(temp.lng) # time.sleep(1) data['lat'] = lat data['lon'] = lon data.to_csv('/Users/Jozo/Projects/Github-local/Workshop/aloha-r/data/CaseLocationsDetails_2014_CSV/201401CaseLocationsDetails-geo.csv') # pk.eyJ1Ijoiam9leWtsZWUiLCJhIjoiMlRDV2lCSSJ9.ZmGAJU54Pa-z8KvwoVXVBw # bing: AjIweCKDtll_jAgD6sJeJJzco_aEzO2mwZ5SgpCLLmC1WIvvJXGVEkeWQtsehwc8 # temp = geocoder.bing('COLLINGWOOD ST and W BROADWAY,Kitsilano, Vancouver, BC, Canada', key='AjIweCKDtll_jAgD6sJeJJzco_aEzO2mwZ5SgpCLLmC1WIvvJXGVEkeWQtsehwc8'); print temp.lat; print temp.lng
def geocoder_search(bot, location_name): geocoded = geocoder.bing(location_name, key=bot.config.apikeys.bing_maps_key) return geocoded.json
def test_bing_batch_reverse(): g = geocoder.bing(locations_reverse, method='batch_reverse') assert g.ok assert len(g) == 2 assert [result.city for result in g] == ['New York', 'Paris']