gtfs

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2021 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package gtfs provides gtfs related CRUD functionality, including DataSet and related operations

Index

Constants

View Source
const (
	MaximumScheduleSeconds int = 60 * 60 * 30
)

Variables

This section is empty.

Functions

func Get12AmTime

func Get12AmTime(date time.Time) time.Time

func GetActiveServiceIds added in v0.1.3

func GetActiveServiceIds(db *sqlx.DB, dataSet *DataSet, serviceDate time.Time) ([]string, error)

GetActiveServiceIds retrieves the active serviceIds on provided serviceDate. both calendar and calendar_date are used

func GetScheduledTripIds added in v0.1.3

func GetScheduledTripIds(db *sqlx.DB,
	at time.Time,
	relevantFrom time.Time,
	relevantTo time.Time) (map[string]bool, error)

GetScheduledTripIds returns all map of trip_ids that are scheduled between relevantFrom and relevantTo at is used to retrieve the active dataSet

func GetShapes

func GetShapes(db *sqlx.DB,
	dataSetId int64,
	shapeIds []string) (map[string][]*Shape, []string, error)

GetShapes collects Shape collections and returns results in ShapePtSequence order inside a map returns:

map with results keyed by shapeIds,
slice of missing shapeIds (where no Shape records could be found)

func GetStopTimeInstances

func GetStopTimeInstances(db *sqlx.DB,
	scheduleSlices []ScheduleSlice,
	dataSetId int64,
	tripIds []string) (map[string][]*StopTimeInstance, []string, []string, error)

GetStopTimeInstances collects StopTimeInstances and returns in order by tripID inside a map ArrivalDateTime and DepartureDateTime are populated from the best ScheduleSlice match from the trips first arrival time. If a ScheduleSlice match can't be found the StopTimeInstances are not included in the map result returns:

map with results keyed by tripId,
slice of missing trip ids (where no StopTimeInstances could be found)
slice of trip ids where no matching ScheduleSlice could be found for the trip

func MakeScheduleTime

func MakeScheduleTime(timeAt12 time.Time, scheduleSeconds int) time.Time

MakeScheduleTime produces a time from by adding seconds to a 12am date. Takes into account day light saving time

func RecordCalendar

func RecordCalendar(calendar *Calendar, dsTx *DataSetTransaction) error

func RecordCalendarDate

func RecordCalendarDate(calendarDate *CalendarDate, dsTx *DataSetTransaction) error

func RecordObservedStopTime

func RecordObservedStopTime(observation *ObservedStopTime, db *sqlx.DB) error

RecordObservedStopTime saves ObservedStopTime into database

func RecordShapes

func RecordShapes(shapes []*Shape, dsTx *DataSetTransaction) error

RecordShapes saves shapes to database in a batch

func RecordStopTimes

func RecordStopTimes(stopTimes []*StopTime, dsTx *DataSetTransaction) error

RecordStopTimes saves stopTimes to database in batch

func RecordTripDeviation added in v0.1.3

func RecordTripDeviation(tripDeviations []*TripDeviation, db *sqlx.DB) error

RecordTripDeviation saves slice of TripDeviations into database in batch

func RecordTrips

func RecordTrips(trips []*Trip, dsTx *DataSetTransaction) error

RecordTrips saves trips to database in batch

func SaveAndTerminateReplacedDataSet

func SaveAndTerminateReplacedDataSet(tx *sqlx.Tx, ds *DataSet, now time.Time) error

SaveAndTerminateReplacedDataSet updates all DataSet where now is between DataSet.SavedAt and DataSet.ReplacedAt and sets DataSet.ReplacedAt to one microsecond before now. ds is then saved with now as DataSet.SavedAt and the default DataSet.ReplacedAt date of 9999-12-31

func SaveDataSet

func SaveDataSet(tx *sqlx.Tx, ds *DataSet) error

SaveDataSet saves new or updates existing DataSets.

Types

type Calendar

type Calendar struct {
	DataSetId int64  `db:"data_set_id"`
	ServiceId string `db:"service_id"`
	Monday    int
	Tuesday   int
	Wednesday int
	Thursday  int
	Friday    int
	Saturday  int
	Sunday    int
	StartDate *time.Time `db:"start_date"`
	EndDate   *time.Time `db:"end_date"`
}

Calendar contains data from a record in a gtfs calendar.txt file

type CalendarDate

