class RateLimitErrorTestCase(unittest.TestCase): def setUp(self): httpretty.enable() self.geocoder = OpenCageGeocode('abcde') def tearDown(self): httpretty.disable() httpretty.reset() def testNoRateLimit(self): httpretty.register_uri(httpretty.GET, self.geocoder.url, body='{"status":{"code":200,"message":"OK"},"thanks":"For using an OpenCage Data API","total_results":0,"we_are_hiring":"http://lokku.com/#jobs","licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"rate":{"reset":1402185600,"limit":"2500","remaining":2479},"results":[],"timestamp":{"created_http":"Sat, 07 Jun 2014 10:38:45 GMT","created_unix":1402137525}}') # shouldn't raise an exception self.geocoder.geocode("whatever") def testRateLimitExceeded(self): httpretty.register_uri(httpretty.GET, self.geocoder.url, body='{"status":{"code":429,"message":"OK"},"thanks":"For using an OpenCage Data API","total_results":0,"we_are_hiring":"http://lokku.com/#jobs","licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"rate":{"reset":1402185600,"limit":"2500","remaining":0},"results":[],"timestamp":{"created_http":"Sat, 07 Jun 2014 10:38:45 GMT","created_unix":1402137525}}', status=429, adding_headers={'X-RateLimit-Limit': '2500', 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': '1402185600'}, ) self.assertRaises(RateLimitExceededError, self.geocoder.geocode, "whatever") # check the exception try: self.geocoder.geocode("whatever") except RateLimitExceededError as ex: self.assertEqual(str(ex), 'Your rate limit has expired. It will reset to 2500 on 2014-06-08T00:00:00') self.assertEqual(ex.reset_to, 2500)
def save(self, *args, **kwargs): if not (self.latitude and self.longitude): try: geocoder = OpenCageGeocode(settings.OPENCAGE_API_KEY) query = '{0}, {1}, {2}'.format(self.name, self.county_state_province, self.country.name) result = geocoder.geocode(query) geometry = result[0].get('geometry') self.latitude = geometry.get('lat') self.longitude = geometry.get('lng') except Exception as e: # If something goes wrong, there's not much we can do, just leave # the coordinates blank. print (e) super(Location, self).save(*args, **kwargs)
def get(self): query = self.request.get('q') key = OPEN_CAGE_KEY geocoder = OpenCageGeocode(key) if not query: latlong = self.request.headers.get('X-AppEngine-CityLatLong') latlong = string.split(latlong, ',') result = geocoder.reverse_geocode(latlong[0], latlong[1]) else: result = geocoder.geocode(query, limit=4, language='fr') self.response.headers['Access-Control-Allow-Origin'] = ACCESS_CONTROL_ALLOW_ORIGIN_URL self.response.headers['Content-Type'] = 'application/json; charset=utf-8' self.response.write(json.dumps(result))
def __init__(self, api_key=None): """ :param key: an OpenSci API key; defaults to None. """ if api_key == None: raise ValueError("An OpenCage API key must be supplied via api_key.") self.geocoder = OpenCageGeocode(key=api_key)
class Geocode: def __init__(self): key = 'Insert token here!' self.geocoder = OpenCageGeocode('995f38d3b781b46526e1b3fd7e9a78a7') def getNearestStation(self, address): result = self.geocoder.geocode(address, format='json',language='fr') if (result == []): return "Aucun resultat pour cette adresse" else: print "*------ Result : " print result latitude = str(result[0]['geometry']['lat']) print latitude longitude = str(result[0]['geometry']['lng']) print longitude url = "http://localhost:8080/restserver/station/nearest-station?latitude=" + latitude + "&longitude=" + longitude print url request = Request(url) try: response = urlopen(request) stationRoot = ET.fromstring(response.read()) m_id = stationRoot.find("id").text name = stationRoot.find("name").text howbig = stationRoot.find("howbig").text latitude = stationRoot.find("coordinates/latitude").text longitude = stationRoot.find("coordinates/longitude").text postal_code = stationRoot.find("postalCode").text city = stationRoot.find("city").text department = stationRoot.find("department").text region =stationRoot.find("region").text station = TrainStation() station.initialize(m_id, name, howbig, latitude, longitude, postal_code, city, department, region) return station except URLError, e: print "Erreur lors de la communication avec le service distant :" , e return -1
import phonenumbers from phonenumbers import geocoder samNumber = phonenumbers.parse("+19162521643") yourLocation = geocoder.description_for_number(samNumber, "en") print(yourLocation) from opencage.geocoder import OpenCageGeocode key = "2c172ea3e27b40cea25e1ad9bb71f12c" geocoder = OpenCageGeocode(key) query = str(yourLocation) results = geocoder.geocode(query) print(results[0]['geometry']['lat']) print(results[0]['geometry']['lng'])
def execute(trial=False): def select(R, s): return [t for t in R if s(t)] def project(R, p): return [p(t) for t in R] # { bike collisions : mode_type = bike # "dispatch_ts": "2015-01-01 00:24:27", # "mode_type": "mv", # "location_type": "Intersection", # "street": "", # "xstreet1": "TRAIN ST", # "xstreet2": "WESTGLOW ST", # "x_cord": 777243.68, # "y_cord": 2930930.11, # "lat": 42.2897498978, # "long": -71.0525153263 # }, startTime = datetime.datetime.now() # Set up the database connection. client = dml.pymongo.MongoClient() repo = client.repo repo.authenticate('nhuang54_tkixi_wud', 'nhuang54_tkixi_wud') bc = repo.nhuang54_tkixi_wud.boston_collisions bt = repo.nhuang54_tkixi_wud.boston_streetlights # Boston Collisions # mode_type, xstreet1, xstreet2 bostonCollisions = bc.find() # select to get all bike collisions bikeCollisions = select(bostonCollisions, lambda x: x['mode_type'] == 'bike') collision_filter = lambda x: { 'xstreet1': x['xstreet1'], 'xstreet2': x['xstreet2'], 'location_type': x['location_type'] } collision_project = project(bikeCollisions, collision_filter) # {'xstreet1': 'OLNEY ST', # 'xstreet2': 'INWOOD ST', # 'location_type': 'Intersection'} # # Boston Street Lights # {"the_geom" : "POINT (-71.07921632936232 42.35482231438127)", # "OBJECTID" : 10, # "TYPE" : "LIGHT", # "Lat" : 42.3548223144, # "Long" : -71.0792163294 # } api_key = dml.auth['services']['openCagePortal']['api_key'] geocoder = OpenCageGeocode(api_key) api_limit = 0 data = [] streetLights = bt.find() for x in streetLights: if trial: if api_limit > 50: break if api_limit > 500: break lat = x['Lat'] lng = x['Long'] # print('lat', lat) api_limit += 1 results = geocoder.reverse_geocode(lat, lng) # print('printing results') if 'road' in results[0]['components']: road = results[0]['components']['road'] road = road.replace('Street', 'St') road = road.replace('Drive', 'Dr') road = road.replace('Avenue', 'Ave') road = road.replace('Court', 'Ct') road = road.replace('Highway', 'Hwy') road = road.replace('Parkway', 'Pkwy') road = road.replace('Road', 'Rd') road = road.replace('Boulevard', 'Blvd') road = road.upper() else: continue for y in collision_project: xstreet1 = str(y['xstreet1']) xstreet2 = str(y['xstreet2']) if road in xstreet1 or road in xstreet2: streetlight_collisions = {} streetlight_collisions['streetlight'] = 1 streetlight_collisions.update({'road': road}) data.append(streetlight_collisions) c = defaultdict(int) # how many accidents have happened at each intersection for d in data: c[d['road']] += d['streetlight'] e = defaultdict(int) for f in collision_project: e[f['xstreet1']] += 1 e[f['xstreet2']] += 1 data2 = [] streetlight_collision_data = [{ 'road': road, 'streetlight': streetlight } for road, streetlight in c.items()] # print(streetlight_collision_data) for x in streetlight_collision_data: if x['road'] in e: # print(e) match = {} match.update(x) match.update({'collisions': e.get(x['road'])}) data2.append(match) # data2 = [{'road': 'MARLBOROUGH ST', 'streetlight': 126, 'collisions': 6}, {'road': 'BACK ST', 'streetlight': 6, 'collisions': 3}, {'road': 'BEACON ST', 'streetlight': 1113, 'collisions': 43}, {'road': 'SAINT BOTOLPH ST', 'streetlight': 54, 'collisions': 6}, {'road': 'HUNTINGTON AVE', 'streetlight': 726, 'collisions': 26}, {'road': 'RING RD', 'streetlight': 4, 'collisions': 4}, {'road': 'EXETER ST', 'streetlight': 63, 'collisions': 7}, {'road': 'BOYLSTON ST', 'streetlight': 1326, 'collisions': 34}, {'road': 'NEWBURY ST', 'streetlight': 480, 'collisions': 10}, {'road': 'DARTMOUTH ST', 'streetlight': 546, 'collisions': 13}, {'road': 'COMMONWEALTH AVE', 'streetlight': 2400, 'collisions': 60}, {'road': 'SAINT JAMES AVE', 'streetlight': 64, 'collisions': 4}, {'road': 'STUART ST', 'streetlight': 49, 'collisions': 7}, {'road': 'CLARENDON ST', 'streetlight': 84, 'collisions': 3}] repo.dropCollection("nhuang54_tkixi_wud.streetlight_collisions") repo.createCollection("nhuang54_tkixi_wud.streetlight_collisions") repo['nhuang54_tkixi_wud.streetlight_collisions'].insert_many(data2) # print("Done with Transformation of Traffic Lights + Bike Collisions") repo.logout() endTime = datetime.datetime.now() return {"start": startTime, "end": endTime}
def predict(): distance_time_speed_dataset = pd.read_csv('distance_time_speed_dataset.csv') s = (distance_time_speed_dataset.dtypes == 'object') object_cols = list(s[s].index) label_encoder = LabelEncoder() # encoding all Object columns list_of_encoding = [] for col in object_cols: distance_time_speed_dataset[col] = label_encoder.fit_transform(distance_time_speed_dataset[col]) le_name_mapping = dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_))) list_of_encoding.append(le_name_mapping) # mapping the labels z_scores = zscore(distance_time_speed_dataset) abs_z_scores = np.abs(z_scores) filtered_entries = (abs_z_scores < 3).all(axis=1) # filtering rows that are three std deviations away from the mean new_df = distance_time_speed_dataset[filtered_entries] distribution = [] carrier = new_df['carrier'] counter = Counter(carrier) for key, value in counter.items(): percent = value / len(carrier) * 100 df = {'Carrier': key, 'Count of unique values': value, 'Percentage': percent} distribution.append(df) dist = pd.DataFrame(distribution) dist = dist.sort_values(by=['Carrier'], ignore_index=True) group = new_df.groupby('carrier') df1 = group.apply(lambda x: len(x['source_city'].unique())) df2 = group.apply(lambda x: len(x['destination_city'].unique())) df3 = group.apply(lambda x: sum(x['time_to_delivery_hours']) / len(x['time_to_delivery_hours'])) df4 = group.apply(lambda x: sum(x['speed']) / len(x['speed'])) df5 = group.apply(lambda x: sum(x['distance']) / len(x['distance'])) dist['Unique source cities'] = df1.values dist['Unique destination cities'] = df2.values dist['Average delivery time'] = df3.values dist['Average speed'] = df4.values dist['Average distance'] = df5.values key = '7d84bfd479184b6fb58f0c46bfc4debc' # API Key geocoder = OpenCageGeocode(key) src = request.form.get("sourceCity", False) dest = request.form.get("destinationCity", False) src_location = geocoder.geocode(src) if src_location and len(src_location): src_longitude = src_location[0]['geometry']['lng'] src_latitude = src_location[0]['geometry']['lat'] src_code = src_location[0]['components']['country_code'] else: return render_template('index.html',pred='Please enter correct source location') dest_location = geocoder.geocode(dest) if dest_location and len(dest_location): dest_longitude = dest_location[0]['geometry']['lng'] dest_latitude = dest_location[0]['geometry']['lat'] dest_code = dest_location[0]['components']['country_code'] else: return render_template('index.html',pred='Please enter correct destination location') distance = vincenty((src_latitude, src_longitude), (dest_latitude, dest_longitude)) srcC, destC = 100, 100 srcS, destD = 396, 2207 if src in list_of_encoding[0]: srcS = list_of_encoding[0].get(src) if dest in list_of_encoding[1]: destD = list_of_encoding[1].get(dest) if src_code in list_of_encoding[2]: srcC = list_of_encoding[2].get(src_code) if dest_code in list_of_encoding[3]: destC = list_of_encoding[3].get(dest_code) carriers = ['Aramex', 'BlueDart', 'Delhivery', 'DHL Express', 'dotzot', 'Ecom Express', 'FedEx', 'XpressBees'] min_value = math.inf best_carrier = "" for id in range(8): item = [[srcS, destD, id, distance, srcC, destC, dist["Average speed"][id]]] value = model.predict(item) if value < min_value and value>0: min_value = value best_carrier = carriers[id] answer = best_carrier + " is predicted to deliver the fastest!" #answer = best_carrier + "is predicted to deliver fastest in" + min_value[0][0] + "hours /" + min_value[0][0] // 24 + "days" return render_template('index.html', pred = answer)
def fetch(entries, verbosity=1): ''' looks up tuples of (city,state) strings coming from load_data, and returns a latitude/longitude pair of the "expected" center of the city. Input: entries: list-like; each entry must itself be a list-like of the form of two strings ["city", "state"]; e.g. ["flint", "mi"]. I expect the format can be flexible, but this is up to the OpenCage API. Output: latlon: numpy array shape (n,2) of the center of the bounding box for each location. Loosely follows https://amaral.northwestern.edu/blog/getting-long-lat-list-cities ''' if isinstance(entries, str): print( 'Warning: expecting list-like of cities; will return a list of a single tuple.' ) entries = [entries] # from opencage.geocoder import OpenCageGeocode import opencage_apikey # get your own. import numpy as np geocoder = OpenCageGeocode(opencage_apikey.key) import pdb #pdb.set_trace() latlons = np.zeros((len(entries), 2), dtype=float) for j, query in enumerate(entries): # query = ', '.join(cs) print('fetching %s...' % query) try: results = geocoder.geocode(query) except: results = [] # # ri = 0 # hit = results[ri] # while hit['components']['country'].lower() not in ['united states of america', 'usa', 'us']: if len(results) == 0: print( "Warning: failed to find data for entry %s, using NaN coordinates." % query) latlon = np.array([np.nan, np.nan]) else: for ri, result in enumerate(results): if result['components']['country'].lower() not in [ 'united states of america', 'usa', 'us' ]: continue hit = result break # if hit['components']['country'].lower() not in [ 'united states of america', 'usa', 'us' ]: print('entry not in the US: %s' % str(hit['components']['country'].lower())) continue # ne = hit['bounds']['northeast'] # sw = hit['bounds']['southwest'] # latlon = np.mean([[ne['lat'], ne['lng']],[sw['lat'],sw['lng']]], axis=0) latlon = np.array([hit['geometry']['lat'], hit['geometry']['lng']]) # latlons[j] = latlon if verbosity > 0: print('done, ', query, latlon) # return latlons
def search(request): address = request.GET['inputaddress'].strip() dest = request.GET['inputdest'].strip() error="" startfound = True destfound = True lat=0 lng=0 destlat=0 destlng=0 routegeojson = """{ "type": "FeatureCollection", "features": [] }""" # Query OpenCage to get coordinates for this address key = '7945028e977fa9593e5b02378cbf0f27' geocoder = OpenCageGeocode(key) if (address != '' and dest != ''): # Get coordinates for start address jsonresult = geocoder.geocode(address) try: resultdict = jsonresult[0] lat = resultdict["geometry"]["lat"] lng = resultdict["geometry"]["lng"] except IndexError: startfound = False # Get coordinates for destination address jsonresult = geocoder.geocode(dest) try: resultdict = jsonresult[0] destlat = resultdict["geometry"]["lat"] destlng = resultdict["geometry"]["lng"] except IndexError: destfound = False if not startfound and destfound: error = "Error: The start address you entered could not be found." elif startfound and not destfound: error = "Error: The end address you entered could not be found." elif not startfound and not destfound: error = "Error: Neither addresses you entered could be found." else: error = "Error: One or more fields were left blank." # Perform raw query to postgres database to find sidewalk closest to start coordinates cursor = connection.cursor() cursor.execute("SELECT source FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), wkb_geometry) ASC LIMIT 1", [lng, lat]) row = cursor.fetchone() start_edge_id = row[0] # Repeat to find sidewalk closest to end coordinates cursor.execute("SELECT target FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), wkb_geometry) ASC LIMIT 1", [destlng, destlat]) row = cursor.fetchone() end_edge_id = row[0] print("Start edge id is "+str(start_edge_id)) print("End edge id is "+str(end_edge_id)) # Find location in the middle of the route for centering the map average_lat = (lat + destlat)/2.0 average_lng = (lng + destlng)/2.0 # Create geojson route routesql = """ SELECT ST_AsGeoJSON(st_union) FROM ( SELECT ST_Union(wkb_geometry) FROM ( SELECT seq, id1 AS node, id2 AS edge, route.cost, dt.wkb_geometry, dt.sidewalk_edge_id FROM pgr_dijkstra(' SELECT sidewalk_edge_id AS id, source::integer, target::integer, calculate_accessible_cost(sidewalk_edge_id)::double precision AS cost FROM sidewalk_edge', %s, %s, false, false) as route join sidewalk_edge dt on route.id2 = dt.sidewalk_edge_id ) as routegeometries ) as final; """ cursor.execute(routesql, [start_edge_id, end_edge_id]) row = cursor.fetchone() routejs = row[0] # We now have the geojson representing the route, but we need to clean it up a little geojson_dirty = json.loads(routejs) print("Info from route:") print(geojson_dirty['coordinates'][-1]) ''' # Take all coordinates and combine into a single large list coordinates_all = [] for path in geojson_dirty['coordinates']: for point in path: pointlng = point[0] pointlat = point[1] coordinates_all.append(round(pointlng,4)) coordinates_all.append(round(pointlat,4)) print(coordinates_all) for path in geojson_dirty['coordinates']: for point in path: pointlng = point[0] poitnlng = point[1] # See if this is the only occurrence of this point if (coordinates_all.count(round(pointlng,4)) == 1 and coordinates_all.count(round(pointlat,4)) == 1): print("Found point that only occurs once") print(point[0],point[1]) ''' # Query the elevation table to get geojson with elevation data ''' data = json.loads(routejs) points = [] for path in data['coordinates']: for point in path: points.append(point) split_path = split(points) elevation_list = get_elevations(split_path) output_string = output_geojson(input_path, input_elevation_list) logger.debug(output_string) print(output_string) ''' logger.error(routejs) routegeojson = routejs # print(routejs) return render(request, 'routeapp/homepage.html', {'centerlat':average_lat, 'centerlng':average_lng, 'defaultzoom': '17', 'lat':lat, 'lng':lng, 'destlat': destlat, 'destlng':destlng, 'start_textbox_value': address, 'dest_textbox_value': dest, 'error_message':error, 'routegeojson':routegeojson, })
class TrainStation: def __init__(self, m_id, name, latitude, longitude): self.m_id=m_id self.prettyName=name self.latitude=latitude self.longitude=longitude def _get_prettyName(self): return self.prettyName def _set_prettyName(self, name): self.prettyName = name #--------------------------------------------------# key = 'Insert token here' geocoder = OpenCageGeocode(key) query = raw_input("Entrez l'adresse recherchee ici : ") result = geocoder.geocode(query, format='json',language='fr') latitude = str(result[0]['geometry']['lat']) longitude = str(result[0]['geometry']['lng']) print "" print "" print "Nous allons rechercher la Gare la plus proche de l'adresse : " + query url = "http://localhost:8080/restserver/station/nearest-station?latitude=" + latitude + "&longitude=" + longitude request = Request(url) try:
class ZeitOpenCage(object): """ ZeitSci relavant tools for interacting with the OpenCage API. """ def __init__(self, api_key=None): """ :param key: an OpenSci API key; defaults to None. """ if api_key == None: raise ValueError("An OpenCage API key must be supplied via api_key.") self.geocoder = OpenCageGeocode(key=api_key) def parser(self, result, request): """ :param result: JSON from geocoder.geocode(query) :param request: 'address', 'geo' or 'conf', all --> formatted addgress, longditude, confidence, all three :return: """ if len(result) == 0: raise ValueError("The result param is invalid") else: if request == 'address': return result[0]['formatted'] if request == 'conf': return result[0]['confidence'] if request == 'geo': return (result[0]['geometry']['lng'], result[0]["geometry"]["lat"]) if request == 'all': loc_dict = { 'Address': result[0]['formatted'] , 'Coordinates': (result[0]['geometry']['lng'], result[0]["geometry"]["lat"]) , 'Confidence': result[0]['confidence'] } return loc_dict def lookup(self, query, request='all', double_check=False, min_conf=1, sleep_bounds=(1, 3)): """ TO CHANGE: double_check --> validate_list, e.g., ['Harvard', 'University', 'United States of America'] :param query: the search term :param request: 'address', 'geo' or 'conf', all --> formatted addgress, (long, lat), confidence, all three :param double_check: :param min_conf: :param sleep_bounds: (min, max) wait time (select from a uniform probability density function). :return: """ # Pause between calls sleep(uniform(sleep_bounds[0], sleep_bounds[1])) if cln(query, 2) == "": raise ValueError("query is an emptry string.") try: parse_return = self.parser(self.geocoder.geocode(query), request) except: return {} # Validate that the country is in the address. # Improve this check and/at least refactor the code... if double_check and "," in query and not \ partial_list_match(parse_return['Address'].lower(), \ [cln(i, 1).lstrip().rstrip().lower() for i in query.split(",")]) \ and int(parse_return["Confidence"]) < min_conf: return {} return parse_return
def main(): if request.method == 'POST': try: filtro_usuario = request.form.get('dropdown') input_user = request.form['input_usuario'] #inicializo GeoCage para localizar input del usuario tokengeo = token('geocagetoken.txt') #tokengeo = token(PATH+'/geocagetoken.txt') -> For pythonanywhere geocoder = OpenCageGeocode(tokengeo) results = geocoder.geocode(input_user) kmeters = 2 fcoordinates = [] coordinates = [] coordinates.append(results[0]['geometry']['lat']) coordinates.append(results[0]['geometry']['lng']) country = results[0]['components']['country'] city = results[0]['components']['city'] localidad = country + ', ' + city fcoordinates.append(coordinates) fcoordinates.append(kmeters) fcoordinates.append(localidad) fcoordinates.append(input_user) #se separa la string latlong = fcoordinates[0] radio = fcoordinates[1] localidad = fcoordinates[2] direccion_solicitud = fcoordinates[3] gymy_modelo = gymy.copy() if filtro_usuario != 'Todas las categorías': gymy_modelo['filtro'] = gymy_modelo.category.apply( filtro, form_user_input=filtro_usuario) else: gymy_modelo['filtro'] = True gymy_modelo = gymy_modelo[gymy_modelo.filtro == True] gymy_modelo['distance'] = gymy_modelo.latlong.apply( lambda x, y=latlong: distance.distance(x, y).km) display_df = gymy_modelo[gymy_modelo.distance < radio] display_df.reset_index(inplace=True) n_gyms = len(display_df) if n_gyms > 0: #creating map object tooltip = 'Dirección elegida: {} \n {}'.format( direccion_solicitud, localidad) mapa = folium.Map(latlong, zoom_start=15, width='100%', height='70%') folium.Marker(latlong, tooltip=tooltip, icon=folium.Icon()).add_to(mapa) for i in range(len(display_df)): htmlpopup = """ <font face = Verdana size = "1"> <label ><b>{}</b></label> <br> </font> <p> <font face= Verdana size = "1"><label><b> Teléfono:</b> {}</label> <br> <label><b>Dirección:</b> {}</label> <br> <label><b>Web:</b> {}</label> <br> <label><b>Categorías: </b>{}</label> </font> </p> """.format(display_df.names[i], display_df.phone[i], display_df.address[i], display_df.web[i], display_df.category[i]) icongym = folium.features.CustomIcon( '/Users/EstebanCardona/Documents/gymy/static/gymyicon.png', icon_size=(40, 40)) iframe = folium.IFrame(html=htmlpopup, width=225, height=125) popup = folium.Popup(iframe) folium.Marker([display_df.lat[i], display_df.long[i]], popup=popup, tooltip=display_df.names[i], icon=icongym).add_to(mapa) mapa.save('templates/{}{}.html'.format(direccion_solicitud, filtro_usuario)) #mapa.save(PATH+'/templates/{}.html'.format(direccion_solicitud)) -> For pythonanywhere devuelta = 'Existen {} GYMYs de {} cerca de {}'.format( n_gyms, filtro_usuario, direccion_solicitud) #agrega el jinja de block al html de folium with open( 'templates/{}{}.html'.format(direccion_solicitud, filtro_usuario), 'a') as f: #with open(PATH+'/templates/{}.html'.format(direccion_solicitud), 'a') as f: -> For pythonanywhere f.write('\n{% block content %} {% endblock %}') return render_template('index.html', gyms_template=devuelta, mapatrue='{}{}.html'.format( direccion_solicitud, filtro_usuario), dropdown=filtro_usuario) else: devuelta = 'No hay Gymys cerca' return render_template('index.html', gyms_template=devuelta, mapatrue='nomapa.html', dropdown='Todas las categorías') except: devuelta = 'Dirección Inválida. Prueba con otra' return render_template('index.html', gyms_template=devuelta, mapatrue='nomapa.html', dropdown='Todas las categorías') else: return render_template('index.html', gyms_template='', mapatrue='nomapa.html', dropdown='Todas las categorías')
# In[6]: table # In[7]: trList = table.find_all('tr') # In[8]: data = [] # In[9]: key = '12ed4bc8d2d0484a9242caa9ae416fd6' geocoder = OpenCageGeocode(key) def getDataFromTableRows(trList, skipText): count = 0 data = [] startPointReached = False for tr in trList: tdList = tr.find_all('td') if len(tdList) == 0: continue if not startPointReached: if tdList[0].get_text() == skipText: startPointReached = True else: continue if tdList[0].get_text() == 'Total:':
#------------------------------------# # import libraries #------------------------------------# import csv import requests import postcodes_io_api import pandas as pd from geopy.geocoders import Nominatim from opencage.geocoder import OpenCageGeocode #------------------------------------# #------------------------------------# geolocator = OpenCageGeocode(key="") # YOUR API KEY #------------------------------------# #------------------------------------# # Reads the scraped data from the output of the scraping script #------------------------------------# with open("UPS_depots.txt") as f: content = f.readlines() line = [x.strip() for x in content] #------------------------------------# #------------------------------------# # Formats and cleans the data #------------------------------------# address = [] postcode = [] name = [] depot = [] word = "el: "
def location(): key = 'b109193b45cf4444b0115e2f16e911f1' geocoder = OpenCageGeocode(key) query = input("Enter location(ex:-delhi,punjab,hyd):") results = geocoder.geocode(query) return results
import os from .app import db from sqlalchemy.dialects.postgresql import UUID from opencage.geocoder import OpenCageGeocode from .AddressModel import AddressModel geocoder = OpenCageGeocode('76b31bcf7f124310a4e76700542e1583') def get_address(uuid): result = db.engine.execute( "SELECT * FROM \"Address\" where request_id = '" + uuid.__str__() + "'") for row in result: model = AddressModel.query.get(row['id']) result = geocoder.reverse_geocode(row['latitude'], row['longitude'], language='en') model.address = result[0]['formatted'] model.save()
from opencage.geocoder import OpenCageGeocode from event import Event from pyspark.sql import SparkSession from us_zipcode import Zipcode # ConfigParser config = configparser.ConfigParser() config.read('config/meetup.cfg') # Environ variables for API KEY os.environ['MEETUP_API_KEY'] = config['MEETUP']['API_KEY'] os.environ['OPENCAGE_KEY'] = config['OPENCAGE']['KEY'] os.environ['MONGO_PASS'] = config['MONGODB']['PASS'] # Initialize OpenCage Geocode geocode = OpenCageGeocode(os.environ['OPENCAGE_KEY']) mongo_conn_str = "mongodb://*****:*****@dev-mongo-shard-00-00-klryn.mongodb.net:27017,dev-mongo-shard-00-01-klryn.mongodb.net:27017,dev-mongo-shard-00-02-klryn.mongodb.net:27017/test?ssl=true&replicaSet=dev-mongo-shard-0&authSource=admin&retryWrites=true".format( os.environ['MONGO_PASS']) def spark_session(): spark = SparkSession \ .builder \ .appName("meetupcollections") \ .config("spark.jars.packages", "org.mongodb.spark:mongo-spark-connector_2.12:2.4.0")\ .config("spark.mongodb.input.uri", mongo_conn_str) \ .config("spark.mongodb.output.uri", mongo_conn_str) \ .getOrCreate() return spark
def index(request): dests = Destination.objects.all() if request.user.is_authenticated: global df engine = create_engine( 'postgresql+psycopg2://postgres:postgres@localhost:5432/telusko') df_d = pd.read_sql_table( "travello_destination", con=engine, schema='public', coerce_float=True, columns=['name', 'img', 'desc', 'state', 'city', 'typeofplace']) df = pd.DataFrame(df_d) geocoder = OpenCageGeocode('ea7fd5e689b149c38ef13cbed352bff5') list_lat = [] list_long = [] for index, row in df.iterrows(): name = get_name_from_index(index, df) state = get_state_from_index(index, df) city = get_city_from_index(index, df) query = str(name) + ',' + str(city) + ',' + str(state) print("hi") results = geocoder.geocode(query) print('$$$$$$', results) if len(results) != 0: lat = results[0]['geometry']['lat'] longi = results[0]['geometry']['lng'] else: print("results is empty") print("hello", index, name, state) list_lat.append(lat) list_long.append(longi) df['lat'] = list_lat df['lon'] = list_long print(df) features = ['desc', 'state', 'typeofplace'] for feature in features: df[feature] = df[feature].fillna('') df['combined_features'] = df.apply(combine_features, axis=1) cv = CountVectorizer() count_matrix = cv.fit_transform(df['combined_features']) cosine_sim = cosine_similarity(count_matrix) custom = CustomPreferences.objects.all() for c in custom: if str(c.user) == str(request.user): user_prefer = c.preferences user_prefer = user_prefer.split(",") rows_data = [] for up in user_prefer: place_index = get_index_from_title(up, df) similar_places = list(enumerate(cosine_sim[place_index])) sorted_similar_places = sorted(similar_places, key=lambda x: x[1], reverse=True) i = 0 for place in sorted_similar_places: row_data = get_title_from_index(place[0], df) rows_data.append(row_data) i = i + 1 if i > 3: break final_data = [] for dest in dests: for lists in rows_data: if dest.name in lists: result = TextBlob(dest.desc) polar = result.sentiment.polarity if polar > 0.0: final_data.append(dest) else: user_prefer = [] final_data = [] return render(request, "index.html", { 'dests': dests, 'recommendations': final_data })
def __init__(self): key = 'Insert token here!' self.geocoder = OpenCageGeocode('995f38d3b781b46526e1b3fd7e9a78a7')
import urllib.parse import bs4 import chromedriver_binary import re import webbrowser import datetime import time import requests import json from geopy.distance import geodesic from opencage.geocoder import OpenCageGeocode listen = False my_name = 'Julien' api_key = 'f43fe96b2191441cbd0c4832d50a8f8a' geocoder = OpenCageGeocode(api_key) def talk(audio): print(audio) for line in audio.splitlines(): mp3 = BytesIO() text_to_speech = gTTS(text=audio, lang='fr') text_to_speech.write_to_fp(mp3) mp3.seek(0) mixer.init() mixer.music.load(mp3) mixer.music.play() def myCommand():
class OpenCageGeocodeTestCase(unittest.TestCase): def setUp(self): httpretty.enable() self.geocoder = OpenCageGeocode('abcde') def tearDown(self): httpretty.disable() httpretty.reset() def testUKPostcode(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body= '{"total_results":10,"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","rate":{"limit":"2500","remaining":2487,"reset":1402185600},"results":[{"annotations":{},"components":{"country_name":"United Kingdom","region":"Islington","locality":"Clerkenwell"},"formatted":"Clerkenwell, Islington, United Kingdom","geometry":{"lat":"51.5221558691","lng":"-0.100838524406"},"bounds":null},{"formatted":"82, Lokku Ltd, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RF, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"82","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"Lokku Ltd","postcode":"EC1M 5RF"},"annotations":{},"bounds":{"northeast":{"lng":"-0.1023889","lat":"51.5226795"},"southwest":{"lat":"51.5225795","lng":"-0.1024889"}},"geometry":{"lat":"51.5226295","lng":"-0.1024389"}},{"components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lat":"51.5225346","lng":"-0.1027003"},"bounds":{"northeast":{"lat":"51.5225759","lng":"-0.1020597"},"southwest":{"lat":"51.5225211","lng":"-0.103223"}}},{"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Craft Central","annotations":{},"components":{"postcode":"EC1M 6DS","arts_centre":"Craft Central","state":"England","suburb":"Clerkenwell","country":"United Kingdom","city":"London Borough of Islington","country_code":"gb","road":"Clerkenwell Road","state_district":"Greater London","county":"London"},"bounds":{"northeast":{"lat":"51.52246","lng":"-0.1027652"},"southwest":{"lng":"-0.1028652","lat":"51.52236"}},"geometry":{"lng":"-0.1028152","lat":"51.52241"}},{"components":{"county":"London","state_district":"Greater London","restaurant":"Noodle Express","road":"Albemarle Way","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Noodle Express, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lng":"-0.10255386845056","lat":"51.5228195"},"bounds":{"southwest":{"lng":"-0.102621","lat":"51.5227781"},"northeast":{"lat":"51.5228603","lng":"-0.1024869"}}},{"geometry":{"lat":"51.5229424","lng":"-0.102380530769224"},"bounds":{"northeast":{"lat":"51.5229759","lng":"-0.1023064"},"southwest":{"lng":"-0.1024639","lat":"51.5229046"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","cafe":"PAR","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"formatted":"PAR, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"},{"formatted":"Workshop Coffee Co., 27, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RN, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"27","cafe":"Workshop Coffee Co.","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 5RN"},"annotations":{},"bounds":{"southwest":{"lng":"-0.1024422","lat":"51.5222246"},"northeast":{"lng":"-0.1022307","lat":"51.5224408"}},"geometry":{"lat":"51.52234585","lng":"-0.102338899572156"}},{"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"Franco & Co","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Franco & Co","geometry":{"lng":"-0.1024118","lat":"51.5231165"},"bounds":{"southwest":{"lng":"-0.1024618","lat":"51.5230665"},"northeast":{"lng":"-0.1023618","lat":"51.5231665"}}},{"bounds":{"northeast":{"lng":"-0.1023218","lat":"51.5231688"},"southwest":{"lat":"51.5229634","lng":"-0.1024934"}},"geometry":{"lng":"-0.102399365567707","lat":"51.5230257"},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, MacCarthy","annotations":{},"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"MacCarthy","state":"England","postcode":"EC1M 6DS"}},{"geometry":{"lng":"-0.102730855172415","lat":"51.52267345"},"bounds":{"northeast":{"lng":"-0.1025498","lat":"51.5227315"},"southwest":{"lat":"51.5226068","lng":"-0.1028931"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","house_number":"84","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"The Printworks","postcode":"EC1M 6DS"},"formatted":"84, The Printworks, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"}],"timestamp":{"created_unix":1402133768,"created_http":"Sat, 07 Jun 2014 09:36:08 GMT"},"we_are_hiring":"http://lokku.com/#jobs"}', ) results = self.geocoder.geocode("EC1M 5RF") self.assertTrue(any( (abs(result['geometry']['lat'] - 51.5201666) < 0.05 and abs(result['geometry']['lng'] - -0.0985142) < 0.05) for result in results), msg="Bad result") def testAustralia(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body= '{"licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","results":[{"geometry":{"lng":"149.5886383","lat":"-32.5980702"},"components":{"country_code":"au","state":"New South Wales","country":"Australia","town":"Mudgee"},"formatted":"Mudgee, New South Wales, Australia, au","annotations":{},"bounds":{"southwest":{"lng":"149.5486383","lat":"-32.6380702"},"northeast":{"lng":"149.6286383","lat":"-32.5580702"}}},{"formatted":"Mudgee, Mid-Western Regional, New South Wales, Australia","components":{"state":"New South Wales","country":"Australia","county":"Mid-Western Regional","town":"Mudgee"},"bounds":{"southwest":{"lng":"149.573196411","lat":"-32.6093025208"},"northeast":{"lng":"149.602890015","lat":"-32.5818252563"}},"annotations":{},"geometry":{"lng":149.5871,"lat":-32.59426}}],"total_results":2,"rate":{"reset":1402185600,"limit":"2500","remaining":2489},"we_are_hiring":"http://lokku.com/#jobs","timestamp":{"created_http":"Sat, 07 Jun 2014 09:31:50 GMT","created_unix":1402133510}}', ) results = self.geocoder.geocode("Mudgee, Australia") self.assertTrue(any( (abs(result['geometry']['lat'] - -32.5980702) < 0.05 and abs(result['geometry']['lng'] - 149.5886383) < 0.05) for result in results), msg="Bad result") def testMustBeUnicodeString(self): # dud mock so this goes quick httpretty.register_uri(httpretty.GET, self.geocoder.url, body='{"results":{}}') # Should not give errors self.geocoder.geocode('xxx') # ascii convertable self.geocoder.geocode(six.u('xxx')) # unicode self.geocoder.geocode(six.u('xxá')) # unicode # But if it isn't a unicode string, it should give error utf8_string = six.u("xxá").encode("utf-8") latin1_string = six.u("xxá").encode("latin1") self.assertRaises(InvalidInputError, self.geocoder.geocode, utf8_string) # check the exception try: self.geocoder.geocode(utf8_string) except InvalidInputError as ex: self.assertEqual(ex.bad_value, utf8_string) self.assertEqual( str(ex), "Input must be a unicode string, not {0!r}".format( utf8_string)) self.assertRaises(InvalidInputError, self.geocoder.geocode, latin1_string) # check the exception try: self.geocoder.geocode(latin1_string) except InvalidInputError as ex: self.assertEqual(ex.bad_value, latin1_string) self.assertEqual( str(ex), "Input must be a unicode string, not {0!r}".format( latin1_string)) def testMunster(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body= '{"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"thanks":"For using an OpenCage Data API","we_are_hiring":"http://lokku.com/#jobs","total_results":10,"status":{"message":"OK","code":200},"results":[{"formatted":"M\u00fcnster, M\u00fcnster, 48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de","geometry":{"lat":"51.9625101","lng":"7.6251879"},"components":{"country_code":"de","postcode":"48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","city":"M\u00fcnster","county":"M\u00fcnster","country":"Germany"},"bounds":{"southwest":{"lng":"7.4651879","lat":"51.8025101"},"northeast":{"lng":"7.7851879","lat":"52.1225101"}},"annotations":{}},{"bounds":{"northeast":{"lat":"52.0600706","lng":"7.7743708"},"southwest":{"lat":"51.8402214","lng":"7.4738051"}},"annotations":{},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","county":"M\u00fcnster","country":"Germany"},"formatted":"M\u00fcnster, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de"},{"formatted":"Munster, Ireland, ie","annotations":{},"bounds":{"southwest":{"lat":"51.4199027","lng":"-10.6891099"},"northeast":{"lng":"-6.9497829","lat":"53.1689062"}},"components":{"country_code":"ie","state_district":"Munster","country":"Ireland"},"geometry":{"lng":"-8.57089717629267","lat":"52.3076216"}},{"formatted":"Germany, de, M\u00fcnster","annotations":{},"bounds":{"southwest":{"lat":"51.8402214","lng":"7.4738051"},"northeast":{"lat":"52.0600706","lng":"7.7743708"}},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","address100":"M\u00fcnster","country":"Germany"}},{"formatted":"M\u00fcnster, M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","components":{"city":"Stuttgart","state":"Baden-W\u00fcrttemberg","city_district":"M\u00fcnster","country":"Germany","country_code":"de","state_district":"Regierungsbezirk Stuttgart","suburb":"M\u00fcnster","county":"Stuttgart"},"geometry":{"lat":"48.8212962","lng":"9.2200016"},"bounds":{"northeast":{"lng":"9.2400016","lat":"48.8412962"},"southwest":{"lng":"9.2000016","lat":"48.8012962"}},"annotations":{}},{"geometry":{"lng":"8.8671181","lat":"49.9229236"},"components":{"country_code":"de","state_district":"Regierungsbezirk Darmstadt","state":"Hesse","city":"M\u00fcnster","county":"Landkreis Darmstadt-Dieburg","country":"Germany"},"annotations":{},"bounds":{"northeast":{"lat":"49.9438725","lng":"8.9161067"},"southwest":{"lat":"49.9056973","lng":"8.7705856"}},"formatted":"M\u00fcnster, Landkreis Darmstadt-Dieburg, Regierungsbezirk Darmstadt, Hesse, Germany, de"},{"formatted":"M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","geometry":{"lat":"48.8272797","lng":"9.2024402537349"},"components":{"country_code":"de","state_district":"Regierungsbezirk Stuttgart","state":"Baden-W\u00fcrttemberg","city":"Stuttgart","city_district":"M\u00fcnster","county":"Stuttgart","country":"Germany"},"bounds":{"northeast":{"lat":"48.8384709","lng":"9.2273738"},"southwest":{"lng":"9.1883711","lat":"48.8152795"}},"annotations":{}},{"bounds":{"southwest":{"lng":"10.8788966","lat":"48.5896428"},"northeast":{"lat":"48.6515558","lng":"10.9314006"}},"annotations":{},"geometry":{"lng":"10.9008883","lat":"48.6242219"},"components":{"country_code":"de","state_district":"Swabia","state":"Free State of Bavaria","city":"M\u00fcnster","county":"Rain (Schwaben)","country":"Germany"},"formatted":"M\u00fcnster, Rain (Schwaben), Swabia, Free State of Bavaria, Germany, de"},{"formatted":"Munster, Lake County, Indiana, United States of America, us","bounds":{"northeast":{"lat":"41.5814003","lng":"-87.4802388"},"southwest":{"lng":"-87.5254509","lat":"41.522608"}},"annotations":{},"geometry":{"lat":"41.5644798","lng":"-87.5125412"},"components":{"country_code":"us","state":"Indiana","city":"Munster","county":"Lake County","country":"United States of America"}},{"bounds":{"northeast":{"lng":"12.5929086","lat":"48.9728073"},"southwest":{"lng":"12.5529086","lat":"48.9328073"}},"annotations":{},"geometry":{"lng":"12.5729086","lat":"48.9528073"},"components":{"country_code":"de","state_district":"Lower Bavaria","village":"M\u00fcnster","state":"Free State of Bavaria","county":"Landkreis Straubing-Bogen","country":"Germany"},"formatted":"M\u00fcnster, Landkreis Straubing-Bogen, Lower Bavaria, Free State of Bavaria, Germany, de"}],"timestamp":{"created_unix":1402135758,"created_http":"Sat, 07 Jun 2014 10:09:18 GMT"},"rate":{"remaining":2485,"reset":1402185600,"limit":"2500"}}', ) results = self.geocoder.geocode(six.u("Münster")) self.assertTrue(any( (abs(result['geometry']['lat'] - 51.9625101) < 0.05 and abs(result['geometry']['lng'] - 7.6251879) < 0.05) for result in results), msg="Bad result") def testDonostia(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body= '{"thanks":"For using an OpenCage Data API","status":{"message":"OK","code":200},"rate":{"remaining":2482,"limit":"2500","reset":1402185600},"we_are_hiring":"http://lokku.com/#jobs","total_results":7,"results":[{"geometry":{"lat":"43.3213324","lng":"-1.9856227"},"annotations":{},"components":{"postcode":"20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018","county":"Donostialdea/Donostia-San Sebasti\u00e1n","state":"Basque Country","country":"Spain","city":"San Sebasti\u00e1n","country_code":"es"},"formatted":"San Sebasti\u00e1n, Donostialdea/Donostia-San Sebasti\u00e1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es","bounds":{"southwest":{"lat":"43.2178373","lng":"-2.086808"},"northeast":{"lng":"-1.8878838","lat":"43.3381344"}}},{"formatted":"Donostia, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lat":"43.3422299","lng":"-1.8022744"},"northeast":{"lng":"-1.8013452","lat":"43.3449598"}},"geometry":{"lng":"-1.8019153","lat":"43.3432784"},"annotations":{}},{"annotations":{},"geometry":{"lng":"-1.8022744","lat":"43.3422299"},"formatted":"Donostia, Anaka, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","suburb":"Anaka","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lng":"-1.8022971","lat":"43.3421635"},"northeast":{"lng":"-1.8022744","lat":"43.3422299"}}},{"geometry":{"lng":"-2.69312049872164","lat":"42.868297"},"annotations":{},"bounds":{"southwest":{"lng":"-2.6933154","lat":"42.8681484"},"northeast":{"lat":"42.8684357","lng":"-2.6929252"}},"formatted":"Donostia kalea, Ibaiondo, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Ibaiondo","country_code":"es","road":"Donostia kalea"}},{"bounds":{"southwest":{"lng":"-2.6889534","lat":"42.8620967"},"northeast":{"lat":"42.8623764","lng":"-2.6885774"}},"formatted":"Donostia kalea, Lakua, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Lakua","country_code":"es","road":"Donostia kalea"},"geometry":{"lat":"42.8622515","lng":"-2.68876582144679"},"annotations":{}},{"annotations":{},"geometry":{"lat":"51.5146888","lng":"-0.1609307"},"components":{"restaurant":"Donostia","country":"United Kingdom","state_district":"Greater London","country_code":"gb","county":"London","state":"England","suburb":"Marylebone","city":"City of Westminster","road":"Great Cumberland Mews"},"formatted":"Donostia, Great Cumberland Mews, Marylebone, City of Westminster, London, Greater London, England, United Kingdom, gb","bounds":{"northeast":{"lng":"-0.1608807","lat":"51.5147388"},"southwest":{"lat":"51.5146388","lng":"-0.1609807"}}},{"geometry":{"lat":43.31283,"lng":-1.97499},"annotations":{},"bounds":{"northeast":{"lng":"-1.92020404339","lat":"43.3401603699"},"southwest":{"lat":"43.2644081116","lng":"-2.04920697212"}},"formatted":"San Sebastian, Gipuzkoa, Basque Country, Spain, Donostia / San Sebasti\u00e1n","components":{"county":"Gipuzkoa","state":"Basque Country","country":"Spain","town":"San Sebastian","local administrative area":"Donostia / San Sebasti\u00e1n"}}],"timestamp":{"created_unix":1402136556,"created_http":"Sat, 07 Jun 2014 10:22:36 GMT"},"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}]}', ) results = self.geocoder.geocode("Donostia") self.assertTrue(any( (abs(result['geometry']['lat'] - 43.300836) < 0.05 and abs(result['geometry']['lng'] - -1.9809529) < 0.05) for result in results), msg="Bad result") # test that the results are in unicode self.assertEqual( results[0]['formatted'], six. u('San Sebasti\xe1n, Donostialdea/Donostia-San Sebasti\xe1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es' ))
""" This module operates over a csv file and the user input of his location and year of the films he would like to check. The locations of the those films are to be displayed on the map. """ import math import folium import pandas as pd from opencage.geocoder import OpenCageGeocode KEY = '69c11d0466ce4af7a4f5e8dd41be807b' GEOCODER = OpenCageGeocode(KEY) def read_sel_year(file_path, inp_year=2006): """ Returns a dataframe, with movies of a chosen year. """ init_df = pd.read_csv(file_path, error_bad_lines=False, warn_bad_lines=False)[::10] del init_df['add_info'] new_df = init_df.loc[init_df['year'] == str(inp_year)] del new_df['year'] return new_df[new_df.location != 'NO DATA'] def get_us_location(): """ (None) -> (list)
def chart_sentiment_analysis(request, keyword, period): # Twitter Authentification geocoder = OpenCageGeocode(key) start = datetime.now() auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) # since_date = '2020-04-25' # until_date = '2020-04-25' until=str(date.today()) start_date = str(date.today() - timedelta(days=period)) print(start_date) item = 3000 if period == 6: item = 10000 graph = sett.graph sess = sett.sess model = sett.model tokenizer = sett.tokenizer tweets = pd.DataFrame() print('the desired items' + str(item)) for tweet in tweepy.Cursor(api.search, q=keyword + ' -filter:retweets', lang='en', wait_on_rate_limit=True, count=400, since=start_date, include_rts=False).items(item): tweets1 = pd.DataFrame(np.zeros(1)) query = tweet.user.location # results = geocoder.geocode(query) tweets1["username"] = tweet.user.screen_name temp_string = str(tweet.text).replace('\n', '') temp_string = str(temp_string).replace('"', '') tweets1["cleaned_tweets"] = str(temp_string).replace(',', '') tweets1["Date"] = tweet.created_at tweets1["Date"] = tweets1["Date"].astype(str) '''if results: # print(results[0]['components']['country_code']) tweets1["location"] = results[0]['components']['country_code'] results = '' else: results = 'us' tweets1["location"] = results ''' tweets = tweets.append(tweets1) data = tweets[['username', 'cleaned_tweets', 'Date']] print(data) labels = ['1', '-1', '0'] data_test = sentiment_analysis_class.sentiment_analysis_class.data_preparation(data) pads_testing_data, test_word_index = sentiment_analysis_class.sentiment_analysis_class.tokenize_pad_sequences( tokenizer, data_test) # start sentiment prediction with graph.as_default(): set_session(sess) predicted = model.predict(pads_testing_data) prediction_labels = [] for p in predicted: prediction_labels.append(labels[np.argmax(p)]) data_test['sentiment'] = prediction_labels data_predicted = data_test[['Date', 'cleaned_tweets','sentiment']] # data_predicted.to_csv('data_location2.csv') # data_predicted = pd.read_csv('data_location.csv') print(data_predicted) end = datetime.now() print('the time needed for this operation is' + str(end - start)) # str_now = datetime.now().strftime(("%Y-%m-%d %H:%M:%S")) # datetime_object = datetime.strptime(str_now, "%Y-%m-%d %H:%M:%S") # data_cal = data_predicted.loc[data_predicted['Date'] >= str(datetime_object - timedelta(days=1))] data_predicted['Date'] = pd.to_datetime(data_predicted['Date'], format='%Y-%m-%d') data_predicted['Date'] = data_predicted['Date'].apply(lambda x: x.strftime("%d/%m/%Y")) data_predicted['Date'] = pd.to_datetime(data_predicted['Date'], format='%d/%m/%Y') # print(data_predicted['Date']) temp1 = [] print(len(data_predicted['Date'].unique())) for i in range(len(data_predicted['Date'].unique())): l = {} temp = data_predicted.loc[data_predicted['Date'] == str(date.today() - timedelta(days=i))] # print(str(date.today() - timedelta(days=i))) l['Date'] = str(date.today() - timedelta(days=i)) daily_sentiment_net = 0 daily_sentiment_neg = 0 daily_sentiment_pos = 0 for p in temp['sentiment']: if p == '1': daily_sentiment_pos = daily_sentiment_pos + 1 elif p == '-1': daily_sentiment_neg = daily_sentiment_neg + 1 elif p == '0': daily_sentiment_net = daily_sentiment_net + 1 l['negative_score'] = daily_sentiment_neg l['positive_score'] = daily_sentiment_pos l['neutre_score'] = daily_sentiment_net temp1.append(l) print(temp1) return JsonResponse(json.dumps(temp1), safe=False)
import json import csv from opencage.geocoder import OpenCageGeocode key = 'c4a90be25d0047b2fa64faaaf456b1cc' geocoder = OpenCageGeocode(key) i = 0 results = {} with open('Roads_Nodes.csv') as csvfile: reader = csv.DictReader(csvfile) values = {} for row in reader: print(row['Start Node'], row['End Node'], row['Via']) values['Start_Node_Cords'] = row['Start Node'] values['End_Node_Cords'] = row['End Node'] values['Via_Cords'] = row['Via'] results[i] = {} results[i]['Start_Node_Cords'] = "No Coords" results[i]['End_Node_Cords'] = "No Coords" results[i]['Via_Cords'] = "No Coords" for key,value in values.iteritems(): query = value print "Querying . . . " try: result = geocoder.geocode(query) except Exception as e:
class OpenCageGeocodeTestCase(unittest.TestCase): def setUp(self): httpretty.enable() self.geocoder = OpenCageGeocode('abcde') def tearDown(self): httpretty.disable() httpretty.reset() def testUKPostcode(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body='{"total_results":10,"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","rate":{"limit":"2500","remaining":2487,"reset":1402185600},"results":[{"annotations":{},"components":{"country_name":"United Kingdom","region":"Islington","locality":"Clerkenwell"},"formatted":"Clerkenwell, Islington, United Kingdom","geometry":{"lat":"51.5221558691","lng":"-0.100838524406"},"bounds":null},{"formatted":"82, Lokku Ltd, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RF, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"82","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"Lokku Ltd","postcode":"EC1M 5RF"},"annotations":{},"bounds":{"northeast":{"lng":"-0.1023889","lat":"51.5226795"},"southwest":{"lat":"51.5225795","lng":"-0.1024889"}},"geometry":{"lat":"51.5226295","lng":"-0.1024389"}},{"components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lat":"51.5225346","lng":"-0.1027003"},"bounds":{"northeast":{"lat":"51.5225759","lng":"-0.1020597"},"southwest":{"lat":"51.5225211","lng":"-0.103223"}}},{"formatted":"Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Craft Central","annotations":{},"components":{"postcode":"EC1M 6DS","arts_centre":"Craft Central","state":"England","suburb":"Clerkenwell","country":"United Kingdom","city":"London Borough of Islington","country_code":"gb","road":"Clerkenwell Road","state_district":"Greater London","county":"London"},"bounds":{"northeast":{"lat":"51.52246","lng":"-0.1027652"},"southwest":{"lng":"-0.1028652","lat":"51.52236"}},"geometry":{"lng":"-0.1028152","lat":"51.52241"}},{"components":{"county":"London","state_district":"Greater London","restaurant":"Noodle Express","road":"Albemarle Way","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"Noodle Express, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb","geometry":{"lng":"-0.10255386845056","lat":"51.5228195"},"bounds":{"southwest":{"lng":"-0.102621","lat":"51.5227781"},"northeast":{"lat":"51.5228603","lng":"-0.1024869"}}},{"geometry":{"lat":"51.5229424","lng":"-0.102380530769224"},"bounds":{"northeast":{"lat":"51.5229759","lng":"-0.1023064"},"southwest":{"lng":"-0.1024639","lat":"51.5229046"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","cafe":"PAR","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 6DS"},"formatted":"PAR, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"},{"formatted":"Workshop Coffee Co., 27, Clerkenwell Road, Clerkenwell, London Borough of Islington, London, EC1M 5RN, Greater London, England, United Kingdom, gb","components":{"county":"London","state_district":"Greater London","road":"Clerkenwell Road","country_code":"gb","house_number":"27","cafe":"Workshop Coffee Co.","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","postcode":"EC1M 5RN"},"annotations":{},"bounds":{"southwest":{"lng":"-0.1024422","lat":"51.5222246"},"northeast":{"lng":"-0.1022307","lat":"51.5224408"}},"geometry":{"lat":"51.52234585","lng":"-0.102338899572156"}},{"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"Franco & Co","state":"England","postcode":"EC1M 6DS"},"annotations":{},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, Franco & Co","geometry":{"lng":"-0.1024118","lat":"51.5231165"},"bounds":{"southwest":{"lng":"-0.1024618","lat":"51.5230665"},"northeast":{"lng":"-0.1023618","lat":"51.5231665"}}},{"bounds":{"northeast":{"lng":"-0.1023218","lat":"51.5231688"},"southwest":{"lat":"51.5229634","lng":"-0.1024934"}},"geometry":{"lng":"-0.102399365567707","lat":"51.5230257"},"formatted":"St. John Street, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb, MacCarthy","annotations":{},"components":{"county":"London","state_district":"Greater London","road":"St. John Street","country_code":"gb","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","hairdresser":"MacCarthy","state":"England","postcode":"EC1M 6DS"}},{"geometry":{"lng":"-0.102730855172415","lat":"51.52267345"},"bounds":{"northeast":{"lng":"-0.1025498","lat":"51.5227315"},"southwest":{"lat":"51.5226068","lng":"-0.1028931"}},"annotations":{},"components":{"county":"London","state_district":"Greater London","road":"Albemarle Way","country_code":"gb","house_number":"84","country":"United Kingdom","city":"London Borough of Islington","suburb":"Clerkenwell","state":"England","house":"The Printworks","postcode":"EC1M 6DS"},"formatted":"84, The Printworks, Albemarle Way, Clerkenwell, London Borough of Islington, London, EC1M 6DS, Greater London, England, United Kingdom, gb"}],"timestamp":{"created_unix":1402133768,"created_http":"Sat, 07 Jun 2014 09:36:08 GMT"},"we_are_hiring":"http://lokku.com/#jobs"}', ) results = self.geocoder.geocode("EC1M 5RF") self.assertTrue( any((abs(result['geometry']['lat'] - 51.5201666) < 0.05 and abs(result['geometry']['lng'] - -0.0985142) < 0.05) for result in results), msg="Bad result" ) def testAustralia(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body='{"licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"status":{"message":"OK","code":200},"thanks":"For using an OpenCage Data API","results":[{"geometry":{"lng":"149.5886383","lat":"-32.5980702"},"components":{"country_code":"au","state":"New South Wales","country":"Australia","town":"Mudgee"},"formatted":"Mudgee, New South Wales, Australia, au","annotations":{},"bounds":{"southwest":{"lng":"149.5486383","lat":"-32.6380702"},"northeast":{"lng":"149.6286383","lat":"-32.5580702"}}},{"formatted":"Mudgee, Mid-Western Regional, New South Wales, Australia","components":{"state":"New South Wales","country":"Australia","county":"Mid-Western Regional","town":"Mudgee"},"bounds":{"southwest":{"lng":"149.573196411","lat":"-32.6093025208"},"northeast":{"lng":"149.602890015","lat":"-32.5818252563"}},"annotations":{},"geometry":{"lng":149.5871,"lat":-32.59426}}],"total_results":2,"rate":{"reset":1402185600,"limit":"2500","remaining":2489},"we_are_hiring":"http://lokku.com/#jobs","timestamp":{"created_http":"Sat, 07 Jun 2014 09:31:50 GMT","created_unix":1402133510}}', ) results = self.geocoder.geocode("Mudgee, Australia") self.assertTrue( any((abs(result['geometry']['lat'] - -32.5980702) < 0.05 and abs(result['geometry']['lng'] - 149.5886383) < 0.05) for result in results), msg="Bad result" ) def testMustBeUnicodeString(self): # dud mock so this goes quick httpretty.register_uri(httpretty.GET, self.geocoder.url, body='{"results":{}}') # Should not give errors self.geocoder.geocode('xxx') # ascii convertable self.geocoder.geocode(six.u('xxx')) # unicode self.geocoder.geocode(six.u('xxá')) # unicode # But if it isn't a unicode string, it should give error utf8_string = six.u("xxá").encode("utf-8") latin1_string = six.u("xxá").encode("latin1") self.assertRaises(InvalidInputError, self.geocoder.geocode, utf8_string) # check the exception try: self.geocoder.geocode(utf8_string) except InvalidInputError as ex: self.assertEqual(ex.bad_value, utf8_string) self.assertEqual(str(ex), "Input must be a unicode string, not {0!r}".format(utf8_string)) self.assertRaises(InvalidInputError, self.geocoder.geocode, latin1_string) # check the exception try: self.geocoder.geocode(latin1_string) except InvalidInputError as ex: self.assertEqual(ex.bad_value, latin1_string) self.assertEqual(str(ex), "Input must be a unicode string, not {0!r}".format(latin1_string)) def testMunster(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body='{"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}],"thanks":"For using an OpenCage Data API","we_are_hiring":"http://lokku.com/#jobs","total_results":10,"status":{"message":"OK","code":200},"results":[{"formatted":"M\u00fcnster, M\u00fcnster, 48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de","geometry":{"lat":"51.9625101","lng":"7.6251879"},"components":{"country_code":"de","postcode":"48143,48144,48145,48146,48147,48148,48149,48150,48151,48152,48153,48154,48155,48156,48157,48158,48159,48160,48161,48162,48163,48164,48165,48166,48167","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","city":"M\u00fcnster","county":"M\u00fcnster","country":"Germany"},"bounds":{"southwest":{"lng":"7.4651879","lat":"51.8025101"},"northeast":{"lng":"7.7851879","lat":"52.1225101"}},"annotations":{}},{"bounds":{"northeast":{"lat":"52.0600706","lng":"7.7743708"},"southwest":{"lat":"51.8402214","lng":"7.4738051"}},"annotations":{},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","state_district":"Regierungsbezirk M\u00fcnster","state":"North Rhine-Westphalia","county":"M\u00fcnster","country":"Germany"},"formatted":"M\u00fcnster, Regierungsbezirk M\u00fcnster, North Rhine-Westphalia, Germany, de"},{"formatted":"Munster, Ireland, ie","annotations":{},"bounds":{"southwest":{"lat":"51.4199027","lng":"-10.6891099"},"northeast":{"lng":"-6.9497829","lat":"53.1689062"}},"components":{"country_code":"ie","state_district":"Munster","country":"Ireland"},"geometry":{"lng":"-8.57089717629267","lat":"52.3076216"}},{"formatted":"Germany, de, M\u00fcnster","annotations":{},"bounds":{"southwest":{"lat":"51.8402214","lng":"7.4738051"},"northeast":{"lat":"52.0600706","lng":"7.7743708"}},"geometry":{"lat":"51.95027025","lng":"7.61334708872901"},"components":{"country_code":"de","address100":"M\u00fcnster","country":"Germany"}},{"formatted":"M\u00fcnster, M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","components":{"city":"Stuttgart","state":"Baden-W\u00fcrttemberg","city_district":"M\u00fcnster","country":"Germany","country_code":"de","state_district":"Regierungsbezirk Stuttgart","suburb":"M\u00fcnster","county":"Stuttgart"},"geometry":{"lat":"48.8212962","lng":"9.2200016"},"bounds":{"northeast":{"lng":"9.2400016","lat":"48.8412962"},"southwest":{"lng":"9.2000016","lat":"48.8012962"}},"annotations":{}},{"geometry":{"lng":"8.8671181","lat":"49.9229236"},"components":{"country_code":"de","state_district":"Regierungsbezirk Darmstadt","state":"Hesse","city":"M\u00fcnster","county":"Landkreis Darmstadt-Dieburg","country":"Germany"},"annotations":{},"bounds":{"northeast":{"lat":"49.9438725","lng":"8.9161067"},"southwest":{"lat":"49.9056973","lng":"8.7705856"}},"formatted":"M\u00fcnster, Landkreis Darmstadt-Dieburg, Regierungsbezirk Darmstadt, Hesse, Germany, de"},{"formatted":"M\u00fcnster, Stuttgart, Stuttgart, Regierungsbezirk Stuttgart, Baden-W\u00fcrttemberg, Germany, de","geometry":{"lat":"48.8272797","lng":"9.2024402537349"},"components":{"country_code":"de","state_district":"Regierungsbezirk Stuttgart","state":"Baden-W\u00fcrttemberg","city":"Stuttgart","city_district":"M\u00fcnster","county":"Stuttgart","country":"Germany"},"bounds":{"northeast":{"lat":"48.8384709","lng":"9.2273738"},"southwest":{"lng":"9.1883711","lat":"48.8152795"}},"annotations":{}},{"bounds":{"southwest":{"lng":"10.8788966","lat":"48.5896428"},"northeast":{"lat":"48.6515558","lng":"10.9314006"}},"annotations":{},"geometry":{"lng":"10.9008883","lat":"48.6242219"},"components":{"country_code":"de","state_district":"Swabia","state":"Free State of Bavaria","city":"M\u00fcnster","county":"Rain (Schwaben)","country":"Germany"},"formatted":"M\u00fcnster, Rain (Schwaben), Swabia, Free State of Bavaria, Germany, de"},{"formatted":"Munster, Lake County, Indiana, United States of America, us","bounds":{"northeast":{"lat":"41.5814003","lng":"-87.4802388"},"southwest":{"lng":"-87.5254509","lat":"41.522608"}},"annotations":{},"geometry":{"lat":"41.5644798","lng":"-87.5125412"},"components":{"country_code":"us","state":"Indiana","city":"Munster","county":"Lake County","country":"United States of America"}},{"bounds":{"northeast":{"lng":"12.5929086","lat":"48.9728073"},"southwest":{"lng":"12.5529086","lat":"48.9328073"}},"annotations":{},"geometry":{"lng":"12.5729086","lat":"48.9528073"},"components":{"country_code":"de","state_district":"Lower Bavaria","village":"M\u00fcnster","state":"Free State of Bavaria","county":"Landkreis Straubing-Bogen","country":"Germany"},"formatted":"M\u00fcnster, Landkreis Straubing-Bogen, Lower Bavaria, Free State of Bavaria, Germany, de"}],"timestamp":{"created_unix":1402135758,"created_http":"Sat, 07 Jun 2014 10:09:18 GMT"},"rate":{"remaining":2485,"reset":1402185600,"limit":"2500"}}', ) results = self.geocoder.geocode(six.u("Münster")) self.assertTrue( any((abs(result['geometry']['lat'] - 51.9625101) < 0.05 and abs(result['geometry']['lng'] - 7.6251879) < 0.05) for result in results), msg="Bad result" ) def testDonostia(self): httpretty.register_uri( httpretty.GET, self.geocoder.url, body='{"thanks":"For using an OpenCage Data API","status":{"message":"OK","code":200},"rate":{"remaining":2482,"limit":"2500","reset":1402185600},"we_are_hiring":"http://lokku.com/#jobs","total_results":7,"results":[{"geometry":{"lat":"43.3213324","lng":"-1.9856227"},"annotations":{},"components":{"postcode":"20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018","county":"Donostialdea/Donostia-San Sebasti\u00e1n","state":"Basque Country","country":"Spain","city":"San Sebasti\u00e1n","country_code":"es"},"formatted":"San Sebasti\u00e1n, Donostialdea/Donostia-San Sebasti\u00e1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es","bounds":{"southwest":{"lat":"43.2178373","lng":"-2.086808"},"northeast":{"lng":"-1.8878838","lat":"43.3381344"}}},{"formatted":"Donostia, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lat":"43.3422299","lng":"-1.8022744"},"northeast":{"lng":"-1.8013452","lat":"43.3449598"}},"geometry":{"lng":"-1.8019153","lat":"43.3432784"},"annotations":{}},{"annotations":{},"geometry":{"lng":"-1.8022744","lat":"43.3422299"},"formatted":"Donostia, Anaka, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","suburb":"Anaka","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lng":"-1.8022971","lat":"43.3421635"},"northeast":{"lng":"-1.8022744","lat":"43.3422299"}}},{"geometry":{"lng":"-2.69312049872164","lat":"42.868297"},"annotations":{},"bounds":{"southwest":{"lng":"-2.6933154","lat":"42.8681484"},"northeast":{"lat":"42.8684357","lng":"-2.6929252"}},"formatted":"Donostia kalea, Ibaiondo, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Ibaiondo","country_code":"es","road":"Donostia kalea"}},{"bounds":{"southwest":{"lng":"-2.6889534","lat":"42.8620967"},"northeast":{"lat":"42.8623764","lng":"-2.6885774"}},"formatted":"Donostia kalea, Lakua, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Lakua","country_code":"es","road":"Donostia kalea"},"geometry":{"lat":"42.8622515","lng":"-2.68876582144679"},"annotations":{}},{"annotations":{},"geometry":{"lat":"51.5146888","lng":"-0.1609307"},"components":{"restaurant":"Donostia","country":"United Kingdom","state_district":"Greater London","country_code":"gb","county":"London","state":"England","suburb":"Marylebone","city":"City of Westminster","road":"Great Cumberland Mews"},"formatted":"Donostia, Great Cumberland Mews, Marylebone, City of Westminster, London, Greater London, England, United Kingdom, gb","bounds":{"northeast":{"lng":"-0.1608807","lat":"51.5147388"},"southwest":{"lat":"51.5146388","lng":"-0.1609807"}}},{"geometry":{"lat":43.31283,"lng":-1.97499},"annotations":{},"bounds":{"northeast":{"lng":"-1.92020404339","lat":"43.3401603699"},"southwest":{"lat":"43.2644081116","lng":"-2.04920697212"}},"formatted":"San Sebastian, Gipuzkoa, Basque Country, Spain, Donostia / San Sebasti\u00e1n","components":{"county":"Gipuzkoa","state":"Basque Country","country":"Spain","town":"San Sebastian","local administrative area":"Donostia / San Sebasti\u00e1n"}}],"timestamp":{"created_unix":1402136556,"created_http":"Sat, 07 Jun 2014 10:22:36 GMT"},"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}]}', ) results = self.geocoder.geocode("Donostia") self.assertTrue( any((abs(result['geometry']['lat'] - 43.300836) < 0.05 and abs(result['geometry']['lng'] - -1.9809529) < 0.05) for result in results), msg="Bad result" ) # test that the results are in unicode self.assertEqual(results[0]['formatted'], six.u('San Sebasti\xe1n, Donostialdea/Donostia-San Sebasti\xe1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es'))
def search(request): """ The bulk of the work is performed in this function, which runs once the user enters a start and end address and clicks Submit. In a nutshell, here's what it does: 1. Retrieve the start and end address that the user submitted using HTTP GET 2. Query the OpenCage Geocoding API to find the coordinates corresponding to the start and end addresses 3. Find the start segment, the sidewalk edge closest to the start coordinates. Also find the end segment, the sidewalk edge closest to the end coordinates 4. Split the start segment at the start coordinates, creating two shorter edges. Repeat for end segment. 5. Create a temporary table containing the four shorter edges in addition to the rest of the sidewalk segments 6. Run a PgRouting query on the temporary table to find the shortest accessible route. The PgRouting query returns the route as a Geojson string. 7. Query for elevation data at various points along the route and generate a Geojson string that contains both the route and the elevation data. 8. Render an HTML page, inserting the generated Geojson into the page. """ # Get the start and end addresses that the user sent address = request.GET['inputaddress'].strip() dest = request.GET['inputdest'].strip() error="" startfound = True destfound = True lat=0 lng=0 destlat=0 destlng=0 routegeojson = """{ "type": "FeatureCollection", "features": [] }""" # Query OpenCage to get coordinates for this address key = '7945028e977fa9593e5b02378cbf0f27' geocoder = OpenCageGeocode(key) # Make sure neither start address nor end address is blank first if (address != '' and dest != ''): # Get coordinates for start address # Opencage returns the result in json format jsonresult = geocoder.geocode(address) try: # Get the start lat/lng coordinates out of the results sent by OpenCage resultdict = jsonresult[0] lat = resultdict["geometry"]["lat"] lng = resultdict["geometry"]["lng"] except IndexError: # The start address was not found startfound = False # Repeat the process for destination address jsonresult = geocoder.geocode(dest) try: # Get the end lat/lng coordinates out of the results sent by OpenCage resultdict = jsonresult[0] destlat = resultdict["geometry"]["lat"] destlng = resultdict["geometry"]["lng"] except IndexError: # The destination address was not found destfound = False # Display appropriate errors if one or both addresses were not found if not startfound and destfound: error = "Error: The start address you entered could not be found." elif startfound and not destfound: error = "Error: The end address you entered could not be found." elif not startfound and not destfound: error = "Error: Neither addresses you entered could be found." else: # Display error if one or more fields were left blank error = "Error: One or more fields were left blank." cursor = connection.cursor() cursor.execute("SET search_path TO sidewalk,public;") # Allow the subsequent queries to access tables under the sidewalk schema without a qualifier (i.e., sidewalk.XXX) http://www.postgresql.org/docs/9.2/static/ddl-schemas.html # Find the sidewalk edge closest to the start location and store the value in its 'source' column as start_source_id cursor.execute("SELECT source FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [lng, lat]) row = cursor.fetchone() start_source_id = row[0] # Find the sidewalk edge closest to the end location and store the value in its 'target' column as end_target_id cursor.execute("SELECT target FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [destlng, destlat]) row = cursor.fetchone() end_target_id = row[0] # Find the sidewalk edge closest to the start location and store its 'sidewalk_edge_id' as start_edge_id cursor.execute("SELECT sidewalk_edge_id FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [lng, lat]) row = cursor.fetchone() start_edge_id = row[0] # Find the sidewalk edge closest to the end location and store its 'sidewalk_edge_id' as end_edge_id cursor.execute("SELECT sidewalk_edge_id FROM sidewalk_edge ORDER BY ST_Distance(ST_GeomFromText('POINT(%s %s)', 4326), geom) ASC LIMIT 1", [destlng, destlat]) row = cursor.fetchone() end_edge_id = row[0] # Find location in the middle of the route for centering the map average_lat = (lat + destlat)/2.0 average_lng = (lng + destlng)/2.0 # The following gigantic SQL query creates a temporary table called combined_sidewalk_edge which contains # four new edges resulting from splitting the start segment at the start coordinates and the end segment at the # end coordinates, in addition to all the original sidewalk edges. This is necessary because we need to route # from the exact start point to the exact end point, not from the start segment to the end segment. cursor.execute("DISCARD TEMP;") create_temp_query = """ CREATE TEMP TABLE combined_sidewalk_edge AS SELECT sidewalk_edge_id, geom, target, source FROM sidewalk_edge UNION SELECT -1000 as sidewalk_edge_id, ( ST_Line_Substring( (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), ( SELECT ST_Line_Locate_Point((SELECT geom FROM sidewalk_edge WHERE sidewalk_edge_id = %s LIMIT 1), ( SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326), (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) )) ) ) ,1) ) as geom, (SELECT target FROM sidewalk_edge WHERE sidewalk_edge_id = %s LIMIT 1) as target, '-123' as source UNION SELECT -1001 as sidewalk_edge_id, ( ST_Line_Substring( (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1),0, ( SELECT ST_Line_Locate_Point((SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), ( SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326),(SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) )) ) ) ) ) as geom, '-123' as target, (SELECT source FROM sidewalk_edge WHERE sidewalk_edge_id=%s LIMIT 1) as source UNION SELECT -1002 as sidewalk_edge_id, ( ST_Line_Substring( (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1),0, ( SELECT ST_Line_Locate_Point((SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), ( SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326),(SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) )) ) ) ) ) as geom, '-124' as target, (SELECT source FROM sidewalk_edge WHERE sidewalk_edge_id=%s LIMIT 1) as source UNION SELECT -1003 as sidewalk_edge_id, ( ST_Line_Substring( (SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), ( SELECT ST_Line_Locate_Point((SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1), ( SELECT ST_ClosestPoint(ST_GeomFromText('POINT(%s %s)', 4326),(SELECT geom from sidewalk_edge where sidewalk_edge_id = %s LIMIT 1) )) ) ) ,1) ) as geom, (SELECT target FROM sidewalk_edge WHERE sidewalk_edge_id = %s LIMIT 1) as target, '-124' as source ; """ cursor.execute(create_temp_query, [start_edge_id, start_edge_id, lng, lat, start_edge_id, start_edge_id, start_edge_id, start_edge_id, lng, lat, start_edge_id, start_edge_id, end_edge_id, end_edge_id, destlng, destlat, end_edge_id, end_edge_id, end_edge_id, end_edge_id, destlng, destlat, end_edge_id, end_edge_id]) cursor.execute("select * from combined_sidewalk_edge") # Now that the temporary table combined_sidewalk_edge has been created, we can query for a route from the start # location to the end location. This query will return a route as a Geojson string. # Todo. I changed calculate_accessible_cost to 1 for now. routesql = """ SELECT ST_AsGeoJSON(st_union) FROM ( SELECT ST_Union(geom) FROM ( SELECT seq, id1 AS node, id2 AS edge, route.cost, dt.geom, dt.sidewalk_edge_id FROM pgr_dijkstra(' SELECT sidewalk_edge_id AS id, source::integer, target::integer, calculate_accessible_cost(sidewalk_edge_id)::double precision AS cost FROM combined_sidewalk_edge', %s, %s, false, false) as route join combined_sidewalk_edge dt on route.id2 = dt.sidewalk_edge_id ) as routegeometries ) as final; """ #routesql = """ #SELECT ST_AsGeoJSON(route_geom) FROM ( #SELECT ST_Union(geom) as route_geom FROM ( # SELECT seq, id1 AS node, id2 AS edge, route.cost, dt.geom, dt.sidewalk_edge_id FROM pgr_dijkstra('SELECT sidewalk_edge_id AS id, # source::int4, target::int4, 1.0::float8 AS cost FROM combined_sidewalk_edge', -123, -124, false, false # ) as route # join combined_sidewalk_edge dt on route.id2 = dt.sidewalk_edge_id #) as routegeometries #) as final;""".replace("\t", "").replace("\n", "") # The source will always be -123 and the target will always be -124 because those are the source/target values we # assigned to the newly generated edges in the gigantic SQL query above. #cursor.execute(routesql) cursor.execute(routesql, ["-123", "-124"]) row = cursor.fetchone() # Store the geojson string describing the route as routejs routejs = row[0] # Unfortunately, the paths that make up the route are not ordered in the geojson returned by PostGIS. Before # we can query for elevation, we need to order the paths. """ Path sorting algorithm description: - Parse the json from PostGIS into a list named 'data'. The 'data' list is nested: It is a list of paths, each path is a list of points, and each point is a list containing lat and lng. So 'data' looks like this: [ [ [5,6][7,8] ] [ [9,10][11,12][13,14] ] ] - Create an empty list 'data_ordered'. It will have the same structure as the 'data' list, but of course it will store the paths in order. - Remove a path from 'data' and put it into data_ordered - Find the lat/lng coordinates of the LAST point in this path - Search through 'data' for a path that either begins or ends at the coordinates found in the last step. This is the path that goes next, so append it to data_ordered and remove it from 'data'. If needed, reverse the order of the points in the newly appended path so that the common coordinates "meet". For instance, if the original path is [ [6,7][12,14][3,6] ] and the new path to append is [ [3,8][3,4][3,6] ], here's what data_ordered should look like after this step: [ [ [6,7][12,14][3,6] ] [ [3,6][3,4][3,8] ] ] - Keep repeating this until no new path to append is found, at which point we have reached the end of the route. Now do this again, but prepending paths that should go before the first path currently in 'data_ordered' (rather than appending paths that go after the last one). The process continues until the 'data' list contains no more paths. - Create a new geojson string from 'data_ordered' """ data = json.loads(routejs) points = [] data_ordered = [] begin_found = False end_found = False # Add the first path to data_ordered data_ordered.append(data['coordinates'].pop()) while not begin_found or not end_found: # If the last segment hasn't been found yet if not end_found: # Find the path that goes after the last one, and append it to data_ordered search_lng = data_ordered[-1][-1][0] search_lat = data_ordered[-1][-1][1] next_segment_found = False for path in data['coordinates']: start_point = path[0] end_point = path[-1] if start_point[0] == search_lng and start_point[1] == search_lat: # We found the path that goes next next_segment_found = True # Append it to data_ordered data_ordered.append(path) # Remove it from data data['coordinates'].remove(path) elif end_point[0] == search_lng and end_point[1] == search_lat: # Same as above, but the points in the path are in the opposite order next_segment_found = True data_ordered.append(path[::-1]) # Reverse the order of points in path before appending it data['coordinates'].remove(path) # If the path that goes next was not found, we have reached the end of the route. if not next_segment_found: end_found = True # Now repeat this process backward until we reach the beginning of the route if not begin_found: # Find the path that goes before the first one, and prepend it to data_ordered search_lng = data_ordered[0][0][0] search_lat = data_ordered[0][0][1] previous_segment_found = False for path in data['coordinates']: start_point = path[0] end_point = path[-1] if start_point[0] == search_lng and start_point[1] == search_lat: # We've found the path that goes before the first path currently in data_ordered previous_segment_found = True # Prepend the path to data_ordered. Order of the points in the path need to be reversed first. data_ordered.insert(0, path[::-1]) # Remove the path from data data['coordinates'].remove(path) elif end_point[0] == search_lng and end_point[1] == search_lat: # Same as above but order of the points in the path does not need to be reversed. previous_segment_found = True data_ordered.insert(0, path) data['coordinates'].remove(path) if not previous_segment_found: begin_found = True firstpath = data_ordered[0] lastpath = data_ordered[-1] route_start_lng = firstpath[0][0] route_start_lat = firstpath[0][1] route_end_lng = lastpath[-1][0] route_end_lat = lastpath[-1][1] # Sometimes, the first path in data_ordered will be the end segment and the last one will be the start # segment, so the entire data_ordered list may need to be reversed. Check if this is necessary and if so, # reverse the order of the paths and the order of the points in each path. # Determine if the order of path in data_ordered needs to be reversed # Find distance from start point to first point in data_ordered start_to_first_dist = math.hypot(lng - route_start_lng, lat - route_start_lat) # Now find distance from start point to last point in data_ordered start_to_last_dist = math.hypot(lng - route_end_lng, lat - route_end_lat) # If the latter is less than the former, data_ordered needs to be reversed if start_to_last_dist < start_to_first_dist: # Reverse order of the paths data_ordered.reverse() for path in data_ordered: # Also reverse order of the points in each path path.reverse() firstpath = data_ordered[0] lastpath = data_ordered[-1] route_start_lng = firstpath[0][0] route_start_lat = firstpath[0][1] route_end_lng = lastpath[-1][0] route_end_lat = lastpath[-1][1] # Finally, query for elevation data # Make a list of all the points along the route for path in data_ordered: for point in path: points.append(point) # Split the route into many shorter segments (so that there are more points to query elevation for) split_path = split(points) # Get elevation for each point on the split route; store in list elevation_list = get_elevations(split_path) # Generate a geojson string that contains both the split route and the elevation data output_string = output_geojson(split_path, elevation_list) routegeojson = routejs # Get nearby accessibility features to mark on map nearby_feature_sql = """ SELECT * FROM accessibility_feature WHERE ST_Distance_Sphere(feature_geometry, ST_MakePoint(%s, %s)) <= 3 * 1609.34 AND feature_type=%s; """ # Get "construction" features cursor.execute(nearby_feature_sql, [lng, lat, 2]) construction_features = cursor.fetchall() construction_points_list = [] print("construction features") for feature in construction_features: feature_lng = feature[3] feature_lat = feature[4] feature_point = Point((feature_lng, feature_lat)) streetview_img_code = "<h3>Reported construction</h3><a target='_blank' href='http://maps.google.com/?cbll="+str(feature_lat)+","+str(feature_lng)+"&cbp=12,235,,0,5&layer=c'><img src='https://maps.googleapis.com/maps/api/streetview?size=200x200&location="+str(feature_lat)+","+str(feature_lng)+"&fov=90&heading=235&pitch=10' /></a>" feature = geojson.Feature(geometry=feature_point, properties={"markertype": "construction","popupContent":streetview_img_code, "markerAddress":address}) construction_points_list.append(feature) construction_collection = geojson.FeatureCollection(construction_points_list, featureid=2) construction_geojson = geojson.dumps(construction_collection, sort_keys=True) logger.debug(construction_geojson) # (Note: Repeat the above code for other kinds of features, if desired.) # print(routejs) return render(request, 'routeapp/homepage.html', {'submitted_start':address, 'submitted_dest':dest,'constructionfeatures':construction_geojson, 'routestartlng':route_start_lng, 'routestartlat':route_start_lat, 'routeendlng':route_end_lng, 'routeendlat':route_end_lat, 'elevationjson':output_string, 'centerlat':average_lat, 'centerlng':average_lng, 'defaultzoom': '17', 'lat':lat, 'lng':lng, 'destlat': destlat, 'destlng':destlng, 'start_textbox_value': address, 'dest_textbox_value': dest, 'error_message':error, 'routegeojson':routegeojson, })
import opencage from opencage.geocoder import OpenCageGeocode import json key = 'your-key-here' geocoder = OpenCageGeocode(key) query = '182 Clerkenwell Road, London' ret = geocoder.geocode(query) print(ret._content)
import config app = Flask(__name__) db = DatabaseConnection() consumer_key = config.consumer_key consumer_secret = config.consumer_secret access_token = config.access_token access_token_secret = config.access_token_secret opencage_key = config.opencage_key darksky_key = config.darksky_key auth = tw.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tw.API(auth, wait_on_rate_limit=True) geocoder = OpenCageGeocode(opencage_key) client = language.LanguageServiceClient() type_ = enums.Document.Type.PLAIN_TEXT language = "en" encoding_type = enums.EncodingType.UTF8 # @app.route("/result", methods=["GET"]) # def result(): # return render_template("result.html") @app.route("/analyze", methods=["POST", "GET"]) def analyze(): if (request.method == 'POST'): document = {
def setUp(self): httpretty.enable() self.geocoder = OpenCageGeocode('abcde')
""" Archivo que inicializa la base de datos """ import os from config import db from models import MetrobusDatos from random import randint, seed from datetime import datetime, timedelta from geopy.geocoders import OpenCage from opencage.geocoder import OpenCageGeocode ## Inicializar geopy geolocators = OpenCage(api_key="fab369dfefcd443d918d5c91970e77d1") geolocator = OpenCageGeocode("fab369dfefcd443d918d5c91970e77d1") ## Array de longitudes y latitudes del metrobus == 18 estaciones lonLat = [ "19.4930495,-99.1210605", "19.44674162268097,-99.15331646617256", "19.4236252,-99.1630861", "19.4096721,-99.1680921", "19.3858323,-99.175063", "19.3700691,-99.1797636", "19.364163,-99.1824701", "19.354986,-99.1860674", "19.3508802,-99.1863723", "19.3424829,-99.1897309", "19.3099899,-99.1845917", "19.3143883,-99.1872434", "19.3040286,-99.1860506", "19.2973346,-99.1847422", "19.2944284,-99.1839697", "19.2903981,-99.1774466", "19.282733,-99.1757382", "19.280384,-99.1717675" ] ## Datos para inicializar la base de datos DATOS = [] for x in range(0, 50): update_time = datetime.now() - timedelta(hours=1)
import csv from opencage.geocoder import OpenCageGeocode key = '...' geocoder = OpenCageGeocode(key) with open('/home3/jaume/Farmacies5.csv') as csvfile: with open('/home3/jaume/FarmaciesGC.csv', 'w') as csvout: equipaments = csv.reader(csvfile, delimiter=',') out = csv.writer(csvout, delimiter=',') i = 0 for row in equipaments: if row[3].startswith('Salut|Farm') or i == 0: query = row[1] + ' ' + row[3] + ', ' + row[5] + ' ' + row[ 4] + ', Catalunya, Spain' query = query.replace("''", '**') query = query.replace("'", '') query = query.replace("**", "'") print(query) results = geocoder.geocode(query) row[9] = results[0]['geometry']['lat'] row[8] = results[0]['geometry']['lng'] out.writerow(row)
print('Number of Neighbourhoods in West Side Borough', len(vnc_ws_neigh['Neighbourhood'].unique())) vnc_ws_neigh['Neighbourhood'].unique() """#####Creating a new Data frame with Lat, Lng being fetched from OpenCage geocoder""" Latitude = [] Longitude = [] Borough = [] Neighbourhood = vnc_ws_neigh['Neighbourhood'].unique() key = '830323b5ca694362904814ff0a11b803' geocoder = OpenCageGeocode(key) for i in range(len(Neighbourhood)): address = '{}, Vancouver, BC, Canada'.format(Neighbourhood[i]) location = geocoder.geocode(address) Latitude.append(location[0]['geometry']['lat']) Longitude.append(location[0]['geometry']['lng']) Borough.append('West Side') print(Latitude, Longitude) #print('The geograpical coordinate of Vancouver City are {}, {}.'.format(latitude, longitude)) """####Glimpse of the new Data Frame with Neighborhoods in West Side Borough of Vancoouver along with centroid of their co-ordinates""" ws_neig_dict = {'Neighbourhood': Neighbourhood,'Borough':Borough,'Latitude': Latitude,'Longitude':Longitude} ws_neig_geo = pd.DataFrame(data=ws_neig_dict, columns=['Neighbourhood', 'Borough', 'Latitude', 'Longitude'], index=None)