models

package
v0.0.0-...-f887610 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2018 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Tram int = iota
	Subway
	Rail
	Bus
	Ferry
	CableCar
	Gondola
	Funicular
)
View Source
const (
	// Possible values for exception_type
	ServiceAdded   = 1
	ServiceRemoved = 2
)
View Source
const (

	// maximum departures to return in a result
	MaxDepartures = 6
)

Variables

View Source
var (
	// ErrNoID is returned by model constructors when no ID is provided
	ErrNoID = errors.New("no ID provided")

	// ErrInvalidRouteType is returned by NewRoute when the route_type
	// cannot be found
	ErrInvalidRouteType = errors.New("invalid route_type")

	// ErrNotFound is returned when something can't be found in a
	// Get call
	ErrNotFound = errors.New("not found")
)

Functions

func DeleteFakeShapes

func DeleteFakeShapes(db sqlx.Ext) error

DeleteFakeShapes removes all existing fake shapes. Typically this should be used in a transaction in conjuction with to rebuild the data via GetFakeShapes

func DeleteRouteShapes

func DeleteRouteShapes(db sqlx.Ext) error

DeleteRouteShapes removes all existing route / fake shapes. Typically this should be used in a transaction in conjuction with GetRouteShapes to rebuild the data

func GetAgencyServiceIDs

func GetAgencyServiceIDs(db sqlx.Ext, agencyIDs []string, day string, now time.Time) (serviceIDs []string, err error)

GetAgencyServiceIDs returns all possible serviceIDs for this day / time / agency for the initial query. However, these values may be later filtered by getRouteServiceIDs

func GetPartialTripIDMatch

func GetPartialTripIDMatch(db sqlx.Ext, agencyID, routeID, partialTripID string) (tripID string, err error)

GetPartialTripIDMatch returns the first trip_id that is "like" the partialTripID sent in Hack for MTA NYCT train API: https://github.com/brnstz/bus/issues/63

Types

type Departure

type Departure struct {
	Time         time.Time `json:"time" db:"-"`
	DepartureSec int       `json:"-" db:"departure_sec"`
	TripID       string    `json:"trip_id" db:"trip_id"`
	ServiceID    string    `json:"service_id" db:"service_id"`
	Live         bool      `json:"live" db:"-" upsert:"omit"`

	// CompassDir is the direction to the next stop
	CompassDir float64 `json:"compass_dir" db:"-" upsert:"omit"`
	// contains filtered or unexported fields
}

func (*Departure) Initialize

func (d *Departure) Initialize() error

type FakeShape

type FakeShape struct {
	AgencyID    string `json:"-" db:"agency_id" upsert:"key"`
	RouteID     string `json:"-" db:"route_id" upsert:"key"`
	DirectionID int    `json:"-" db:"direction_id" upsert:"key"`
	Headsign    string `json:"-" db:"headsign" upsert:"key"`
	Seq         int    `json:"-" db:"seq" upsert:"key"`

	Lat null.Float `json:"lat" db:"lat"`
	Lon null.Float `json:"lon" db:"lon"`

	// Location is PostGIS field value that combines lat and lon into a single
	// field.
	Location interface{} `json:"-" db:"location" upsert_value:"ST_SetSRID(ST_MakePoint(:lat, :lon),4326)"`

	TripID  string `json:"-" db:"trip_id" upsert:"omit"`
	ShapeID string `json:"-" db:"shape_id" upsert:"omit"`
}

func GetFakeRouteShapes

func GetFakeRouteShapes(db sqlx.Ext) ([]*FakeShape, error)

GetFakeRouteShapes returns fake shapes for agency/route/headsign/direction combos that don't have a shape

func (*FakeShape) Save

func (fs *FakeShape) Save(db sqlx.Ext) error

Save saves a fake shape to the database

func (*FakeShape) Table

func (s *FakeShape) Table() string

type HereQuery

