tlcsv

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: GPL-3.0 Imports: 24 Imported by: 1

Documentation

Overview

Package tlcsv provides adapters to read and write GTFS from CSV format files.

Index

Constants

This section is empty.

Variables

Functions

func GetString

func GetString(ent tt.Entity, key string) (string, error)

GetString convenience method; gets a string representation of a field.

func NewShapeFromShapes added in v0.18.0

func NewShapeFromShapes(shapes []gtfs.Shape) gtfs.Shape

NewShapeFromShapes takes Shapes with single points and returns a Shape with linestring geometry. Any errors from the input errors, or errors such as duplicate sequences, are added as entity errors.

func ReadEntities added in v0.11.0

func ReadEntities[T any](reader *Reader, efn string) chan T

func ReadRows

func ReadRows(in io.Reader, cb func(Row)) error

ReadRows iterates through csv rows with callback.

func SetString

func SetString(ent tt.Entity, key string, value string) error

SetString convenience method; checks for SetString method.

func ValidateShapes added in v0.18.0

func ValidateShapes(shapes []gtfs.Shape) []error

ValidateShapes returns errors for an array of shapes.

Types

type Adapter

type Adapter interface {
	OpenFile(string, func(io.Reader)) error
	ReadRows(string, func(Row)) error
	Open() error
	Close() error
	Exists() bool
	Path() string
	SHA1() (string, error)
	DirSHA1() (string, error)
	String() string
}

Adapter provides an interface for working with various kinds of GTFS sources: zip, directory, url.

func NewAdapter added in v0.10.1

func NewAdapter(address string) (Adapter, error)

NewAdapter returns a basic adapter for the given URL. Use NewURLAdapter() to provide additional options.

type DirAdapter

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

DirAdapter supports plain directories of CSV files.

func NewDirAdapter

func NewDirAdapter(path string) *DirAdapter

NewDirAdapter returns an initialized DirAdapter.

func (*DirAdapter) AddFile added in v0.8.5

func (adapter *DirAdapter) AddFile(filename string, reader io.Reader) error

AddFile directly adds a file to this directory. Useful for manual feed operations.

func (*DirAdapter) Close

func (adapter *DirAdapter) Close() error

Close the adapter.

func (*DirAdapter) DirSHA1

func (adapter *DirAdapter) DirSHA1() (string, error)

DirSHA1 returns the SHA1 of all the .txt files in the main directory, sorted, and concatenated.

func (*DirAdapter) Exists

func (adapter *DirAdapter) Exists() bool

Exists checks if the specified directory exists.

func (*DirAdapter) FileInfos

func (adapter *DirAdapter) FileInfos() ([]os.FileInfo, error)

FileInfos returns a list of os.FileInfo for all top-level .txt files.

func (*DirAdapter) Open

func (adapter *DirAdapter) Open() error

Open the adapter. Return an error if the directory does not exist.

func (*DirAdapter) OpenFile

func (adapter *DirAdapter) OpenFile(filename string, cb func(io.Reader)) error

OpenFile opens a file in the directory. Returns an error if the file cannot be read.

func (*DirAdapter) Path

func (adapter *DirAdapter) Path() string

Path returns the directory path.

func (*DirAdapter) ReadRows

func (adapter *DirAdapter) ReadRows(filename string, cb func(Row)) error

ReadRows opens the file and runs the callback for each row. An error is returned if the file cannot be read.

func (*DirAdapter) SHA1

func (adapter *DirAdapter) SHA1() (string, error)

SHA1 returns an error.

func (*DirAdapter) String added in v0.11.0

func (adapter *DirAdapter) String() string

String

func (*DirAdapter) WriteRows

func (adapter *DirAdapter) WriteRows(filename string, rows [][]string) error

WriteRows writes with only Flush at the end.

type OverlayAdapter

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

OverlayAdapter searches a specified list of directories for the specified file. Used for reducing the complexity of writing tests.

func NewOverlayAdapter

func NewOverlayAdapter(paths ...string) OverlayAdapter

NewOverlayAdapter returns a new OverlayAdapter.

func (OverlayAdapter) Close

func (adapter OverlayAdapter) Close() error

Close implements CSV Adapter.Close.

func (OverlayAdapter) CreateZip added in v0.8.5

func (adapter OverlayAdapter) CreateZip(outfile string) error

func (OverlayAdapter) DirSHA1

func (adapter OverlayAdapter) DirSHA1() (string, error)

DirSHA1 returns the SHA1 of all the .txt files in the main directory, sorted, and concatenated.

func (OverlayAdapter) Exists

func (adapter OverlayAdapter) Exists() bool

Exists implements CSV Adapter.Exists.

func (OverlayAdapter) Files added in v0.8.5

func (adapter OverlayAdapter) Files() ([]string, error)

func (OverlayAdapter) Open

func (adapter OverlayAdapter) Open() error

Open implements CSV Adapter Open.

