gtfs

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

README

gtfs

Test PkgGoDev Go Report Card

Model and tooling for General Transit Feed Specification (GTFS) data.

Getting started

Using the CLI tool

Get, build and install the gtfs binary by running:

go install github.com/heimdalr/gtfs/cmd/gtfs@latest

If all goes well, the above installed ~/go/bin/gtfs.

Now download and extract (e.g.) the VBB GTFS data to ./vbb/ by running:

mkdir ./vbb
cd ./vbb 
wget https://www.vbb.de/fileadmin/user_upload/VBB/Dokumente/API-Datensaetze/gtfs-mastscharf/GTFS.zip
unzip GTFS.zip

Finally, run:

gtfs import ./vbb ./vbb.db

to import the VBB GTFS CSV files within ./vbb/ into the SQLite DB file ./vbb.db.

Using the Model

Now, using this package on the DB we populated in the previous step, we may build and run the following:

package main

import (
	"fmt"
	"github.com/heimdalr/gtfs"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

func main() {

	const dbPath = "vbb.db"

	// open the DB
	db, _ := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Error),
	})

	// SELECT * FROM agencies WHERE id = 1;
	agency := gtfs.Agency{}
	db.First(&agency, "id = ?", 1)
	fmt.Println(agency)
}

to query for the agency with the ID "1":

{1 S-Bahn Berlin GmbH https://sbahn.berlin/}

Documentation

Overview

Example
package main

import (
	"fmt"
	"github.com/heimdalr/gtfs"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

func main() {

	const dbPath = "_fixture/test.db"

	// open the DB
	db, _ := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Error),
	})

	// SELECT * FROM agencies WHERE id = 1;
	agency := gtfs.Agency{}
	db.First(&agency, "id = ?", 1)
	fmt.Println(agency)

}
Output:

{1 S-Bahn Berlin GmbH https://sbahn.berlin/}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(db *gorm.DB) error

Migrate ensure the given DB matches our models.

Types

type Agency

type Agency struct {
	ID   string `csv:"agency_id"`
	Name string `csv:"agency_name"`
	URL  string `csv:"agency_url"`
}

Agency model.

type Calendar

type Calendar struct {
	ID        uint   `gorm:"primaryKey,autoIncrement"`
	ServiceID string `csv:"service_id"`
	Monday    int    `csv:"monday"`
	Tuesday   int    `csv:"tuesday"`
	Wednesday int    `csv:"wednesday"`
	Thursday  int    `csv:"thursday"`
	Friday    int    `csv:"friday"`
	Saturday  int    `csv:"saturday"`
	Sunday    int    `csv:"sunday"`
	StartDate string `csv:"start_date"`
	EndDate   string `csv:"end_date"`
}

Calendar model.

type CalendarDate

type CalendarDate struct {
	ID            uint   `gorm:"primaryKey,autoIncrement"`
	ServiceID     string `csv:"service_id"`
	Date          string `csv:"date"`
	ExceptionType int    `csv:"exception_type"`
}

CalendarDate model.

type DateTime

type DateTime struct {
	Int32 int32
}

DateTime is used to represent GTFS times (hh:mm) (in the DB) as seconds since midnight.

func (*DateTime) MarshalCSV

func (dt *DateTime) MarshalCSV() (string, error)

MarshalCSV marshals DateTime to CSV (i.e. when writing to CSV).

func (*DateTime) Scan

func (dt *DateTime) Scan(value interface{}) error

Scan converts from DB to DateTime.

func (*DateTime) UnmarshalCSV

func (dt *DateTime) UnmarshalCSV(csv string) error

UnmarshalCSV unmarshalls CSV to DateTime (i.e. when reading from CSV).

func (DateTime) Value

func (dt DateTime) Value() (driver.Value, error)

Value converts from DateTime to DB.

type ItemType

type ItemType uint32

ItemType enumerates different item types.

const (

	// Agencies the item type for agency items.
	Agencies ItemType = iota

	// Routes the item type for route items.
	Routes

	// Trips the item type for trip items.
	Trips

	// Stops the item type for stop items.
	Stops

	// StopTimes the item type for stop time items.
	StopTimes

	// Shapes the item type for shape items.
	Shapes

	// Calendars the item type for shape items.
	Calendars

	// CalendarDates the item type for shape items.
	CalendarDates
)

func (ItemType) String

func (it ItemType) String() string

String returns a human-readable representation of ItemType.

type Route

type Route struct {
	ID        string `csv:"route_id"`
	AgencyID  string `csv:"agency_id"`
	Agency    Agency
	ShortName string `csv:"route_short_name"`
	LongName  string `csv:"route_long_name"`
	Type      int    `csv:"route_type"`
}

Route model.

type Shape

type Shape struct {
	ID         uint    `gorm:"primaryKey,autoIncrement"`
	ShapeID    string  `csv:"shape_id"`
	PtLat      float64 `csv:"shape_pt_lat"`
	PtLon      float64 `csv:"shape_pt_lon"`
	PtSequence int     `csv:"shape_pt_sequence"`
}

Shape model.

type Stop

type Stop struct {
	ID        string  `csv:"stop_id"`
	Name      string  `csv:"stop_name"`
	Latitude  float64 `csv:"stop_lat"`
	Longitude float64 `csv:"stop_lon"`
}

Stop model.

type StopTime

type StopTime struct {
	ID        uint   `gorm:"primaryKey,autoIncrement"`
	StopID    string `csv:"stop_id"`
	Stop      Stop
	TripID    string `csv:"trip_id"`
	Trip      Trip
	Departure DateTime `csv:"departure_time"`
	Arrival   DateTime `csv:"arrival_time"`
	StopSeq   int      `csv:"stop_sequence"`
}

StopTime model.

type Trip

type Trip struct {
	ID          string `csv:"trip_id"`
	Name        string `csv:"trip_short_name"`
	RouteID     string `csv:"route_id"`
	Route       Route
	ServiceID   string `csv:"service_id"`
	DirectionID string `csv:"direction_id"`
	ShapeID     string `csv:"shape_id"`
}

Trip model.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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