type CalendarDate struct {
	DataSetId     int64  `db:"data_set_id"`
	ServiceId     string `db:"service_id"`
	Date          time.Time
	ExceptionType int `db:"exception_type"`
}

CalendarDate contains data from a record in a gtfs calendar_dates.txt file

type DataSet

type DataSet struct {
	Id  int64
	URL string
	// ETag is the ETag header if available from the source web site for the gtfs file. Is empty if not available
	ETag string `db:"e_tag"`
	// LastModifiedTimestamp is the unix epoch seconds the source web site provided for the last time the gtfs file was modified
	// is 0 if not available
	LastModifiedTimestamp int64      `db:"last_modified_timestamp"`
	DownloadedAt          time.Time  `db:"downloaded_at"`
	SavedAt               *time.Time `db:"saved_at"`
	ReplacedAt            *time.Time `db:"replaced_at"`
}

DataSet encompasses a gtfs schedule available from a source at a point in time. The same source will be loaded over time. Each record from a gtfs file shares the DataSet.Id value as part of the primary key.

func GetAllDataSets

func GetAllDataSets(db *sqlx.DB) ([]DataSet, error)

GetAllDataSets retrieves all DataSets currently loaded

func GetDataSet

func GetDataSet(db *sqlx.DB, dataSetId int64) (*DataSet, error)

GetDataSet retrieves DataSet with dataSetId

func GetDataSetAt

func GetDataSetAt(db *sqlx.DB, at time.Time) (*DataSet, error)

GetDataSetAt retrieves the DataSet that was active at a time

func GetLatestDataSet

func GetLatestDataSet(db *sqlx.DB) (*DataSet, error)

GetLatestDataSet retrieves the latest DataSet that is active

func (DataSet) String

func (d DataSet) String() string

type DataSetOperation

type DataSetOperation struct {
	DS *DataSet
	Db *sqlx.DB
}

DataSetOperation contains required data for operating on gtfs records owned by a DataSet

type DataSetTransaction

type DataSetTransaction struct {
	DS DataSet
	Tx *sqlx.Tx
}

DataSetTransaction contains required data for recording new gtfs records owned by a DataSet

type ObservedStopTime

type ObservedStopTime struct {
	//ObservedTime is the time the vehicle movement was seen
	ObservedTime time.Time `db:"observed_time"`
	//StopId is the stopId the vehicle moved from
	StopId string `db:"stop_id"`
	//NextStopId is the stopId the vehicle moved to
	NextStopId string `db:"next_stop_id"`
	VehicleId  string `db:"vehicle_id"`

	RouteId string `db:"route_id"`
	//ObservedAtStop is true when a gtfs-rt vehicle record indicated the vehicle was located at the stop the vehicle moved from
	ObservedAtStop bool `db:"observed_at_stop"`

	//ObservedAtNextStop is true when a gtfs-rt vehicle record indicated the vehicle was located at the stop the vehicle moved to
	ObservedAtNextStop bool `db:"observed_at_next_stop"`

	//TravelSeconds is the number of seconds the vehicle is assumed to have taken to move between the stops
	TravelSeconds    int  `db:"travel_seconds"`
	ScheduledSeconds *int `db:"scheduled_seconds"`
	//DataSetId identifies the DataSet used during this ObservedStopTime
	DataSetId int64     `db:"data_set_id" json:"data_set_id"`
	TripId    string    `db:"trip_id"`
	CreatedAt time.Time `db:"created_at"`
}

ObservedStopTime contains details when a vehicle is observed to have transitioned between two stops, or assumed to have passed a two stops based on the subsequent vehicle positions indicating it passed two or more stops on a trip, in which case the travel time is interpolated primary key consists of ObservedTime, StopId, NextStopId, VehicleId

func (*ObservedStopTime) AssumedDepartTime

func (ost *ObservedStopTime) AssumedDepartTime() int

AssumedDepartTime returns the time the vehicle is assumed to have departed the from stopId, this is calculated based on the last time the vehicle was observed at or before the from stopId

type ScheduleSlice

type ScheduleSlice struct {
	ServiceDate  time.Time
	StartSeconds int
	EndSeconds   int
}

ScheduleSlice contains a service date and a section of service time

func GetScheduleSlices

func GetScheduleSlices(start time.Time, end time.Time) []ScheduleSlice

GetScheduleSlices produces array of schedule slices based on start and end times

type Shape

