Beispiel #1
0
def index():
    if request.method == 'GET':
        
        return render_template('index.html')
    else:
        if request.form.get("address"):
            app.address = request.form['address']
        else:
            app.address = False
        
        app.dayofweek = request.form['dayofweek']        
        app.month = int(request.form['month'])        
        app.direction = request.form['direction']
        app.timeofday = int(request.form['timeofday'])
        app.ampm = request.form['ampm']
        
        if app.ampm == "pm":
            app.timeofday+=12
            
        #print "address", app.address
        #print "dayofweek" , app.dayofweek
        #print "month", app.month
        #print "direction", app.direction
        #print "timeofday", app.timeofday
        #print "ampm", app.ampm
        
        if app.address != False:
         
            params = {'address':str(app.address)}        
            r=(requests.get('https://maps.googleapis.com/maps/api/geocode/json',params=params)).json()
                   
            app.formatted_address = r['results'][0]['formatted_address']
            my_latitude = r['results'][0]['geometry']['location']['lat']
            my_longitude = r['results'][0]['geometry']['location']['lng']
            
            x_min = -74.293396 #longitude  SW: 40.481965, -74.293396 NE:40.911486, -73.733866
            x_max = -73.733866 #longitude
            y_min = 40.481965 #latitude
            y_max = 40.911486 #latitude
            mesh_space = 0.0025
        
            if my_latitude < y_min or my_latitude >= y_max or my_longitude < x_min or my_longitude>= x_max or app.address == "" or app.address == False:
                app.msg = 'The address you entered is outside the boundaries of NYC.'
                return render_template('error_page.html', msg = app.msg)
        
        
            centers_x,centers_rx=np.linspace(x_min,x_max,(x_max-x_min)/mesh_space,retstep="True")
            centers_y,centers_ry=np.linspace(y_min,y_max,(y_max-y_min)/mesh_space,retstep="True")
          
            my_center_id = get_center_id(my_longitude,my_latitude,x_min=x_min,x_max=x_max,y_min=y_min,y_max=y_max,mesh_space=mesh_space)
            
        # Make the dataframe
        f=open('../network_mesh_space=0.0025_month='+str(app.month)+'_day='+str(app.dayofweek)+'.pickle','rb')
        network = pickle.load(f)
        f.close()
               
        if app.address != False: 
            if app.direction == "from":
                #n0 = network.loc(axis=0)[:,:,my_center_id,:].reset_index()
                n0 = network.loc(axis=0)[0,0,:,:].reset_index()
                rows = [ 4309, 19125,  6619, 13648, 21587,  6194, 22422,  8247, 10405,  1773, 17665,  1911,
                         9502, 10573, 10028, 19780, 23623,  5649, 24906,  4656,   213, 14772, 26234, 25437,
                        14741,  2265, 21974,  1639,  3720, 13809,  7272, 27489,  9211,  3625,  9885, 12534,
                        22791,  9720, 11411,  9722, 12769, 22393, 16275,  5585,   288,  8995, 27861, 10421,
                         3379, 19055, 16321,  2058, 18610, 20557, 10360, 26626, 13097,  7304, 12535, 18328,
                         2825, 25743, 26901, 28509, 14744, 23308,  9451, 21275,    46, 18453, 18003, 13028,
                        16514,  4399, 28852, 22544, 27117,  4448,  6113,  1891, 25475, 28014, 24150, 11133,
                        15311,  7725,  1907,  2831,  2432,    71, 24829, 21427,  8710, 10858, 16413,   503,
                        18146, 12381, 26018, 13411,  1211, 28883, 24705,  2996, 25022,  7968,  3822, 12439,
                        22254, 11898, 17638, 24629, 13408,  5508, 18977, 21079,  5197,  7006, 19837,  4839,
                        13180, 21963, 20695, 24777, 20128, 16570, 12167, 22923,  7314,   919,  4058, 16426,
                        13154, 23515, 27967, 28810, 12379, 23697, 14048, 14816, 18752,  9806, 28308, 24397,
                        22059,    46, 24013,  1409, 22342, 23886]
                n00 = n0.ix[rows[0:150]]
                gr = n0[['dropoff_center',0]].groupby('dropoff_center', as_index=True).sum()
                gr = gr.sort(columns=0,ascending=False).iloc[0:10]
            elif app.direction == "to":
                n0 = network.loc(axis=0)[:,:,:,my_center_id].reset_index()
                gr = n0[['pickup_center',0]].groupby('pickup_center', as_index=True).sum()
                gr = gr.sort(columns=0,ascending=False).iloc[0:10]        
                
        trips = {} #{center_id: [outgoing trips, incoming trips]}
        count_pick_ups = network.loc(axis=0)[app.timeofday,:,:,:]
        count_pick_ups_index = count_pick_ups.index.values
        for i in count_pick_ups_index:
            try:
                trips[str(int(i[2]))][0] += int(count_pick_ups.ix[i])
            except KeyError:
                trips[str(int(i[2]))] = [int(count_pick_ups.ix[i]),0]
        count_drop_offs = network.loc(axis=0)[:,app.timeofday,:,:]
        count_drop_offs_index = count_drop_offs.index.values
        for i in count_drop_offs_index:
            try:
                trips[str(int(i[3]))][1] += int(count_drop_offs.ix[i])
            except KeyError:
                trips[str(int(i[3]))] = [0,int(count_drop_offs.ix[i])]
                
        tk = trips.keys()
        tv = trips.values()

        map_data = pd.DataFrame({'centers': tk, 'activity' : [i[0]+i[1] for i in tv], 
                                 'attractiveness': [i[1]-i[0] for i in tv]})
        # Make the grid
        
        f=open('../centers_long_lat_id_mesh_space=0.0025.pickle','rb')
        grid = pickle.load(f)
        f.close()
        grid_json = {"type":"FeatureCollection", "features":[]}
        
        add_mesh = 0.0025/2
        for index, row in grid.iterrows():
            if str(int(row[2])) in trips.keys():
                popup_content= "Incoming: "+str(trips[str(int(row[2]))][0])+"<br /> outgoing: "+str(trips[str(int(row[2]))][1]) +"<br /> Activity: "+str(trips[str(int(row[2]))][1] + trips[str(int(row[2]))][0]) +"<br /> Attractiveness: "+str(trips[str(int(row[2]))][1] - trips[str(int(row[2]))][0])
                coord = [[row[0]+add_mesh,row[1]+add_mesh],[row[0]+add_mesh,row[1]-add_mesh],[row[0]-add_mesh,row[1]-add_mesh],[row[0]-add_mesh,row[1]+add_mesh],[row[0]+add_mesh,row[1]+add_mesh]]
                dd = {"type":"Feature","id":str(int(row[2])),
                      "properties":{"name":str(int(row[2])),"popupContent":popup_content},
                     "geometry":{"type":"Polygon","coordinates":[coord]}
                     }
                grid_json['features'].append(dd)
        
        
        with open('static/grid.json', 'w') as outfile:
            json.dump(grid_json, outfile)
        # Make the map
        
        map_1 = folium.Map(location=[40.7114, -73.9088], zoom_start=12, tiles='Stamen Terrain',width=900, height = 900)
        map_1.lat_lng_popover()
        
        #############################################################################
        ###############ADD MAIN IN AND OUT NETWORK HUBS##############################
        
        #net = network.loc(axis=0)[app.timeofday,:,:,:]
        #net = net.reset_index()
        #net = net[['pickup_center','dropoff_center',0]]
        #net = np.array(net)
        #net = net.astype(int)
        #g=nx.MultiDiGraph()
        #g.add_weighted_edges_from(net,weight='weight')
        #outdegrees = g.out_degree(weight='weight')
        #outdegrees = [[i, outdegrees[i]] for i in outdegrees]
        #outdegrees = sorted(outdegrees,key=lambda x: x[1])
        #outdegrees = outdegrees[-5:]
        #
        #net = network.loc(axis=0)[:,app.timeofday,:,:]
        #net = net.reset_index()
        #net = net[['pickup_center','dropoff_center',0]]
        #net = np.array(net)
        #net = net.astype(int)
        #g=nx.MultiDiGraph()
        #g.add_weighted_edges_from(net,weight='weight')
        #indegrees = g.in_degree(weight='weight')
        #indegrees = [[i, indegrees[i]] for i in indegrees]
        #indegrees = sorted(indegrees,key=lambda x: x[1])
        #indegrees = indegrees[-5:]
        #
        #for i in outdegrees:
        #    location = get_center_coordinates(i[0],x_min=x_min,x_max=x_max,y_min=y_min,y_max=y_max,mesh_space=mesh_space)
        #    map_1.circle_marker(location=(location[1],location[0]), radius=80, popup="Number of trips from hub: "+str(i[1]),
        #                    fill_color='green',line_color='green',fill_opacity=0.7)        
        #
        #for i in indegrees:
        #    location = get_center_coordinates(i[0],x_min=x_min,x_max=x_max,y_min=y_min,y_max=y_max,mesh_space=mesh_space)
        #    map_1.circle_marker(location=(location[1],location[0]), radius=50, popup="Number of trips to hub: "+str(i[1]),
        #                        fill_color='blue',line_color='blue',fill_opacity=0.7)
        
        ##########################################################################################
        ##########################################################################################
        
        ##########################################################################################
        ###############ADD MARKER AT CHOSEN ADDRESS###############################################
        
        #if app.address != False: #add a marker at my address
        #    map_1.circle_marker(location=(my_latitude,my_longitude),radius=60,popup=str(app.formatted_address),line_color='red',
        #          fill_color='red', fill_opacity=0.4)

        ##########################################################################################
        ##########################################################################################
        
        ##########################################################################################
        #############ADD LINES TO MAP#############################################################
        
        if app.address != False:
            for index, row in n00.iterrows():
                p_f = get_center_coordinates(int(row['pickup_center']),x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,mesh_space=mesh_space)
                p_i = get_center_coordinates(int(row['dropoff_center']),x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,mesh_space=mesh_space)
                map_1.line(locations=[(p_i[1], p_i[0]), (p_f[1], p_f[0])],line_weight=2,line_opacity=1,line_color='black',popup=0)            
        
        ##########################################################################################
        ##########################################################################################

        ##########################################################################################
        #############Add grid to map##############################################################
        
        #map_1.geo_json(geo_path='static/grid.json', data=map_data,
        #               columns=['centers', 'activity'],
        #               data_out='static/data.json',
        #               threshold_scale=[10, 100, 200, 300, 500, 600],
        #               key_on='feature.id',fill_color='BuPu', fill_opacity=0.5, line_weight=1,
        #               line_opacity=0.2,line_color='black',
        #               legend_name='Activity Rate',reset=True)

        ##########################################################################################
        ##########################################################################################
        
        ##########################################################################################
        ##############Create map and prepare Flask variables######################################
        
        map_1.create_map(path='nyc.html')        
        soup = bs(open('nyc.html'), 'html.parser') 
        app.map_head = soup.head
        app.map_div = str(soup.body.div) #.replace("100%","800px")        
        app.map_script= soup.body.script
        ##########################################################################################
        ##########################################################################################
        
        ##########################################################################################
        ###ADD plot of NUMBER OF TRIPS (PICK UP AND DROPOFF FROM LOCATION)########################
        ###BOKEH BLOCK############################################################################
        app.script_graph2 = None
        app.div_graph2 = None
        if app.address != False: #This get plotted only if the user entered a location
            pickup_count = [0 for i in range(24)]
            dropoff_count = [0 for i in range(24)]
            idx = pd.IndexSlice
            for ind in network.index.levels[0]:
                pickup_count[ind] = int(network.loc(axis=0)[ind,:,26241,:].sum())
                dropoff_count[ind] = int(network.loc(axis=0)[:,ind,:,26241].sum()[0])            
            TOOLS="pan,wheel_zoom,box_zoom,reset,save"
            
            p2 = figure(tools=TOOLS, plot_width=400, plot_height=400, x_axis_label='Time',y_axis_label='Number of trips')#,x_axis_type='datetime')
            p2.line(np.array(range(24)), pickup_count,line_width=2, color="blue", legend="Average pickups from your location")
            p2.line(np.array(range(24)), dropoff_count,line_width=2, color="red",legend="Average dropoffs at your location")
            
            script_graph2, div_graph2 = components(p2)        
            app.script_graph2 = script_graph2
            app.div_graph2 = div_graph2
        
        ###END BOKEH BLOCK########################################################################
        ##########################################################################################
             
        return redirect('/graph_page')
