forked from BenFields22/transitfeed
/
schedule.py
executable file
·1351 lines (1160 loc) · 54.9 KB
/
schedule.py
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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python2.5
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import bisect
import cStringIO as StringIO
import datetime
import itertools
import os
try:
import sqlite3 as sqlite
native_sqlite = True
except ImportError:
try:
from pysqlite2 import dbapi2 as sqlite
native_sqlite = True
except ImportError:
from com.ziclix.python.sql import zxJDBC as sqlite
native_sqlite = False
import tempfile
import time
import warnings
# Objects in a schedule (Route, Trip, etc) should not keep a strong reference
# to the Schedule object to avoid a reference cycle. Schedule needs to use
# __del__ to cleanup its temporary file. The garbage collector can't handle
# reference cycles containing objects with custom cleanup code.
import weakref
import zipfile
from . import gtfsfactoryuser
from . import problems as problems_module
from .util import defaultdict
from . import util
class Schedule(object):
"""Represents a Schedule, a collection of stops, routes, trips and
an agency. This is the main class for this module."""
def __init__(self, problem_reporter=None,
memory_db=True, check_duplicate_trips=False,
gtfs_factory=None):
if gtfs_factory is None:
# This hackery is due to the cyclic dependency mess we currently have.
# See gtfsfactoryuser for more.
gtfs_factory = gtfsfactoryuser.GtfsFactoryUser().GetGtfsFactory()
self._gtfs_factory = gtfs_factory
# Map from table name to list of columns present in this schedule
self._table_columns = {}
self._agencies = {}
self.stops = {}
self.routes = {}
self.trips = {}
self.service_periods = {}
self.fares = {}
self.fare_zones = {} # represents the set of all known fare zones
self.feed_info = None
self._shapes = {} # shape_id to Shape
# A map from transfer._ID() to a list of transfers. A list is used so
# there can be more than one transfer with each ID. Once GTFS explicitly
# prohibits duplicate IDs this might be changed to a simple dict of
# Transfers.
self._transfers = defaultdict(lambda: [])
self._default_service_period = None
self._default_agency = None
if problem_reporter is None:
self.problem_reporter = problems_module.default_problem_reporter
else:
self.problem_reporter = problem_reporter
self._check_duplicate_trips = check_duplicate_trips
self.ConnectDb(memory_db)
def AddTableColumn(self, table, column):
"""Add column to table if it is not already there."""
if column not in self._table_columns[table]:
self._table_columns[table].append(column)
def AddTableColumns(self, table, columns):
"""Add columns to table if they are not already there.
Args:
table: table name as a string
columns: an iterable of column names"""
table_columns = self._table_columns.setdefault(table, [])
for attr in columns:
if attr not in table_columns:
table_columns.append(attr)
def GetTableColumns(self, table):
"""Return list of columns in a table."""
return self._table_columns[table]
def __del__(self):
self._connection.cursor().close()
self._connection.close()
if hasattr(self, '_temp_db_filename'):
os.remove(self._temp_db_filename)
def ConnectDb(self, memory_db):
def connector(db_file):
if native_sqlite:
return sqlite.connect(db_file)
else:
return sqlite.connect("jdbc:sqlite:%s" % db_file,
"", "", "org.sqlite.JDBC")
if memory_db:
self._connection = connector(":memory:")
else:
try:
self._temp_db_file = tempfile.NamedTemporaryFile()
self._connection = connector(self._temp_db_file.name)
except sqlite.OperationalError:
# Windows won't let a file be opened twice. mkstemp does not remove the
# file when all handles to it are closed.
self._temp_db_file = None
(fd, self._temp_db_filename) = tempfile.mkstemp(".db")
os.close(fd)
self._connection = connector(self._temp_db_filename)
cursor = self._connection.cursor()
cursor.execute("""CREATE TABLE stop_times (
trip_id CHAR(50),
arrival_secs INTEGER,
departure_secs INTEGER,
stop_id CHAR(50),
stop_sequence INTEGER,
stop_headsign VAR CHAR(100),
pickup_type INTEGER,
drop_off_type INTEGER,
shape_dist_traveled FLOAT,
timepoint INTEGER);""")
cursor.execute("""CREATE INDEX trip_index ON stop_times (trip_id);""")
cursor.execute("""CREATE INDEX stop_index ON stop_times (stop_id);""")
def GetStopBoundingBox(self):
return (min(s.stop_lat for s in self.stops.values()),
min(s.stop_lon for s in self.stops.values()),
max(s.stop_lat for s in self.stops.values()),
max(s.stop_lon for s in self.stops.values()),
)
def AddAgency(self, name, url, timezone, agency_id=None):
"""Adds an agency to this schedule."""
agency = self._gtfs_factory.Agency(name, url, timezone, agency_id)
self.AddAgencyObject(agency)
return agency
def AddAgencyObject(self, agency, problem_reporter=None, validate=False):
assert agency._schedule is None
if not problem_reporter:
problem_reporter = self.problem_reporter
if agency.agency_id in self._agencies:
problem_reporter.DuplicateID('agency_id', agency.agency_id)
return
self.AddTableColumns('agency', agency._ColumnNames())
agency._schedule = weakref.proxy(self)
if validate:
agency.Validate(problem_reporter)
self._agencies[agency.agency_id] = agency
def GetAgency(self, agency_id):
"""Return Agency with agency_id or throw a KeyError"""
return self._agencies[agency_id]
def GetDefaultAgency(self):
"""Return the default Agency. If no default Agency has been set select the
default depending on how many Agency objects are in the Schedule. If there
are 0 make a new Agency the default, if there is 1 it becomes the default,
if there is more than 1 then return None.
"""
if not self._default_agency:
if len(self._agencies) == 0:
self.NewDefaultAgency()
elif len(self._agencies) == 1:
self._default_agency = self._agencies.values()[0]
return self._default_agency
def NewDefaultAgency(self, **kwargs):
"""Create a new Agency object and make it the default agency for this Schedule"""
agency = self._gtfs_factory.Agency(**kwargs)
if not agency.agency_id:
agency.agency_id = util.FindUniqueId(self._agencies)
self._default_agency = agency
self.SetDefaultAgency(agency, validate=False) # Blank agency won't validate
return agency
def SetDefaultAgency(self, agency, validate=True):
"""Make agency the default and add it to the schedule if not already added"""
assert isinstance(agency, self._gtfs_factory.Agency)
self._default_agency = agency
if agency.agency_id not in self._agencies:
self.AddAgencyObject(agency, validate=validate)
def GetAgencyList(self):
"""Returns the list of Agency objects known to this Schedule."""
return self._agencies.values()
def GetServicePeriod(self, service_id):
"""Returns the ServicePeriod object with the given ID."""
return self.service_periods[service_id]
def GetDefaultServicePeriod(self):
"""Return the default ServicePeriod. If no default ServicePeriod has been
set select the default depending on how many ServicePeriod objects are in
the Schedule. If there are 0 make a new ServicePeriod the default, if there
is 1 it becomes the default, if there is more than 1 then return None.
"""
if not self._default_service_period:
if len(self.service_periods) == 0:
self.NewDefaultServicePeriod()
elif len(self.service_periods) == 1:
self._default_service_period = self.service_periods.values()[0]
return self._default_service_period
def NewDefaultServicePeriod(self):
"""Create a new ServicePeriod object, make it the default service period and
return it. The default service period is used when you create a trip without
providing an explict service period. """
service_period = self._gtfs_factory.ServicePeriod()
service_period.service_id = util.FindUniqueId(self.service_periods)
# blank service won't validate in AddServicePeriodObject
self.SetDefaultServicePeriod(service_period, validate=False)
return service_period
def SetDefaultServicePeriod(self, service_period, validate=True):
assert isinstance(service_period, self._gtfs_factory.ServicePeriod)
self._default_service_period = service_period
if service_period.service_id not in self.service_periods:
self.AddServicePeriodObject(service_period, validate=validate)
def AddServicePeriodObject(self, service_period, problem_reporter=None,
validate=True):
if not problem_reporter:
problem_reporter = self.problem_reporter
if service_period.service_id in self.service_periods:
problem_reporter.DuplicateID('service_id', service_period.service_id)
return
if validate:
service_period.Validate(problem_reporter)
self.service_periods[service_period.service_id] = service_period
def GetServicePeriodList(self):
return self.service_periods.values()
def GetDateRange(self):
"""Returns a tuple of (earliest, latest) dates on which the service periods
in the schedule define service, in YYYYMMDD form.
"""
(minvalue, maxvalue, minorigin, maxorigin) = self.GetDateRangeWithOrigins()
return (minvalue, maxvalue)
def GetDateRangeWithOrigins(self):
"""Returns a tuple of (earliest, latest, earliest_origin, latest_origin)
dates on which the service periods in the schedule define service, in
YYYYMMDD form.
The origins specify where the earliest or latest dates come from. In
particular, whether the date is a regular ServicePeriod start_date or
end_date in calendar.txt, a service exception of type add in
calendar_dates.txt, or feed start/end date defined in feed_info.txt.
"""
period_list = self.GetServicePeriodList()
ranges = [period.GetDateRange() for period in period_list]
starts = filter(lambda x: x, [item[0] for item in ranges])
ends = filter(lambda x: x, [item[1] for item in ranges])
if not starts or not ends:
return (None, None, None, None)
minvalue, minindex = min(itertools.izip(starts, itertools.count()))
maxvalue, maxindex = max(itertools.izip(ends, itertools.count()))
minreason = (period_list[minindex].HasDateExceptionOn(minvalue) and
"earliest service exception date in calendar_dates.txt" or
"earliest service date in calendar.txt")
maxreason = (period_list[maxindex].HasDateExceptionOn(maxvalue) and
"last service exception date in calendar_dates.txt" or
"last service date in calendar.txt")
# Override with feed_info.txt feed_start_date and feed_end_date values, if
# defined
if self.feed_info and self.feed_info.feed_start_date:
minvalue = self.feed_info.feed_start_date
minreason = "feed_start_date in feed_info.txt"
if self.feed_info and self.feed_info.feed_end_date:
maxvalue = self.feed_info.feed_end_date
maxreason = "feed_end_date in feed_info.txt"
return (minvalue, maxvalue, minreason, maxreason)
def GetServicePeriodsActiveEachDate(self, date_start, date_end):
"""Return a list of tuples (date, [period1, period2, ...]).
For each date in the range [date_start, date_end) make list of each
ServicePeriod object which is active.
Args:
date_start: The first date in the list, a date object
date_end: The first date after the list, a date object
Returns:
A list of tuples. Each tuple contains a date object and a list of zero or
more ServicePeriod objects.
"""
date_it = date_start
one_day = datetime.timedelta(days=1)
date_service_period_list = []
while date_it < date_end:
periods_today = []
date_it_string = date_it.strftime("%Y%m%d")
for service in self.GetServicePeriodList():
if service.IsActiveOn(date_it_string, date_it):
periods_today.append(service)
date_service_period_list.append((date_it, periods_today))
date_it += one_day
return date_service_period_list
def AddStop(self, lat, lng, name, stop_id=None):
"""Add a stop to this schedule.
Args:
lat: Latitude of the stop as a float or string
lng: Longitude of the stop as a float or string
name: Name of the stop, which will appear in the feed
stop_id: stop_id of the stop or None, in which case a unique id is picked
Returns:
A new Stop object
"""
if stop_id is None:
stop_id = util.FindUniqueId(self.stops)
stop = self._gtfs_factory.Stop(stop_id=stop_id, lat=lat, lng=lng, name=name)
self.AddStopObject(stop)
return stop
def AddStopObject(self, stop, problem_reporter=None):
"""Add Stop object to this schedule if stop_id is non-blank."""
assert stop._schedule is None
if not problem_reporter:
problem_reporter = self.problem_reporter
if not stop.stop_id:
return
if stop.stop_id in self.stops:
problem_reporter.DuplicateID('stop_id', stop.stop_id)
return
stop._schedule = weakref.proxy(self)
self.AddTableColumns('stops', stop._ColumnNames())
self.stops[stop.stop_id] = stop
if hasattr(stop, 'zone_id') and stop.zone_id:
self.fare_zones[stop.zone_id] = True
def GetStopList(self):
return self.stops.values()
def AddRoute(self, short_name, long_name, route_type, route_id=None):
"""Add a route to this schedule.
Args:
short_name: Short name of the route, such as "71L"
long_name: Full name of the route, such as "NW 21st Ave/St Helens Rd"
route_type: A type such as "Tram", "Subway" or "Bus"
route_id: id of the route or None, in which case a unique id is picked
Returns:
A new Route object
"""
if route_id is None:
route_id = util.FindUniqueId(self.routes)
route = self._gtfs_factory.Route(short_name=short_name, long_name=long_name,
route_type=route_type, route_id=route_id)
route.agency_id = self.GetDefaultAgency().agency_id
self.AddRouteObject(route)
return route
def AddRouteObject(self, route, problem_reporter=None):
if not problem_reporter:
problem_reporter = self.problem_reporter
if route.route_id in self.routes:
problem_reporter.DuplicateID('route_id', route.route_id)
return
if route.agency_id not in self._agencies:
if not route.agency_id and len(self._agencies) == 1:
# we'll just assume that the route applies to the only agency
pass
else:
problem_reporter.InvalidValue('agency_id', route.agency_id,
'Route uses an unknown agency_id.')
return
self.AddTableColumns('routes', route._ColumnNames())
route._schedule = weakref.proxy(self)
self.routes[route.route_id] = route
def GetRouteList(self):
return self.routes.values()
def GetRoute(self, route_id):
return self.routes[route_id]
def AddShapeObject(self, shape, problem_reporter=None):
if not problem_reporter:
problem_reporter = self.problem_reporter
shape.Validate(problem_reporter)
if shape.shape_id in self._shapes:
problem_reporter.DuplicateID('shape_id', shape.shape_id)
return
self._shapes[shape.shape_id] = shape
def GetShapeList(self):
return self._shapes.values()
def GetShape(self, shape_id):
return self._shapes[shape_id]
def AddTripObject(self, trip, problem_reporter=None, validate=False):
if not problem_reporter:
problem_reporter = self.problem_reporter
if trip.trip_id in self.trips:
problem_reporter.DuplicateID('trip_id', trip.trip_id)
return
self.AddTableColumns('trips', trip._ColumnNames())
trip._schedule = weakref.proxy(self)
self.trips[trip.trip_id] = trip
# Call Trip.Validate after setting trip._schedule so that references
# are checked. trip.ValidateChildren will be called directly by
# schedule.Validate, after stop_times has been loaded.
if validate:
if not problem_reporter:
problem_reporter = self.problem_reporter
trip.Validate(problem_reporter, validate_children=False)
try:
self.routes[trip.route_id]._AddTripObject(trip)
except KeyError:
# Invalid route_id was reported in the Trip.Validate call above
pass
def GetTripList(self):
return self.trips.values()
def GetTrip(self, trip_id):
return self.trips[trip_id]
def AddFareObject(self, fare, problem_reporter=None):
"""Deprecated. Please use AddFareAttributeObject."""
warnings.warn("No longer supported. The Fare class was renamed to "
"FareAttribute, and all related functions were renamed "
"accordingly.", DeprecationWarning)
self.AddFareAttributeObject(fare, problem_reporter)
def AddFareAttributeObject(self, fare, problem_reporter=None):
if not problem_reporter:
problem_reporter = self.problem_reporter
fare.Validate(problem_reporter)
if fare.fare_id in self.fares:
problem_reporter.DuplicateID('fare_id', fare.fare_id)
return
self.fares[fare.fare_id] = fare
def GetFareList(self):
"""Deprecated. Please use GetFareAttributeList instead"""
warnings.warn("No longer supported. The Fare class was renamed to "
"FareAttribute, and all related functions were renamed "
"accordingly.", DeprecationWarning)
return self.GetFareAttributeList()
def GetFareAttributeList(self):
return self.fares.values()
def GetFare(self, fare_id):
"""Deprecated. Please use GetFareAttribute instead"""
warnings.warn("No longer supported. The Fare class was renamed to "
"FareAttribute, and all related functions were renamed "
"accordingly.", DeprecationWarning)
return self.GetFareAttribute(fare_id)
def GetFareAttribute(self, fare_id):
return self.fares[fare_id]
def AddFareRuleObject(self, rule, problem_reporter=None):
if not problem_reporter:
problem_reporter = self.problem_reporter
if util.IsEmpty(rule.fare_id):
problem_reporter.MissingValue('fare_id')
return
if rule.route_id and rule.route_id not in self.routes:
problem_reporter.InvalidValue('route_id', rule.route_id)
if rule.origin_id and rule.origin_id not in self.fare_zones:
problem_reporter.InvalidValue('origin_id', rule.origin_id)
if rule.destination_id and rule.destination_id not in self.fare_zones:
problem_reporter.InvalidValue('destination_id', rule.destination_id)
if rule.contains_id and rule.contains_id not in self.fare_zones:
problem_reporter.InvalidValue('contains_id', rule.contains_id)
if rule.fare_id in self.fares:
self.GetFareAttribute(rule.fare_id).rules.append(rule)
else:
problem_reporter.InvalidValue('fare_id', rule.fare_id,
'(This fare_id doesn\'t correspond to any '
'of the IDs defined in the '
'fare attributes.)')
def AddFeedInfoObject(self, feed_info, problem_reporter=None, validate=False):
assert feed_info._schedule is None
if not problem_reporter:
problem_reporter = self.problem_reporter
feed_info._schedule = weakref.proxy(self)
if validate:
feed_info.Validate(problem_reporter)
self.AddTableColumns('feed_info', feed_info._ColumnNames())
self.feed_info = feed_info
def AddTransferObject(self, transfer, problem_reporter=None):
assert transfer._schedule is None, "only add Transfer to a schedule once"
if not problem_reporter:
problem_reporter = self.problem_reporter
transfer_id = transfer._ID()
if transfer_id in self._transfers:
self.problem_reporter.DuplicateID(self._gtfs_factory.Transfer._ID_COLUMNS,
transfer_id,
type=problems_module.TYPE_WARNING)
# Duplicates are still added, while not prohibited by GTFS.
transfer._schedule = weakref.proxy(self) # See weakref comment at top
self.AddTableColumns('transfers', transfer._ColumnNames())
self._transfers[transfer_id].append(transfer)
def GetTransferIter(self):
"""Return an iterator for all Transfer objects in this schedule."""
return itertools.chain(*self._transfers.values())
def GetTransferList(self):
"""Return a list containing all Transfer objects in this schedule."""
return list(self.GetTransferIter())
def GetStop(self, id):
return self.stops[id]
def GetFareZones(self):
"""Returns the list of all fare zones that have been identified by
the stops that have been added."""
return self.fare_zones.keys()
def GetNearestStops(self, lat, lon, n=1):
"""Return the n nearest stops to lat,lon"""
dist_stop_list = []
for s in self.stops.values():
# TODO: Use util.ApproximateDistanceBetweenStops?
dist = (s.stop_lat - lat)**2 + (s.stop_lon - lon)**2
if len(dist_stop_list) < n:
bisect.insort(dist_stop_list, (dist, s))
elif dist < dist_stop_list[-1][0]:
bisect.insort(dist_stop_list, (dist, s))
dist_stop_list.pop() # Remove stop with greatest distance
return [stop for dist, stop in dist_stop_list]
def GetStopsInBoundingBox(self, north, east, south, west, n):
"""Return a sample of up to n stops in a bounding box"""
stop_list = []
for s in self.stops.values():
if (s.stop_lat <= north and s.stop_lat >= south and
s.stop_lon <= east and s.stop_lon >= west):
stop_list.append(s)
if len(stop_list) == n:
break
return stop_list
def Load(self, feed_path, extra_validation=False):
loader = self._gtfs_factory.Loader(feed_path,
self, problems=self.problem_reporter,
extra_validation=extra_validation)
loader.Load()
def _WriteArchiveString(self, archive, filename, stringio):
zi = zipfile.ZipInfo(filename)
# See
# http://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zipf
zi.external_attr = 0o666 << 16 # Set unix permissions to -rw-rw-rw
# ZIP_DEFLATED requires zlib. zlib comes with Python 2.4 and 2.5
zi.compress_type = zipfile.ZIP_DEFLATED
archive.writestr(zi, stringio.getvalue())
def WriteGoogleTransitFeed(self, file):
"""Output this schedule as a Google Transit Feed in file_name.
Args:
file: path of new feed file (a string) or a file-like object
Returns:
None
"""
# Compression type given when adding each file
archive = zipfile.ZipFile(file, 'w')
if 'agency' in self._table_columns:
agency_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(agency_string)
columns = self.GetTableColumns('agency')
writer.writerow(columns)
for a in self._agencies.values():
writer.writerow([util.EncodeUnicode(a[c]) for c in columns])
self._WriteArchiveString(archive, 'agency.txt', agency_string)
if 'feed_info' in self._table_columns:
feed_info_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(feed_info_string)
columns = self.GetTableColumns('feed_info')
writer.writerow(columns)
writer.writerow([util.EncodeUnicode(self.feed_info[c]) for c in columns])
self._WriteArchiveString(archive, 'feed_info.txt', feed_info_string)
calendar_dates_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(calendar_dates_string)
writer.writerow(
self._gtfs_factory.ServicePeriod._FIELD_NAMES_CALENDAR_DATES)
has_data = False
for period in self.service_periods.values():
for row in period.GenerateCalendarDatesFieldValuesTuples():
has_data = True
writer.writerow(row)
wrote_calendar_dates = False
if has_data:
wrote_calendar_dates = True
self._WriteArchiveString(archive, 'calendar_dates.txt',
calendar_dates_string)
calendar_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(calendar_string)
writer.writerow(self._gtfs_factory.ServicePeriod._FIELD_NAMES)
has_data = False
for s in self.service_periods.values():
row = s.GetCalendarFieldValuesTuple()
if row:
has_data = True
writer.writerow(row)
if has_data or not wrote_calendar_dates:
self._WriteArchiveString(archive, 'calendar.txt', calendar_string)
if 'stops' in self._table_columns:
stop_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(stop_string)
columns = self.GetTableColumns('stops')
writer.writerow(columns)
for s in self.stops.values():
writer.writerow([util.EncodeUnicode(s[c]) for c in columns])
self._WriteArchiveString(archive, 'stops.txt', stop_string)
if 'routes' in self._table_columns:
route_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(route_string)
columns = self.GetTableColumns('routes')
writer.writerow(columns)
for r in self.routes.values():
writer.writerow([util.EncodeUnicode(r[c]) for c in columns])
self._WriteArchiveString(archive, 'routes.txt', route_string)
if 'trips' in self._table_columns:
trips_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(trips_string)
columns = self.GetTableColumns('trips')
writer.writerow(columns)
for t in self.trips.values():
writer.writerow([util.EncodeUnicode(t[c]) for c in columns])
self._WriteArchiveString(archive, 'trips.txt', trips_string)
# write frequencies.txt (if applicable)
headway_rows = []
for trip in self.GetTripList():
headway_rows += trip.GetFrequencyOutputTuples()
if headway_rows:
headway_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(headway_string)
writer.writerow(self._gtfs_factory.Frequency._FIELD_NAMES)
writer.writerows(headway_rows)
self._WriteArchiveString(archive, 'frequencies.txt', headway_string)
# write fares (if applicable)
if self.GetFareAttributeList():
fare_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(fare_string)
writer.writerow(self._gtfs_factory.FareAttribute._FIELD_NAMES)
writer.writerows(
f.GetFieldValuesTuple() for f in self.GetFareAttributeList())
self._WriteArchiveString(archive, 'fare_attributes.txt', fare_string)
# write fare rules (if applicable)
rule_rows = []
for fare in self.GetFareAttributeList():
for rule in fare.GetFareRuleList():
rule_rows.append(rule.GetFieldValuesTuple())
if rule_rows:
rule_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(rule_string)
writer.writerow(self._gtfs_factory.FareRule._FIELD_NAMES)
writer.writerows(rule_rows)
self._WriteArchiveString(archive, 'fare_rules.txt', rule_string)
stop_times_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(stop_times_string)
writer.writerow(self._gtfs_factory.StopTime._FIELD_NAMES)
for t in self.trips.values():
writer.writerows(t._GenerateStopTimesTuples())
self._WriteArchiveString(archive, 'stop_times.txt', stop_times_string)
# write shapes (if applicable)
shape_rows = []
for shape in self.GetShapeList():
seq = 1
for (lat, lon, dist) in shape.points:
shape_rows.append((shape.shape_id, lat, lon, seq, dist))
seq += 1
if shape_rows:
shape_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(shape_string)
writer.writerow(self._gtfs_factory.Shape._FIELD_NAMES)
writer.writerows(shape_rows)
self._WriteArchiveString(archive, 'shapes.txt', shape_string)
if 'transfers' in self._table_columns:
transfer_string = StringIO.StringIO()
writer = util.CsvUnicodeWriter(transfer_string)
columns = self.GetTableColumns('transfers')
writer.writerow(columns)
for t in self.GetTransferIter():
writer.writerow([util.EncodeUnicode(t[c]) for c in columns])
self._WriteArchiveString(archive, 'transfers.txt', transfer_string)
archive.close()
def GenerateDateTripsDeparturesList(self, date_start, date_end):
"""Return a list of (date object, number of trips, number of departures).
The list is generated for dates in the range [date_start, date_end).
Args:
date_start: The first date in the list, a date object
date_end: The first date after the list, a date object
Returns:
a list of (date object, number of trips, number of departures) tuples
"""
service_id_to_trips = defaultdict(lambda: 0)
service_id_to_departures = defaultdict(lambda: 0)
for trip in self.GetTripList():
headway_start_times = trip.GetFrequencyStartTimes()
if headway_start_times:
trip_runs = len(headway_start_times)
else:
trip_runs = 1
service_id_to_trips[trip.service_id] += trip_runs
service_id_to_departures[trip.service_id] += (
(trip.GetCountStopTimes() - 1) * trip_runs)
date_services = self.GetServicePeriodsActiveEachDate(date_start, date_end)
date_trips = []
for date, services in date_services:
day_trips = sum(service_id_to_trips[s.service_id] for s in services)
day_departures = sum(
service_id_to_departures[s.service_id] for s in services)
date_trips.append((date, day_trips, day_departures))
return date_trips
def ValidateAgenciesHaveSameAgencyTimezone(self, problems):
timezones_set = set(map(lambda agency:agency.agency_timezone,
self.GetAgencyList()))
if len(timezones_set) > 1:
timezones_str = '"%s"' % ('", "'.join(timezones_set))
problems.InvalidValue('agency_timezone', timezones_str,
'All agencies should have the same time zone. ' \
'Please review agency.txt.')
def ValidateFeedInfoLangMatchesAgencyLang(self, problems):
if self.feed_info is None:
return
if self.feed_info.feed_lang is None:
return
agencies = self.GetAgencyList()
for agency in agencies:
if not util.IsEmpty(agency.agency_lang) and (
not self.feed_info.feed_lang == agency.agency_lang):
problems.InvalidValue("feed_lang",
"The languages specified in feedinfo.txt and in "
"agency.txt for agency with ID %s differ." %
agency.agency_id)
def ValidateFeedStartAndExpirationDates(self, problems, first_date, last_date,
first_date_origin, last_date_origin,
today):
"""Validate the start and expiration dates of the feed.
Issue a warning if it only starts in the future, or if
it expires within 60 days.
Args:
problems: The problem reporter object
first_date: A date object representing the first day the feed is active
last_date: A date object representing the last day the feed is active
today: A date object representing the date the validation is being run on
Returns:
None
"""
warning_cutoff = today + datetime.timedelta(days=60)
if last_date < warning_cutoff:
problems.ExpirationDate(time.mktime(last_date.timetuple()),
last_date_origin)
if first_date > today:
problems.FutureService(time.mktime(first_date.timetuple()),
first_date_origin)
def ValidateServiceGaps(self,
problems,
validation_start_date,
validation_end_date,
service_gap_interval):
"""Validate consecutive dates without service in the feed.
Issue a warning if it finds service gaps of at least
"service_gap_interval" consecutive days in the date range
[validation_start_date, last_service_date)
Args:
problems: The problem reporter object
validation_start_date: A date object representing the date from which the
validation should take place
validation_end_date: A date object representing the first day the feed is
active
service_gap_interval: An integer indicating how many consecutive days the
service gaps need to have for a warning to be issued
Returns:
None
"""
if service_gap_interval is None:
return
departures = self.GenerateDateTripsDeparturesList(validation_start_date,
validation_end_date)
# The first day without service of the _current_ gap
first_day_without_service = validation_start_date
# The last day without service of the _current_ gap
last_day_without_service = validation_start_date
consecutive_days_without_service = 0
for day_date, day_trips, _ in departures:
if day_trips == 0:
if consecutive_days_without_service == 0:
first_day_without_service = day_date
consecutive_days_without_service += 1
last_day_without_service = day_date
else:
if consecutive_days_without_service >= service_gap_interval:
problems.TooManyDaysWithoutService(first_day_without_service,
last_day_without_service,
consecutive_days_without_service)
consecutive_days_without_service = 0
# We have to check if there is a gap at the end of the specified date range
if consecutive_days_without_service >= service_gap_interval:
problems.TooManyDaysWithoutService(first_day_without_service,
last_day_without_service,
consecutive_days_without_service)
def ValidateServiceExceptions(self,
problems,
first_service_day,
last_service_day):
# good enough approximation
six_months = datetime.timedelta(days=182)
service_span = last_service_day - first_service_day
if service_span < six_months:
# We don't check for exceptions because the feed is
# active for less than six months
return
for period in self.GetServicePeriodList():
# If at least one ServicePeriod has service exceptions we don't issue the
# warning, so we can stop looking at the list of ServicePeriods.
if period.HasExceptions():
return
problems.NoServiceExceptions(start=first_service_day,
end=last_service_day)
def ValidateServiceRangeAndExceptions(self, problems, today,
service_gap_interval):
if today is None:
today = datetime.date.today()
(start_date, end_date,
start_date_origin, end_date_origin) = self.GetDateRangeWithOrigins()
if not end_date or not start_date:
problems.OtherProblem('This feed has no effective service dates!',
type=problems_module.TYPE_WARNING)
else:
try:
last_service_day = datetime.datetime(
*(time.strptime(end_date, "%Y%m%d")[0:6])).date()
first_service_day = datetime.datetime(
*(time.strptime(start_date, "%Y%m%d")[0:6])).date()
except ValueError:
# Format of start_date and end_date checked in class ServicePeriod
pass
else:
self.ValidateServiceExceptions(problems,
first_service_day,
last_service_day)
self.ValidateFeedStartAndExpirationDates(problems,
first_service_day,
last_service_day,
start_date_origin,
end_date_origin,
today)
# We start checking for service gaps a bit in the past if the
# feed was active then. See
# https://github.com/google/transitfeed/issues/188
#
# We subtract 1 from service_gap_interval so that if today has
# service no warning is issued.
#
# Service gaps are searched for only up to one year from today
if service_gap_interval is not None:
service_gap_timedelta = datetime.timedelta(
days=service_gap_interval - 1)
one_year = datetime.timedelta(days=365)
self.ValidateServiceGaps(
problems,
max(first_service_day,
today - service_gap_timedelta),
min(last_service_day,
today + one_year),
service_gap_interval)
def ValidateStops(self, problems, validate_children):
# Check for stops that aren't referenced by any trips and broken
# parent_station references. Also check that the parent station isn't too
# far from its child stops.
for stop in self.stops.values():
if validate_children:
stop.Validate(problems)
cursor = self._connection.cursor()
cursor.execute("SELECT count(*) FROM stop_times WHERE stop_id=? LIMIT 1",
(stop.stop_id,))
count = cursor.fetchone()[0]
if stop.location_type == 0 and count == 0:
problems.UnusedStop(stop.stop_id, stop.stop_name)
elif stop.location_type == 1 and count != 0:
problems.UsedStation(stop.stop_id, stop.stop_name)
if stop.location_type != 1 and stop.parent_station:
if stop.parent_station not in self.stops:
problems.InvalidValue("parent_station",
util.EncodeUnicode(stop.parent_station),
"parent_station '%s' not found for stop_id "