func (OverlayAdapter) OpenFile

func (adapter OverlayAdapter) OpenFile(filename string, cb func(io.Reader)) error

OpenFile searches paths until it finds the specified file.

func (OverlayAdapter) Path

func (adapter OverlayAdapter) Path() string

Path implements CSV Adapter.Path.

func (OverlayAdapter) ReadRows

func (adapter OverlayAdapter) ReadRows(filename string, cb func(Row)) error

ReadRows implements CSV Adapter ReadRows.

func (OverlayAdapter) SHA1

func (adapter OverlayAdapter) SHA1() (string, error)

SHA1 is an alias for DirSHA1

func (OverlayAdapter) String added in v0.11.0

func (adapter OverlayAdapter) String() string

type Reader

type Reader struct {
	Adapter
}

Reader reads GTFS entities from CSV files.

func NewReader

func NewReader(path string) (*Reader, error)

NewReader returns an initialized CSV Reader.

func NewReaderFromAdapter added in v0.10.1

func NewReaderFromAdapter(a Adapter) (*Reader, error)

func (*Reader) Agencies

func (reader *Reader) Agencies() (out chan gtfs.Agency)

func (*Reader) Areas added in v0.11.0

func (reader *Reader) Areas() (out chan gtfs.Area)

func (*Reader) Attributions added in v0.10.0

func (reader *Reader) Attributions() (out chan gtfs.Attribution)

func (*Reader) CalendarDates

func (reader *Reader) CalendarDates() (out chan gtfs.CalendarDate)

func (*Reader) Calendars

func (reader *Reader) Calendars() (out chan gtfs.Calendar)

func (*Reader) ContainsFile

func (reader *Reader) ContainsFile(filename string) bool

ContainsFile checks if filename is present and contains a readable row.

func (*Reader) FareAttributes

func (reader *Reader) FareAttributes() (out chan gtfs.FareAttribute)

func (*Reader) FareLegRules added in v0.11.0

func (reader *Reader) FareLegRules() (out chan gtfs.FareLegRule)

func (*Reader) FareMedia added in v0.13.0

func (reader *Reader) FareMedia() (out chan gtfs.FareMedia)

func (*Reader) FareProducts added in v0.11.0

func (reader *Reader) FareProducts() (out chan gtfs.FareProduct)

func (*Reader) FareRules

func (reader *Reader) FareRules() (out chan gtfs.FareRule)

func (*Reader) FareTransferRules added in v0.11.0

func (reader *Reader) FareTransferRules() (out chan gtfs.FareTransferRule)

func (*Reader) FeedInfos

func (reader *Reader) FeedInfos() (out chan gtfs.FeedInfo)

func (*Reader) Frequencies

func (reader *Reader) Frequencies() (out chan gtfs.Frequency)

func (*Reader) Levels

func (reader *Reader) Levels() (out chan gtfs.Level)

func (*Reader) Pathways

func (reader *Reader) Pathways() (out chan gtfs.Pathway)

func (*Reader) ReadEntities

func (reader *Reader) ReadEntities(c interface{}) error

ReadEntities provides a generic interface for reading entities.

func (*Reader) RiderCategories added in v0.11.0

func (reader *Reader) RiderCategories() (out chan gtfs.RiderCategory)

func (*Reader) Routes

func (reader *Reader) Routes() (out chan gtfs.Route)

func (*Reader) Shapes

func (reader *Reader) Shapes() chan gtfs.Shape

Shapes sends single-geometry LineString Shapes

func (*Reader) StopAreas added in v0.11.0

func (reader *Reader) StopAreas() (out chan gtfs.StopArea)

func (*Reader) StopTimes

func (reader *Reader) StopTimes() (out chan gtfs.StopTime)

func (*Reader) StopTimesByTripID

func (reader *Reader) StopTimesByTripID(tripIDs ...string) chan []gtfs.StopTime

StopTimesByTripID sends StopTimes for selected trips.

func (*Reader) Stops

func (reader *Reader) Stops() (out chan gtfs.Stop)

func (*Reader) String added in v0.10.1

func (reader *Reader) String() string

func (*Reader) Transfers

func (reader *Reader) Transfers() (out chan gtfs.Transfer)

func (*Reader) Translations added in v0.10.0

func (reader *Reader) Translations() (out chan gtfs.Translation)

func (*Reader) Trips

func (reader *Reader) Trips() (out chan gtfs.Trip)

func (*Reader) ValidateStructure

func (reader *Reader) ValidateStructure() []error

ValidateStructure returns if all required CSV files are present.

type Row

type Row struct {
	Row    []string
	Header []string
	Hindex map[string]int
	Line   int
	Err    error
}

Row is a row value with a header.

func (*Row) Get

func (row *Row) Get(k string) (string, bool)

Get a value from the row as a string.

type TmpZipAdapter added in v0.12.0

type TmpZipAdapter struct {
	ZipAdapter
	// contains filtered or unexported fields
}

