dbqueries
index
/Users/colin/colin_dev/transit/gps2gtfs/core/src/dbqueries.py

Database query interface, for use by all other pieces of the software.
This should allow easier db-specific or dbms-specific modifications.

 
Modules
       
datetime
posixpath
sys

 
Functions
       
correct_gps_schedule(segment_id, trip_id, gtfs_error, offset_seconds, gps_data)
export_gps_route(trip_id, trip_date, segment_id, vehicle_id, gtfs_error, offset_seconds, gps_data, segment_exists=False)
Writes the given entry to the "tracked_routes" table. This table is used
to cache the results of finding and filtering only the valid routes as
represented in the GPS dataset.
 
trip_id: the GTFS trip id
trip_date: the date of the trip
segment_id: unique identifier for this GPS segment (see getMaxSegID())
vehicle_id: as reported in the GPS data
gtfs_error: The distance from the matched GTFS trip as measured by
            the GPSBusTrack metric
offset_seconds: Number of seconds to subtract from GTFS trip to normalize.
 
gps_data: A list of (lat, lon, reported_update_time) values, exactly as
          reported in the GPS dat. Note that reported_update_time should
          be a timestamp.
 
WARNING: No effort is made to prevent duplicate entries! If you do this
more than once for the same route then YOU MUST DELETE IT FIRST!
export_gps_schedule(segment_id, schedule)
Given a gps segment ID and a list of dictlike rows having the keys:
  'stop_id', GTFS stop ID
  'stop_sequence', GTFS trip stop sequence
  'stop_headsign', 
  'pickup_type',
  'drop_off_type',
  'shape_dist_traveled',
  'timepoint',
  'arrival_time_seconds', time in seconds into day
  'departure_time_seconds', time in seconds into day
  'actual_arrival_time_seconds', actual arrival in seconds into day
  'actual_departure_time_seconds' actual departure in seconds into day
  'seconds_since_last_stop' time elapsed since arrival at previous stop
  
exports the rows appropriately into the database.
export_trip_information(trip_id, first_arrive, first_depart, trip_length, trip_duration)
Given a GTFS trip ID, the time (in seconds) of its first arrival and
departure, the length of the whole trip in meters, and the duration
of the trip in seconds, pushes it to the database.
export_trip_stop_information(trip_id, stop_sequence, stop_number, prev_stop_distance, cumulative_distance, prev_stop_travel_time)
Given a GTFS trip ID and stop sequence number, the number of this stop 
along the route, the distance from the previous stop to this stop in meters,
the cumulative distance from the beginning of the trip to this stop in
meters, and the time in seconds it takes (according to the schedule) to
get to this stop from the previous stop, pushes it to the database.
getGTFSRouteData(route_id)
Given a route ID, returns a row from the GTFS table as a dict-like type.
Keys:
'route_long_name',
'route_type',
'route_text_color',
'route_color',
'agency_id',
'route_id',
'route_url',
'route_desc',
'route_short_name'
getGTFSTripData(trip_id)
Given a trip ID, returns (trip_header, trip, shape),
where trip_header is a row from the GTFS table,
Keys:
'block_id',
'route_id',
'direction_id',
'trip_headsign',
'shape_id',
'service_id',
'trip_id',
'trip_short_name'
 
trip is a list of rows holding info about each stop along the route
(The order of the rows in trip is the order of the stops along the route),
Keys:
'stop_sequence',
'parent_station',
'trip_id',
'pickup_type',
'stop_headsign',
'arrival_time_seconds',
'stop_name',
'departure_time_seconds',
'stop_timezone',
'timepoint',
'arrival_time',
'stop_url',
'stop_id',
'stop_desc',
'location_type',
'departure_time',
'stop_code',
'stop_lat',
'zone_id',
'stop_lon',
'shape_dist_traveled',
'drop_off_type'
 
and shape is a list of rows holding info about the geometry traversed by
the route.
Keys:
'shape_pt_lat',
'shape_id',
'shape_pt_lon',
'shape_pt_sequence',
'shape_dist_traveled'
getMaxSegID()
Returns the largest vehicle segment ID found in the "tracked_routes"
table (see the export_gps_route function below). This can be used to
construct a unique ID for further segments.
 
Note that there should be a 1-1 correspondence between segment and
trip IDs for any particular service day. Of course part of the point
of this is to eliminate any cases where this is not true in a meaningful
way.
get_all_trip_ids()
get_best_matching_trip_ID(route_id, dir_id, start_date, start_time, num_results=1)
Given GTFS route and direction IDs, a datetime.date object representing
the day of the route, and start_time an integer representing the begin 
time of the route as seconds into the day, returns a list of the
num_results best matching GTFS trips, in order of best matching start
datetime.
Each item in the list is of the form:
  (trip_id, offset_seconds)