Beispiel #2
0
    
    #print longitude, latitude
    params = {'latitude': str(latitude), 'longitude':str(longitude), 'format':'json'}
    r=requests.get('http://www.broadbandmap.gov/broadbandmap/census/county',params=params)
    d = r.json()

    return d['Results']['county'][0]['name']
    

for id in range(4,174000):  #let's make this in chunks of data
    print 'id', id
    #sql_taxi_trips ="SELECT id, pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude FROM taxi_trip WHERE id>"+str(id*1000)+" AND id<="+str((id+1)*1000)
    #df_taxi_trip = pd.read_sql_query(sql_taxi_trips, con, index_col='id')
    df_taxi_trip['pickup_county'] = df_taxi_trip.apply(lambda x: get_county(x['pickup_longitude'], x['pickup_latitude']), axis=1)
    df_taxi_trip['dropoff_county'] = df_taxi_trip.apply(lambda x: get_county(x['dropoff_longitude'], x['dropoff_latitude']), axis=1)
    df_taxi_trip['zone_id_pickup'] = df_taxi_trip.apply(lambda x: get_center_id(x['pickup_longitude'], x['pickup_latitude']), axis=1)
    df_taxi_trip['zone_id_dropoff'] = df_taxi_trip.apply(lambda x: get_center_id(x['dropoff_longitude'], x['dropoff_latitude']), axis=1)
    print 'writting to DB'
    df_taxi_trip[['zone_id_pickup','zone_id_dropoff','pickup_county','dropoff_county']].to_sql(con=con, name='trip_zones', index_label='id',if_exists='append', flavor='mysql')
    
#
#
#
#
#
#add_zone = ("INSERT INTO zone_centers "
#              "(longitude, latitude) "
#              "VALUES (%(longitude)s, %(latitude)s)")
#
#cur.execute(add_zone, zone_center)
#con.commit()
Beispiel #3
0
    return d["Results"]["county"][0]["name"]


for id in range(4, 174000):  # let's make this in chunks of data
    print "id", id
    # sql_taxi_trips ="SELECT id, pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude FROM taxi_trip WHERE id>"+str(id*1000)+" AND id<="+str((id+1)*1000)
    # df_taxi_trip = pd.read_sql_query(sql_taxi_trips, con, index_col='id')
    df_taxi_trip["pickup_county"] = df_taxi_trip.apply(
        lambda x: get_county(x["pickup_longitude"], x["pickup_latitude"]), axis=1
    )
    df_taxi_trip["dropoff_county"] = df_taxi_trip.apply(
        lambda x: get_county(x["dropoff_longitude"], x["dropoff_latitude"]), axis=1
    )
    df_taxi_trip["zone_id_pickup"] = df_taxi_trip.apply(
        lambda x: get_center_id(x["pickup_longitude"], x["pickup_latitude"]), axis=1
    )
    df_taxi_trip["zone_id_dropoff"] = df_taxi_trip.apply(
        lambda x: get_center_id(x["dropoff_longitude"], x["dropoff_latitude"]), axis=1
    )
    print "writting to DB"
    df_taxi_trip[["zone_id_pickup", "zone_id_dropoff", "pickup_county", "dropoff_county"]].to_sql(
        con=con, name="trip_zones", index_label="id", if_exists="append", flavor="mysql"
    )

