gtfs

package module
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 13 Imported by: 0

README

gtfs

This is a Go library for parsing GTFS static and realtime feeds.

The static parser is pretty straightforward and simply maps fields in the GTFS static CSV files to associated Go types.

For realtime parsing the library offers two options. Option one is to use the parser and types generated by the protobuf compiler with are available in the proto subpackage. This saves you from having to compile the protobufs yourself.

Option two is to use the package's custom parser, which takes the protobuf output from option one, does a bunch of post-processing, and then outputs the result in standard (non-proto) Go types. This post-processing does useful things like:

  • Converts some data fields to more idiomatic Go types - for example, times are converted from int64 Unix timestamps to time.Time values.

  • Makes the representation of trips and vehicles more explicit - see details below.

  • Implements business logic to map data in various GTFS realtime extensions to regular GTFS realtime fields.

gtfs-rt-pb

Examples

Parse the GTFS static feed for the New York City Subway:


Parse the GTFS realtime feed for the San Francisco Bay Area BART:


Parse the GTFS realtime feed for the New York City Subway's G train:


Command line tool

Static parser

Realtime parser

What the realtime parser adds:

  • Restuctures the proto message to
    • have the concept of Trip and Vehicle
    • make more explicit the link between these
    • create implicit trips and vehicles
  • Converts to native Go types like Time and Duration
  • Handles extensions (currently just the NYCT)

Documentation

Overview

Package gtfs contains parsers for GTFS static and realtime feeds.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortScheduledStopTimes

func SortScheduledStopTimes(stopTimes []ScheduledStopTime)

SortScheduledStopTimes sorts the provided stop times based on the stop sequence field.

Types

type Agency

type Agency struct {
	Id       string
	Name     string
	Url      string
	Timezone string
	Language *string
	Phone    *string
	FareUrl  *string
	Email    *string
}

Agency corresponds to a single row in the agency.txt file.

type Alert

type Alert struct {
	ID               string
	Cause            AlertCause
	Effect           AlertEffect
	ActivePeriods    []AlertActivePeriod
	InformedEntities []AlertInformedEntity
	Header           []AlertText
	Description      []AlertText
	URL              []AlertText
}

type AlertActivePeriod

type AlertActivePeriod struct {
	StartsAt *time.Time
	EndsAt   *time.Time
}

type AlertInformedEntity

type AlertInformedEntity struct {
	AgencyID    *string
	RouteID     *string
	RouteType   *RouteType
	DirectionID *DirectionID
	TripID      *TripID
	StopID      *string
}

type AlertText

type AlertText struct {
	Text     string
	Language string
}

type DirectionID

type DirectionID uint8
const (
	DirectionIDUnspecified DirectionID = 0
	DirectionIDTrue        DirectionID = 1
	DirectionIDFalse       DirectionID = 2
)

func (DirectionID) String

func (d DirectionID) String() string

type ParseRealtimeOptions

type ParseRealtimeOptions struct {
	// The timezone to interpret date field.
	//
	// It can be nil, in which case UTC will used.
	Timezone *time.Location

	// The GTFS Realtime extension to use when parsing.
	//
	// This can be nil, in which case no extension is used.
	Extension extensions.Extension
}

type ParseStaticOptions

type ParseStaticOptions struct{}

type Realtime

type Realtime struct {
	CreatedAt time.Time

	Trips []Trip

	Vehicles []Vehicle

	Alerts []Alert
}

Realtime contains the parsed content for a single GTFS realtime message.

func ParseRealtime

func ParseRealtime(content []byte, opts *ParseRealtimeOptions) (*Realtime, error)

type Route

type Route struct {
	Id                string
	Agency            *Agency
	Color             string
	TextColor         string
	ShortName         *string
	LongName          *string
	Description       *string
	Type              RouteType
	Url               *string
	SortOrder         *int32
	ContinuousPickup  RoutePolicy
	ContinuousDropOff RoutePolicy
}

type RoutePolicy

type RoutePolicy int32
const (
	NotAllowed           RoutePolicy = 0
	Continuous           RoutePolicy = 1
	PhoneAgency          RoutePolicy = 2
	CoordinateWithDriver RoutePolicy = 3
)

func NewRoutePolicy

func NewRoutePolicy(i int) RoutePolicy

func (RoutePolicy) String

func (t RoutePolicy) String() string

type RouteType

type RouteType int32
const (
	Tram       RouteType = 0
	Subway     RouteType = 1
	Rail       RouteType = 2
	Bus        RouteType = 3
	Ferry      RouteType = 4
	CableTram  RouteType = 5
	AerialLift RouteType = 6
	Funicular  RouteType = 7
	TrolleyBus RouteType = 11
	Monorail   RouteType = 12

	UnknownRouteType RouteType = 10000
)

func NewRouteType

func NewRouteType(i int) (RouteType, bool)

func (RouteType) String

func (t RouteType) String() string

type ScheduledStopTime