type HereQuery struct {
	// The southwest and northeast bounding points of the box we are
	// searching
	SWLat float64 `db:"sw_lat"`
	SWLon float64 `db:"sw_lon"`
	NELat float64 `db:"ne_lat"`
	NELon float64 `db:"ne_lon"`

	// The midpoint of our search box
	MidLat float64 `db:"mid_lat"`
	MidLon float64 `db:"mid_lon"`

	LineString  string `db:"line_string"`
	PointString string `db:"point_string"`

	YesterdayDepartureMin  int
	YesterdayDepartureMax  int
	YesterdaySecDiff       int
	YesterdayDepartureBase time.Time
	YesterdayServiceIDs    []string
	YesterdayRelevantIDs   map[string]bool

	TodayDepartureMin  int
	TodayDepartureMax  int
	TodayDepartureBase time.Time
	TodayServiceIDs    []string
	TodayRelevantIDs   map[string]bool

	TomorrowDepartureMin  int
	TomorrowDepartureMax  int
	TomorrowSecDiff       int
	TomorrowDepartureBase time.Time
	TomorrowServiceIDs    []string
	TomorrowRelevantIDs   map[string]bool

	Limit int `db:"limit"`

	Query string
}

func NewHereQuery

func NewHereQuery(lat, lon, swlat, swlon, nelat, nelon float64, routeTypes []int, now time.Time) (hq *HereQuery, err error)

type HereResult

type HereResult struct {
	AgencyID  string `db:"agency_id"`
	RouteID   string `db:"route_id"`
	StopID    string `db:"stop_id"`
	ServiceID string `db:"service_id"`

	TripIDs       string `db:"trip_ids"`
	ArrivalSecs   string `db:"arrival_secs"`
	DepartureSecs string `db:"departure_secs"`
	StopSequences string `db:"stop_sequences"`
	NextStopLats  string `db:"next_stop_lats"`
	NextStopLons  string `db:"next_stop_lons"`

	TripHeadsign string `db:"trip_headsign"`

	StopName     string  `db:"stop_name"`
	StopHeadsign string  `db:"stop_headsign"`
	DirectionID  int     `db:"direction_id"`
	Lat          float64 `db:"lat"`
	Lon          float64 `db:"lon"`
	Dist         float64 `db:"dist"`

	RouteType      int    `db:"route_type"`
	RouteColor     string `db:"route_color"`
	RouteTextColor string `db:"route_text_color"`
	RouteShortName string `db:"route_short_name"`
	RouteLongName  string `db:"route_long_name"`

	HQ *HereQuery

	Stop  *Stop
	Route *Route

	Departures []*Departure
}

func (*HereResult) Initialize

func (h *HereResult) Initialize() error

type Route

type Route struct {
	RouteID   string `json:"route_id" db:"route_id" upsert:"key"`
	AgencyID  string `json:"agency_id" db:"agency_id" upsert:"key"`
	Type      int    `json:"route_type" db:"route_type"`
	TypeName  string `json:"route_type_name" db:"-" upsert:"omit"`
	Color     string `json:"route_color" db:"route_color"`
	TextColor string `json:"route_text_color" db:"route_text_color"`
	ShortName string `json:"route_short_name" db:"route_short_name"`
	LongName  string `json:"route_long_name" db:"route_long_name"`

	UniqueID    string        `json:"unique_id" db:"-" upsert:"omit"`
	RouteShapes []*RouteShape `json:"route_shapes" upsert:"omit"`
}

Route is https://developers.google.com/transit/gtfs/reference#routestxt

func GetAllRoutes

func GetAllRoutes(db sqlx.Ext, agencyID string) (routes []*Route, err error)

func GetPreloadRoutes

func GetPreloadRoutes(db sqlx.Ext, agencyIDs []string) (routes []*Route, err error)

test func for static json file

func GetRoute

func GetRoute(db sqlx.Ext, agencyID, routeID string) (r *Route, err error)

GetRoute returns a Route with the given ID

func NewRoute

func NewRoute(id string, rtype int, color, textColor, agencyID, shortName, longName string) (r *Route, err error)

NewRoute creates a Route given incoming data, typically from a routes.txt file

func (*Route) Initialize

func (r *Route) Initialize() (err error)

