Skip to content

Commit 7c353de

Browse files
authored
Transformation filter (#509)
* Transformation filter * Remove * Fix * Exclude unused routes option * Tests * go generate
1 parent cbcf2cb commit 7c353de

30 files changed

+454
-53
lines changed

cmds/extract_cmd.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,27 @@ type ExtractCommand struct {
2727
create bool
2828
extensionDefs []string
2929
// extract specific arguments
30-
Prefix string
31-
PrefixFilesInclude []string
32-
PrefixFilesExclude []string
33-
extractAgencies []string
34-
extractStops []string
35-
extractTrips []string
36-
extractCalendars []string
37-
extractRoutes []string
38-
extractRouteTypes []string
39-
extractSet []string
40-
excludeAgencies []string
41-
excludeStops []string
42-
excludeTrips []string
43-
excludeCalendars []string
44-
excludeRoutes []string
45-
excludeRouteTypes []string
46-
bbox string
47-
writeExtraColumns bool
48-
readerPath string
49-
writerPath string
30+
excludeUnusedRoutes bool
31+
Prefix string
32+
PrefixFilesInclude []string
33+
PrefixFilesExclude []string
34+
extractAgencies []string
35+
extractStops []string
36+
extractTrips []string
37+
extractCalendars []string
38+
extractRoutes []string
39+
extractRouteTypes []string
40+
extractSet []string
41+
excludeAgencies []string
42+
excludeStops []string
43+
excludeTrips []string
44+
excludeCalendars []string
45+
excludeRoutes []string
46+
excludeRouteTypes []string
47+
bbox string
48+
writeExtraColumns bool
49+
readerPath string
50+
writerPath string
5051
}
5152

5253
func (cmd *ExtractCommand) HelpDesc() (string, string) {
@@ -94,6 +95,7 @@ func (cmd *ExtractCommand) AddFlags(fl *pflag.FlagSet) {
9495
fl.StringArrayVar(&cmd.extensionDefs, "ext", nil, "Include GTFS Extension")
9596
fl.IntVar(&cmd.fvid, "fvid", 0, "Specify FeedVersionID when writing to a database")
9697
fl.BoolVar(&cmd.create, "create", false, "Create a basic database schema if none exists")
98+
9799
// Copy options
98100
fl.Float64Var(&cmd.SimplifyShapes, "simplify-shapes", 0.0, "Simplify shapes with this tolerance (ex. 0.000005)")
99101
fl.BoolVar(&cmd.AllowEntityErrors, "allow-entity-errors", false, "Allow entities with errors to be copied")
@@ -124,6 +126,7 @@ func (cmd *ExtractCommand) AddFlags(fl *pflag.FlagSet) {
124126
fl.StringArrayVar(&cmd.excludeCalendars, "exclude-calendar", nil, "Exclude Calendar")
125127
fl.StringArrayVar(&cmd.excludeRoutes, "exclude-route", nil, "Exclude Route")
126128
fl.StringArrayVar(&cmd.excludeRouteTypes, "exclude-route-type", nil, "Exclude Routes matching route_type")
129+
fl.BoolVar(&cmd.excludeUnusedRoutes, "exclude-unused-routes", false, "Exclude routes that have no trips in the source data")
127130

128131
fl.StringVar(&cmd.bbox, "bbox", "", "Extract bbox as (min lon, min lat, max lon, max lat), e.g. -122.276,37.794,-122.259,37.834")
129132

@@ -179,6 +182,20 @@ func (cmd *ExtractCommand) Run(ctx context.Context) error {
179182
cmd.Options.AddExtension(pfx)
180183
}
181184

185+
// Additional exclude
186+
if cmd.excludeUnusedRoutes {
187+
log.For(ctx).Debug().Msgf("Extract filter: excluding unused routes")
188+
usedRoutes := map[string]bool{}
189+
for trip := range reader.Trips() {
190+
usedRoutes[trip.RouteID.Val] = true
191+
}
192+
for route := range reader.Routes() {
193+
if _, used := usedRoutes[route.RouteID.Val]; !used {
194+
cmd.excludeRoutes = append(cmd.excludeRoutes, route.RouteID.Val)
195+
}
196+
}
197+
}
198+
182199
// Create SetterFilter
183200
setvalues := [][]string{}
184201
for _, setv := range cmd.extractSet {

copier/copier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func NewCopier(ctx context.Context, reader adapters.Reader, writer adapters.Writ
340340
// Add option extensions
341341
for _, e := range addExtLevels {
342342
if err := copier.addExtension(e.ext, e.level); err != nil {
343-
return nil, fmt.Errorf("failed to add extension: %s", err.Error())
343+
return nil, fmt.Errorf("failed to add extension '%T': %s", e.ext, err.Error())
344344
}
345345
}
346346
return copier, nil

doc/cli/transitland.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ transitland-lib utilities
3131
* [transitland validate](transitland_validate.md) - Validate a GTFS feed
3232
* [transitland version](transitland_version.md) - Program version and supported GTFS and GTFS-RT versions
3333

34-
###### Auto generated by spf13/cobra on 25-Sep-2025
34+
###### Auto generated by spf13/cobra on 14-Oct-2025

doc/cli/transitland_checksum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ transitland checksum <feed-path> [flags]
4141

4242
* [transitland](transitland.md) - transitland-lib utilities
4343

44-
###### Auto generated by spf13/cobra on 25-Sep-2025
44+
###### Auto generated by spf13/cobra on 14-Oct-2025

doc/cli/transitland_completion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ See each sub-command's help for details on how to use the generated script.
2222
* [transitland completion powershell](transitland_completion_powershell.md) - Generate the autocompletion script for powershell
2323
* [transitland completion zsh](transitland_completion_zsh.md) - Generate the autocompletion script for zsh
2424

25-
###### Auto generated by spf13/cobra on 25-Sep-2025
25+
###### Auto generated by spf13/cobra on 14-Oct-2025

doc/cli/transitland_completion_bash.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ transitland completion bash
4141

4242
* [transitland completion](transitland_completion.md) - Generate the autocompletion script for the specified shell
4343

44-
###### Auto generated by spf13/cobra on 25-Sep-2025
44+
###### Auto generated by spf13/cobra on 14-Oct-2025

doc/cli/transitland_completion_fish.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ transitland completion fish [flags]
3232

3333
* [transitland completion](transitland_completion.md) - Generate the autocompletion script for the specified shell
3434

35-
###### Auto generated by spf13/cobra on 25-Sep-2025
35+
###### Auto generated by spf13/cobra on 14-Oct-2025

doc/cli/transitland_completion_powershell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ transitland completion powershell [flags]
2929

3030
* [transitland completion](transitland_completion.md) - Generate the autocompletion script for the specified shell
3131

32-
###### Auto generated by spf13/cobra on 25-Sep-2025
32+
###### Auto generated by spf13/cobra on 14-Oct-2025

doc/cli/transitland_completion_zsh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ transitland completion zsh [flags]
4343

4444
* [transitland completion](transitland_completion.md) - Generate the autocompletion script for the specified shell
4545

46-
###### Auto generated by spf13/cobra on 25-Sep-2025
46+
###### Auto generated by spf13/cobra on 14-Oct-2025

doc/cli/transitland_copy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ BART,Bay Area Rapid Transit,https://www.bart.gov/,America/Los_Angeles,,510-464-6
4242

4343
* [transitland](transitland.md) - transitland-lib utilities
4444

45-
###### Auto generated by spf13/cobra on 25-Sep-2025
45+
###### Auto generated by spf13/cobra on 14-Oct-2025

0 commit comments

Comments
 (0)