type ScheduledStopTime struct {
	Trip          *ScheduledTrip
	Stop          *Stop
	ArrivalTime   time.Duration
	DepartureTime time.Duration
	StopSequence  int
	Headsign      *string
}

type ScheduledTrip

type ScheduledTrip struct {
	Route                *Route
	Service              *Service
	ID                   string
	Headsign             *string
	ShortName            *string
	DirectionId          *bool
	WheelchairAccessible *bool
	BikesAllowed         *bool
	StopTimes            []ScheduledStopTime
}

type Service

type Service struct {
	Id           string
	Monday       bool
	Tuesday      bool
	Wednesday    bool
	Thursday     bool
	Friday       bool
	Saturday     bool
	Sunday       bool
	StartDate    time.Time
	EndDate      time.Time
	AddedDates   []time.Time
	RemovedDates []time.Time
}

type Static

type Static struct {
	Agencies  []Agency
	Routes    []Route
	Stops     []Stop
	Transfers []Transfer
	Services  []Service
	Trips     []ScheduledTrip
}

Static contains the parsed content for a single GTFS static message.

func ParseStatic

func ParseStatic(content []byte, opts ParseStaticOptions) (*Static, error)

ParseStatic parses the content as a GTFS static feed.

type Stop

type Stop struct {
	Id                 string
	Code               *string
	Name               *string
	Description        *string
	ZoneId             *string
	Longitude          *float64
	Latitude           *float64
	Url                *string
	Type               StopType
	Parent             *Stop
	Timezone           *string
	WheelchairBoarding WheelchairBoarding
	PlatformCode       *string
}

func (*Stop) Root

func (stop *Stop) Root() *Stop

type StopTimeEvent

type StopTimeEvent struct {
	Time        *time.Time
	Delay       *time.Duration
	Uncertainty *int32
}

type StopTimeUpdate

type StopTimeUpdate struct {
	StopSequence *uint32
	StopID       *string
	Arrival      *StopTimeEvent
	Departure    *StopTimeEvent
	NyctTrack    *string
}

TODO: shouldn't this just be StopTime?

func (*StopTimeUpdate) GetArrival

func (stopTimeUpdate *StopTimeUpdate) GetArrival() StopTimeEvent

func (*StopTimeUpdate) GetDeparture

func (stopTimeUpdate *StopTimeUpdate) GetDeparture() StopTimeEvent

type StopType

type StopType int32
const (
	Platform      StopType = 0
	Station       StopType = 1
	EntanceOrExit StopType = 2
	GenericNode   StopType = 3
	BoardingArea  StopType = 4
)

func NewStopType

func NewStopType(i int) (StopType, bool)

func (StopType) String

func (t StopType) String() string

type Transfer

type Transfer struct {
	From            *Stop
	To              *Stop
	Type            TransferType
	MinTransferTime *int32
}

type TransferType

type TransferType int32
const (
	Recommended         TransferType = 0
	Timed               TransferType = 1
	RequiresTime        TransferType = 2
	TransferNotPossible TransferType = 3
)

func NewTransferType

func NewTransferType(i int32) (TransferType, bool)

func (TransferType) String

func (t TransferType) String() string

type Trip

type Trip struct {
	ID              TripID
	StopTimeUpdates []StopTimeUpdate
	NyctIsAssigned  bool

	Vehicle *Vehicle

	IsEntityInMessage bool
}

func (*Trip) GetVehicle

func (trip *Trip) GetVehicle() Vehicle

type TripID

type TripID struct {
	ID          string
	RouteID     string
	DirectionID DirectionID

	HasStartTime bool
	StartTime    time.Duration

	HasStartDate bool
	StartDate    time.Time
}

type Vehicle

type Vehicle struct {
	ID *VehicleID

	Trip *Trip

	IsEntityInMessage bool
}

func (*Vehicle) GetID

func (vehicle *Vehicle) GetID() VehicleID

func (*Vehicle) GetTrip

func (vehicle *Vehicle) GetTrip() Trip

type VehicleID

type VehicleID struct {
	ID           string
	Label        string
	LicencePlate string
}

type WheelchairBoarding

type WheelchairBoarding int32
const (
	NotSpecified WheelchairBoarding = 0
	Possible     WheelchairBoarding = 1
	NotPossible  WheelchairBoarding = 2
)

func NewWheelchairBoarding

func NewWheelchairBoarding(i int) (WheelchairBoarding, bool)

func (WheelchairBoarding) String

func (w WheelchairBoarding) String() string

Directories

Path Synopsis
Package csv is a wrapper around the stdlib csv library that provides a nice API for the GTFS static parser.
Package csv is a wrapper around the stdlib csv library that provides a nice API for the GTFS static parser.
nycttrips
Package nycttrips contains logic for the New York City Transit GTFS realtime extenstions
Package nycttrips contains logic for the New York City Transit GTFS realtime extenstions
internal

Jump to

Keyboard shortcuts

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