def get_current_graph(self): amenities = [l for l in self.labels if l in self.amenity_options] graph = sc_lib.graph() city_entered = self.city_entry.get().strip() coords_entered = self.coords_entry.get().strip() if len(city_entered): graph.city = city_entered center_point = (0,0) if len(coords_entered): center_point = tuple([float(s) for s in coords_entered.split(',')]) elif len(city_entered): gn = geocoders.GeoNames(username='******') loc = gn.geocode(city_entered) center_point = (loc.latitude,loc.longitude) else: print('must enter city name or coordinates') return None if len(self.sensor_locs_path): graph.add_sensor_locs(self.sensor_locs_path) rang = 1000 entered_rang = self.range_entry.get().strip() if len(entered_rang): rang = float(entered_rang)*1000 graph.add_OSMnx_pois(amenities,center_point,rang) if graph: for label in self.labels: for node in self.label_to_nodes[label]: graph.add_node(node) return graph
def index(): if request.method == 'POST': device_id = request.form['device_id'] gender = request.form['gender'] age = request.form['age'] city = request.form['city'] phone_brand = request.form['phone_brand'] event_id = 0 with open(data_path + 'events.csv', 'r') as f: for row in reversed(list(csv.reader(f))): event_id = int(row[0]) + 1 break with open(data_path + 'gender_age_train.csv', 'a') as newFile: newFileWriter = csv.writer(newFile) newFileWriter.writerow([device_id, gender, age]) timestamp = datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S') gn = geocoders.GeoNames(username="******") location = gn.geocode(city) print(timestamp) print(location.latitude) print(location.longitude) with open(data_path + 'events.csv', 'a') as newFile: newFileWriter = csv.writer(newFile) newFileWriter.writerow([ event_id, device_id, timestamp, location.longitude, location.latitude ]) with open(data_path + 'phone_brand_device_model.csv', 'a') as newFile: newFileWriter = csv.writer(newFile) newFileWriter.writerow([device_id, phone_brand]) return render_template("index.html")
def get_user_event_distance(self, user, event, is_search_eachtime=False): g = geocoders.GeoNames() user_event_distance = 0 if user['location'] is not None and event[ 'latitude'] is not None and event['longitude'] is not None: #不超过400 user_event_distance = 400 event_coordinate = (event['latitude'], event['longitude']) if 'latitude' in user and is_search_eachtime == False: user_coordinate = (user['latitude'], user['longitude']) d = distance.distance(user_coordinate, event_coordinate).miles if d < user_event_distance: user_event_distance = d else: user_event_distance = user_event_distance else: #多个距离选最近的 results = g.geocode(user['location'], exactly_one=False) closest_index = 0 for i, (_, user_coordinate) in enumerate(results): d = distance.distance(user_coordinate, event_coordinate).miles if d < user_event_distance: user_event_distance = d closest_index = i self.db.user.update({'id': user['id']}, { '$set': { 'latitude': results[closest_index][1][0], 'longitude': results[closest_index][1][1] } }) return user_event_distance
def loc_resolve(geocodable_string): ''' resolve any string using geonames geocoder. output the most populous place found ''' # see http://www.geonames.org/export/codes.html ##city_fcodes=set(['PPL','PPLC','PPLA','PPLA2','PPLA3','PPLA4','PPLG','PPLS']) # better use 'fcl'='P' (as below) ## @@todo: make username configurable gn = geocoders.GeoNames(username="******") locs = None # remove '?' and ' ' from name (they can be used as meta-info in webforms) geocodable_string = geocodable_string.lstrip(' ?') try: locs = gn.geocode(geocodable_string, exactly_one=False, timeout=3) except: pass if not locs: return None elif len(locs) == 1: return locs[0] elif len(locs) > 1: locs = [l for l in locs if 'fcl' in l.raw and l.raw['fcl'] == 'P'] #locs.sort(key=lambda x: x.raw['population'],reverse=True) #print "%s (%s) vs %s (%s)" % ( locs[-2], locs[-2].raw['population'], locs[-1], locs[-1].raw['population'] ) if len(locs) > 0: return locs[0] else: return None else: return None
def get_lat_lon(data): """ Get latitude and longitude from cities in data. Args: data: Data file outputted from `get_data`. Returns: A dictionary where the keys are the name of the cities and the values are tuples with latitude and longitude of these cities. """ from time import sleep from geopy import geocoders from geopy.exc import GeocoderTimedOut gn = geocoders.GeoNames(username='******') cities = get_cities(data).keys() coords = {} for city in cities: while True: try: loc = gn.geocode(city + ", Brazil") except GeocoderTimedOut: sleep(2) else: break coords[city] = (loc.latitude, loc.longitude) return coords
def search(request): user_id = request.session['user_id'] user = conn.get_user(user_id) user_id = user['_id'] query = request.REQUEST['query'] matches = re.search('near (.*)', query) lat = None lng = None if matches is not None: gh = geocoders.GeoNames(username="******") location_string = matches.group(1) location = gh.geocode(location_string) if location: lat = location[1][0] lng = location[1][1] starttime = None endtime = None if query == 'two weeks ago': starttime = datetime(2014, 1, 10) endtime = datetime(2014, 1, 16) images = [ x for x in conn.get_images_detailed( user_id, start_time=starttime, end_time=endtime, coords=[lat, lng]) ] return HttpResponse(json.dumps(images, default=json_util.default), content_type="application/json")
def get_active_geocoders(): active_geocoders = [] active_geocoders.append({ 'name': _('Google geocoder'), 'geocoder': geocoders.GoogleV3(), 'is_exact': True, }) if MAPQUEST_API_KEY: active_geocoders.append({ 'name': _('MapQuest geocoder'), 'geocoder': geocoders.MapQuest(api_key=MAPQUEST_API_KEY), 'is_exact': False, }) if GEONAMES_USERNAME: active_geocoders.append({ 'name': _('Geonames places database'), 'geocoder': geocoders.GeoNames(username=GEONAMES_USERNAME), 'is_exact': False, }) active_geocoders.append({ 'name': _('Nominatim database'), 'geocoder': geocoders.OpenMapQuest(), 'is_exact': False, }) return active_geocoders
def getCoor(s): ## inser the username for geopy account: gn = geocoders.GeoNames(username='******') geolocator = Nominatim() location = geolocator.geocode(s) #takes the string as an input return (location.latitude, location.longitude)
def get_lat_long(place_name, geonames_username): gn = geocoders.GeoNames(username=geonames_username, timeout=None) location = gn.geocode(place_name, timeout=None) if location == None: print(place_name, "not found.") return None, None else: return location.latitude, location.longitude
def get_all_scores(self): gn = geocoders.GeoNames(username='******') #df = #il dataframe che esce fuori dalla query 3.1 q = self._query_builder() price_max = input('What price you want?') room_count = input('Hou many rooms you want?') wanted_city = input('In which city you would stay?') _, city_coords = gn.geocode(wanted_city+', TX',timeout=30) #wanted city è un input df['score'] = df.apply(lambda x: self._score(), axis = 1)
def execute(args): dbase_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), "flickr.db") dbase_file = open(dbase_file_name, "r") dbase_post_gis = dbase_file.read().strip().replace("\n", " ") dbase_file.close() database = psycopg2.connect(dbase_post_gis) cur = database.cursor() update = database.cursor() search_SQL = "SELECT locale FROM locales WHERE result IS NULL" notfound_SQL = "UPDATE locales SET result='None', result_count=0 WHERE locale=%s" manyfound_SQL = "UPDATE locales SET longitude=%f, latitude=%f, result=%s, result_count=%i WHERE locale=%s" onefound_SQL = "UPDATE locales SET longitude=%f, latitude=%f, result=%s, result_count=1 WHERE locale=%s" time_SQL = "UPDATE locales SET source=%s, geocoded=%s WHERE locale=%s" source = "GeoNames" gn = geocoders.GeoNames() cur.execute(search_SQL) for i, row in enumerate(cur): locale,= row print i, "Checking %s" % locale t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") update.execute(time_SQL, (source, t, locale)) result = gn.geocode(locale, exactly_one=False) if result == None: print i, "Unfound location %s" % locale update.execute(notfound_SQL, (locale)) elif type(result) == list: print i, "Multiple locations %s" % locale result_count = len(result) _, (lat, lng) = result[0] update.execute(manyfound_SQL, (lng, lat, repr(result), result_count, locale)) elif type(result) == tuple: _, (lat, lng) = result if lat < -90 or lat > 90 or lng < -180 or lng >180: print i, "Out of range %s" % locale print "Found %s" % locale update.execute(onefound_SQL, (lng, lat, repr(result), locale)) else: print i, "Results not recognized %s" % locale database.commit() cur.close() database.commit() database.close()
def geo_info_for_geo_name(geo_name: str, username: str = CONFIG["geonames_username"] ) -> GeoInfo: """Get geo information (latitude and longitude) for given region name.""" logging.info("Decoding latitude and longitude of '{}'...".format(geo_name)) gn = geocoders.GeoNames(username=username, timeout=10) location = gn.geocode(geo_name) return GeoInfo(geo_name=geo_name, lat=location.latitude, long=location.longitude)
def findLocations(user_name, locs, file_name_s): #TODO: delete before committing geolocator = GeoNames(username=user_name) g = geocoders.GeoNames(username=user_name) csv_writer = csv.writer(open(file_name_s, 'wb')) csv_writer.writerow(['LOCATION', 'LAT', 'LON']) for loc in locs: loc = str(loc) coord = coordFromLocName(loc, geolocator, g) csv_writer.writerow([loc, coord[0], coord[1]])
def main(): search_string = 'Poly Prep Country Day School, NY' geolocator = geocoders.GeoNames() location = geolocator.geocode(search_string) if location == None: print('None found') else: print(location.address)
def getGeonamesResults(place_name): g = geocoders.GeoNames() try: results = g.geocode(place_name, False) formatted_results = [] for result in results: formatted_results.append(formatExternalGeocode('Geonames', result)) return formatted_results except: return []
def weathernow(nameofcity): gn = geocoders.GeoNames(username='******') coordinates = gn.geocode(nameofcity) weatherinfo = forecast('873f4066624ce226aba8b4a882829f93', coordinates.latitude, coordinates.longitude) weatherinfo.temperature = ( (5 / 9) * (weatherinfo.temperature - 32)) #converting F to C # print(weatherinfo.temperature, weatherinfo.summary, weatherinfo.daily.summary,sep= '\n') return round(weatherinfo.temperature ), weatherinfo.summary, weatherinfo.daily.summary
def getGeonamesResults(place_name): g = geocoders.GeoNames(username=settings.GAZETTEER_GEONAMES_USER) try: results = g.geocode(place_name, False) formatted_results = [] for result in results: formatted_results.append(formatExternalGeocode('Geonames', result)) return formatted_results except Exception: return []
def findTimezones(user_name, file_name, file_name_s): geolocator = GeoNames(username=user_name) g = geocoders.GeoNames(username=user_name) location_index = 0 lat_index = 1 lon_index = 2 res = [] data = [] HOUR = 60 * (60 + 4) utc = pytz.utc utc.zone dat = csv.reader(open(file_name)) w = tzwhere.tzwhere() i = 0 for row in dat: if i > 0: data.append([row[location_index], row[lat_index], row[lon_index]]) i = i + 1 csv_writer = csv.writer(open(file_name_s, 'wb')) #print "number of rows: ", len(data) csv_writer.writerow(HEADER2) for row in data: if (row[lat_index] <> '0' and row[lon_index] <> '0'): lat = float(row[lat_index]) lon = float(row[lon_index]) timezone = w.tzNameAt(lat, lon) print lat print lon print timezone try: country_info = reverceGeoCode([row[lat_index], row[lon_index]], g, geolocator, user_name) except GeocoderServiceError: print "hourly limit has been exceeded, time to wait for an hour..." time.sleep(HOUR) print "starting again..." country_info = reverceGeoCode([row[lat_index], row[lon_index]], g, geolocator, user_name) try: time_diff = timeDifference(utc, timezone) except AttributeError: time_diff = 0 print timezone temp = [ row[location_index], row[lat_index], row[lon_index], timezone, time_diff, country_info[2], country_info[3], country_info[4] ] else: temp = row + [0, 0, 0, 0, 0] res.append(temp) try: csv_writer.writerow(temp) except UnicodeEncodeError: csv_writer.writerow(row + [0, 0, 0, 0, 0]) return res
def tornadoes_preprocessing(): df_tornado = pd.read_csv('tornadoes.csv', sep=",") df_graldata = general_data_preprocessing() # Drop columns Im not interested in df_tornado = df_tornado.drop('YYYYMMDDHHMM_UTC', 1) df_tornado = df_tornado.drop('HHMM_LOCAL', 1) df_tornado = df_tornado.drop('MOTION_DEG', 1) df_tornado = df_tornado.drop('FORECAST_REGION', 1) df_tornado = df_tornado.drop(df_tornado.columns[15], 1) df_tornado = df_tornado.drop(df_tornado.columns[15], 1) df_tornado = df_tornado.drop(df_tornado.columns[15], 1) # Fill nans df_tornado = fix_nans(df_tornado, 'DD_LOCAL') df_tornado = fix_nans(df_tornado, 'MM_LOCAL') df_tornado['MM_LOCAL'] = df_tornado['MM_LOCAL'].round() df_tornado['DD_LOCAL'] = df_tornado['DD_LOCAL'].round() df_tornado['HUMAN_FATAL'].fillna("0", inplace=True) df_tornado['HUMAN_INJ'].fillna("0", inplace=True) df_tornado['DMG_THOUS'].fillna("0", inplace=True) # end latitude and end longitude if they are empty, they will get the value of start lat and start long df_tornado['END_LAT_N'].fillna(df_tornado['START_LAT_N'], inplace=True) df_tornado['END_LON_W'].fillna(df_tornado['START_LON_W'], inplace=True) # I insert in df_tornado the latest data of tornadoes from general data recent_tornadoes = df_graldata[(df_graldata['EVENT TYPE'] == 'Tornado') & (df_graldata['EVENT'] > '2010-01-01')] gn = geocoders.GeoNames(username='******') i = len(df_tornado) j = 0 print(df_tornado) for m in range(len(recent_tornadoes) - 1): date = recent_tornadoes['EVENT'].values[j] dt = datetime.strptime(date, '%Y-%m-%d') location = gn.geocode(recent_tornadoes['PLACE'].values[j]) aux = [ dt.year, dt.month, dt.day, recent_tornadoes['PLACE'].values[j], recent_tornadoes['PLACE'].values[j], recent_tornadoes['MAGNITUDE'], location.longitude, location.latitude, location.longitude, location.latitude, 0, 0, recent_tornadoes['FATALITIES'].values[j], recent_tornadoes['INJURED / INFECTED'].values[j], recent_tornadoes['ESTIMATED TOTAL COST'].values[j] ] df_tornado.loc[len(df_tornado)] = aux i = i + 1 j = j + 1 df_tornado.to_csv(r'C:\Users\miksm\Desktop\result_tornadoes.csv')
def lat_long(i, l): gn = geocoders.GeoNames(username='******') location = findGeocode(i) if location is not None: if l == 1: return location.longitude else: return location.latitude else: return np.nan
def retrieve_lat_and_lon(geonames_username, institute_of_origin): """ Retrieve latitude and longitude of the institute of origin of the paper from GeoNames server (or manually input if for some reason server is unavailable) """ try: username = flags.geonames_username geolocater = geocoders.GeoNames(username=username) location_info = geolocater.geocode(institute_of_origin) return location_info # If the GeoName server cannot be accessed for some reason, ask # user for manual input except HTTPError as e: print("Request to GeoNames server failed due to ", e) confirm_man_coord_input = ask_for_confirmation( "Manually input lat/long?") if confirm_man_coord_input: while True: latitude = input("\nLatitude of institute: ").strip() longitude = input("\nLongitude of institute: ").strip() # See if lat/lon input can be converted to a float. If it # can, treat it as a valid entry. We can check later to # see if the input lat/lon actually correlate to valid # coordinates # TODO: write some verification function to see if the lat/lon # input is actually valid try: float(latitude) float(longitude) location_info = { 'lat': latitude, 'lon': longiutde, } return location_info except ValueError: print("Error: input could not be converted to float") confirm_loop_continuation = ask_for_confirmation( "Try again?") if confirm_loop_continuation: continue else: return None # If user does not want to manually input lat/lon at this time, # return nothing else: return None
def _find_location(self): """ Find names of cities or countries in the title or text of the article, if found, look up the coordinates of the place and return it as a tuple """ places = GeoText(' '.join((self.results.get('title', ''), self.results.get('text', '')))) gn = geocoders.GeoNames(username='******') if places.cities: return Point(gn.geocode(places.cities[0])[1]) elif places.country_mentions: return Point(gn.geocode(places.country_mentions[0][0])[1])
def __init__(self): #initialize geocoders once: self.google = geocoders.GoogleV3() #doesn't look like yahoo supports free api any longer: #http://developer.yahoo.com/forum/General-Discussion-at-YDN/Yahoo-GeoCode-404-Not-Found/1362061375511-7faa66ba-191d-4593-ba63-0bb8f5d43c06 #yahoo = geocoders.Yahoo('PCqXY9bV34G8P7jzm_9JeuOfIviv37mvfyTvA62Ro_pBrwDtoIaiNLT_bqRVtETpb79.avb0LFV4U1fvgyz0bQlX_GoBA0s-') self.usgeo = geocoders.GeocoderDotUS() self.geonames = geocoders.GeoNames() self.bing = geocoders.Bing('AnFGlcOgRppf5ZSLF8wxXXN2_E29P-W9CMssWafE1RC9K9eXhcAL7nqzTmjwzMQD') self.openmq = geocoders.OpenMapQuest() self.mq = geocoders.MapQuest('Fmjtd%7Cluub2hu7nl%2C20%3Do5-9uzg14')
def get_lon_lat(self): for i in range(3): gn = geocoders.GeoNames(username="******", timeout=100) try: return [ float(y) for y in [ x.split(")") for x in str( gn.geocode(self.loc, exactly_one=False)).split("(") ][2][0].split(",")[:2] ] except: print("Could not get coordinates for " + self.loc) return []
def get_position(source): gn = geocoders.GeoNames('EU', 'dwoolst') citi = None if source == '': return None g = gn.geocode(source, timeout=90) if g: citi = Position(g.address, g.longitude, g.latitude) addr, lon, lat = g.address, g.longitude, g.latitude print() print(f'{addr} is located at Latitude: {round(lat,2)}, Longitude: {round(lon,2)}') return citi
def __init__(self, access_token, user_name): """ Initializes the member variables for the facebook api. """ # initializes the access token and user name variables self.access_token = access_token self.user_name = user_name # creates a variable for the api host self.api_host = 'https://graph.facebook.com/v3.0/search' # sets up geocoder to convert city and state into coordinates self.geocoder = geocoders.GeoNames(username=self.user_name)
def geopy_latlng(location, username='******', country='UK'): """ This function uses the GeoPy API. ----from geopy import geocoders---- Input :this function takes in a string PARAMETERS: country :specify which country you want to focus on username :to access the API you are required to sign up to the GeoNames service on http://www.geonames.org/ website Return :an array of coordinates is returned """ gn = geocoders.GeoNames(country_bias=country, username=username) address = gn.geocode(location) return [address.latitude, address.longitude]
def hurricanes_preprocessing(): df_graldata = general_data_preprocessing() df_hurricanes = df_graldata[( df_graldata['EVENT TYPE'] == 'Hurricane / Typhoon / Tropical Storm')] df_hurricanes = df_hurricanes.reset_index(drop=True) df_hurricanes['YEAR'] = pd.to_datetime(df_hurricanes['EVENT']).dt.year gn = geocoders.GeoNames(username='******') df_hurricanes['LONGITUDE'] = df_hurricanes['PLACE'].apply( lambda x: lat_long(x, 1)) df_hurricanes['LATITUDE'] = df_hurricanes['PLACE'].apply( lambda x: lat_long(x, 2)) df_hurricanes.dropna() df_hurricanes.to_csv(r'C:\Users\miksm\Desktop\result_hurricanes.csv')
def get_all_scores(self): """ This method get the score according to the parameters passed by input by the user """ geo_names = geocoders.GeoNames(username='******') #df = #il dataframe che esce fuori dalla query 3.1 docs_containing_enitre_query = self.conjunctive_result() price_max = int(input('What price you want?')) room_count = int(input('Hou many rooms you want?')) wanted_city = input('In which city you would stay?') _, city_coords = geo_names.geocode(wanted_city + ', TX', timeout=30) #wanted city è un input docs_containing_enitre_query[ 'score'] = docs_containing_enitre_query.apply( lambda x: self._score(price_max, room_count, city_coords, x), axis=1) return docs_containing_enitre_query
def setGeoCoders(gCoderStrings, configFilePtr): gCoders = {} config = ConfigParser.RawConfigParser() config.readfp(configFilePtr) for gCoderKey in gCoderStrings: if gCoderKey == "google": gCoders[gCoderKey] = geocoders.GoogleV3() elif gCoderKey == "geocoder.us": gCoders[gCoderKey] = geocoders.GeocoderDotUS() elif gCoderKey == "GeoNames": gCoders[gCoderKey] = geocoders.GeoNames() elif gCoderKey == "MediaWiki": gCoders[gCoderKey] = geocoders.MediaWiki() elif gCoderKey == "yahoo": gCoders[gCoderKey] = geocoders.Yahoo(config.get('API KEYS', gCoderKey)) elif gCoderKey == 'bing': gCoders[gCoderKey] = geocoders.Bing(config.get('API KEYS', gCoderKey)) return gCoders