init ensures any derived values are correct after creating/loading an object

func (*Route) Save

func (r *Route) Save() error

Save saves a route to the database

func (*Route) Table

func (r *Route) Table() string

Table returns the table name for the Route struct, implementing the upsert.Upserter interface

type RouteShape

type RouteShape struct {
	AgencyID    string `json:"agency_id" db:"agency_id" upsert:"key"`
	RouteID     string `json:"route_id" db:"route_id" upsert:"key"`
	Headsign    string `json:"headsign" db:"headsign" upsert:"key"`
	DirectionID int    `json:"direction_id" db:"direction_id" upsert:"key"`
	ShapeID     string `json:"shape_id" db:"shape_id"`

	Count  int    `json:"-" db:"count" upsert:"omit"`
	TripID string `json:"-" db:"trip_id" upsert:"omit"`

	Shapes []*Shape `json:"shapes" db:"-" upsert:"omit"`
}

RouteShape is a one-to-many mapping between routes and shapes. The loader should identify which shapes represent the typical / full route and create/save these objects

func GetRouteShapes

func GetRouteShapes(db sqlx.Ext) ([]*RouteShape, error)

GetRouteShapes returns distinct shapes for every route ordered by the "size" (number of points) of the route from least to most for each given combination of agency/route/headsign/direction. Given the ordering, you can Save() each value in a tx and end up with the "best" value live in the db

func GetSavedRouteShapes

func GetSavedRouteShapes(db sqlx.Ext, agencyID, routeID string) ([]*RouteShape, error)

GetSavedRouteShapes returns all shapes for this combination of agencyID and routeID

func (*RouteShape) Save

func (rs *RouteShape) Save(db sqlx.Ext) error

Save saves the route_shape to the db

func (*RouteShape) Table

func (rs *RouteShape) Table() string

Table returns the name of the RouteShape table, implementing the upsert.Upserter interface

type ScheduledStopTime

type ScheduledStopTime struct {
	AgencyID  string `db:"agency_id" upsert:"key"`
	RouteID   string `db:"route_id" upsert:"key"`
	StopID    string `db:"stop_id" upsert:"key"`
	ServiceID string `db:"service_id" upsert:"key"`
	TripID    string `db:"trip_id" upsert:"key"`

	ArrivalSec   int `db:"arrival_sec"`
	DepartureSec int `db:"departure_sec"`

	StopSequence int `db:"stop_sequence"`

	LastStop sql.NullBool `db:"last_stop"`

	NextStopID  sql.NullString `db:"next_stop_id"`
	NextStopLat null.Float     `json:"next_stop_lat" db:"next_stop_lat"`
	NextStopLon null.Float     `json:"next_stop_lon" db:"next_stop_lon"`

	// Location is PostGIS field value that combines lat and lon into a single
	// field.
	NextStopLocation interface{} `json:"-" db:"next_stop_location" upsert_value:"ST_SetSRID(ST_MakePoint(:next_stop_lat, :next_stop_lon),4326)"`
}

func NewScheduledStopTime

func NewScheduledStopTime(routeID, stopID, serviceID, arrivalStr, depatureStr, agencyID, tripID string, sequence int, lastStop bool) (sst *ScheduledStopTime, err error)

func (*ScheduledStopTime) Save

func (sst *ScheduledStopTime) Save() error

Save saves a scheduled_stop_time to the database

func (*ScheduledStopTime) String

func (s *ScheduledStopTime) String() string

func (*ScheduledStopTime) Table

func (sst *ScheduledStopTime) Table() string

type Service

type Service struct {
	ID      string
	RouteID string
}

Service is used by the Loader to map service IDs to route IDs

type ServiceRouteDay

type ServiceRouteDay struct {
	ServiceID string `db:"service_id" upsert:"key"`
	RouteID   string `db:"route_id" upsert:"key"`
	AgencyID  string `db:"agency_id" upsert:"key"`
	Day       string `db:"day" upsert:"key"`

	StartDate time.Time `db:"start_date" upsert:"key"`
	EndDate   time.Time `db:"end_date" upsert:"key"`
}