TmpZipAdapter is similar to ZipAdapter, but deletes the file on close.

func NewStoreAdapter added in v0.18.0

func NewStoreAdapter(storage string, key string, fragment string) (*TmpZipAdapter, error)

NewStoreAdapter is a convenience method for getting a GTFS Zip reader from the store.

func NewTmpZipAdapterFromReader added in v0.12.0

func NewTmpZipAdapterFromReader(reader io.Reader, fragment string) (*TmpZipAdapter, error)

func (*TmpZipAdapter) Close added in v0.12.0

func (adapter *TmpZipAdapter) Close() error

func (*TmpZipAdapter) Open added in v0.12.0

func (adapter *TmpZipAdapter) Open() error

type URLAdapter

type URLAdapter struct {
	TmpZipAdapter
	// contains filtered or unexported fields
}

URLAdapter downloads a GTFS URL to a temporary file, and removes the file when it is closed.

func NewURLAdapter added in v0.10.1

func NewURLAdapter(address string, opts ...request.RequestOption) *URLAdapter

func (*URLAdapter) Open

func (adapter *URLAdapter) Open() error

Open the adapter, and download the provided URL to a temporary file.

func (*URLAdapter) String added in v0.11.0

func (adapter *URLAdapter) String() string

type Writer

type Writer struct {
	WriterAdapter
	// contains filtered or unexported fields
}

Writer implements a GTFS CSV Writer.

func NewWriter

func NewWriter(path string) (*Writer, error)

NewWriter returns a new Writer.

func (*Writer) AddEntities

func (writer *Writer) AddEntities(ents []tt.Entity) ([]string, error)

AddEntities writes entities to the output.

func (*Writer) AddEntity

func (writer *Writer) AddEntity(ent tt.Entity) (string, error)

AddEntity writes an entity to the output.

func (*Writer) Create

func (writer *Writer) Create() error

Create the necessary files for the Writer.

func (*Writer) Delete

func (writer *Writer) Delete() error

Delete the Writer.

func (*Writer) NewReader

func (writer *Writer) NewReader() (adapters.Reader, error)

NewReader returns a new Reader for the Writer destination.

func (*Writer) String added in v0.10.1

func (writer *Writer) String() string

func (*Writer) WriteExtraColumns added in v0.11.0

func (writer *Writer) WriteExtraColumns(val bool)

type WriterAdapter

type WriterAdapter interface {
	WriteRows(string, [][]string) error
	Adapter
}

WriterAdapter provides a writing interface.

type ZipAdapter

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

ZipAdapter supports reading from zip archives.

func NewZipAdapter

func NewZipAdapter(path string) *ZipAdapter

NewZipAdapter returns an initialized zip adapter.

func (*ZipAdapter) Close

func (adapter *ZipAdapter) Close() error

Close the adapter.

func (*ZipAdapter) DirSHA1

func (adapter *ZipAdapter) DirSHA1() (string, error)

DirSHA1 returns the SHA1 of all the .txt files in the main directory, sorted, and concatenated.

func (*ZipAdapter) Exists

func (adapter *ZipAdapter) Exists() bool

Exists returns if the zip file exists.

func (*ZipAdapter) FileInfos

func (adapter *ZipAdapter) FileInfos() ([]os.FileInfo, error)

FileInfos returns a list of os.FileInfo for all .txt files.

func (*ZipAdapter) Open

func (adapter *ZipAdapter) Open() error

Open the adapter. Return an error if the file does not exist.

func (*ZipAdapter) OpenFile

func (adapter *ZipAdapter) OpenFile(filename string, cb func(io.Reader)) error

OpenFile opens the file inside the archive and passes it to the provided callback.

func (*ZipAdapter) Path

func (adapter *ZipAdapter) Path() string

Path returns the path to the zip file.

func (*ZipAdapter) ReadRows

func (adapter *ZipAdapter) ReadRows(filename string, cb func(Row)) error

ReadRows opens the specified file and runs the callback on each Row. An error is returned if the file cannot be read.

func (*ZipAdapter) SHA1

func (adapter *ZipAdapter) SHA1() (string, error)

SHA1 returns the SHA1 checksum of the zip archive.

func (*ZipAdapter) String added in v0.11.0

func (adapter *ZipAdapter) String() string

type ZipWriterAdapter

type ZipWriterAdapter struct {
	DirAdapter
	// contains filtered or unexported fields
}

ZipWriterAdapter functions the same as DirAdapter, but writes to a temporary directory, and creates a zip archive when closed.

func NewZipWriterAdapter

func NewZipWriterAdapter(path string) *ZipWriterAdapter

NewZipWriterAdapter returns a new ZipWriterAdapter.

func (*ZipWriterAdapter) Close

func (adapter *ZipWriterAdapter) Close() error

Close creates a zip archive of all the written files at the specified destination.

Jump to

Keyboard shortcuts

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