#
#
#
#
#
# add_zone = ("INSERT INTO zone_centers "
Beispiel #4
0
def index():
    if request.method == 'GET':
        try:
            os.remove('nyc.html')
        except OSError:
            pass
        try:
            os.remove('static/data.json')
        except OSError:
            pass
        try:
            os.remove('static/grid.json')
        except OSError:
            pass
        return render_template('index.html')
    else:
        if request.form.get("address"):
            app.address = request.form['address']
        else:
            app.address = False

        app.dayofweek = request.form['dayofweek']
        app.month = int(request.form['month'])
        app.direction = request.form['direction']
        app.timeofday = int(request.form['timeofday'])
        app.ampm = request.form['ampm']

        if app.ampm == "pm":
            app.timeofday += 12

        #print "address", app.address
        #print "dayofweek" , app.dayofweek
        #print "month", app.month
        #print "direction", app.direction
        #print "timeofday", app.timeofday
        #print "ampm", app.ampm
        #t0 = time.time()
        if app.address != False:
            params = {'address': str(app.address)}
            r = (requests.get(
                'https://maps.googleapis.com/maps/api/geocode/json',
                params=params)).json()
            app.formatted_address = r['results'][0]['formatted_address']
            my_latitude = r['results'][0]['geometry']['location']['lat']
            my_longitude = r['results'][0]['geometry']['location']['lng']

            x_min = -74.293396  #longitude  SW: 40.481965, -74.293396 NE:40.911486, -73.733866
            x_max = -73.733866  #longitude
            y_min = 40.481965  #latitude
            y_max = 40.911486  #latitude
            mesh_space = 0.0025

            if my_latitude < y_min or my_latitude >= y_max or my_longitude < x_min or my_longitude >= x_max or app.address == "" or app.address == False:
                app.msg = 'The address you entered is outside the boundaries of NYC.'
                return render_template('error_page.html', msg=app.msg)

            centers_x, centers_rx = np.linspace(x_min,
                                                x_max,
                                                (x_max - x_min) / mesh_space,
                                                retstep="True")
            centers_y, centers_ry = np.linspace(y_min,
                                                y_max,
                                                (y_max - y_min) / mesh_space,
                                                retstep="True")

            my_center_id = get_center_id(my_longitude,
                                         my_latitude,
                                         x_min=x_min,
                                         x_max=x_max,
                                         y_min=y_min,
                                         y_max=y_max,
                                         mesh_space=mesh_space)
        #t1 = time.time()

        # Make the dataframe
        f = open(
            '../network_mesh_space=0.0025_month=' + str(app.month) + '_day=' +
            str(app.dayofweek) + '.pickle', 'rb')
        network = pickle.load(f)
        f.close()

        #t2 = time.time()

        if app.address != False:
            if app.direction == "from":
                n0 = network.loc(axis=0)[:, :, my_center_id, :].reset_index()
                gr = n0[['dropoff_center', 0]].groupby('dropoff_center',
                                                       as_index=True).sum()
                gr = gr.sort(columns=0, ascending=False).iloc[0:10]
            elif app.direction == "to":
                n0 = network.loc(axis=0)[:, :, :, my_center_id].reset_index()
                gr = n0[['pickup_center', 0]].groupby('pickup_center',
                                                      as_index=True).sum()
                gr = gr.sort(columns=0, ascending=False).iloc[0:10]

        #t3 = time.time()

        count_pick_ups = network.loc(axis=0)[app.timeofday, :, :, :]
        count_pick_ups_cols = count_pick_ups.reset_index()
        count_drop_offs = network.loc(axis=0)[:, app.timeofday, :, :]
        count_drop_offs_cols = count_pick_ups.reset_index()

        pickups = pd.DataFrame(
            count_pick_ups_cols.groupby('pickup_center')[0].sum())
        dropoffs = pd.DataFrame(
            count_drop_offs_cols.groupby('dropoff_center')[0].sum())
        pickups_dropoffs = pd.merge(pickups,
                                    dropoffs,
                                    how='outer',
                                    left_index=True,
                                    right_index=True)
        pickups_dropoffs = pickups_dropoffs.fillna(value=0)
        pickups_dropoffs = pickups_dropoffs.reset_index()
        map_data = pd.DataFrame()
        map_data['centers'] = pickups_dropoffs['index'].astype(int)
        map_data[
            'activity'] = pickups_dropoffs['0_x'] + pickups_dropoffs['0_y']
        map_data['attractiveness'] = pickups_dropoffs[
            '0_y'] - pickups_dropoffs['0_x']
        map_data['incoming'] = pickups_dropoffs['0_y']
        map_data['outgoing'] = pickups_dropoffs['0_x']

        ###########trips = {} #{center_id: [outgoing trips, incoming trips]}
        ###########t3b = time.time()
        ###########for i in count_pick_ups_index:
        ###########    try:
        ###########        trips[str(int(i[2]))][0] += int(count_pick_ups.ix[i])
        ###########    except KeyError:
        ###########        trips[str(int(i[2]))] = [int(count_pick_ups.ix[i]),0]
        ###########t3c = time.time()
        ###########print t3c - t3b
        ###########count_drop_offs = network.loc(axis=0)[:,app.timeofday,:,:]
        ###########count_drop_offs_index = count_drop_offs.index.values
        ###########for i in count_drop_offs_index:
        ###########    try:
        ###########        trips[str(int(i[3]))][1] += int(count_drop_offs.ix[i])
        ###########    except KeyError:
        ###########        trips[str(int(i[3]))] = [0,int(count_drop_offs.ix[i])]
        ###########
        ###########tk = trips.keys()
        ###########tv = trips.values()
        ###########
        ###########map_data = pd.DataFrame({'centers': tk, 'activity' : [i[0]+i[1] for i in tv],
        ###########                         'attractiveness': [i[1]-i[0] for i in tv]})
        # Make the grid
        #t4 = time.time()

        f = open('../centers_long_lat_id_mesh_space=0.0025.pickle', 'rb')
        grid = pickle.load(f)
        f.close()
        grid_json = {"type": "FeatureCollection", "features": []}

        add_mesh = 0.0025 / 2
        gridMap_data = pd.merge(map_data,
                                grid,
                                how='inner',
                                left_on='centers',
                                right_on='center_id')
        for index, row in gridMap_data.iterrows():
            popup_content = "Incoming: " + str(int(
                row[3])) + "<br /> outgoing: " + str(int(
                    row[4])) + "<br /> Activity: " + str(int(
                        row[1])) + "<br /> Attractiveness: " + str(int(row[2]))
            coord = [[row[5] + add_mesh, row[6] + add_mesh],
                     [row[5] + add_mesh, row[6] - add_mesh],
                     [row[5] - add_mesh, row[6] - add_mesh],
                     [row[5] - add_mesh, row[6] + add_mesh],
                     [row[5] + add_mesh, row[6] + add_mesh]]
            dd = {
                "type": "Feature",
                "id": str(int(row[0])),
                "properties": {
                    "name": str(int(row[0])),
                    "popupContent": popup_content
                },
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [coord]
                }
            }
            grid_json['features'].append(dd)

        ##for index, row in grid.iterrows():
        ##    if str(int(row[2])) in trips.keys():
        ##        popup_content= "Incoming: "+str(trips[str(int(row[2]))][0])+"<br /> outgoing: "+str(trips[str(int(row[2]))][1]) +"<br /> Activity: "+str(trips[str(int(row[2]))][1] + trips[str(int(row[2]))][0]) +"<br /> Attractiveness: "+str(trips[str(int(row[2]))][1] - trips[str(int(row[2]))][0])
        ##        coord = [[row[0]+add_mesh,row[1]+add_mesh],[row[0]+add_mesh,row[1]-add_mesh],[row[0]-add_mesh,row[1]-add_mesh],[row[0]-add_mesh,row[1]+add_mesh],[row[0]+add_mesh,row[1]+add_mesh]]
        ##        dd = {"type":"Feature","id":str(int(row[2])),
        ##              "properties":{"name":str(int(row[2])),"popupContent":popup_content},
        ##             "geometry":{"type":"Polygon","coordinates":[coord]}
        ##             }
        ##        grid_json['features'].append(dd)

        with open('static/grid.json', 'w') as outfile:
            json.dump(grid_json, outfile)

        #t5 = time.time()

        # Make the map

        map_1 = folium.Map(location=[40.74006, -73.98605],
                           zoom_start=13,
                           tiles='Stamen Terrain',
                           width=900,
                           height=900)
        map_1.lat_lng_popover()

        #t6 = time.time()

        #############################################################################
        ###############ADD MAIN IN AND OUT NETWORK HUBS##############################

        net = network.loc(axis=0)[app.timeofday, :, :, :]
        net = net.reset_index()
        net = net[['pickup_center', 'dropoff_center', 0]]
        net = np.array(net)
        net = net.astype(int)
        g = nx.MultiDiGraph()
        g.add_weighted_edges_from(net, weight='weight')
        outdegrees = g.out_degree(weight='weight')
        outdegrees = [[i, outdegrees[i]] for i in outdegrees]
        outdegrees = sorted(outdegrees, key=lambda x: x[1])
        outdegrees = outdegrees[-5:]

        net = network.loc(axis=0)[:, app.timeofday, :, :]
        net = net.reset_index()
        net = net[['pickup_center', 'dropoff_center', 0]]
        net = np.array(net)
        net = net.astype(int)
        g = nx.MultiDiGraph()
        g.add_weighted_edges_from(net, weight='weight')
        indegrees = g.in_degree(weight='weight')
        indegrees = [[i, indegrees[i]] for i in indegrees]
        indegrees = sorted(indegrees, key=lambda x: x[1])
        indegrees = indegrees[-5:]

        for i in outdegrees:
            location = get_center_coordinates(i[0],
                                              x_min=x_min,
                                              x_max=x_max,
                                              y_min=y_min,
                                              y_max=y_max,
                                              mesh_space=mesh_space)
            map_1.circle_marker(location=(location[1], location[0]),
                                radius=80,
                                popup="Number of trips from hub: " + str(i[1]),
                                fill_color='green',
                                line_color='green',
                                fill_opacity=0.7)

        for i in indegrees:
            location = get_center_coordinates(i[0],
                                              x_min=x_min,
                                              x_max=x_max,
                                              y_min=y_min,
                                              y_max=y_max,
                                              mesh_space=mesh_space)
            map_1.circle_marker(location=(location[1], location[0]),
                                radius=50,
                                popup="Number of trips to hub: " + str(i[1]),
                                fill_color='blue',
                                line_color='blue',
                                fill_opacity=0.7)

        #t7 = time.time()

        ##########################################################################################
        ##########################################################################################

        ##########################################################################################
        ###############ADD MARKER AT CHOSEN ADDRESS###############################################

        if app.address != False:  #add a marker at my address
            map_1.circle_marker(location=(my_latitude, my_longitude),
                                radius=60,
                                popup=str(app.formatted_address),
                                line_color='red',
                                fill_color='red',
                                fill_opacity=0.4)

        ##########################################################################################
        ##########################################################################################

        #t8 = time.time()

        ##########################################################################################
        #############ADD LINES TO MAP#############################################################

        if app.address != False:
            for index, row in gr.iterrows():
                p_f = get_center_coordinates(index,
                                             x_min=x_min,
                                             x_max=x_max,
                                             y_min=y_min,
                                             y_max=y_max,
                                             mesh_space=mesh_space)
                map_1.line(locations=[(my_latitude, my_longitude),
                                      (p_f[1], p_f[0])],
                           line_weight=3,
                           line_opacity=1,
                           line_color='black',
                           popup=str(int(row)) + " trips")

        ##########################################################################################
        ##########################################################################################
        #t9 = time.time()

        ##########################################################################################
        #############Add grid to map##############################################################

        map_1.geo_json(geo_path='static/grid.json',
                       data=map_data,
                       columns=['centers', 'activity'],
                       data_out='static/data.json',
                       threshold_scale=[10, 100, 200, 300, 500, 600],
                       key_on='feature.id',
                       fill_color='BuPu',
                       fill_opacity=0.5,
                       line_weight=1,
                       line_opacity=0.2,
                       line_color='black',
                       legend_name='Activity Rate',
                       reset=True)

        ##########################################################################################
        ##########################################################################################
        #t10 = time.time()

        ##########################################################################################
        ##############Create map and prepare Flask variables######################################

        map_1.create_map(path='nyc.html')
        soup = bs(open('nyc.html'), 'html.parser')
        app.map_head = soup.head
        app.map_div = str(soup.body.div)  #.replace("100%","800px")
        app.map_script = soup.body.script
        ##########################################################################################
        ##########################################################################################
        #t11 = time.time()

        ##########################################################################################
        ###ADD plot of NUMBER OF TRIPS (PICK UP AND DROPOFF FROM LOCATION)########################
        ###BOKEH BLOCK############################################################################
        app.script_graph2 = None
        app.div_graph2 = None
        if app.address != False:  #This get plotted only if the user entered a location
            pickup_count = [0 for i in range(24)]
            dropoff_count = [0 for i in range(24)]
            idx = pd.IndexSlice
            for ind in network.index.levels[0]:
                pickup_count[ind] = int(
                    network.loc(axis=0)[ind, :, my_center_id, :].sum())
                dropoff_count[ind] = int(
                    network.loc(axis=0)[:, ind, :, my_center_id].sum()[0])
            TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

            p2 = figure(
                tools=TOOLS,
                plot_width=400,
                plot_height=400,
                x_axis_label='Time',
                y_axis_label='Number of trips')  #,x_axis_type='datetime')
            p2.line(np.array(range(24)),
                    pickup_count,
                    line_width=2,
                    color="blue",
                    legend="Average pickups from your location")
            p2.line(np.array(range(24)),
                    dropoff_count,
                    line_width=2,
                    color="red",
                    legend="Average dropoffs at your location")

            script_graph2, div_graph2 = components(p2)
            app.script_graph2 = script_graph2
            app.div_graph2 = div_graph2

        ###END BOKEH BLOCK########################################################################
        ##########################################################################################
        #t12 = time.time()

        #print t1-t0, t2-t1, t3-t2, t4-t3, t5-t4, t6-t5, t7-t6, t8-t7, t9-t8, t10-t9, t11-t10, t12-t11

        return redirect('/graph_page')
