def get_map_from_database(self): travelSensors = db_session.query(TravelSensor).all() self.READER_IDs = [x.READER_ID for x in travelSensors] for sensor in self.READER_IDs: adjs = db_session.query(Summary.Destination).filter( Summary.Origin == sensor).distinct(Summary.Destination).all() self.ADJ_INTERSECTIONS[sensor] = [adj[0] for adj in adjs]
def getTravelTime(self, s, t, dtime, weekday): # first attempt summary = db_session.query(Summary.Year, Summary.Avg_Travel_Time).filter( Summary.Origin == s, Summary.Destination == t, Summary.Weekday == weekday, Summary.Time >= dtime / 60, Summary.Time <= dtime / 60 + 60, ).order_by(Summary.Time).all() if len(summary) == 0: if dtime >= 23 * 3600: # after 11pm, look at the next day summary = db_session.query(Summary.Year, Summary.Avg_Travel_Time).filter( Summary.Origin == s, Summary.Destination == t, Summary.Weekday == weekday + 1).order_by(Summary.Time).all() if len(summary) == 0: beg_day = 0 end_day = 0 if weekday in [0, 1, 2, 3, 4]: beg_day = 0 end_day = 4 else: beg_day = 5 end_day = 6 summary = db_session.query(Summary.Year, Summary.Avg_Travel_Time).filter( Summary.Origin == s, Summary.Destination == t, Summary.Weekday >= beg_day, Summary.Weekday <= end_day, Summary.Time >= dtime / 60, Summary.Time <= dtime / 60 + 60, ).order_by(Summary.Time).all() if len(summary) == 0: # report nothing #print "nothing" #print {'s':s, 't':t, 'dtime':dtime} return self.INFINITY looked_years = set() total = 0 for ss in summary: if ss.Year not in looked_years: looked_years.add(ss.Year) total += ss.Avg_Travel_Time #print {'s':s, 't':t, 'dtime':dtime, 'total':total, 'n': len(looked_years)} return total / len(looked_years)
def travelTimePrediction_function(): travelSensors = db_session.query(TravelSensor).all() startTimeList = time_list() weekdayList = weekday_list() time_pre = None origin = 'congress_oltorf' destination = 'congress_11th' start_time = 37 weekday = 0 if request.method == 'POST': origin = request.form['origin'] destination = request.form['destination'] start_time = int(request.form['start_time']) weekday = int(request.form['weekday']) minutes = (start_time - 1) * 15 time_pre = time_prediction.findRoute(origin, destination, minutes, weekday) if time_pre['time'] != -1: time_pre['time'] = round(time_pre['time'] / 60, 2) return render_template("travelTimePrediction.html", travelSensors=travelSensors, time_pre=time_pre, startTimeList=startTimeList, weekdayList=weekdayList, weekday=weekday, origin=origin, destination=destination, start_time=start_time)
def __init__(self): travelSensors = db_session.query(TravelSensor).all() self.travelSensors = { x.READER_ID: (x.LATITUDE, x.LONGITUDE) for x in travelSensors } self.myMap = Map() self.myMap.get_map_from_file()
def historyStats_function(): # Generate display strings for frontend. timeList = time_list() weekdayList = weekday_list() # Parse request params and set default values. year = request.args.get('year') if year is None: year = 2017 else: year = int(year) weekday = request.args.get('weekday') if weekday is None: weekday = 0 else: weekday = int(weekday) time = request.args.get('time') start_time = 37 if time is None: time = 0 else: start_time = int(time) time = (int(time) - 1) * 15 # Query database for average travel time and travel sensor geographic data. origin = aliased(TravelSensor, name='origin') destination = aliased(TravelSensor, name='destination') result = db_session.query( Summary.Origin, Summary.Destination, Summary.Time, Summary.Weekday, Summary.Avg_Travel_Time, origin, destination).join(origin, Summary.Origin == origin.READER_ID).join( destination, Summary.Destination == destination.READER_ID).filter( Summary.Weekday == weekday, Summary.Time == time, Summary.Year == year) traffic_list = result.all() console_print("result count: " + str(result.count())) # console_print("result count: " + str(db_session.query(TravelSensor).all())) # console_print("result count: " + str(result.column_descriptions)) return render_template("historyStats.html", traffic_list=traffic_list, timeList=timeList, start_time=start_time, weekdayList=weekdayList, weekday=weekday)