gtfs

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 17, 2025 License: MIT Imports: 21 Imported by: 0

README

gtfs-go

A simple Go library for parsing GTFS (General Transit Feed Specification) data, and storing it in a database for easy querying.

The following GTFS files are currently supported:

  • agency.txt
  • calendar.txt
  • calendar_dates.txt
  • routes.txt
  • shapes.txt
  • stops.txt
  • stop_times.txt
  • trips.txt

Documentation

Index

Constants

View Source
const CurrentVersion = 3

Current version of the GTFS database

Variables

This section is empty.

Functions

func Populate added in v0.1.0

func Populate(
	db *bolt.DB,
	agencies AgencyMap,
	routes RouteMap,
	services ServiceMap,
	serviceExceptions ServiceExceptionMap,
	shapes ShapeMap,
	stops StopMap,
	trips TripMap,
) error

Populates the GTFS database with data from the provided maps.

Types

type Agency added in v0.0.5

type Agency struct {
	ID       Key
	Name     string
	URL      string
	Timezone string
}

Represents an agency that provides transit services

func (*Agency) Decode added in v0.1.0

func (a *Agency) Decode(id Key, data []byte) error

Decode deserializes the byte slice into the Agency struct.

func (Agency) Encode added in v0.1.0

func (a Agency) Encode() []byte

Encode serializes the Agency struct (excluding ID) into a byte slice. Format: - Name: 4-byte length + UTF-8 string - URL: 4-byte length + UTF-8 string - Timezone: 4-byte length + UTF-8 string

type AgencyMap added in v0.0.5

type AgencyMap map[Key]*Agency

func ParseAgencies added in v0.0.5

func ParseAgencies(file io.Reader) (AgencyMap, error)

Load and parse agencies from the GTFS agency.txt file

type Coordinate added in v0.0.5

type Coordinate struct {
	Latitude  float64
	Longitude float64
}

Represents a geographical coordinate with latitude and longitude.

func NewCoordinate added in v0.0.5

func NewCoordinate(lat, lon float64) Coordinate

Create a new Coordinate instance with the given latitude and longitude.

func NewCoordinateFromString added in v0.0.5

func NewCoordinateFromString(coord string) (Coordinate, error)

Create a new Coordinate instance from a string in the format "lat,lon".

func (Coordinate) BearingTo added in v0.1.6

func (c Coordinate) BearingTo(other Coordinate) float64

Calculate the bearing to another coordinate in degrees

func (*Coordinate) Decode added in v0.1.0

func (c *Coordinate) Decode(data []byte) error

Decode the byte slice into a Coordinate

func (Coordinate) DistanceTo added in v0.0.5

func (c Coordinate) DistanceTo(other Coordinate) float64

Calculate the distance to another coordinate in metres using the Haversine formula

func (Coordinate) Encode added in v0.1.0

func (c Coordinate) Encode() []byte

Encode the Coordinate into a byte slice Format: - Latitude: 8 bytes (float64) - Longitude: 8 bytes (float64)

func (Coordinate) IsValid added in v0.0.5

func (c Coordinate) IsValid() bool

Check if the coordinate is valid (latitude between -90 and 90, longitude between -180 and 180).

func (Coordinate) IsZero added in v0.0.5

func (c Coordinate) IsZero() bool

Check if the coordinate is zero (0, 0).

func (Coordinate) String added in v0.0.5

func (c Coordinate) String() string

Return a string representation of the coordinate in the format "lat,lon".

type CoordinateArray added in v0.0.5

type CoordinateArray []Coordinate

func (*CoordinateArray) Decode added in v0.1.0

func (ca *CoordinateArray) Decode(data []byte) error

Decode the byte slice into the CoordinateArray

func (CoordinateArray) Encode added in v0.1.0

func (ca CoordinateArray) Encode() []byte

Encode the CoordinateArray into a byte slice Format: - Count: 4 bytes (number of coordinates) - Each coordinate: 8 bytes (float64 for latitude) + 8 bytes (float64 for longitude)