Beispiel #5
0
def index():
    if request.method == 'GET':
        try:
            os.remove('nyc.html')
        except OSError:
            pass
        try:
            os.remove('static/data.json')
        except OSError:
            pass
        try:
            os.remove('static/grid.json')
        except OSError:
            pass
        return render_template('index.html')
    else:
        if request.form.get("address"):
            app.address = request.form['address']
        else:
            app.address = False
        
        app.dayofweek = request.form['dayofweek']        
        app.month = int(request.form['month'])        
        app.direction = request.form['direction']
        app.timeofday = int(request.form['timeofday'])
        app.ampm = request.form['ampm']
        
        if app.ampm == "pm":
            app.timeofday+=12
            
        #print "address", app.address
        #print "dayofweek" , app.dayofweek
        #print "month", app.month
        #print "direction", app.direction
        #print "timeofday", app.timeofday
        #print "ampm", app.ampm
        #t0 = time.time()
        if app.address != False:
            params = {'address':str(app.address)}        
            r=(requests.get('https://maps.googleapis.com/maps/api/geocode/json',params=params)).json()
            app.formatted_address = r['results'][0]['formatted_address']
            my_latitude = r['results'][0]['geometry']['location']['lat']
            my_longitude = r['results'][0]['geometry']['location']['lng']
            
            x_min = -74.293396 #longitude  SW: 40.481965, -74.293396 NE:40.911486, -73.733866
            x_max = -73.733866 #longitude
            y_min = 40.481965 #latitude
            y_max = 40.911486 #latitude
            mesh_space = 0.0025
        
            if my_latitude < y_min or my_latitude >= y_max or my_longitude < x_min or my_longitude>= x_max or app.address == "" or app.address == False:
                app.msg = 'The address you entered is outside the boundaries of NYC.'
                return render_template('error_page.html', msg = app.msg)
        
        
            centers_x,centers_rx=np.linspace(x_min,x_max,(x_max-x_min)/mesh_space,retstep="True")
            centers_y,centers_ry=np.linspace(y_min,y_max,(y_max-y_min)/mesh_space,retstep="True")
          
            my_center_id = get_center_id(my_longitude,my_latitude,x_min=x_min,x_max=x_max,y_min=y_min,y_max=y_max,mesh_space=mesh_space)
        #t1 = time.time()
        
        # Make the dataframe
        f=open('../network_mesh_space=0.0025_month='+str(app.month)+'_day='+str(app.dayofweek)+'.pickle','rb')
        network = pickle.load(f)
        f.close()
        
        #t2 = time.time()       
        
        if app.address != False: 
            if app.direction == "from":
                n0 = network.loc(axis=0)[:,:,my_center_id,:].reset_index()
                gr = n0[['dropoff_center',0]].groupby('dropoff_center', as_index=True).sum()
                gr = gr.sort(columns=0,ascending=False).iloc[0:10]
            elif app.direction == "to":
                n0 = network.loc(axis=0)[:,:,:,my_center_id].reset_index()
                gr = n0[['pickup_center',0]].groupby('pickup_center', as_index=True).sum()
                gr = gr.sort(columns=0,ascending=False).iloc[0:10]        
        
        #t3 = time.time()
        
        count_pick_ups = network.loc(axis=0)[app.timeofday,:,:,:]
        count_pick_ups_cols = count_pick_ups.reset_index()
        count_drop_offs = network.loc(axis=0)[:,app.timeofday,:,:]
        count_drop_offs_cols = count_pick_ups.reset_index()
        
        pickups = pd.DataFrame(count_pick_ups_cols.groupby('pickup_center')[0].sum())
        dropoffs = pd.DataFrame(count_drop_offs_cols.groupby('dropoff_center')[0].sum())
        pickups_dropoffs = pd.merge(pickups,dropoffs,how='outer',left_index=True, right_index=True)
        pickups_dropoffs = pickups_dropoffs.fillna(value=0)
        pickups_dropoffs = pickups_dropoffs.reset_index()
        map_data = pd.DataFrame()
        map_data['centers'] = pickups_dropoffs['index'].astype(int)
        map_data['activity'] = pickups_dropoffs['0_x'] + pickups_dropoffs['0_y']
        map_data['attractiveness'] = pickups_dropoffs['0_y'] - pickups_dropoffs['0_x']
        map_data['incoming'] = pickups_dropoffs['0_y']
        map_data['outgoing'] = pickups_dropoffs['0_x']
            
        ###########trips = {} #{center_id: [outgoing trips, incoming trips]}
        ###########t3b = time.time()
        ###########for i in count_pick_ups_index:
        ###########    try:
        ###########        trips[str(int(i[2]))][0] += int(count_pick_ups.ix[i])
        ###########    except KeyError:
        ###########        trips[str(int(i[2]))] = [int(count_pick_ups.ix[i]),0]
        ###########t3c = time.time()
        ###########print t3c - t3b
        ###########count_drop_offs = network.loc(axis=0)[:,app.timeofday,:,:]
        ###########count_drop_offs_index = count_drop_offs.index.values
        ###########for i in count_drop_offs_index:
        ###########    try:
        ###########        trips[str(int(i[3]))][1] += int(count_drop_offs.ix[i])
        ###########    except KeyError:
        ###########        trips[str(int(i[3]))] = [0,int(count_drop_offs.ix[i])]
        ###########
        ###########tk = trips.keys()
        ###########tv = trips.values()
        ###########
        ###########map_data = pd.DataFrame({'centers': tk, 'activity' : [i[0]+i[1] for i in tv], 
        ###########                         'attractiveness': [i[1]-i[0] for i in tv]})
        # Make the grid
        #t4 = time.time()
        
        f=open('../centers_long_lat_id_mesh_space=0.0025.pickle','rb')
        grid = pickle.load(f)
        f.close()
        grid_json = {"type":"FeatureCollection", "features":[]}
        
        add_mesh = 0.0025/2
        gridMap_data = pd.merge(map_data,grid,how='inner',left_on='centers',right_on='center_id')
        for index, row in gridMap_data.iterrows():
            popup_content= "Incoming: "+str(int(row[3]))+"<br /> outgoing: "+str(int(row[4])) +"<br /> Activity: "+str(int(row[1])) +"<br /> Attractiveness: "+str(int(row[2]))
            coord = [[row[5]+add_mesh,row[6]+add_mesh],[row[5]+add_mesh,row[6]-add_mesh],[row[5]-add_mesh,row[6]-add_mesh],[row[5]-add_mesh,row[6]+add_mesh],[row[5]+add_mesh,row[6]+add_mesh]]
            dd = {"type":"Feature","id":str(int(row[0])),
                  "properties":{"name":str(int(row[0])),"popupContent":popup_content},
                 "geometry":{"type":"Polygon","coordinates":[coord]}
                 }
            grid_json['features'].append(dd)
            
        
        ##for index, row in grid.iterrows():
        ##    if str(int(row[2])) in trips.keys():
        ##        popup_content= "Incoming: "+str(trips[str(int(row[2]))][0])+"<br /> outgoing: "+str(trips[str(int(row[2]))][1]) +"<br /> Activity: "+str(trips[str(int(row[2]))][1] + trips[str(int(row[2]))][0]) +"<br /> Attractiveness: "+str(trips[str(int(row[2]))][1] - trips[str(int(row[2]))][0])
        ##        coord = [[row[0]+add_mesh,row[1]+add_mesh],[row[0]+add_mesh,row[1]-add_mesh],[row[0]-add_mesh,row[1]-add_mesh],[row[0]-add_mesh,row[1]+add_mesh],[row[0]+add_mesh,row[1]+add_mesh]]
        ##        dd = {"type":"Feature","id":str(int(row[2])),
        ##              "properties":{"name":str(int(row[2])),"popupContent":popup_content},
        ##             "geometry":{"type":"Polygon","coordinates":[coord]}
        ##             }
        ##        grid_json['features'].append(dd)
        
        
        with open('static/grid.json', 'w') as outfile:
            json.dump(grid_json, outfile)
        
        #t5 = time.time()
        
        # Make the map
        
        map_1 = folium.Map(location=[40.74006, -73.98605], zoom_start=13, tiles='Stamen Terrain',width=900, height = 900)
        map_1.lat_lng_popover()
        
        #t6 = time.time()
        
        #############################################################################
        ###############ADD MAIN IN AND OUT NETWORK HUBS##############################
        
        net = network.loc(axis=0)[app.timeofday,:,:,:]
        net = net.reset_index()
        net = net[['pickup_center','dropoff_center',0]]
        net = np.array(net)
        net = net.astype(int)
        g=nx.MultiDiGraph()
        g.add_weighted_edges_from(net,weight='weight')
        outdegrees = g.out_degree(weight='weight')
        outdegrees = [[i, outdegrees[i]] for i in outdegrees]
        outdegrees = sorted(outdegrees,key=lambda x: x[1])
        outdegrees = outdegrees[-5:]
        
        net = network.loc(axis=0)[:,app.timeofday,:,:]
        net = net.reset_index()
        net = net[['pickup_center','dropoff_center',0]]
        net = np.array(net)
        net = net.astype(int)
        g=nx.MultiDiGraph()
        g.add_weighted_edges_from(net,weight='weight')
        indegrees = g.in_degree(weight='weight')
        indegrees = [[i, indegrees[i]] for i in indegrees]
        indegrees = sorted(indegrees,key=lambda x: x[1])
        indegrees = indegrees[-5:]
        
        for i in outdegrees:
            location = get_center_coordinates(i[0],x_min=x_min,x_max=x_max,y_min=y_min,y_max=y_max,mesh_space=mesh_space)
            map_1.circle_marker(location=(location[1],location[0]), radius=80, popup="Number of trips from hub: "+str(i[1]),
                            fill_color='green',line_color='green',fill_opacity=0.7)        
        
        for i in indegrees:
            location = get_center_coordinates(i[0],x_min=x_min,x_max=x_max,y_min=y_min,y_max=y_max,mesh_space=mesh_space)
            map_1.circle_marker(location=(location[1],location[0]), radius=50, popup="Number of trips to hub: "+str(i[1]),
                                fill_color='blue',line_color='blue',fill_opacity=0.7)
        
        #t7 = time.time()
        
        ##########################################################################################
        ##########################################################################################
        
        ##########################################################################################
        ###############ADD MARKER AT CHOSEN ADDRESS###############################################
        
        if app.address != False: #add a marker at my address
            map_1.circle_marker(location=(my_latitude,my_longitude),radius=60,popup=str(app.formatted_address),line_color='red',
                  fill_color='red', fill_opacity=0.4)

        ##########################################################################################
        ##########################################################################################
        
        #t8 = time.time()
        
        ##########################################################################################
        #############ADD LINES TO MAP#############################################################
        
        if app.address != False:
            for index, row in gr.iterrows():
                p_f = get_center_coordinates(index,x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,mesh_space=mesh_space)
                map_1.line(locations=[(my_latitude, my_longitude), (p_f[1], p_f[0])],line_weight=3,line_opacity=1,line_color='black',popup=str(int(row))+" trips")            
        
        ##########################################################################################
        ##########################################################################################
        #t9 = time.time()
        
        ##########################################################################################
        #############Add grid to map##############################################################
        
        map_1.geo_json(geo_path='static/grid.json', data=map_data,
                       columns=['centers', 'activity'],
                       data_out='static/data.json',
                       threshold_scale=[10, 100, 200, 300, 500, 600],
                       key_on='feature.id',fill_color='BuPu', fill_opacity=0.5, line_weight=1,
                       line_opacity=0.2,line_color='black',
                       legend_name='Activity Rate',reset=True)

        ##########################################################################################
        ##########################################################################################
        #t10 = time.time()
        
        ##########################################################################################
        ##############Create map and prepare Flask variables######################################
        
        map_1.create_map(path='nyc.html')        
        soup = bs(open('nyc.html'), 'html.parser') 
        app.map_head = soup.head
        app.map_div = str(soup.body.div) #.replace("100%","800px")        
        app.map_script= soup.body.script
        ##########################################################################################
        ##########################################################################################
        #t11 = time.time()
        
        ##########################################################################################
        ###ADD plot of NUMBER OF TRIPS (PICK UP AND DROPOFF FROM LOCATION)########################
        ###BOKEH BLOCK############################################################################
        app.script_graph2 = None
        app.div_graph2 = None
        if app.address != False: #This get plotted only if the user entered a location
            pickup_count = [0 for i in range(24)]
            dropoff_count = [0 for i in range(24)]
            idx = pd.IndexSlice
            for ind in network.index.levels[0]:
                pickup_count[ind] = int(network.loc(axis=0)[ind,:,my_center_id,:].sum())
                dropoff_count[ind] = int(network.loc(axis=0)[:,ind,:,my_center_id].sum()[0])            
            TOOLS="pan,wheel_zoom,box_zoom,reset,save"
            
            p2 = figure(tools=TOOLS, plot_width=400, plot_height=400, x_axis_label='Time',y_axis_label='Number of trips')#,x_axis_type='datetime')
            p2.line(np.array(range(24)), pickup_count,line_width=2, color="blue", legend="Average pickups from your location")
            p2.line(np.array(range(24)), dropoff_count,line_width=2, color="red",legend="Average dropoffs at your location")
            
            script_graph2, div_graph2 = components(p2)        
            app.script_graph2 = script_graph2
            app.div_graph2 = div_graph2
        
        ###END BOKEH BLOCK########################################################################
        ##########################################################################################
        #t12 = time.time()
        
        #print t1-t0, t2-t1, t3-t2, t4-t3, t5-t4, t6-t5, t7-t6, t8-t7, t9-t8, t10-t9, t11-t10, t12-t11
             
        return redirect('/graph_page')