where trip_id is the GTFS trip ID, and offset_seconds represents the
offset (in seconds) that need be applied to the trip for any times 
in the format of "seconds into day." This is necessary because
sometimes the best matching trip will come from the previous day's
service schedule, and so you will need to subtract 1 day = 86400 seconds
from the times for that bus.
 
For example, a bus running at 3am might be from today's schedule at
03:00, or it might be from yesterday's schedule at 27:00. If a match
is found for 03:00 today, then the offset returned is 0. If a match
is found for 27:00 yesterday, then the offset returned is 86400.
get_direction_for_dirtag(dirtag)
Given a nextbus dirtag, returns the GTFS direction ID
get_previous_trip_ID(trip_id, start_date, offset, numtrips=10)
Given GTFS trip ID, the date it ran on, and the schedule's offset in seconds,
finds the immediately previous GTFS trip ID with the same direction and 
route. The start_date is necessary in cases close to midnight.
get_route_dirtags(route_short_name)
Given the short name for a route, returns a list of dirtags
which are associated with that route.
get_route_for_dirtag(dirtag)
Given a nextbus dirtag, returns the GTFS route ID
get_route_names()
get_segment_IDs(scheduled_only=True)
get_serviceIDs_for_date(date)
Given a datetime.date object, returns a list of GTFS service ID's
active for that date.
get_shapes_for_route(route_short_name)
Given a route short name, returns a list of dictlike rows
containing the shapes associated with that route, sorted in
order of ascending shape ID then ascending shape point sequence.
Keys:
'shape_id',
'shape_pt_lat',
'shape_pt_lon',
'shape_pt_sequence',
'shape_dist_traveled',
'dirtag'
get_vehicle_reports(dirtags)
Given a list of dirtags, returns a list of dictlike rows of 
vehicle tracking reports, sorted in ascending order of update time.
Keys:
'id',
'lat',
'lon',
'routetag',
'dirtag',
'reported_update_time'
load_gps_route(segment_id)
Given a segment ID, loads the associated trip from the tracked_routes table
in order of increasing report time. 
 
Returns (trip_id, trip_date, vehicle_id, schedule_error, offset, route)
where trip_id is the gtfs trip ID, trip_date is the date on whose schedule
the trip took place, vehicle_id is the gps vehicle's ID, schedule_error
is the measured error between the GPS route and the GTFS schedule, offset 
is the number of seconds to substract from any GTFS schedule times, and
route is a list of [lat,lon,reported_update_time] triples.
load_gps_schedule(segment_id)
Given a segment_id, loads the corresponding arrival schedule from the
gps_stop_times table in the database.
 
Returns a list of dictlike rows, each with the following keys:
    'stop_id'
    'stop_sequence'
    'stop_headsign'
    'pickup_type'
    'drop_off_type',
    'shape_dist_traveled',
    'timepoint',
    'arrival_time_seconds',
    'departure_time_seconds',
    'actual_arrival_time_seconds'
    'seconds_since_last_stop'
 
The rows will be in order of increasing stop sequence.
load_gps_segment_header(segment_id)
Given a segment ID, returns:
   (trip_id,trip_date,vehicle_id,schedule_error,offset)
where trip_id is the gtfs trip ID, trip_date is the date on whose schedule
the trip took place, vehicle_id is the gps vehicle's ID, schedule_error
is the measured error between the GPS route and the GTFS schedule, and 
offset is the number of seconds to substract from any GTFS schedule times.
populate_routeid_dirtag(deletefirst=False)
Populates routeid_dirtag table with all distinct instances of 
(routeid,dirtag) from the vehicle tracking data table joined with
the gtfs routes table.
If deletefirst is true, then the routeid_dirtag table is truncated
first.
update_routes(vehicle_data)
Inserts GPS tracking data into the database.
Vehicle data is a list of dicts mapping attributes to values.
Attributes can be the following:
 
  id -- the vehicle id (string)
  routeTag -- the route id (string)
  dirTag -- identifier for the route direction (string)
  lat -- latitude of the vehicle (float)
  lon -- longitude of the vehicle (float)
  secsSinceReport -- how old the data was upon recording time (integer)
  predictable -- i have no idea what this field means, ask nextbus (string)
  heading -- the direction the bus is moving (integer -- degrees?)
  update_time -- the recording time (when this data was retrieved) (timestamp)
 
Of the above, update_time and secsSinceReport are required. If you
don't have any value for secsSinceReport then put in a 0. If you don't
have any value for update_time then what exactly are you trying to
accomplish?

 
Data
        SDHandler = <ServiceDateHandler.ServiceDateHandler object at 0x7e2e30>
dbdir = '/Library/Frameworks/Python.framework/Versions/Current/bin/../../common/src/'
mydir = '/Library/Frameworks/Python.framework/Versions/Current/bin'