forked from patrickbr/gtfstidy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtfsclean.go
956 lines (814 loc) · 32.6 KB
/
gtfsclean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
// Copyright 2016 Patrick Brosi
// Authors: info@patrickbrosi.de
//
// Use of this source code is governed by a GPL v2
// license that can be found in the LICENSE file
package main
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
_ "time/tzdata"
geojson "github.com/paulmach/go.geojson"
"github.com/public-transport/gtfsclean/processors"
"github.com/public-transport/gtfsparser"
"github.com/public-transport/gtfsparser/gtfs"
"github.com/public-transport/gtfswriter"
flag "github.com/spf13/pflag"
)
func getGtfsPoly(poly [][][]float64) gtfsparser.Polygon {
outer := make([][2]float64, len(poly[0]))
inners := make([][][2]float64, 0)
for i, c := range poly[0] {
outer[i] = [2]float64{c[0], c[1]}
}
for i := 1; i < len(poly); i++ {
inners = append(inners, make([][2]float64, len(poly[i])))
for j, c := range poly[i] {
inners[i-1][j] = [2]float64{c[0], c[1]}
}
}
return gtfsparser.NewPolygon(outer, inners)
}
func parseDate(str string) gtfs.Date {
var day, month, year int
var e error
if len(str) < 8 {
e = fmt.Errorf("only has %d characters, expected 8", len(str))
}
if e == nil {
day, e = strconv.Atoi(str[6:8])
}
if e == nil {
month, e = strconv.Atoi(str[4:6])
}
if e == nil {
year, e = strconv.Atoi(str[0:4])
}
if e == nil && day < 1 || day > 31 {
e = fmt.Errorf("day must be in the range [1, 31]")
}
if e == nil && month < 1 || month > 12 {
e = fmt.Errorf("month must be in the range [1, 12]")
}
if e == nil && year < 1900 || year > (1900+255) {
e = fmt.Errorf("date must be in the range [19000101, 21551231]")
}
if e != nil {
panic(fmt.Errorf("expected YYYYMMDD date, found '%s' (%s)", str, e.Error()))
}
return gtfs.NewDate(uint8(day), uint8(month), uint16(year))
}
func parseCoords(s string) ([][2]float64, error) {
coords := strings.Split(s, ",")
if len(coords)%2 != 0 {
return nil, errors.New("uneven number of coordinates")
}
ret := make([][2]float64, 0)
for i := 0; i < len(coords)/2; i++ {
var x, y float64
var err error
y, err = strconv.ParseFloat(strings.Trim(coords[i*2], "\n "), 64)
if err == nil {
x, err = strconv.ParseFloat(strings.Trim(coords[i*2+1], "\n "), 64)
}
if err != nil {
return nil, err
}
coord := [2]float64{x, y}
ret = append(ret, coord)
}
return ret, nil
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "gtfstidy - (C) 2016-2023 by Patrick Brosi <info@patrickbrosi.de>\ngtfsclean - (C) by Transitous Team\nUsage:\n\n %s [<options>] [-o <outputfile>] <input GTFS>\n\nAllowed options:\n\n", os.Args[0])
flag.PrintDefaults()
}
polys := make([]gtfsparser.Polygon, 0)
var bboxStrings []string
var polygonStrings []string
var polygonFiles []string
onlyValidate := flag.BoolP("validation-mode", "v", false, "only validate the feed, no processors will be called")
outputPath := flag.StringP("output", "o", "gtfs-out", "gtfs output directory or zip file (must end with .zip)")
startDateFilter := flag.StringP("date-start", "", "", "start date filter, as YYYYMMDD")
endDateFilter := flag.StringP("date-end", "", "", "end date filter, as YYYYMMDD")
fixShortHand := flag.BoolP("fix", "", false, "shorthand for -eDnz -p '-'")
compressShortHand := flag.BoolP("compress", "", false, "shorthand for -OSRCcIAP")
minimizeShortHand := flag.BoolP("Compress", "", false, "shorthand for -OSRCcIAPdT --red-stops-fuzzy --red-trips-fuzzy, like --compress, but additionally compress stop times into frequencies, use fuzzy matching for redundant trip and stop removal and use dense character ids. The latter destroys any existing external references (like in GTFS realtime streams)")
mergeShortHand := flag.BoolP("merge", "", false, "shorthand for -ARPICO")
fuzzyMergeShortHand := flag.BoolP("Merge", "", false, "shorthand for -EARPICO --red-trips-fuzzy --red-stops-fuzzy")
assumeCleanCsv := flag.BoolP("assume-clean-csv", "", false, "assume clean csv (no leading spaces, clean line breaks)")
useDefaultValuesOnError := flag.BoolP("default-on-errs", "e", false, "if non-required fields have errors, fall back to the default values")
fixZip := flag.BoolP("fix-zip", "z", false, "try to fix some errors in the ZIP file directory hierarchy")
emptyStrRepl := flag.StringP("empty-str-repl", "p", "", "string to use if a non-critical required string field is empty (like stop_name, agency_name, ...)")
emptyAgencyUrlRepl := flag.StringP("empty-agency-url-repl", "", "", "url to use if agency_url is empty")
dropErroneousEntities := flag.BoolP("drop-errs", "D", false, "drop erroneous entries from feed")
checkNullCoords := flag.BoolP("check-null-coords", "n", false, "check for (0, 0) coordinates")
dropPlatformCodesForParentless := flag.BoolP("drop-platform-for-parentless", "", false, "drop platform codes for parentless stops")
nonOverlappingServices := flag.BoolP("non-overlapping-services", "", false, "create non-overlapping services")
groupAdjEquStops := flag.BoolP("group-adj-stop-times", "", false, "group adjacent stop times with eqv. stops")
removeFillers := flag.BoolP("remove-fillers", "", false, "remove fill values (., .., .., -, ?) from some optional fields")
idPrefix := flag.StringP("prefix", "", "", "prefix used before all ids")
keepIds := flag.BoolP("keep-ids", "", false, "preserve station, fare, shape, route, trip, level, agency, pathway, and service IDs")
keepStationIds := flag.BoolP("keep-station-ids", "", false, "preserve station IDs")
keepBlockIds := flag.BoolP("keep-block-ids", "", false, "preserve block IDs")
keepFareIds := flag.BoolP("keep-fare-ids", "", false, "preserve fare IDs")
keepShapeIds := flag.BoolP("keep-shape-ids", "", false, "preserve shape IDs")
keepRouteIds := flag.BoolP("keep-route-ids", "", false, "preserve route IDs")
keepTripIds := flag.BoolP("keep-trip-ids", "", false, "preserve trip IDs")
keepLevelIds := flag.BoolP("keep-level-ids", "", false, "preserve level IDs")
keepPathwayIds := flag.BoolP("keep-pathway-ids", "", false, "preserve pathway IDs")
keepAttributionIds := flag.BoolP("keep-attribution-ids", "", false, "preserve attribution IDs")
keepServiceIds := flag.BoolP("keep-service-ids", "", false, "preserve service IDs in calendar.txt and calendar_dates.txt")
keepAgencyIds := flag.BoolP("keep-agency-ids", "", false, "preserve agency IDs")
useOrphanDeleter := flag.BoolP("delete-orphans", "O", false, "remove entities that are not referenced anywhere")
useShapeMinimizer := flag.BoolP("min-shapes", "s", false, "minimize shapes (using Douglas-Peucker)")
useShapeRemeasurer := flag.BoolP("remeasure-shapes", "m", false, "remeasure shapes (filling measurement-holes)")
dropSingleStopTrips := flag.BoolP("drop-single-stop-trips", "", false, "drop trips with only 1 stop")
useShapeSnapper := flag.BoolP("snap-stops", "", false, "snap stop points to shape if dist > 100 m")
useRedShapeRemover := flag.BoolP("remove-red-shapes", "S", false, "remove shape duplicates")
useRedRouteMinimizer := flag.BoolP("remove-red-routes", "R", false, "remove route duplicates")
useRedServiceMinimizer := flag.BoolP("remove-red-services", "C", false, "remove duplicate services in calendar.txt and calendar_dates.txt")
useIDMinimizerNum := flag.BoolP("minimize-ids-num", "i", false, "minimize IDs using numerical IDs (e.g. 144, 145, 146...)")
useIDMinimizerChar := flag.BoolP("minimize-ids-char", "d", false, "minimize IDs using character IDs (e.g. abc, abd, abe, abf...)")
useServiceMinimizer := flag.BoolP("minimize-services", "c", false, "minimize services by searching for the optimal exception/range coverage")
useFrequencyMinimizer := flag.BoolP("minimize-stoptimes", "T", false, "search for frequency patterns in explicit trips and combine them, using a CAP approach")
useCalDatesRemover := flag.BoolP("remove-cal-dates", "", false, "don't use calendar_dates.txt")
explicitCals := flag.BoolP("explicit-calendar", "", false, "add calendar.txt entry for every service, even irregular ones")
ensureParents := flag.BoolP("ensure-stop-parents", "", false, "ensure that every stop (location_type=0) has a parent station")
keepColOrder := flag.BoolP("keep-col-order", "", false, "keep the original column ordering of the input feed")
keepFields := flag.BoolP("keep-additional-fields", "F", false, "keep all non-GTFS fields from the input")
dropTooFast := flag.BoolP("drop-too-fast-trips", "", false, "drop trips that are too fast to realistically occur")
useRedStopMinimizer := flag.BoolP("remove-red-stops", "P", false, "remove stop and level duplicates")
useRedTripMinimizer := flag.BoolP("remove-red-trips", "I", false, "remove trip duplicates")
useRedTripMinimizerFuzzyRoute := flag.BoolP("red-trips-fuzzy", "", false, "only check MOT of routes for trip duplicate removal")
redTripMinimizerAggressive := flag.BoolP("red-trips-aggressive", "", false, "aggressive merging of equal trips, even if this would create complicated services")
useRedStopsMinimizerFuzzy := flag.BoolP("red-stops-fuzzy", "", false, "fuzzy station match for station duplicate removal")
useRedAgencyMinimizer := flag.BoolP("remove-red-agencies", "A", false, "remove agency duplicates")
useStopReclusterer := flag.BoolP("recluster-stops", "E", false, "recluster stops")
useStopAverager := flag.BoolP("fix-far-away-parents", "", false, "try to fix too far away parent stations by averaging their position to childrens")
dropShapes := flag.BoolP("drop-shapes", "", false, "drop shapes")
polygonFilterCompleteTrips := flag.BoolP("complete-filtered-trips", "", false, "always include complete data for trips filtered e.g. using a geo filter")
flag.StringArrayVar(&bboxStrings, "bounding-box", []string{}, "bounding box filter, as comma separated latitude,longitude pairs (multiple boxes allowed by defining --bounding-box multiple times)")
flag.StringArrayVar(&polygonStrings, "polygon", []string{}, "polygon filter, as comma separated latitude,longitude pairs (multiple polygons allowed by defining --polygon multiple times)")
flag.StringArrayVar(&polygonFiles, "polygon-file", []string{}, "polygon filter, as a file containing comma separated latitude,longitude pairs (multiple polygons allowed by defining --polygon-file multiple times), or a GeoJSON file ending with .geojson or .json")
showWarnings := flag.BoolP("show-warnings", "W", false, "show warnings")
minHeadway := flag.IntP("min-headway", "", 1, "min allowed headway (in seconds) for frequency found with -T")
maxHeadway := flag.IntP("max-headway", "", 3600*24, "min allowed headway (in seconds) for frequency found with -T")
zipCompressionLevel := flag.IntP("zip-compression-level", "", 9, "output ZIP file compression level, between 0 and 9")
dontSortZipFiles := flag.BoolP("unsorted-files", "", false, "don't sort the output ZIP files (might increase final ZIP size)")
useStandardRouteTypes := flag.BoolP("standard-route-types", "", false, "Always use standard route types")
useGoogleSupportedRouteTypes := flag.BoolP("google-supported-route-types", "", false, "Only use (extended) route types supported by Google")
motFilterStr := flag.StringP("keep-mots", "M", "", "comma-separated list of MOTs to keep, empty filter (default) keeps all")
motFilterNegStr := flag.StringP("drop-mots", "N", "", "comma-separated list of MOTs to drop")
help := flag.BoolP("help", "?", false, "this message")
flag.Parse()
if *help {
flag.Usage()
return
}
gtfsPaths := flag.Args()
if len(gtfsPaths) == 0 {
fmt.Fprintln(os.Stderr, "No GTFS location specified, see --help")
os.Exit(1)
}
defer func() {
if r := recover(); r != nil {
fmt.Fprintln(os.Stderr, "Error:", r)
}
}()
motFilter := make(map[int16]bool, 0)
motFilterNeg := make(map[int16]bool, 0)
if motFilterStr != nil {
for _, s := range strings.Split(*motFilterStr, ",") {
s = strings.TrimSpace(s)
if len(s) == 0 {
continue
}
i, err := strconv.Atoi(s)
if err != nil {
panic(fmt.Errorf("%s is not a valid GTFS MOT", s))
}
motFilter[int16(i)] = true
}
}
if motFilterNegStr != nil {
for _, s := range strings.Split(*motFilterNegStr, ",") {
s = strings.TrimSpace(s)
if len(s) == 0 {
continue
}
i, err := strconv.Atoi(s)
if err != nil {
panic(fmt.Errorf("%s is not a valid GTFS MOT", s))
}
motFilterNeg[int16(i)] = true
}
}
startDate := gtfs.Date{}
endDate := gtfs.Date{}
if len(*startDateFilter) > 0 {
startDate = parseDate(*startDateFilter)
}
if len(*endDateFilter) > 0 {
endDate = parseDate(*endDateFilter)
}
if *keepIds {
*keepStationIds = true
*keepFareIds = true
*keepShapeIds = true
*keepRouteIds = true
*keepTripIds = true
*keepLevelIds = true
*keepServiceIds = true
*keepAgencyIds = true
*keepBlockIds = true
*keepPathwayIds = true
}
if *fixShortHand {
*useDefaultValuesOnError = true
*dropErroneousEntities = true
*checkNullCoords = true
*fixZip = true
*emptyStrRepl = "-"
}
if *fuzzyMergeShortHand {
*mergeShortHand = true
*useRedTripMinimizerFuzzyRoute = true
*useStopReclusterer = true
*useRedStopsMinimizerFuzzy = true
}
if *mergeShortHand {
*useServiceMinimizer = true
*useRedServiceMinimizer = true
*useRedTripMinimizer = true
*useRedAgencyMinimizer = true
*useRedStopMinimizer = true
*useRedRouteMinimizer = true
*useOrphanDeleter = true
}
if *minimizeShortHand {
*compressShortHand = true
*useIDMinimizerChar = true
*useFrequencyMinimizer = true
*useRedTripMinimizerFuzzyRoute = true
*useRedStopsMinimizerFuzzy = true
}
if *compressShortHand {
*useOrphanDeleter = true
*useShapeMinimizer = true
*useRedShapeRemover = true
*useRedRouteMinimizer = true
*useRedServiceMinimizer = true
*useRedStopMinimizer = true
*useServiceMinimizer = true
*useRedTripMinimizer = true
*useRedAgencyMinimizer = true
}
for _, polyFile := range polygonFiles {
if strings.HasSuffix(polyFile, ".json") || strings.HasSuffix(polyFile, ".geojson") {
json, err := os.ReadFile(polyFile)
if err != nil {
fmt.Fprintf(os.Stderr, "\nCould not parse polygon filter file: ")
fmt.Fprintf(os.Stderr, err.Error()+".\n")
os.Exit(1)
}
fc1, err := geojson.UnmarshalFeatureCollection(json)
if err != nil {
fmt.Fprintf(os.Stderr, "\nCould not parse polygon filter file: ")
fmt.Fprintf(os.Stderr, err.Error()+".\n")
os.Exit(1)
}
for _, feature := range fc1.Features {
if feature.Geometry.IsMultiPolygon() {
for _, poly := range feature.Geometry.MultiPolygon {
polys = append(polys, getGtfsPoly(poly))
}
}
if feature.Geometry.IsPolygon() {
polys = append(polys, getGtfsPoly(feature.Geometry.Polygon))
}
}
} else {
bytes, err := os.ReadFile(polyFile)
if err != nil {
fmt.Fprintf(os.Stderr, "\nCould not parse polygon filter file: ")
fmt.Fprintf(os.Stderr, err.Error()+".\n")
os.Exit(1)
}
polygonStrings = append(polygonStrings, string(bytes))
}
}
for _, polyString := range polygonStrings {
poly := make([][2]float64, 0)
if len(polyString) > 0 {
var err error
poly, err = parseCoords(polyString)
if err != nil {
fmt.Fprintf(os.Stderr, "\nCould not parse polygon filter: ")
fmt.Fprintf(os.Stderr, err.Error()+".\n")
os.Exit(1)
}
}
// ensure polygon is closed
if len(poly) > 1 && (poly[0][0] != poly[len(poly)-1][0]) {
poly = append(poly, [2]float64{poly[0][0], poly[0][1]})
}
polys = append(polys, gtfsparser.NewPolygon(poly, make([][][2]float64, 0)))
}
for _, bboxString := range bboxStrings {
bbox := make([][2]float64, 0)
bboxString = strings.Trim(bboxString, " ")
if len(bboxString) > 0 {
var err error
bbox, err = parseCoords(bboxString)
if err != nil {
fmt.Fprintf(os.Stderr, "\nCould not parse bounding box filter: ")
fmt.Fprintf(os.Stderr, err.Error()+".\n")
os.Exit(1)
}
}
if len(bbox) == 2 {
poly := make([][2]float64, 4)
poly[0] = [2]float64{bbox[0][0], bbox[0][1]}
poly[1] = [2]float64{bbox[0][0], bbox[1][1]}
poly[2] = [2]float64{bbox[1][0], bbox[1][1]}
poly[3] = [2]float64{bbox[1][0], bbox[0][1]}
// ensure polygon is closed
if len(poly) > 1 && (poly[0][0] != poly[len(poly)-1][0]) {
poly = append(poly, [2]float64{poly[0][0], poly[0][1]})
}
polys = append(polys, gtfsparser.NewPolygon(poly, make([][][2]float64, 0)))
}
}
feed := gtfsparser.NewFeed()
opts := gtfsparser.ParseOptions{UseDefValueOnError: false, DropErroneous: false, DryRun: *onlyValidate, CheckNullCoordinates: false, EmptyStringRepl: "", ZipFix: false, UseStandardRouteTypes: *useStandardRouteTypes, MOTFilter: motFilter, MOTFilterNeg: motFilterNeg, AssumeCleanCsv: *assumeCleanCsv, RemoveFillers: *removeFillers, UseGoogleSupportedRouteTypes: *useGoogleSupportedRouteTypes, DropSingleStopTrips: *dropSingleStopTrips}
opts.DropErroneous = *dropErroneousEntities && !*onlyValidate
opts.UseDefValueOnError = *useDefaultValuesOnError && !*onlyValidate
opts.CheckNullCoordinates = *checkNullCoords
opts.EmptyStringRepl = *emptyStrRepl
if *emptyAgencyUrlRepl != "" {
opts.EmptyAgencyUrlRepl = *emptyAgencyUrlRepl
}
opts.ZipFix = *fixZip
opts.ShowWarnings = *showWarnings
opts.DropShapes = *dropShapes
opts.KeepAddFlds = *keepFields
opts.DateFilterStart = startDate
opts.DateFilterEnd = endDate
if !*polygonFilterCompleteTrips {
// only use built-in polygon filter if trips should not be completed
opts.PolygonFilter = polys
}
feed.SetParseOpts(opts)
var e error
if *onlyValidate {
for _, gtfsPath := range gtfsPaths {
locFeed := gtfsparser.NewFeed()
locFeed.SetParseOpts(opts)
fmt.Fprintf(os.Stdout, "Parsing GTFS feed in '%s' ...", gtfsPath)
if opts.ShowWarnings {
fmt.Fprintf(os.Stdout, "\n")
}
e = locFeed.Parse(gtfsPath)
if e != nil {
fmt.Fprintf(os.Stderr, "\nError while parsing GTFS feed:\n")
fmt.Fprintln(os.Stderr, e.Error())
os.Exit(1)
}
if opts.ShowWarnings {
fmt.Fprintf(os.Stdout, "... done.\n")
} else {
fmt.Fprintf(os.Stdout, " done.\n")
}
}
fmt.Fprintln(os.Stdout, "No errors.")
os.Exit(0)
}
prefixes := make(map[string]bool, 0)
for i, gtfsPath := range gtfsPaths {
fmt.Fprintf(os.Stdout, "Parsing GTFS feed in '%s' ...", gtfsPath)
if opts.ShowWarnings {
fmt.Fprintf(os.Stdout, "\n")
}
if len(gtfsPaths) > 1 {
prefix := strconv.FormatInt(int64(i), 10) + "#"
if len(*idPrefix) > 0 {
prefix = *idPrefix + prefix
}
prefixes[prefix] = true
e = feed.PrefixParse(gtfsPath, prefix)
} else if len(*idPrefix) > 0 {
prefix := *idPrefix
prefixes[prefix] = true
e = feed.PrefixParse(gtfsPath, prefix)
} else {
e = feed.Parse(gtfsPath)
}
if e != nil {
break
}
if opts.DropErroneous {
s := feed.ErrorStats
if opts.ShowWarnings {
fmt.Fprintf(os.Stdout, "... done.")
} else {
fmt.Fprintf(os.Stdout, " done.")
}
fmt.Fprintf(os.Stdout, " (%d trips [%.2f%%], %d stop times [%.2f%%], %d stops [%.2f%%], %d shapes [%.2f%%], %d services [%.2f%%], %d routes [%.2f%%], %d agencies [%.2f%%], %d transfers [%.2f%%], %d pathways [%.2f%%], %d levels [%.2f%%], %d fare attributes [%.2f%%], %d translations [%.2f%%] dropped due to errors.",
s.DroppedTrips,
100.0*float64(s.DroppedTrips)/(float64(s.DroppedTrips+len(feed.Trips))+0.001),
s.DroppedStopTimes,
100.0*float64(s.DroppedStopTimes)/(float64(s.DroppedStopTimes+feed.NumStopTimes)+0.001),
s.DroppedStops,
100.0*float64(s.DroppedStops)/(float64(s.DroppedStops+len(feed.Stops))+0.001),
s.DroppedShapes,
100.0*float64(s.DroppedShapes)/(float64(s.DroppedShapes+feed.NumShpPoints)+0.001),
s.DroppedServices,
100.0*float64(s.DroppedServices)/(float64(s.DroppedServices+len(feed.Services))+0.001),
s.DroppedRoutes,
100.0*float64(s.DroppedRoutes)/(float64(s.DroppedRoutes+len(feed.Routes))+0.001),
s.DroppedAgencies,
100.0*float64(s.DroppedAgencies)/(float64(s.DroppedAgencies+len(feed.Agencies))+0.001),
s.DroppedTransfers,
100.0*float64(s.DroppedTransfers)/(float64(s.DroppedTransfers+len(feed.Transfers))+0.001),
s.DroppedPathways,
100.0*float64(s.DroppedPathways)/(float64(s.DroppedPathways+len(feed.Pathways))+0.001),
s.DroppedLevels,
100.0*float64(s.DroppedLevels)/(float64(s.DroppedLevels+len(feed.Levels))+0.001),
s.DroppedFareAttributes,
100.0*float64(s.DroppedFareAttributes)/(float64(s.DroppedFareAttributes+len(feed.FareAttributes))+0.001),
s.DroppedTranslations,
100.0*float64(s.DroppedTranslations)/(float64(s.DroppedTranslations+s.NumTranslations)+0.001))
if !opts.ShowWarnings && (s.DroppedTrips+s.DroppedStops+s.DroppedShapes+s.DroppedServices+s.DroppedRoutes+s.DroppedAgencies+s.DroppedTransfers+s.DroppedPathways+s.DroppedLevels+s.DroppedFareAttributes+s.DroppedTranslations) > 0 {
fmt.Fprintf(os.Stdout, " Use -W to display them.")
}
fmt.Print(")\n")
} else {
fmt.Fprintf(os.Stdout, " done.\n")
}
}
if e != nil {
fmt.Fprintf(os.Stderr, "\nError while parsing GTFS feed:\n")
fmt.Fprintln(os.Stderr, e.Error())
fmt.Fprintln(os.Stdout, "\nYou may want to try running gtfsclean with --fix for error fixing / skipping. See --help for details.")
os.Exit(1)
} else {
minzers := make([]processors.Processor, 0)
if *dropTooFast {
minzers = append(minzers, processors.TooFastTripRemover{})
}
if *polygonFilterCompleteTrips {
minzers = append(minzers, processors.CompleteTripsGeoFilter{Polygons: polys})
}
if *useOrphanDeleter {
minzers = append(minzers, processors.OrphanRemover{})
}
if *useRedAgencyMinimizer {
minzers = append(minzers, processors.AgencyDuplicateRemover{})
}
if *useStopAverager {
minzers = append(minzers, processors.StopParentAverager{
MaxDist: 100,
})
}
if *useRedStopMinimizer {
minzers = append(minzers, processors.StopDuplicateRemover{
DistThresholdStop: 5.0,
DistThresholdStation: 50,
Fuzzy: *useRedStopsMinimizerFuzzy,
})
}
if *useStopReclusterer {
minzers = append(minzers, processors.StopReclusterer{
DistThreshold: 75,
NameSimiThreshold: 0.55,
GridCellSize: 10000,
})
}
if *dropPlatformCodesForParentless {
minzers = append(minzers, processors.PlatformCodeDropper{})
// remove redundant stops again
minzers = append(minzers, processors.StopDuplicateRemover{
DistThresholdStop: 5.0,
DistThresholdStation: 50,
Fuzzy: *useRedStopsMinimizerFuzzy,
})
}
if *useShapeRemeasurer || *useShapeMinimizer || *useRedShapeRemover {
minzers = append(minzers, processors.ShapeRemeasurer{})
}
if *useShapeMinimizer {
minzers = append(minzers, processors.ShapeMinimizer{Epsilon: 1.0})
}
if *useShapeSnapper {
minzers = append(minzers, processors.ShapeSnapper{MaxDist: 100.0})
if *useRedStopMinimizer {
minzers = append(minzers, processors.StopDuplicateRemover{
DistThresholdStop: 5.0,
DistThresholdStation: 50,
Fuzzy: *useRedStopsMinimizerFuzzy,
})
}
// may have created route and stop orphans
if *useOrphanDeleter {
minzers = append(minzers, processors.OrphanRemover{})
}
}
if *useRedShapeRemover {
minzers = append(minzers, processors.ShapeDuplicateRemover{MaxEqDist: 1.0})
}
if *useRedRouteMinimizer {
minzers = append(minzers, processors.RouteDuplicateRemover{})
}
if *useRedServiceMinimizer {
minzers = append(minzers, processors.ServiceDuplicateRemover{})
}
if *groupAdjEquStops {
minzers = append(minzers, processors.AdjacentStopTimeGrouper{})
}
if *useRedTripMinimizer {
// to convert calendar_dates based services into regular calendar.txt services
// before concatenating equivalent trips
if *useServiceMinimizer {
minzers = append(minzers, processors.ServiceMinimizer{})
}
minzers = append(minzers, processors.TripDuplicateRemover{Fuzzy: *useRedTripMinimizerFuzzyRoute, Aggressive: *redTripMinimizerAggressive, MaxDayDist: 7})
// may have created route and stop orphans
if *useOrphanDeleter {
minzers = append(minzers, processors.OrphanRemover{})
}
// may have created service duplicates
if *useRedServiceMinimizer {
minzers = append(minzers, processors.ServiceDuplicateRemover{})
}
}
if *nonOverlappingServices {
minzers = append(minzers, processors.ServiceNonOverlapper{DayNames: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}, YearWeekName: "WW"})
}
if *useServiceMinimizer {
minzers = append(minzers, processors.ServiceMinimizer{})
}
if *useFrequencyMinimizer {
minzers = append(minzers, processors.FrequencyMinimizer{MinHeadway: *minHeadway, MaxHeadway: *maxHeadway})
}
if *useCalDatesRemover {
minzers = append(minzers, processors.ServiceCalDatesRem{})
}
if *ensureParents {
minzers = append(minzers, processors.StopParentEnforcer{})
}
if *useIDMinimizerNum {
minzers = append(minzers, processors.IDMinimizer{Prefix: *idPrefix, Base: 10, KeepStations: *keepStationIds, KeepBlocks: *keepBlockIds, KeepFares: *keepFareIds, KeepShapes: *keepShapeIds, KeepRoutes: *keepRouteIds, KeepTrips: *keepTripIds, KeepLevels: *keepLevelIds, KeepServices: *keepServiceIds, KeepAgencies: *keepAgencyIds, KeepPathways: *keepPathwayIds, KeepAttributions: *keepAttributionIds})
} else if *useIDMinimizerChar {
minzers = append(minzers, processors.IDMinimizer{Prefix: *idPrefix, Base: 36, KeepStations: *keepStationIds, KeepBlocks: *keepBlockIds, KeepFares: *keepFareIds, KeepShapes: *keepShapeIds, KeepRoutes: *keepRouteIds, KeepTrips: *keepTripIds, KeepLevels: *keepLevelIds, KeepServices: *keepServiceIds, KeepAgencies: *keepAgencyIds, KeepPathways: *keepPathwayIds, KeepAttributions: *keepAttributionIds})
}
// do processing
for _, m := range minzers {
m.Run(feed)
}
// restore stop IDs, if requested
if *keepStationIds && len(prefixes) > 0 {
for id, s := range feed.Stops {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Stops[oldId]; !ok {
feed.Stops[oldId] = s
feed.Stops[oldId].Id = oldId
// update additional fields
for k := range feed.StopsAddFlds {
feed.StopsAddFlds[k][oldId] = feed.StopsAddFlds[k][id]
delete(feed.StopsAddFlds[k], id)
}
feed.DeleteStop(id)
}
break
}
}
}
}
// restore block IDs, if requested
if *keepBlockIds && len(prefixes) > 0 {
// build set of existing block ids
existingBlockIds := make(map[string]bool)
oldToNewBlockIds := make(map[string]string)
for _, t := range feed.Trips {
if t.Block_id != nil && *t.Block_id != "" {
existingBlockIds[*t.Block_id] = true
}
}
for _, s := range feed.Trips {
for prefix := range prefixes {
if s.Block_id != nil && strings.HasPrefix(*s.Block_id, prefix) {
oldId := strings.TrimPrefix(*s.Block_id, prefix)
if _, ok := existingBlockIds[oldId]; !ok {
oldToNewBlockIds[*s.Block_id] = oldId
*s.Block_id = oldId
existingBlockIds[*s.Block_id] = true
} else if newId, ok := oldToNewBlockIds[*s.Block_id]; ok && newId == oldId {
*s.Block_id = oldId
}
break
}
}
}
}
// restore agency IDs, if requested
if *keepAgencyIds && len(prefixes) > 0 {
for id, s := range feed.Agencies {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Agencies[oldId]; !ok {
feed.Agencies[oldId] = s
feed.Agencies[oldId].Id = oldId
// update additional fields
for k := range feed.AgenciesAddFlds {
feed.AgenciesAddFlds[k][oldId] = feed.AgenciesAddFlds[k][id]
delete(feed.AgenciesAddFlds[k], id)
}
feed.DeleteAgency(id)
}
break
}
}
}
}
// restore fare attribute IDs, if requested
if *keepFareIds && len(prefixes) > 0 {
for id, s := range feed.FareAttributes {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.FareAttributes[oldId]; !ok {
feed.FareAttributes[oldId] = s
feed.FareAttributes[oldId].Id = oldId
// update additional fields
for k := range feed.FareAttributesAddFlds {
feed.FareAttributesAddFlds[k][oldId] = feed.FareAttributesAddFlds[k][id]
delete(feed.FareAttributesAddFlds[k], id)
}
for k := range feed.FareRulesAddFlds {
feed.FareRulesAddFlds[k][oldId] = feed.FareRulesAddFlds[k][id]
delete(feed.FareRulesAddFlds[k], id)
}
feed.DeleteFareAttribute(id)
}
break
}
}
}
}
// restore service IDs, if requested
if *keepServiceIds && len(prefixes) > 0 {
for id, s := range feed.Services {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Services[oldId]; !ok {
feed.Services[oldId] = s
feed.Services[oldId].SetId(oldId)
feed.DeleteService(id)
}
break
}
}
}
}
// restore route IDs, if requested
if *keepRouteIds && len(prefixes) > 0 {
for id, s := range feed.Routes {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Routes[oldId]; !ok {
feed.Routes[oldId] = s
feed.Routes[oldId].Id = oldId
// update additional fields
for k := range feed.RoutesAddFlds {
feed.RoutesAddFlds[k][oldId] = feed.RoutesAddFlds[k][id]
delete(feed.RoutesAddFlds[k], id)
}
feed.DeleteRoute(id)
}
break
}
}
}
}
// restore shape IDs, if requested
if *keepShapeIds && len(prefixes) > 0 {
for id, s := range feed.Shapes {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Shapes[oldId]; !ok {
feed.Shapes[oldId] = s
feed.Shapes[oldId].Id = oldId
// update additional fields
for k := range feed.ShapesAddFlds {
feed.ShapesAddFlds[k][oldId] = feed.ShapesAddFlds[k][id]
delete(feed.ShapesAddFlds[k], id)
}
feed.DeleteShape(id)
}
break
}
}
}
}
// restore trip IDs, if requested
if *keepTripIds && len(prefixes) > 0 {
for id, s := range feed.Trips {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Trips[oldId]; !ok {
feed.Trips[oldId] = s
feed.Trips[oldId].Id = oldId
// update additional fields
for k := range feed.TripsAddFlds {
feed.TripsAddFlds[k][oldId] = feed.TripsAddFlds[k][id]
delete(feed.TripsAddFlds[k], id)
}
for k := range feed.StopTimesAddFlds {
feed.StopTimesAddFlds[k][oldId] = feed.StopTimesAddFlds[k][id]
delete(feed.StopTimesAddFlds[k], id)
}
for k := range feed.FrequenciesAddFlds {
feed.FrequenciesAddFlds[k][oldId] = feed.FrequenciesAddFlds[k][id]
delete(feed.FrequenciesAddFlds[k], id)
}
feed.DeleteTrip(id)
}
break
}
}
}
}
// restore level IDs, if requested
if *keepLevelIds && len(prefixes) > 0 {
for id, s := range feed.Levels {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Levels[oldId]; !ok {
feed.Levels[oldId] = s
feed.Levels[oldId].Id = oldId
// update additional fields
for k := range feed.LevelsAddFlds {
feed.LevelsAddFlds[k][oldId] = feed.LevelsAddFlds[k][id]
delete(feed.LevelsAddFlds[k], id)
}
feed.DeleteLevel(id)
}
break
}
}
}
}
// restore pathway IDs, if requested
if *keepPathwayIds && len(prefixes) > 0 {
for id, s := range feed.Pathways {
for prefix := range prefixes {
if strings.HasPrefix(id, prefix) {
oldId := strings.TrimPrefix(id, prefix)
if _, ok := feed.Pathways[oldId]; !ok {
feed.Pathways[oldId] = s
feed.Pathways[oldId].Id = oldId
// update additional fields
for k := range feed.PathwaysAddFlds {
feed.PathwaysAddFlds[k][oldId] = feed.PathwaysAddFlds[k][id]
delete(feed.PathwaysAddFlds[k], id)
}
feed.DeletePathway(id)
}
break
}
}
}
}
fmt.Fprintf(os.Stdout, "Outputting GTFS feed to '%s'...", *outputPath)
if _, err := os.Stat(*outputPath); os.IsNotExist(err) {
if path.Ext(*outputPath) == ".zip" {
os.Create(*outputPath)
} else {
os.Mkdir(*outputPath, os.ModePerm)
}
}
// write feed back to output
w := gtfswriter.Writer{ZipCompressionLevel: *zipCompressionLevel, Sorted: !*dontSortZipFiles, ExplicitCalendar: *explicitCals, KeepColOrder: *keepColOrder}
e := w.Write(feed, *outputPath)
if e != nil {
fmt.Fprintf(os.Stderr, "\nError while writing GTFS feed in '%s':\n ", *outputPath)
fmt.Fprintln(os.Stderr, e.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stdout, " done.\n")
}
}