def results_procLocation(): user_location = request.form.get("location") if user_location == '': user_location = 'San Francisco, CA, United States' lat,lon,full_add,data = maps.geocode(user_location) # set the bounding box for the requested area res = data['results'][0] lng_sw = res['geometry']['viewport']['southwest']['lng'] lng_ne = res['geometry']['viewport']['northeast']['lng'] lat_sw = res['geometry']['viewport']['southwest']['lat'] lat_ne = res['geometry']['viewport']['northeast']['lat'] if lat_sw >= 36.94 and lat_ne <= 38.0 and lng_ne >= -122.53 and lng_sw <= -121.8: # get the times endTime = pd.datetime.replace(pd.datetime.now(), microsecond=0) startTime = pd.datetime.isoformat(endTime - pd.tseries.offsets.Hour(timeWindow_hours)) endTime = pd.datetime.isoformat(endTime) return redirect(url_for('.results', lng_sw=lng_sw, lng_ne=lng_ne, lat_sw=lat_sw, lat_ne=lat_ne, startTime=startTime, endTime=endTime, city=full_add)) else: this_lon, this_lat = sd.set_get_boundBox(area_str='bayarea') # for our loc, just set the average user_lon = np.mean(this_lon) user_lat = np.mean(this_lat) latlng_sw = [this_lat[1], this_lon[1]] latlng_ne = [this_lat[0], this_lon[0]] selected = "1" return render_template('out_of_bounds.html', examples=examples,\ user_lat=user_lat, user_lon=user_lon,\ latlng_sw=latlng_sw, latlng_ne=latlng_ne,\ selected=selected)
def food_groups_page(): user_location = request.args.get("origin") lat,lon,full_add,data = maps.geocode(user_location) clusters = foodgroups.foodGroups(lat,lon) restaurants = [] for i in range(len(clusters['X'])): restaurants.append(dict(lat=clusters['X'][i][0], long=clusters['X'][i][1], clusterid=clusters['labels'][i])) return render_template('results.html',results=restaurants,user_lat = lat, user_long = lon, faddress = full_add, ncluster = clusters['n_clusters'])
def food_groups_page(): user_location = request.args.get("origin") lat, lon, full_add, data = maps.geocode(user_location) clusters = foodgroups.foodGroups(lat, lon) restaurants = [] for i in range(len(clusters['X'])): restaurants.append( dict(lat=clusters['X'][i][0], long=clusters['X'][i][1], clusterid=clusters['labels'][i])) return render_template('results.html', results=restaurants, user_lat=lat, user_long=lon, faddress=full_add, ncluster=clusters['n_clusters'])
def food_groups_page(): try: user_location = request.args.get("origin") if len(user_location) == 0: return render_template('oops.html') lat,lon,full_add,data = maps.geocode(user_location) sortkey = int(request.args.get("keychain")) clusters,restdata, cluster_info = foodgroups.foodGroups(lat,lon,key = sortkey,cache=True) restaurants = [] for ix,a in restdata.iterrows(): thisdat = a restaurants.append(dict(lat=thisdat['latitude'] ,long=thisdat['longitude'] ,clusterid=thisdat['ranking'] ,url=thisdat['url'] ,name=thisdat['name'] ,pic=thisdat['photo'] )) return render_template('results3.html',results=restaurants,c_info = cluster_info, user_lat = lat, user_long = lon, faddress = full_add, ncluster = clusters['n_clusters']) except: # Well something went wrong in here return render_template('oops.html')
def cleanData(data): ''' Clean YELP result dictionaries into handy dataframes Want business location (lat, long), business name, ''' businesslist = data['businesses'] df = pd.DataFrame(columns = ('name','full_address','rating', 'review_count','distance','categories','snippet','latitude','longitude','latlongfound')) failures = [] for i in range(len(businesslist)): try: full_address = '' for a in businesslist[i]['location']['display_address']: full_address = full_address +' %s'%a except: continue # full_address = businesslist[i]['location']['display_address'][0] + ' '+businesslist[i]['location']['display_address'][1] try: loc = businesslist[i]['location']['coordinate'] lat = loc['latitude'] long = loc['longitude'] latlongfound = 1 except: # Probably doesn't have a coordinate failures.append(full_address) latlongfound = 0 lat = 0.0 long = 0.0 categories = '' try: for a in businesslist[i]['categories']: categories+= a[1]+',' #lowercase except: pass try: df.loc[len(df)+1] = [businesslist[i]['name'] , full_address , businesslist[i]['rating'] , businesslist[i]['review_count'] , businesslist[i]['distance'] , categories , businesslist[i]['snippet_text'] , lat , long , latlongfound ] except: pass try: url = 'http://www.datasciencetoolkit.org/street2coordinates' # Convert failures to json and read from web API (datasciencetoolkit.org) req = urllib2.Request(url,json.dumps(failures)) response = urllib2.urlopen(req) healing = json.loads(response.read()) except: healing = {} for a in failures: try: lat,lon,full_add,data = maps.geocode(a) healing[a] = {} healing[a]['latitude'] = lat healing[a]['longitude'] = lon except: break for a in healing.keys(): try: df.loc[df[df['full_address']==a].index,'latitude'] = healing[a]['latitude'] df.loc[df[df['full_address']==a].index,'longitude'] = healing[a]['longitude'] df.loc[df[df['full_address']==a].index,'latlongfound'] = 1 except: pass return df[df['latlongfound']==1]
def cleanData4Square(data): ''' Clean 4Square result dictionaries into handy dataframes Want business location (lat, long), business name, ''' businesslist = data['groups'][0]['items']; df = pd.DataFrame(columns = ('name','full_address','street','rating', 'review_count','distance','categories','price','latitude','longitude','latlongfound', 'photo','hasphoto','IsOpenNow','url')) failures = [] for i in range(len(businesslist)): try: full_address = '' full_address = full_address + businesslist[i]['venue']['location']['address']+', ' full_address = full_address + businesslist[i]['venue']['location']['formattedAddress'][1] except: continue try: lat = businesslist[i]['venue']['location']['lat'] long = businesslist[i]['venue']['location']['lng'] latlongfound = 1 except: # Probably doesn't have a coordinate failures.append(full_address) latlongfound = 0 lat = 0.0 long = 0.0 categories = '' try: for a in businesslist[i]['venue']['categories']: categories+= a['shortName']+',' except: pass photourl = '' try: prefix = businesslist[i]['venue']['specials']['items'][0]['photo']['prefix'] suffix = businesslist[i]['venue']['specials']['items'][0]['photo']['suffix'] photourl = prefix + '100x100' + suffix hasphoto = 2 except: try: prefix = businesslist[i]['venue']['featuredPhotos']['items'][0]['prefix'] suffix = businesslist[i]['venue']['featuredPhotos']['items'][0]['suffix'] photourl = prefix + '100x100' + suffix hasphoto = 1 except: hasphoto = 0 pass try: df.loc[len(df)+1] = [businesslist[i]['venue']['name'] , full_address , businesslist[i]['venue']['location']['address'] , businesslist[i]['venue']['rating'] , businesslist[i]['venue']['ratingSignals'] , businesslist[i]['venue']['location']['distance'] , categories , businesslist[i]['venue']['price']['tier'] , lat , long , latlongfound , photourl , hasphoto , businesslist[i]['venue']['hours']['isOpen'] , 'https://foursquare.com/v/'+businesslist[i]['venue']['id'] ] except: pass try: url = 'http://www.datasciencetoolkit.org/street2coordinates' # Convert failures to json and read from web API (datasciencetoolkit.org) req = urllib2.Request(url,json.dumps(failures)) response = urllib2.urlopen(req) healing = json.loads(response.read()) except: healing = {} for a in failures: try: lat,lon,full_add,data = maps.geocode(a) healing[a] = {} healing[a]['latitude'] = lat healing[a]['longitude'] = lon except: break for a in healing.keys(): try: df.loc[df[df['full_address']==a].index,'latitude'] = healing[a]['latitude'] df.loc[df[df['full_address']==a].index,'longitude'] = healing[a]['longitude'] df.loc[df[df['full_address']==a].index,'latlongfound'] = 1 except: pass return df[df['latlongfound']==1]