ServiceRouteDay defines what service ID is valid for this route ID on this Day of the week. Service IDs are valid for a period of time which is indicated by StartDate and EndDate. All fields in this object form a unique key.

func (*ServiceRouteDay) Save

func (s *ServiceRouteDay) Save() error

Save saves a ServiceRouteDay to the database

func (ServiceRouteDay) String

func (s ServiceRouteDay) String() string

String returns a text representation of the ServiceRouteDay

func (*ServiceRouteDay) Table

func (s *ServiceRouteDay) Table() string

Table implements the upsert.Upserter interface by defining the table we're saving to

type ServiceRouteException

type ServiceRouteException struct {
	AgencyID      string    `db:"agency_id" upsert:"key"`
	RouteID       string    `db:"route_id" upsert:"key"`
	ServiceID     string    `db:"service_id" upsert:"key"`
	ExceptionDate time.Time `db:"exception_date" upsert:"key"`
	ExceptionType int       `db:"exception_type"`
}

func (*ServiceRouteException) Save

func (s *ServiceRouteException) Save() error

Save saves a ServiceRouteDay to the database

func (*ServiceRouteException) Table

func (s *ServiceRouteException) Table() string

type Shape

type Shape struct {
	ID       string `json:"-" db:"shape_id" upsert:"key"`
	AgencyID string `json:"-" db:"agency_id" upsert:"key"`
	Seq      int    `json:"-" db:"seq" upsert:"key"`

	Lat null.Float `json:"lat" db:"lat"`
	Lon null.Float `json:"lon" db:"lon"`

	// Location is PostGIS field value that combines lat and lon into a single
	// field.
	Location interface{} `json:"-" db:"location" upsert_value:"ST_SetSRID(ST_MakePoint(:lat, :lon),4326)"`
}

func GetFakeShapePoints

func GetFakeShapePoints(db sqlx.Ext, agencyID, routeID, headsign string, directionID int) ([]*Shape, error)

func GetShapes

func GetShapes(db sqlx.Ext, agencyID, shapeID string) ([]*Shape, error)

func NewShape

func NewShape(id, agencyID string, seq int, lat, lon float64) (s *Shape, err error)

func (*Shape) Save

func (s *Shape) Save(db sqlx.Ext) error

Save saves a shape to the database

func (*Shape) Table

func (s *Shape) Table() string

type SortableDepartures

type SortableDepartures []*Departure

func (SortableDepartures) Len

func (d SortableDepartures) Len() int

func (SortableDepartures) Less

func (d SortableDepartures) Less(i, j int) bool

func (SortableDepartures) Swap

func (d SortableDepartures) Swap(i, j int)

type Stop

type Stop struct {
	StopID      string `json:"stop_id" db:"stop_id" upsert:"key"`
	RouteID     string `json:"route_id" db:"route_id" upsert:"key"`
	AgencyID    string `json:"agency_id" db:"agency_id" upsert:"key"`
	DirectionID int    `json:"direction_id" db:"direction_id" upsert:"key"`
	Name        string `json:"stop_name" db:"stop_name"`

	UniqueID string `json:"unique_id" db:"-" upsert:"omit"`

	Headsign string `json:"headsign" db:"headsign"`

	Lat null.Float `json:"lat" db:"lat"`
	Lon null.Float `json:"lon" db:"lon"`

	// Location is PostGIS field value that combines lat and lon into a single
	// field.
	Location interface{} `json:"-" db:"location" upsert_value:"ST_SetSRID(ST_MakePoint(:lat, :lon),4326)"`

	// info we steal from route when doing a here query
	RouteType      int    `json:"route_type" db:"-" upsert:"omit"`
	RouteTypeName  string `json:"route_type_name" db:"-" upsert:"omit"`
	RouteColor     string `json:"route_color" db:"-" upsert:"omit"`
	RouteTextColor string `json:"route_text_color" db:"-" upsert:"omit"`
	RouteShortName string `json:"route_short_name" db:"-" upsert:"omit"`
	RouteLongName  string `json:"route_long_name" db:"-" upsert:"omit"`

	DisplayName      string `json:"display_name" db:"-" upsert:"omit"`
	RouteAndHeadsign string `json:"route_and_headsign" db:"-" upsert:"omit"`
	JustHeadsign     string `json:"just_headsign" db:"-" upsert:"omit"`
	GroupExtraKey    string `json:"group_extra_key" db:"-" upsert:"omit"`
	TripHeadsign     string `json:"trip_headsign" db:"-" upsert:"omit"`

	Seq int `json:"seq" db:"stop_sequence" upsert:"omit"`

	FallbackTripID string `json:"fallback_trip_id" db:"-" upsert:"omit"`

	Dist       float64      `json:"dist,omitempty" db:"-" upsert:"omit"`
	Departures []*Departure `json:"departures,omitempty" db:"-" upsert:"omit"`
	Vehicles   []Vehicle    `json:"vehicles,omitempty" db:"-" upsert:"omit"`
}