Beispiel #6
0
                                  user="******",
                                  passwd="hernan",
                                  db="trips")
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)

for id in range(1001, 1741):  #let's make this in chunks of data
    print 'id', id
    sql_taxi_trips = "SELECT id, pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude FROM taxi_trip WHERE id>" + str(
        id * 100000) + " AND id<=" + str((id + 1) * 100000)
    df_taxi_trip = pd.read_sql_query(sql_taxi_trips, con, index_col='id')
    #df_taxi_trip['pickup_county'] = df_taxi_trip.apply(lambda x: get_county(x['pickup_longitude'], x['pickup_latitude']), axis=1)
    #df_taxi_trip['dropoff_county'] = df_taxi_trip.apply(lambda x: get_county(x['dropoff_longitude'], x['dropoff_latitude']), axis=1)
    df_taxi_trip['pickup_center'] = df_taxi_trip.apply(
        lambda x: get_center_id(x['pickup_longitude'], x['pickup_latitude']),
        axis=1)
    df_taxi_trip['dropoff_center'] = df_taxi_trip.apply(
        lambda x: get_center_id(x['dropoff_longitude'], x['dropoff_latitude']),
        axis=1)
    df_taxi_trip[['pickup_center',
                  'dropoff_center']].to_sql(con=con,
                                            name='trip_centers',
                                            index_label='id',
                                            if_exists='append',
                                            flavor='mysql')
