def __get_trip_distances(self, trip): trip.od_distance = CommonHelper.point_distance(trip.origin, trip.destination) #TODO TEMP #if trip.od_distance == 0: # print("WARNING!!! in __get_trip_distances(): od_distance is 0, for trip ID:",trip.id," OD:",trip.origin, trip.destination) trip.legs_without_points = [] trip.distance = 0 trip.distance_by_mode = {} for leg in trip.legs: # distance by mode .....: # first calcualtes and SETs leg['distance'] values ... TODO!!! should be done elsewhere for example during leg-detection? if leg['is_moprim_leg']: pass #TODO: for legs retrieved from OTP, presumably, leg['distance'] is accurate enough (is point-to-point based) elif leg['is_otp_leg']: pass #TODO: for legs retrieved from OTP, presumably, leg['distance'] is accurate enough (is point-to-point based) else: #i.e: leg from original points recorded by user device leg['od_distance'] = CommonHelper.point_distance(CommonHelper.geoJSON_to_pointRow(leg['origin']), CommonHelper.geoJSON_to_pointRow(leg['destination'])) # Calc travelled-distance, point to point based here, OR Write a plsql-like function that does so on DB server leg_points_rows = self.legs_dal.get_leg_points(leg) leg['distance'] = self.__calculate_trajectory_distance(leg_points_rows) if leg_points_rows.rowcount == 0: log(["Warning in __get_trip_distances()!: legs_dal.get_leg_points(leg) returned ZERO points. For leg ("+str(leg['user_id'])+' '+ str(trip.id)+" "+str(leg['id'])+ "; device_id="+str(leg['device_id'])+'; '+str(leg['time_start'])+', '+str(leg['time_end'])]) trip.legs_without_points.append(leg) # add up leg-distances to total trip.distance: trip.add_travelled_distance(leg['mode'], leg['distance']) # NOTE: 'total distance' of trip is increased inside this function
def delete_trip_plan(self, trip, mode, numItineraries, maxWalkDistance): qstr = """DELETE FROM trip_plans WHERE start_time = '{0}' AND origin = '{1}' AND destination = '{2}' AND mode = '{3}' AND max_walk_distance = {4} AND no_of_itins = {5}; """.format( CommonHelper.DateTime_to_Text(trip.starttime), CommonHelper.pointRow_to_postgisPoint(trip.origin), CommonHelper.pointRow_to_postgisPoint(trip.destination), mode, maxWalkDistance, numItineraries) res, db_res = self.db_command.ExecuteSQL(qstr, LOG_IMPORTANT_CUSTOM=False) return res, self._get_delete_macthed_count(res, db_res)
def store_trip_plan(self, trip, mode, numItineraries, maxWalkDistance, plan): qstr = """INSERT INTO trip_plans (start_time, origin, destination, mode, max_walk_distance, no_of_itins, plan) VALUES ('{0}', ST_GeomFromText('{1}'), ST_GeomFromText('{2}'), '{3}',{4},{5}, '{6}' ); """.format( CommonHelper.DateTime_to_Text(trip.starttime), CommonHelper.pointRow_to_postgisPoint(trip.origin), CommonHelper.pointRow_to_postgisPoint(trip.destination), mode, maxWalkDistance, numItineraries, CommonHelper.json_to_sqlstr(plan)) res, db_res = self.db_command.ExecuteSQL(qstr, LOG_IMPORTANT_CUSTOM=False) return res
def load_trip_plan(self, trip, mode, numItineraries, maxWalkDistance): qstr = """SELECT start_time, ST_AsText(origin) as origin, ST_AsText(destination) as destination, mode, max_walk_distance, no_of_itins, plan FROM trip_plans WHERE start_time = '{0}' AND origin = '{1}' AND destination = '{2}' AND mode = '{3}' AND max_walk_distance = {4} AND no_of_itins = {5}; """.format( CommonHelper.DateTime_to_Text(trip.starttime), CommonHelper.pointRow_to_postgisPoint(trip.origin), CommonHelper.pointRow_to_postgisPoint(trip.destination), mode, maxWalkDistance, numItineraries) res, plan_rows = self.db_command.ExecuteSQL(qstr, LOG_IMPORTANT_CUSTOM=False) return res, plan_rows
def __calculate_trajectory_distance(self, point_rows): maxDistanceForPointMatch = MAX_GPS_ERROR + MAX_VEHICLE_LENGTH distance = 0 if point_rows.rowcount > 1: oldpoint = point_rows.fetchone()['coo'] for leg_point in point_rows: point = leg_point['coo'] #coo = json.loads(point)['coordinates'] #(coo[1], coo[0]) p2p_distance = CommonHelper.point_distance_byGeoJSON(point, oldpoint) if p2p_distance >= maxDistanceForPointMatch: distance += p2p_distance oldpoint = point if oldpoint != point: #exception ... don't miss the last one p2p_distance = CommonHelper.point_distance_byGeoJSON(point, oldpoint) distance += p2p_distance #print('__calculate_trajectory_distance():: Did not miss the last point!!!!!!!!!!!!!!!!!!!') return distance
def get_next_user_trip_id(self, user_id, date_range_start, date_range_end): qstr = """SELECT max(id) as max_id FROM trips_alts WHERE user_id = {0} AND (user_id, id, plan_id) NOT IN (SELECT user_id, id, plan_id FROM trips_alts WHERE user_id = {0} AND start_time >= '{1}' AND end_time <= '{2}'); """.format(user_id, CommonHelper.DateTime_to_Text(date_range_start), CommonHelper.DateTime_to_Text(date_range_end)) log(["get_next_user_trip_id():: qstr:", qstr]) log("") res, maxid_rows = self.db_command.ExecuteSQL(qstr) if maxid_rows.rowcount > 0: for maxid_row in maxid_rows: if maxid_row['max_id'] == None: return 0 else: return maxid_row['max_id'] return 0
def HACK_compute_ebike_alternatives(self, trips): ebike_trips = [] for trip in trips: ebike_alt = None for alt in trip.alternative_trips: if alt.mainmode == 'BICYCLE': # compute ebike alternative ----- # create a new alternative ebike_alt = deepcopy( alt) #almost all values of ebike trip is same as bike # duration and calories defer, and maybe also emission defers # change the values that need recalculation speed_coeff = 1.161290323 # speed of ordinary bike is 15.5 km/h and e-bike is 18 km/h duration_coeff = 1 / speed_coeff ebike_alt.mainmode = 'EBICYCLE' # TODO: also edit multimodal_summary ebike_alt.duration = CommonHelper.multiply_duration( ebike_alt.duration, duration_coeff) ebike_alt.plan_id = 1 + max( trip.alternative_trips, key=lambda x: x.plan_id).plan_id # TODO, revise later # for item in ebike_alt.duration_by_mode.iteritems(): # newleg = {} # if item[0] == 'BICYCLE': # item[0] = 'EBICYCLE' # legduration = CommonHelper.str_to_DateTimeDuration(item[1]) # item[1] = CommonHelper.multiply_duration(legduration, duration_coeff) # #leg[1] = legduration # # for legmode, legduration in ebike_alt.duration_by_mode.iteritems(): # if legmode == 'BICYCLE': # legmode = 'EBICYCLE' # legduration = CommonHelper.str_to_DateTimeDuration(legduration) # legduration = CommonHelper.multiply_duration(legduration, duration_coeff) # #leg[1] = legduration # calculate total duration of trip and leg-durations total_legs_duration = datetime.timedelta(0) for legmode, legduration in ebike_alt.duration_by_mode.iteritems( ): legduration = CommonHelper.str_to_DateTimeDuration( legduration) # convert to datetime if legmode == 'BICYCLE': legduration = CommonHelper.multiply_duration( legduration, duration_coeff) #ebike_alt.add_duration(leg['mode'], legduration) # for now 'total' trip duration is NOT calculated in this function total_legs_duration += legduration #ebike_alt.add_duration(intermediateMode, ebike_alt.duration - total_legs_duration) #if duration from OTP and duration computated here do not match ebike_alt.duration = total_legs_duration ebike_alt.duration_by_mode['BICYCLE'] = datetime.timedelta( 0) ebike_alt.add_duration('EBICYCLE', ebike_alt.duration) # for leg in ebike_alt.legs: # if leg['mode'] == 'BICYCLE': # leg['mode'] = 'EBICYCLE' # leg['duration'] = leg['duration'] * duration_coeff # # # calculate total duration of trip and leg-durations # for leg in ebike_alt.legs: # legduration = leg['duration'] # ebike_alt.add_duration(leg['mode'], legduration) # for now 'total' trip duration is NOT calculated in this function # total_legs_duration += legduration # #ebike_alt.add_duration(intermediateMode, ebike_alt.duration - total_legs_duration) #if duration from OTP and duration computated here do not match # ebike_alt.duration = total_legs_duration ebike_alt.endtime = ebike_alt.starttime + ebike_alt.duration break # add the computed ebike to alternative list if ebike_alt is not None: trip.alternative_trips.append(ebike_alt) ebike_trips.append(ebike_alt) return trips, ebike_trips