type ExceptionType added in v0.0.5

type ExceptionType bool

Enum for the types of service exception

const (
	AddedExceptionType   ExceptionType = false
	RemovedExceptionType ExceptionType = true
)

type GTFS

type GTFS struct {
	Version int
	Created int64
	// contains filtered or unexported fields
}

Represents a GTFS database connection

func (*GTFS) Close added in v0.1.0

func (g *GTFS) Close() error

Closes the GTFS database connection and saves metadata

func (*GTFS) FromDB

func (g *GTFS) FromDB(dbFile string) error

Load GTFS data from a local database file

func (*GTFS) FromURL

func (g *GTFS) FromURL(gtfsURL, dbFile string) error

Construct a new GTFS database from a hosted GTFS URL

func (*GTFS) GetAgenciesByIDs added in v0.1.1

func (g *GTFS) GetAgenciesByIDs(agencyIDs []Key) (AgencyMap, error)

Returns the agencies with the given IDs

func (*GTFS) GetAgencyByID

func (g *GTFS) GetAgencyByID(agencyID Key) (*Agency, error)

Returns the agency with the given ID

func (*GTFS) GetAllAgencies

func (g *GTFS) GetAllAgencies() (AgencyMap, error)

Returns all agencies in the GTFS database

func (*GTFS) GetAllCurrentTrips

func (g *GTFS) GetAllCurrentTrips() (TripMap, error)

Returns all trips that are currently running

func (*GTFS) GetAllRoutes added in v0.0.3

func (g *GTFS) GetAllRoutes() (RouteMap, error)

Returns all routes in the GTFS database

func (*GTFS) GetAllServiceExceptions added in v0.0.6

func (g *GTFS) GetAllServiceExceptions() (ServiceExceptionMap, error)

Returns all service exceptions in the GTFS database

func (*GTFS) GetAllServices added in v0.0.6

func (g *GTFS) GetAllServices() (ServiceMap, error)

Returns all services in the GTFS database

func (*GTFS) GetAllShapes added in v0.1.1

func (g *GTFS) GetAllShapes() (ShapeMap, error)

Returns all shapes in the GTFS database

func (*GTFS) GetAllStops added in v0.0.6

func (g *GTFS) GetAllStops() (StopMap, error)

Returns all stops in the GTFS database

func (*GTFS) GetAllTrips added in v0.0.6

func (g *GTFS) GetAllTrips() (TripMap, error)

Returns all trips in the GTFS database

func (*GTFS) GetCurrentTrips added in v0.0.8

func (g *GTFS) GetCurrentTrips(trips TripMap) (TripMap, error)

Returns the trips that are currently running from the given array

func (*GTFS) GetCurrentTripsAt added in v0.0.3

func (g *GTFS) GetCurrentTripsAt(trips TripMap, t time.Time) (TripMap, error)

Returns the trips that are running at the given time from the given array

func (*GTFS) GetCurrentTripsBetween added in v0.0.11

func (g *GTFS) GetCurrentTripsBetween(trips TripMap, start, end time.Time) (TripMap, error)

Returns the trips that are running between the given start and end times from the given array

func (*GTFS) GetCurrentTripsWithBuffer added in v0.0.7

func (g *GTFS) GetCurrentTripsWithBuffer(trips TripMap, t time.Time, buffer time.Duration) (TripMap, error)

Returns the trips that are running at the given time with a buffer, from the given array

func (*GTFS) GetRouteByID

func (g *GTFS) GetRouteByID(routeID Key) (*Route, error)

Returns the route with the given ID

func (*GTFS) GetRouteByName added in v0.0.6

func (g *GTFS) GetRouteByName(routeName string) (*Route, error)

Returns the route with the given name

func (*GTFS) GetRoutesByIDs added in v0.1.1

func (g *GTFS) GetRoutesByIDs(routeIDs []Key) (RouteMap, error)

Returns the routes with the given IDs