import requests
import simplejson as json
import mysql.connector
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
from identify_points import get_center_id

try:
    con = mysql.connector.connect(host="localhost", user="******",  passwd="hernan", db="trips")
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)

for id in range(1001,1741):  #let's make this in chunks of data
    print 'id', id
    sql_taxi_trips ="SELECT id, pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude FROM taxi_trip WHERE id>"+str(id*100000)+" AND id<="+str((id+1)*100000)
    df_taxi_trip = pd.read_sql_query(sql_taxi_trips, con, index_col='id')
    #df_taxi_trip['pickup_county'] = df_taxi_trip.apply(lambda x: get_county(x['pickup_longitude'], x['pickup_latitude']), axis=1)
    #df_taxi_trip['dropoff_county'] = df_taxi_trip.apply(lambda x: get_county(x['dropoff_longitude'], x['dropoff_latitude']), axis=1)
    df_taxi_trip['pickup_center'] = df_taxi_trip.apply(lambda x: get_center_id(x['pickup_longitude'], x['pickup_latitude']), axis=1)
    df_taxi_trip['dropoff_center'] = df_taxi_trip.apply(lambda x: get_center_id(x['dropoff_longitude'], x['dropoff_latitude']), axis=1)
    df_taxi_trip[['pickup_center','dropoff_center']].to_sql(con=con, name='trip_centers', index_label='id',if_exists='append', flavor='mysql')
