@@ -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
5253func (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 {
0 commit comments