func (*GTFS) GetServiceByID

func (g *GTFS) GetServiceByID(serviceID Key) (*Service, error)

Returns the service with the given ID

func (*GTFS) GetServiceException added in v0.0.3

func (g *GTFS) GetServiceException(serviceID Key, date time.Time) (*ServiceException, error)

Returns all services exceptions for a given service ID and date

func (*GTFS) GetServicesByIDs added in v0.1.1

func (g *GTFS) GetServicesByIDs(serviceIDs []Key) (ServiceMap, error)

Returns the services with the given IDs

func (*GTFS) GetShapeByID added in v0.0.12

func (g *GTFS) GetShapeByID(shapeID Key) (*Shape, error)

Returns the shape with the given ID

func (*GTFS) GetShapesByIDs added in v0.1.1

func (g *GTFS) GetShapesByIDs(shapeIDs []Key) (ShapeMap, error)

Returns the shapes with the given IDs

func (*GTFS) GetStopByID

func (g *GTFS) GetStopByID(stopID Key) (*Stop, error)

Returns the stop with the given ID

func (*GTFS) GetStopByName added in v0.0.6

func (g *GTFS) GetStopByName(stopName string) (*Stop, error)

Returns the stop with the given name

func (*GTFS) GetStopsByIDs added in v0.1.1

func (g *GTFS) GetStopsByIDs(stopIDs []Key) (StopMap, error)

Returns the stops with the given IDs

func (*GTFS) GetTripByID

func (g *GTFS) GetTripByID(tripID Key) (*Trip, error)

Returns the trip with the given ID

func (*GTFS) GetTripsByIDs added in v0.1.1

func (g *GTFS) GetTripsByIDs(tripIDs []Key) (TripMap, error)

Returns the trips with the given IDs

func (*GTFS) GetTripsByRouteID

func (g *GTFS) GetTripsByRouteID(routeID Key) (TripMap, error)

Returns all trips for a given route ID

type Key added in v0.0.5

type Key string

type KeyArray added in v0.0.5

type KeyArray []Key

func (*KeyArray) Append added in v0.0.6

func (ka *KeyArray) Append(key Key)

func (*KeyArray) Decode added in v0.1.0

func (ka *KeyArray) Decode(data []byte) error

Decodes the byte slice into the KeyArray

func (KeyArray) Encode added in v0.1.0

func (ka KeyArray) Encode() []byte

Encodes the KeyArray into a byte slice Format: - Count: 4 bytes (number of keys) - Each key: 4 bytes (length of the key) + UTF-8 string

type LocationType added in v0.0.5

type LocationType uint8
const (
	StopLocationType LocationType = iota
	StationLocationType
	EntranceExitLocationType
	GenericNodeLocationType
	BoardingAreaLocationType
	UnknownLocationType
)

type ModeFlag added in v0.0.5

type ModeFlag uint8
const (
	BusModeFlag ModeFlag = 1 << iota
	SchoolBusModeFlag
	RailModeFlag
	FerryModeFlag
	UnknownModeFlag = 0
)

type Route added in v0.0.5

type Route struct {
	ID              Key
	AgencyID        Key
	Name            string
	Type            RouteType
	Colour          string
	InboundShapeID  *Key
	OutboundShapeID *Key
	Stops           KeyArray
}

Represents a route in a transit system

func (*Route) Decode added in v0.1.0

func (r *Route) Decode(id Key, data []byte) error

Decode the byte slice into the Route struct

func (Route) Encode added in v0.1.0

func (r Route) Encode() []byte

Encode the Route struct into a byte slice Format: - AgencyID: 4-byte length + UTF-8 string - Name: 4-byte length + UTF-8 string - Type: 1-byte enum (RouteType) - Colour: 4-byte length + UTF-8 string - InboundShapeID: 4-byte length + UTF-8 string - OutboundShapeID: 4-byte length + UTF-8 string - Stops: KeyArray (encoded as a byte slice)