Stop is a single transit stop for a particular route. If a stop serves more than one route, there are multiple distinct entries for that stop.

func GetHereResults

func GetHereResults(db sqlx.Ext, hq *HereQuery) (stops []*Stop, stopRoutes map[string]*Route, err error)

func GetStopsByTrip

func GetStopsByTrip(db sqlx.Ext, t *Trip) (stops []*Stop, err error)

func (*Stop) Initialize

func (s *Stop) Initialize() error

func (Stop) Key

func (s Stop) Key() string

Key() returns the unique string for this stop, so we can identify unique stops in the loader.

func (*Stop) Save

func (s *Stop) Save() error

Save saves a stop to the database

func (Stop) String

func (s Stop) String() string

String returns a descriptive string for this stop.

func (*Stop) Table

func (s *Stop) Table() string

Table implements the upsert.Upserter interface, returning the table where we save stops.

type Trip

type Trip struct {
	AgencyID string `json:"agency_id" db:"agency_id" upsert:"key"`
	RouteID  string `json:"route_id" db:"route_id" upsert:"key"`
	TripID   string `json:"trip_id" db:"trip_id" upsert:"key"`
	UniqueID string `json:"unique_id" db:"-" upsert:"omit"`

	ServiceID string `json:"service_id" db:"service_id"`
	ShapeID   string `json:"shape_id" db:"shape_id"`

	Headsign    string `json:"headsign" db:"headsign"`
	DirectionID int    `json:"direction_id" db:"direction_id"`

	ShapePoints []*Shape `json:"shape_points" db:"-" upsert:"omit"`
	Stops       []*Stop  `json:"stops" db:"-" upsert:"omit"`
}

func GetTrip

func GetTrip(db sqlx.Ext, agencyID, routeID, tripID string, includeShape bool) (t Trip, err error)

GetTrip returns the trip for this agency and trip ID

func NewTrip

func NewTrip(id, routeID, agencyID, serviceID, shapeID, headsign string, direction int) (t *Trip, err error)

func ReallyGetTrip

func ReallyGetTrip(db sqlx.Ext, agencyID, routeID, tripID, firstTripID string, includeShape bool) (*Trip, error)

ReallyGetTrip tries all possible methods for getting a trip

func (*Trip) Initialize

func (t *Trip) Initialize() (err error)

Initialize ensures any derived values are correct after creating/loading an object

func (*Trip) Save

func (t *Trip) Save() error

Save saves a trip to the database

func (*Trip) Table

func (t *Trip) Table() string

type Vehicle

type Vehicle struct {
	Lat float64 `json:"lat"`
	Lon float64 `json:"lon"`

	// Is this location live or estimated based on scheduled?
	Live bool `json:"live"`
}

Vehicle is the location (and maybe any other info) we want to provide about a

func GetVehicle

func GetVehicle(agencyID, routeID, stopID string, directionID int) (vehicle Vehicle, err error)

func GetVehicles

func GetVehicles(agencyID, routeID string, directionID int, serviceIDs []string, minSec int) (vehicles []Vehicle, err error)

Jump to

Keyboard shortcuts

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