Example #1
0
def deviationrecords( request ):

    trip_id = request.GET['trip_id']
    stus = StopTimeUpdate.objects.all().filter(trip__trip_id=trip_id).order_by("fetch_timestamp").select_related('trip')

    # buckets dict of (trip_id, start_date) -> stu
    buckets = {}

    for stu in stus:
        if (stu.trip_id, stu.start_date) not in buckets:
            buckets[(stu.trip_id, stu.start_date)] = []

        # make the stoptimeupdate into a jsonable dict
        obj = stu.to_jsonable()

        trip_start_dt = build_datetime( stu.start_date, stu.trip.start_time )
       
        # datetime for the data
        data_dt = datetime.fromtimestamp( stu.data_timestamp )

        # time of the stoptimeupdate with respect to when the trip starts 
        time_since_trip_start = data_dt - trip_start_dt
        obj['time_since_trip_start'] = time_since_trip_start.days*24*3600 + time_since_trip_start.seconds

        buckets[(stu.trip_id, stu.start_date)].append( obj )

    return HttpResponse( json.dumps( buckets.items(), indent=2 ), mimetype="text/plain" )
Example #2
0
def deviationrecords(request):

    trip_id = request.GET['trip_id']
    stus = StopTimeUpdate.objects.all().filter(trip__trip_id=trip_id).order_by(
        "fetch_timestamp").select_related('trip')

    # buckets dict of (trip_id, start_date) -> stu
    buckets = {}

    for stu in stus:
        if (stu.trip_id, stu.start_date) not in buckets:
            buckets[(stu.trip_id, stu.start_date)] = []

        # make the stoptimeupdate into a jsonable dict
        obj = stu.to_jsonable()

        trip_start_dt = build_datetime(stu.start_date, stu.trip.start_time)

        # datetime for the data
        data_dt = datetime.fromtimestamp(stu.data_timestamp)

        # time of the stoptimeupdate with respect to when the trip starts
        time_since_trip_start = data_dt - trip_start_dt
        obj['time_since_trip_start'] = time_since_trip_start.days * 24 * 3600 + time_since_trip_start.seconds

        buckets[(stu.trip_id, stu.start_date)].append(obj)

    return HttpResponse(json.dumps(buckets.items(), indent=2),
                        mimetype="text/plain")
Example #3
0
    def set_vehicle_dist_along_route(self, shape, shapelen, first_stoptime):

        scheduled_start_dt = build_datetime(self.start_date, first_stoptime.departure_time)

        for vp in self.vps:
            scheddiff = vp.data_time - scheduled_start_dt
            vp.time_since_start = scheddiff.days * 3600 * 24 + scheddiff.seconds + scheddiff.microseconds / 1.0e6

            vp.percent_along_route = shape.project(vp.shape, normalized=True)
            vp.dist_along_route = vp.percent_along_route * shapelen
Example #4
0
    def set_vehicle_dist_along_route( self, shape, shapelen, first_stoptime ):

        scheduled_start_dt = build_datetime( self.start_date, first_stoptime.departure_time )

        for vp in self.vps:
            scheddiff  = vp.data_time - scheduled_start_dt
            vp.time_since_start = scheddiff.days*3600*24 + scheddiff.seconds + scheddiff.microseconds/1.0e6

            vp.percent_along_route = shape.project( vp.shape, normalized=True )
            vp.dist_along_route = vp.percent_along_route*shapelen
Example #5
0
    def set_vehicle_position_deviation_metadata( self, shape, stoptimes ):
        # adds a 'sched_deviation' property to each vehicle position in 'vps'
        # in the process it writes all over all vps and stoptime instances

        for stoptime in stoptimes:
            stoptime.percent_along_route = shape.project( stoptime.stop.shape, normalized=True )

        for vp in self.vps:
            vp.percent_along_route = shape.project( vp.shape, normalized=True )
            vp.scheduled_time = self._time_at_percent_along_route( stoptimes, vp.percent_along_route ) # seconds since midnight; can go over 24 hours
            vp.scheduled_time_str = gtfs_timestr( vp.scheduled_time )

            scheduled_time_dt = build_datetime( vp.start_date, vp.scheduled_time )
             
            scheddiff  = vp.data_time - scheduled_time_dt
            vp.sched_deviation = scheddiff.days*3600*24 + scheddiff.seconds + scheddiff.microseconds/1.0e6
Example #6
0
    def set_vehicle_position_deviation_metadata(self, shape, stoptimes):
        # adds a 'sched_deviation' property to each vehicle position in 'vps'
        # in the process it writes all over all vps and stoptime instances

        for stoptime in stoptimes:
            stoptime.percent_along_route = shape.project(stoptime.stop.shape, normalized=True)

        for vp in self.vps:
            vp.percent_along_route = shape.project(vp.shape, normalized=True)
            vp.scheduled_time = self._time_at_percent_along_route(
                stoptimes, vp.percent_along_route
            )  # seconds since midnight; can go over 24 hours
            vp.scheduled_time_str = gtfs_timestr(vp.scheduled_time)

            scheduled_time_dt = build_datetime(vp.start_date, vp.scheduled_time)

            scheddiff = vp.data_time - scheduled_time_dt
            vp.sched_deviation = scheddiff.days * 3600 * 24 + scheddiff.seconds + scheddiff.microseconds / 1.0e6