type RouteMap added in v0.0.5

type RouteMap map[Key]*Route

func ParseRoutes added in v0.0.5

func ParseRoutes(file io.Reader) (RouteMap, error)

Load and parse routes from the GTFS routes.txt file

type RouteType added in v0.0.5

type RouteType uint8
const (
	TramRouteType RouteType = iota
	SubwayRouteType
	RailRouteType
	BusRouteType
	FerryRouteType
	CableCarRouteType
	GondolaRouteType
	FunicularRouteType
	TrolleybusRouteType = iota + 3
	MonorailRouteType
)

type Service added in v0.0.5

type Service struct {
	ID        Key
	Weekdays  WeekdayFlag
	StartDate time.Time
	EndDate   time.Time
}

Represents the days of the week a service is active

func (*Service) Decode added in v0.1.0

func (s *Service) Decode(id Key, data []byte) error

Decode deserializes the byte slice into the Service struct.

func (Service) Encode added in v0.1.0

func (s Service) Encode() []byte

Encode serializes the Service struct (excluding ID) into a byte slice. Format: - Weekdays: 1 byte (bitmask for each day of the week) - StartDate: 8 bytes (Unix timestamp) - EndDate: 8 bytes (Unix timestamp)

type ServiceException added in v0.0.5

type ServiceException struct {
	ServiceID Key
	Date      time.Time
	Type      ExceptionType
}

Represents an exception for a service on a specific date

func (*ServiceException) Decode added in v0.1.0

func (se *ServiceException) Decode(data []byte) error

Decode deserializes the byte slice into the ServiceException struct.

func (ServiceException) Encode added in v0.1.0

func (se ServiceException) Encode() []byte

Encode serializes the ServiceException struct into a byte slice. Format: - ServiceID: 4-byte length + UTF-8 string - Date: 8 bytes (Unix timestamp) - Type: 1 byte (bool as uint8)

type ServiceExceptionKey added in v0.1.5

type ServiceExceptionKey struct {
	ServiceID Key
	Date      time.Time
}

type ServiceExceptionMap added in v0.0.5

type ServiceExceptionMap map[ServiceExceptionKey]*ServiceException

func ParseServiceExceptions added in v0.0.5

func ParseServiceExceptions(file io.Reader) (ServiceExceptionMap, error)

Load and parse service exceptions from the GTFS calendar_dates.txt file

type ServiceMap added in v0.0.5

type ServiceMap map[Key]*Service

func ParseServices added in v0.0.5

func ParseServices(file io.Reader) (ServiceMap, error)

Load and parse services from the GTFS calendar.txt file

type Shape added in v0.0.5

type Shape struct {
	ID          Key
	Coordinates CoordinateArray
}

Represents the shape of a transit route

func (*Shape) Decode added in v0.1.0

func (s *Shape) Decode(id Key, data []byte) error

Decode deserializes the byte slice into the Shape struct.

func (Shape) Encode added in v0.1.0

func (s Shape) Encode() []byte

Encode serializes the Shape struct (excluding ID) into a byte slice. Format: - Coordinates: CoordinateArray (encoded as a byte slice)

type ShapeMap added in v0.0.5

type ShapeMap map[Key]*Shape

func ParseShapes added in v0.0.5

func ParseShapes(file io.Reader) (ShapeMap, int, error)

Load and parse shapes from the GTFS shapes.txt file

type Stop added in v0.0.5

type Stop struct {
	ID             Key
	Code           string
	Name           string
	ParentID       Key
	Location       Coordinate
	LocationType   LocationType
	SupportedModes ModeFlag
}

Represents a stop, platform, or station in a transit system

func (*Stop) Decode added in v0.1.0

func (s *Stop) Decode(id Key, data []byte) error

Decode deserializes the byte slice into the Stop struct.

func (Stop) Encode added in v0.1.0

func (s Stop) Encode() []byte