Beispiel #8
0
def index():
    if request.method == 'GET':

        return render_template('index.html')
    else:
        if request.form.get("address"):
            app.address = request.form['address']
        else:
            app.address = False
        app.dayofweek = request.form['dayofweek']
        app.direction = request.form['direction']
        app.timeofday = int(request.form['timeofday'])
        app.ampm = request.form['ampm']

        if app.address == False:
            app.msg = 'You must enter your desired address.'
            return render_template('error_page.html', msg=app.msg)

        params = {'address': str(app.address)}
        r = (requests.get('https://maps.googleapis.com/maps/api/geocode/json',
                          params=params)).json()

        app.formatted_address = r['results'][0]['formatted_address']
        my_latitude = r['results'][0]['geometry']['location']['lat']
        my_longitude = r['results'][0]['geometry']['location']['lng']

        x_min = -74.293396  #longitude  SW: 40.481965, -74.293396 NE:40.911486, -73.733866
        x_max = -73.733866  #longitude
        y_min = 40.481965  #latitude
        y_max = 40.911486  #latitude
        mesh_space = 0.01

        if my_latitude < y_min or my_latitude >= y_max or my_longitude < x_min or my_longitude >= x_max or app.address == "" or app.address == False:
            app.msg = 'The address you entered is outside the boundaries of NYC.'
            return render_template('error_page.html', msg=app.msg)

        centers_x, centers_rx = np.linspace(x_min,
                                            x_max,
                                            (x_max - x_min) / mesh_space,
                                            retstep="True")
        centers_y, centers_ry = np.linspace(y_min,
                                            y_max,
                                            (y_max - y_min) / mesh_space,
                                            retstep="True")

        my_center_id = get_center_id(my_longitude,
                                     my_latitude,
                                     x_min=x_min,
                                     x_max=x_max,
                                     y_min=y_min,
                                     y_max=y_max,
                                     mesh_space=mesh_space)

        input = open(
            '../network_mesh_space=0.01_day=' + str(app.dayofweek) + '.pickle',
            'rb')
        network_df = pickle.load(input)
        input.close()

        input = open(
            '../main_data_mesh_space=0.01_day=' + str(app.dayofweek) +
            '.pickle', 'rb')
        data_df = pickle.load(input)
        input.close()

        ##################################################
        ########Bokeh MAP block##############################
        x_range = Range1d()
        y_range = Range1d()

        # JSON style string taken from: https://snazzymaps.com/style/1/pale-dawn
        map_options = GMapOptions(lat=my_latitude,
                                  lng=my_longitude,
                                  map_type="roadmap",
                                  zoom=13,
                                  styles="""
        [{"featureType":"administrative","elementType":"all","stylers":[{"visibility":"on"},{"lightness":33}]},
        {"featureType":"landscape","elementType":"all","stylers":[{"color":"#f2e5d4"}]},
        {"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#c5dac6"}]},
        {"featureType":"poi.park","elementType":"labels","stylers":[{"visibility":"on"},{"lightness":20}]},
        {"featureType":"road","elementType":"all","stylers":[{"lightness":20}]},
        {"featureType":"road.highway","elementType":"geometry","stylers":[{"color":"#c5c6c6"}]},
        {"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#e4d7c6"}]},
        {"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#fbfaf7"}]},
        {"featureType":"water","elementType":"all","stylers":[{"visibility":"on"},{"color":"#acbcc9"}]}]"""
                                  )

        plot = GMapPlot(x_range=x_range,
                        y_range=y_range,
                        map_options=map_options,
                        title=""
                        #plot_width=750, plot_height=750
                        )

        plot.plot_width = 800
        plot.plot_height = 800

        source = ColumnDataSource(data=dict(lat=[my_latitude],
                                            lon=[my_longitude],
                                            fill=['orange', 'blue', 'green']))
        circle = Circle(x="lon",
                        y="lat",
                        size=10,
                        fill_color="fill",
                        line_color="black")
        plot.add_glyph(source, circle)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude - 0.005, my_latitude + 0.005],
            lon=[my_longitude - 0.005, my_longitude - 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude + 0.005, my_latitude + 0.005],
            lon=[my_longitude - 0.005, my_longitude + 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude - 0.005, my_latitude + 0.005],
            lon=[my_longitude + 0.005, my_longitude + 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude - 0.005, my_latitude - 0.005],
            lon=[my_longitude - 0.005, my_longitude + 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        #hover.tooltips = [
        #("index", "$index"),
        #("(x,y)", "($x, $y)"),
        #("radius", "@radius"),
        #("fill color", "$color[hex, swatch]:fill_color"),
        #("foo", "@foo"),
        #("bar", "@bar"),
        #]

        if app.ampm == "pm":
            app.timeofday = app.timeofday + 24

        if app.direction == "from":
            n0 = network_df[app.timeofday][my_center_id]
            n0.sort(ascending=False, axis=1)
            n0 = n0.irow(range(20))
        elif app.direction == "to":
            n0 = network_df[app.timeofday][:, my_center_id]
            n0.sort(ascending=False, axis=1)
            n0 = n0.irow(range(20))

        #widths = [9,8,8,7,6,5,4,3,2,1]
        ww = 0.
        for index, row in n0.iteritems():
            ww += 0.3
            #p_i = get_center_coordinates(my_center_id,x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,mesh_space=mesh_space)
            p_f = get_center_coordinates(index,
                                         x_min=x_min,
                                         x_max=x_max,
                                         y_min=y_min,
                                         y_max=y_max,
                                         mesh_space=mesh_space)

            ss = ColumnDataSource(data=dict(
                lat=[my_latitude, p_f[1]],
                lon=[my_longitude, p_f[0]],
            ))
            line = Line(x="lon", y="lat", line_width=ww)
            plot.add_glyph(ss, line)

        pan = PanTool()
        wheel_zoom = WheelZoomTool()
        box_select = BoxSelectTool()

        plot.add_tools(pan, wheel_zoom, box_select)

        #xaxis = LinearAxis(axis_label="lat", major_tick_in=0, formatter=NumeralTickFormatter(format="0.000"))
        #plot.add_layout(xaxis, 'below')

        #yaxis = LinearAxis(axis_label="lon", major_tick_in=0, formatter=PrintfTickFormatter(format="%.3f"))
        #plot.add_layout(yaxis, 'left')

        overlay = BoxSelectionOverlay(tool=box_select)
        plot.add_layout(overlay)

        app.script, app.div = components(plot)

        ##################################################
        ##################################################

        ##################################################
        ########Bokeh FIG block##############################

        # select the tools we want
        TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

        #print datetime.datetime(hour=int(33)/2, minute=int(33)%2*30)
        #print data_df.index

        p1 = figure(tools=TOOLS,
                    plot_width=400,
                    plot_height=400,
                    x_axis_label='Time',
                    y_axis_label='Number of trips')  #,x_axis_type='datetime')
        p1.line((data_df.index) / 2,
                data_df['number_of_pickups_at_time_slot'],
                line_width=2,
                color="blue",
                legend="Total number of pickups in NYC in a typical day")
        p1.line((data_df.index) / 2,
                data_df['number_of_dropoffs_at_time_slot'],
                line_width=2,
                color="red",
                legend="Total number of dropoffs in NYC in a typical day")
        #p1.circle(dates, closing_prices, fill_color="red", size=6)

        #plots = {'Red': p1}

        #script_graph1, div_graph1 = components(plots)
        #app.script_graph1 = script_graph1
        #app.div_graph1 = div_graph1.values()[0]
        ##################################################

        ##################################################

        ###ADD plot of NUMBER OF TRIPS (PICK UP AND DROPOFF FROM LOCATION)
        pickup_count = [0 for i in range(48)]
        dropoff_count = [0 for i in range(48)]
        for ind in network_df.index.levels[0]:
            try:
                pickup_count[ind] = network_df[ind][my_center_id].count()
            except KeyError:
                pass
            try:
                dropoff_count[ind] = network_df[ind][:, my_center_id].count()
            except KeyError:
                continue

        TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

        p2 = figure(tools=TOOLS,
                    plot_width=400,
                    plot_height=400,
                    x_axis_label='Time',
                    y_axis_label='Number of trips')  #,x_axis_type='datetime')
        p2.line(np.array(range(48)) / 2,
                pickup_count,
                line_width=2,
                color="blue",
                legend="Average pickups from your location")
        p2.line(np.array(range(48)) / 2,
                dropoff_count,
                line_width=2,
                color="red",
                legend="Average dropoffs at your location")
        #p1.circle(dates, closing_prices, fill_color="red", size=6)

        #plots2 = {'Red': p2}

        plots1and2 = gridplot([[p1, p2]])

        script_graph2, div_graph2 = components(plots1and2)
        app.script_graph2 = script_graph2
        app.div_graph2 = div_graph2

        return redirect('/graph_page')
Beispiel #9
0
def index():
    if request.method == 'GET':
        
        return render_template('index.html')
    else:
        if request.form.get("address"):
            app.address = request.form['address']
        else:
            app.address = False
        app.dayofweek = request.form['dayofweek']        
        app.direction = request.form['direction']
        app.timeofday = int(request.form['timeofday'])
        app.ampm = request.form['ampm']
        
        if app.address == False:
            app.msg = 'You must enter your desired address.'
            return render_template('error_page.html', msg = app.msg)
         
        params = {'address':str(app.address)}        
        r=(requests.get('https://maps.googleapis.com/maps/api/geocode/json',params=params)).json()
                
        app.formatted_address = r['results'][0]['formatted_address']
        my_latitude = r['results'][0]['geometry']['location']['lat']
        my_longitude = r['results'][0]['geometry']['location']['lng']

        x_min = -74.293396 #longitude  SW: 40.481965, -74.293396 NE:40.911486, -73.733866
        x_max = -73.733866 #longitude
        y_min = 40.481965 #latitude
        y_max = 40.911486 #latitude
        mesh_space = 0.01
        
        if my_latitude < y_min or my_latitude >= y_max or my_longitude < x_min or my_longitude>= x_max or app.address == "" or app.address == False:
            app.msg = 'The address you entered is outside the boundaries of NYC.'
            return render_template('error_page.html', msg = app.msg)


        centers_x,centers_rx=np.linspace(x_min,x_max,(x_max-x_min)/mesh_space,retstep="True")
        centers_y,centers_ry=np.linspace(y_min,y_max,(y_max-y_min)/mesh_space,retstep="True")
          
        my_center_id = get_center_id(my_longitude,my_latitude,x_min=x_min,x_max=x_max,y_min=y_min,y_max=y_max,mesh_space=mesh_space)
        
        input = open('../network_mesh_space=0.01_day='+str(app.dayofweek)+'.pickle','rb')        
        network_df = pickle.load(input)
        input.close()
        
        input = open('../main_data_mesh_space=0.01_day='+str(app.dayofweek)+'.pickle','rb')
        data_df = pickle.load(input)
        input.close()
                
                
        ##################################################
        ########Bokeh MAP block##############################
        x_range = Range1d()
        y_range = Range1d()
        
        # JSON style string taken from: https://snazzymaps.com/style/1/pale-dawn
        map_options = GMapOptions(lat=my_latitude, lng=my_longitude, map_type="roadmap", zoom=13, styles="""
        [{"featureType":"administrative","elementType":"all","stylers":[{"visibility":"on"},{"lightness":33}]},
        {"featureType":"landscape","elementType":"all","stylers":[{"color":"#f2e5d4"}]},
        {"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#c5dac6"}]},
        {"featureType":"poi.park","elementType":"labels","stylers":[{"visibility":"on"},{"lightness":20}]},
        {"featureType":"road","elementType":"all","stylers":[{"lightness":20}]},
        {"featureType":"road.highway","elementType":"geometry","stylers":[{"color":"#c5c6c6"}]},
        {"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#e4d7c6"}]},
        {"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#fbfaf7"}]},
        {"featureType":"water","elementType":"all","stylers":[{"visibility":"on"},{"color":"#acbcc9"}]}]""")
         
        plot = GMapPlot(
            x_range=x_range, y_range=y_range,
            map_options=map_options,
            title = ""
            #plot_width=750, plot_height=750
        )
        
        plot.plot_width = 800
        plot.plot_height = 800
        
        source = ColumnDataSource(
            data=dict(
                lat=[my_latitude],
                lon=[my_longitude],
                fill=['orange', 'blue', 'green']
            )
        )
        circle = Circle(x="lon", y="lat", size=10, fill_color="fill", line_color="black")
        plot.add_glyph(source, circle)

        source = ColumnDataSource(
            data=dict(
                lat=[my_latitude-0.005,my_latitude+0.005],
                lon=[my_longitude-0.005,my_longitude-0.005],
            )
        )
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)
        

        source = ColumnDataSource(
            data=dict(
                lat=[my_latitude+0.005,my_latitude+0.005],
                lon=[my_longitude-0.005,my_longitude+0.005],
            )
        )
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(
            data=dict(
                lat=[my_latitude-0.005,my_latitude+0.005],
                lon=[my_longitude+0.005,my_longitude+0.005],
            )
        )
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(
            data=dict(
                lat=[my_latitude-0.005,my_latitude-0.005],
                lon=[my_longitude-0.005,my_longitude+0.005],
            )
        )
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)
           
        #hover.tooltips = [
            #("index", "$index"),
            #("(x,y)", "($x, $y)"),
            #("radius", "@radius"),
            #("fill color", "$color[hex, swatch]:fill_color"),
            #("foo", "@foo"),
            #("bar", "@bar"),
        #]
        
        if app.ampm == "pm":
            app.timeofday = app.timeofday +24
        
        if app.direction == "from":
            n0 = network_df[app.timeofday][my_center_id]
            n0.sort(ascending=False,axis=1)
            n0 = n0.irow(range(20))
        elif app.direction == "to":
            n0 = network_df[app.timeofday][:,my_center_id]
            n0.sort(ascending=False,axis=1)
            n0 = n0.irow(range(20))
        
        #widths = [9,8,8,7,6,5,4,3,2,1]
        ww = 0.
        for index, row in n0.iteritems():
            ww += 0.3
            #p_i = get_center_coordinates(my_center_id,x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,mesh_space=mesh_space)
            p_f = get_center_coordinates(index,x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,mesh_space=mesh_space)

            ss = ColumnDataSource(
            data=dict(
                lat=[my_latitude,p_f[1]],
                lon=[my_longitude,p_f[0]],
            ))
            line = Line(x="lon", y="lat", line_width=ww)
            plot.add_glyph(ss, line)
            
        pan = PanTool()
        wheel_zoom = WheelZoomTool()
        box_select = BoxSelectTool()
        
        plot.add_tools(pan, wheel_zoom, box_select)
        
        #xaxis = LinearAxis(axis_label="lat", major_tick_in=0, formatter=NumeralTickFormatter(format="0.000"))
        #plot.add_layout(xaxis, 'below')
        
        #yaxis = LinearAxis(axis_label="lon", major_tick_in=0, formatter=PrintfTickFormatter(format="%.3f"))
        #plot.add_layout(yaxis, 'left')
        
        overlay = BoxSelectionOverlay(tool=box_select)
        plot.add_layout(overlay)
                
        app.script, app.div = components(plot)

        ##################################################
        ##################################################
            
        
        ##################################################
        ########Bokeh FIG block##############################
    
        # select the tools we want
        TOOLS="pan,wheel_zoom,box_zoom,reset,save"
    
        #print datetime.datetime(hour=int(33)/2, minute=int(33)%2*30)
        #print data_df.index
        
        p1 = figure(tools=TOOLS, plot_width=400, plot_height=400, x_axis_label='Time',y_axis_label='Number of trips')#,x_axis_type='datetime')
        p1.line((data_df.index)/2, data_df['number_of_pickups_at_time_slot'],line_width=2, color="blue", legend="Total number of pickups in NYC in a typical day")
        p1.line((data_df.index)/2, data_df['number_of_dropoffs_at_time_slot'],line_width=2, color="red",legend="Total number of dropoffs in NYC in a typical day")
        #p1.circle(dates, closing_prices, fill_color="red", size=6)

        #plots = {'Red': p1}
    
        #script_graph1, div_graph1 = components(plots)        
        #app.script_graph1 = script_graph1
        #app.div_graph1 = div_graph1.values()[0]
        ##################################################
        
        ##################################################

        ###ADD plot of NUMBER OF TRIPS (PICK UP AND DROPOFF FROM LOCATION)
        pickup_count = [0 for i in range(48)]
        dropoff_count = [0 for i in range(48)]
        for ind in network_df.index.levels[0]:
            try:
                pickup_count[ind] = network_df[ind][my_center_id].count()
            except KeyError:
                pass
            try:
                dropoff_count[ind] = network_df[ind][:,my_center_id].count()
            except KeyError:
                continue
        
        TOOLS="pan,wheel_zoom,box_zoom,reset,save"
                
        p2 = figure(tools=TOOLS, plot_width=400, plot_height=400, x_axis_label='Time',y_axis_label='Number of trips')#,x_axis_type='datetime')
        p2.line(np.array(range(48))/2, pickup_count,line_width=2, color="blue", legend="Average pickups from your location")
        p2.line(np.array(range(48))/2, dropoff_count,line_width=2, color="red",legend="Average dropoffs at your location")
        #p1.circle(dates, closing_prices, fill_color="red", size=6)
        
        
        #plots2 = {'Red': p2}
        
        plots1and2 = gridplot([[p1, p2]])
        
        script_graph2, div_graph2 = components(plots1and2)        
        app.script_graph2 = script_graph2
        app.div_graph2 = div_graph2
        
        return redirect('/graph_page')