forked from google/transitfeed
/
filter_unused_stops.py
executable file
·63 lines (53 loc) · 1.89 KB
/
filter_unused_stops.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
#!/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.
"""Filter the unused stops out of a transit feed file."""
from __future__ import print_function
import optparse
import sys
import transitfeed
def main():
parser = optparse.OptionParser(
usage="usage: %prog [options] input_feed output_feed",
version="%prog "+transitfeed.__version__)
parser.add_option("-l", "--list_removed", dest="list_removed",
default=False,
action="store_true",
help="Print removed stops to stdout")
(options, args) = parser.parse_args()
if len(args) != 2:
print(parser.format_help(), file=sys.stderr)
print("\n\nYou must provide input_feed and output_feed\n\n", file=sys.stderr)
sys.exit(2)
input_path = args[0]
output_path = args[1]
loader = transitfeed.Loader(input_path)
schedule = loader.Load()
print("Removing unused stops...")
removed = 0
for stop_id, stop in schedule.stops.items():
if not stop.GetTrips(schedule):
removed += 1
del schedule.stops[stop_id]
if options.list_removed:
print("Removing %s (%s)" % (stop_id, stop.stop_name))
if removed == 0:
print("No unused stops.")
elif removed == 1:
print("Removed 1 stop")
else:
print("Removed %d stops" % removed)
schedule.WriteGoogleTransitFeed(output_path)
if __name__ == "__main__":
main()