Encode serializes the Stop struct (excluding ID) into a byte slice. Format: - Code: 4-byte length + UTF-8 string - Name: 4-byte length + UTF-8 string - ParentID: 4-byte length + UTF-8 string - Location: 2 * float64 (fixed size) - LocationType: 1 byte (LocationType enum) - SupportedModes: 1 byte (bitmask for each mode)

type StopMap added in v0.0.5

type StopMap map[Key]*Stop

func ParseStops added in v0.0.5

func ParseStops(file io.Reader) (StopMap, error)

Load and parse stops from the GTFS stops.txt file

type Trip added in v0.0.5

type Trip struct {
	ID        Key
	RouteID   Key
	ServiceID Key
	ShapeID   Key
	Direction TripDirection
	Headsign  string
	Stops     TripStopArray
}

Represents a trip on a particular route in a transit system

func (*Trip) Decode added in v0.1.0

func (t *Trip) Decode(id Key, data []byte) error

Decode the byte slice into the Trip struct

func (Trip) Encode added in v0.1.0

func (t Trip) Encode() []byte

Encode the Trip struct into a byte slice Format: - RouteID: 4-byte length + UTF-8 string - ServiceID: 4-byte length + UTF-8 string - ShapeID: 4-byte length + UTF-8 string - Direction: 1 byte (bool as uint8) - Headsign: 4-byte length + UTF-8 string - Stops: TripStopArray (see TripStopArray.Encode)

func (*Trip) EndTime added in v0.0.5

func (t *Trip) EndTime() uint

Get the time that a trip ends at the last stop

func (*Trip) StartTime added in v0.0.5

func (t *Trip) StartTime() uint

Get the time that a trip starts at the first stop

type TripDirection added in v0.0.5

type TripDirection bool
const (
	OutboundTripDirection TripDirection = false
	InboundTripDirection  TripDirection = true
)

type TripMap added in v0.0.5

type TripMap map[Key]*Trip

func ParseTrips added in v0.0.5

func ParseTrips(tripsFile io.Reader, stopTimesFile io.Reader) (TripMap, error)

Load and parse trips from the GTFS trips.txt and stop_times.txt files

type TripStop added in v0.0.5

type TripStop struct {
	StopID        Key           `json:"stop_id"`
	ArrivalTime   uint          `json:"arrival_time"`
	DepartureTime uint          `json:"departure_time"`
	Timepoint     TripTimepoint `json:"timepoint"`
}

Represents a stop in a trip

func (*TripStop) Decode added in v0.1.0

func (ts *TripStop) Decode(data []byte) error

Decodes the byte slice into the TripStop struct

func (*TripStop) Encode added in v0.1.0

func (ts *TripStop) Encode() []byte

Encodes the TripStop struct into a byte slice Format: - StopID: 4-byte length + UTF-8 string - ArrivalTime: 4 bytes (uint32) - DepartureTime: 4 bytes (uint32) - Timepoint: 1 byte (bool as uint8)

type TripStopArray added in v0.0.5

type TripStopArray []*TripStop

func (*TripStopArray) Decode added in v0.1.0

func (tsa *TripStopArray) Decode(data []byte) error

Decode the byte slice into the TripStopArray

func (TripStopArray) Encode added in v0.1.0

func (tsa TripStopArray) Encode() []byte

Encode the TripStopArray into a byte slice Format: - Count: 4 bytes (uint32) - Each TripStop (see TripStop.Encode)

type TripTimepoint added in v0.0.5

type TripTimepoint bool
const (
	ApproximateTripTimepoint TripTimepoint = false
	ExactTripTimepoint       TripTimepoint = true
)

type WeekdayFlag added in v0.0.5

type WeekdayFlag uint8

Flag for each day of the week

const (
	MondayWeekdayFlag WeekdayFlag = 1 << iota
	TuesdayWeekdayFlag
	WednesdayWeekdayFlag
	ThursdayWeekdayFlag
	FridayWeekdayFlag
	SaturdayWeekdayFlag
	SundayWeekdayFlag
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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