type Shape struct {
	DataSetId         int64    `db:"data_set_id" json:"data_set_id"`
	ShapeId           string   `db:"shape_id" json:"shape_id"`
	ShapePtLat        float64  `db:"shape_pt_lat" json:"shape_pt_lat"`
	ShapePtLng        float64  `db:"shape_pt_lon" json:"shape_pt_lon"`
	ShapePtSequence   int      `db:"shape_pt_sequence" json:"shape_pt_sequence"`
	ShapeDistTraveled *float64 `db:"shape_dist_traveled" json:"shape_dist_traveled"`
}

Shape contains rows from the GTFS shapes.txt file

type StopTime

type StopTime struct {
	DataSetId         int64   `db:"data_set_id" json:"data_set_id"`
	TripId            string  `db:"trip_id" json:"trip_id"`
	StopSequence      uint32  `db:"stop_sequence" json:"stop_sequence"`
	StopId            string  `db:"stop_id" json:"stop_id"`
	ArrivalTime       int     `db:"arrival_time" json:"arrival_time"`
	DepartureTime     int     `db:"departure_time" json:"departure_time"`
	ShapeDistTraveled float64 `db:"shape_dist_traveled" json:"shape_dist_traveled"`
	Timepoint         int     `db:"timepoint" json:"timepoint"`
}

StopTime contains a record from a gtfs stop_times.txt file represents a scheduled arrival and departure at a stop.

type StopTimeInstance

type StopTimeInstance struct {
	StopTime
	FirstStop         bool `json:"first_stop"`
	ArrivalDateTime   time.Time
	DepartureDateTime time.Time
}

type Trip

type Trip struct {
	DataSetId     int64   `db:"data_set_id" json:"data_set_id"`
	TripId        string  `db:"trip_id" json:"trip_id"`
	RouteId       string  `db:"route_id" json:"route_id"`
	ServiceId     string  `db:"service_id" json:"service_id"`
	TripHeadsign  *string `db:"trip_headsign" json:"trip_headsign"`
	TripShortName *string `db:"trip_short_name" json:"trip_short_name"`
	BlockId       string  `db:"block_id" json:"block_id"`
	ShapeId       string  `db:"shape_id" json:"shape_id"`
	StartTime     int     `db:"start_time" json:"start_time"`
	EndTime       int     `db:"end_time" json:"end_time"`
	TripDistance  float64 `db:"trip_distance" json:"trip_distance"`
}

Trip contains data from a gtfs trip definition in a trips.txt file

type TripDeviation added in v0.1.3

type TripDeviation struct {
	Id                 int64
	CreatedAt          time.Time `db:"created_at" json:"created_at"`
	DeviationTimestamp time.Time `db:"deviation_timestamp" json:"deviation_timestamp"`
	//TripProgress is the distance of the trip that has been traversed.
	//a negative number indicates a position on a prior trip to this one
	TripProgress float64 `db:"trip_progress" json:"trip_progress"`
	//DataSetId identifies the DataSet used when this TripDeviation was calculated
	DataSetId int64  `db:"data_set_id" json:"data_set_id"`
	TripId    string `db:"trip_id" json:"trip_id"`
	VehicleId string `db:"vehicle_id" json:"vehicle_id"`
	AtStop    bool   `db:"at_stop" json:"at_stop"`
	Delay     int    `db:"delay"`
}

type TripInstance

type TripInstance struct {
	Trip
	StopTimeInstances []*StopTimeInstance `json:"stop_time_instances"`
	Shapes            []*Shape            `json:"shapes"`
}

func (*TripInstance) ShapesBetweenDistances

func (t *TripInstance) ShapesBetweenDistances(start float64, end float64) []*Shape

ShapesBetweenDistances returns slice of Shapes where Shape.ShapeDistTraveled is between start and end

type TripInstanceBatchQueryResults

type TripInstanceBatchQueryResults struct {
	TripInstancesByTripId   map[string]*TripInstance
	MissingTripIds          []string
	ScheduleSliceOutOfRange []string
	MissingShapeIds         []string
}

TripInstanceBatchQueryResults provides results from batch querying trips tripIds that were not found (MissingTripIds) or where the schedule time was outside of date range (ScheduleSliceOutOfRange) can be logged

func GetTripInstances

func GetTripInstances(db *sqlx.DB,
	at time.Time,
	relevantFrom time.Time,
	relevantTo time.Time,
	tripIds []string) (*TripInstanceBatchQueryResults, error)

GetTripInstances loads trip instances with tripIds. Appropriate scheduleDates are selected where trip start and end times are within range of relevantFrom and relevantTo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL