gtfsparser

package module
v0.0.0-...-f85e0e2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: GPL-2.0 Imports: 19 Imported by: 16

README

Go Report Card Build Status GoDoc

go gtfsparser

A complete*, easy to use parsing library for GTFS data. Implemented in go. Accepts folders containing GTFS files and ZIPs. Feeds are validated during parsing. ID references are transformed into pointer references where appropriate.

This is a fork from the gtfsparser I developed at geOps, containing some minor improvements. Because of some changes to member types, this is not a drop-in replacement for the geOps gtfsparser.

Usage

feed := gtfsparser.NewFeed()
error := feed.Parse("sample-feed.zip")

See feed.go for exported fields.

Example

Parsing of the GTFS example feed:

import (
	"github.com/patrickbrosi/gtfsparser"
	"fmt"
)

func main() {
    feed := gtfsparser.NewFeed()

    feed.Parse("sample-feed.zip")

    fmt.Printf("Done, parsed %d agencies, %d stops, %d routes, %d trips, %d fare attributes\n\n", len(feed.Agencies), len(feed.Stops), len(feed.Routes), len(feed.Trips), len(feed.FareAttributes))

    for k, v := range feed.Stops {
        fmt.Printf("[%s] %s (@ %f,%f)\n", k, v.Name, v.Lat, v.Lon)
    }
}
Output
Done, parsed 1 agencies, 9 stops, 5 routes, 11 trips, 2 fare attributes

[BULLFROG] Bullfrog (Demo) (@ 36.881081,-116.817970)
[NADAV] North Ave / D Ave N (Demo) (@ 36.914894,-116.768211)
[NANAA] North Ave / N A Ave (Demo) (@ 36.914944,-116.761475)
[AMV] Amargosa Valley (Demo) (@ 36.641495,-116.400940)
[FUR_CREEK_RES] Furnace Creek Resort (Demo) (@ 36.425289,-117.133163)
[BEATTY_AIRPORT] Nye County Airport (Demo) (@ 36.868446,-116.784584)
[STAGECOACH] Stagecoach Hotel & Casino (Demo) (@ 36.915684,-116.751678)
[DADAN] Doing Ave / D Ave N (Demo) (@ 36.909489,-116.768242)
[EMSI] E Main St / S Irving St (Demo) (@ 36.905697,-116.762177)

*Known restrictions

Validation may not be 100% complete. Tests are missing.

License

GPL v2, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgencyFields

type AgencyFields struct {
	// contains filtered or unexported fields
}

func (AgencyFields) FldName

func (flds AgencyFields) FldName(idx int) (name string)

type AttributionFields

type AttributionFields struct {
	// contains filtered or unexported fields
}

func (AttributionFields) FldName

func (flds AttributionFields) FldName(idx int) (name string)

type CalendarDatesFields

type CalendarDatesFields struct {
	// contains filtered or unexported fields
}

func (CalendarDatesFields) FldName

func (flds CalendarDatesFields) FldName(idx int) (name string)

type CalendarFields

type CalendarFields struct {
	// contains filtered or unexported fields
}

func (CalendarFields) FldName

func (flds CalendarFields) FldName(idx int) (name string)

type ColOrders

type ColOrders struct {
	Agencies           []string
	Stops              []string
	Routes             []string
	Trips              []string
	StopTimes          []string
	Frequencies        []string
	Calendar           []string
	CalendarDates      []string
	FareAttributes     []string
	FareAttributeRules []string
	Shapes             []string
	Levels             []string
	Pathways           []string
	Transfers          []string
	FeedInfos          []string
	Attributions       []string
}

Holds the original column ordering

type CsvParser

type CsvParser struct {
	Curline int
	// contains filtered or unexported fields
}

CsvParser is a wrapper around csv.Reader

func NewCsvParser

func NewCsvParser(file io.Reader, silentfail bool, assumeclean bool) CsvParser

NewCsvParser creates a new CsvParser

func (*CsvParser) GetHeader

func (c *CsvParser) GetHeader() []string

func (*CsvParser) ParseCsvLine

func (p *CsvParser) ParseCsvLine() []string

func (*CsvParser) ParseRecord

func (p *CsvParser) ParseRecord() map[string]string

ParseRecord reads a single line into a map

type ErrStats

type ErrStats struct {
	DroppedAgencies           int
	DroppedStops              int
	DroppedRoutes             int
	DroppedTrips              int
	DroppedStopTimes          int
	DroppedFrequencies        int
	DroppedServices           int
	DroppedFareAttributes     int
	DroppedFareAttributeRules int
	DroppedAttributions       int
	DroppedShapes             int
	DroppedLevels             int
	DroppedPathways           int
	DroppedTransfers          int
	DroppedFeedInfos          int
	DroppedTranslations       int
	NumTranslations           int
}

type FareAttributeFields

type FareAttributeFields struct {
	// contains filtered or unexported fields
}

func (FareAttributeFields) FldName

func (flds FareAttributeFields) FldName(idx int) (name string)

type FareRuleFields

type FareRuleFields struct {
	// contains filtered or unexported fields
}

func (FareRuleFields) FldName

func (flds FareRuleFields) FldName(idx int) (name string)

type Feed

type Feed struct {
	Agencies       map[string]*gtfs.Agency
	Stops          map[string]*gtfs.Stop
	Routes         map[string]*gtfs.Route
	Trips          map[string]*gtfs.Trip
	Services       map[string]*gtfs.Service
	FareAttributes map[string]*gtfs.FareAttribute
	Shapes         map[string]*gtfs.Shape
	Levels         map[string]*gtfs.Level
	Pathways       map[string]*gtfs.Pathway
	Transfers      map[gtfs.TransferKey]gtfs.TransferVal
	FeedInfos      []*gtfs.FeedInfo

	StopsAddFlds          map[string]map[string]string
	AgenciesAddFlds       map[string]map[string]string
	RoutesAddFlds         map[string]map[string]string
	TripsAddFlds          map[string]map[string]string
	StopTimesAddFlds      map[string]map[string]map[int]string
	FrequenciesAddFlds    map[string]map[string]map[*gtfs.Frequency]string
	ShapesAddFlds         map[string]map[string]map[int]string
	FareRulesAddFlds      map[string]map[string]map[*gtfs.FareAttributeRule]string
	LevelsAddFlds         map[string]map[string]string
	PathwaysAddFlds       map[string]map[string]string
	FareAttributesAddFlds map[string]map[string]string
	TransfersAddFlds      map[string]map[gtfs.TransferKey]string
	FeedInfosAddFlds      map[string]map[*gtfs.FeedInfo]string
	AttributionsAddFlds   map[string]map[*gtfs.Attribution]string
	TranslationsAddFlds   map[string]map[*gtfs.Translation]string

	// this only holds feed-wide attributions
	Attributions []*gtfs.Attribution

	ErrorStats   ErrStats
	NumShpPoints int
	NumStopTimes int

	ColOrders ColOrders
	// contains filtered or unexported fields
}

Feed represents a single GTFS feed

func NewFeed

func NewFeed() *Feed

NewFeed creates a new, empty feed

func (*Feed) CleanTransfers

func (feed *Feed) CleanTransfers()

func (*Feed) DeleteAgency

func (feed *Feed) DeleteAgency(id string)

func (*Feed) DeleteFareAttribute

func (feed *Feed) DeleteFareAttribute(id string)

func (*Feed) DeleteLevel

func (feed *Feed) DeleteLevel(id string)

func (*Feed) DeletePathway

func (feed *Feed) DeletePathway(id string)

func (*Feed) DeleteRoute

func (feed *Feed) DeleteRoute(id string)

func (*Feed) DeleteService

func (feed *Feed) DeleteService(id string)

func (*Feed) DeleteShape

func (feed *Feed) DeleteShape(id string)

func (*Feed) DeleteStop

func (feed *Feed) DeleteStop(id string)

func (*Feed) DeleteTransfer

func (feed *Feed) DeleteTransfer(tk gtfs.TransferKey)

func (*Feed) DeleteTrip

func (feed *Feed) DeleteTrip(id string)

func (*Feed) Parse

func (feed *Feed) Parse(path string) error

Parse the GTFS data in the specified folder into the feed

func (*Feed) PrefixParse

func (feed *Feed) PrefixParse(path string, prefix string) error

Parse the GTFS data in the specified folder into the feed, use and id prefix

func (*Feed) SetParseOpts

func (feed *Feed) SetParseOpts(opts ParseOptions)

SetParseOpts sets the ParseOptions for this feed

type FeedInfoFields

type FeedInfoFields struct {
	// contains filtered or unexported fields
}

func (FeedInfoFields) FldName

func (flds FeedInfoFields) FldName(idx int) (name string)

type Fields

type Fields interface {
	FldName(idx int) string
}

csv lookup structs

type FrequencyFields

type FrequencyFields struct {
	// contains filtered or unexported fields
}

func (FrequencyFields) FldName

func (flds FrequencyFields) FldName(idx int) (name string)

type HeaderIdx

type HeaderIdx map[string]int

func (HeaderIdx) GetFldId

func (d HeaderIdx) GetFldId(key string, def int) (result int)

type LevelFields

type LevelFields struct {
	// contains filtered or unexported fields
}

func (LevelFields) FldName

func (flds LevelFields) FldName(idx int) (name string)

type ParseError

type ParseError struct {
	// contains filtered or unexported fields
}

A ParseError indicates an error during parsing

func (ParseError) Error

func (e ParseError) Error() string

type ParseOptions

type ParseOptions struct {
	UseDefValueOnError    bool
	DropErroneous         bool
	DryRun                bool
	CheckNullCoordinates  bool
	EmptyStringRepl       string
	ZipFix                bool
	ShowWarnings          bool
	DropShapes            bool
	KeepAddFlds           bool
	DateFilterStart       gtfs.Date
	DateFilterEnd         gtfs.Date
	PolygonFilter         []Polygon
	UseStandardRouteTypes bool
	MOTFilter             map[int16]bool
	MOTFilterNeg          map[int16]bool
	AssumeCleanCsv        bool
}

A ParseOptions object holds options for parsing a the feed

type PathwayFields

type PathwayFields struct {
	// contains filtered or unexported fields
}

func (PathwayFields) FldName

func (flds PathwayFields) FldName(idx int) (name string)

type Polygon

type Polygon struct {
	OuterRing  [][2]float64
	InnerRings [][][2]float64
	// contains filtered or unexported fields
}

func NewPolygon

func NewPolygon(outer [][2]float64, inners [][][2]float64) Polygon

NewPolygon creates a new Polygon from an outer ring

func (*Polygon) PolyContains

func (p *Polygon) PolyContains(x float64, y float64) bool

type RouteFields

type RouteFields struct {
	// contains filtered or unexported fields
}

func (RouteFields) FldName

func (flds RouteFields) FldName(idx int) (name string)

type RouteNotFoundErr

type RouteNotFoundErr struct {
	// contains filtered or unexported fields
}

func (*RouteNotFoundErr) Error

func (e *RouteNotFoundErr) Error() string

func (*RouteNotFoundErr) PayloadId

func (e *RouteNotFoundErr) PayloadId() string

func (*RouteNotFoundErr) RouteId

func (e *RouteNotFoundErr) RouteId() string

type ShapeFields

type ShapeFields struct {
	// contains filtered or unexported fields
}

func (ShapeFields) FldName

func (flds ShapeFields) FldName(idx int) (name string)

type StopFields

type StopFields struct {
	// contains filtered or unexported fields
}

func (StopFields) FldName

func (flds StopFields) FldName(idx int) (name string)

type StopNotFoundErr

type StopNotFoundErr struct {
	// contains filtered or unexported fields
}

custom error types for later checking

func (*StopNotFoundErr) Error

func (e *StopNotFoundErr) Error() string

func (*StopNotFoundErr) StopId

func (e *StopNotFoundErr) StopId() string

type StopTimeFields

type StopTimeFields struct {
	// contains filtered or unexported fields
}

func (StopTimeFields) FldName

func (flds StopTimeFields) FldName(idx int) (name string)

type TransferFields

type TransferFields struct {
	FromStopId      int
	ToStopId        int
	FromRouteId     int
	ToRouteId       int
	FromTripId      int
	ToTripId        int
	TransferType    int
	MinTransferTime int
}

func (TransferFields) FldName

func (flds TransferFields) FldName(idx int) (name string)

type TranslationFields

type TranslationFields struct {
	// contains filtered or unexported fields
}

func (TranslationFields) FldName

func (flds TranslationFields) FldName(idx int) (name string)

type TripFields

type TripFields struct {
	// contains filtered or unexported fields
}

func (TripFields) FldName

func (flds TripFields) FldName(idx int) (name string)

type TripNotFoundErr

type TripNotFoundErr struct {
	// contains filtered or unexported fields
}

func (*TripNotFoundErr) Error

func (e *TripNotFoundErr) Error() string

func (*TripNotFoundErr) TripId

func (e *TripNotFoundErr) TripId() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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