Add TUSCONS to the timetable
[citygo-google-transit.git] / generate_trips_times.py
bloba1c2dbf7f1b59e6bc4df2e37ac9a475de661aaae
1 #!/usr/bin/python
2 ###
3 # Copyright (C) 2008 Ian Weller <ianweller@gmail.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 ###
20 from xml.dom import minidom
21 from csv import DictWriter
23 def time_addition(time, addthis):
24 addthis = int(addthis)
25 (hours, minutes) = time.split(':')
26 hours = int(hours)
27 minutes = int(minutes) + addthis
28 while minutes >= 60:
29 minutes = minutes - 60
30 hours = hours + 1
31 if hours < 10:
32 hours = "0" + str(hours)
33 else:
34 hours = str(hours)
35 if minutes < 10:
36 minutes = "0" + str(minutes)
37 else:
38 minutes = str(minutes)
39 return "%s:%s" % (hours, minutes)
41 if __name__ == "__main__":
42 trips = []
43 trips.append(["route_id", "service_id", "trip_id", "direction_id",
44 "block_id"])
45 stop_times = []
46 stop_times.append(["trip_id", "arrival_time", "departure_time", "stop_id",
47 "stop_sequence"])
48 dom = minidom.parse("time_table.xml")
49 time_table = dom.documentElement
50 for route in time_table.getElementsByTagName("route"):
51 route_id = route.getAttribute("id")
52 direction_id = route.getAttribute("direction")
53 stops = route.getElementsByTagName("stops")[0]
54 stops = stops.getElementsByTagName("stop")
55 for schedule in route.getElementsByTagName("start_times"):
56 service_id = schedule.getAttribute("schedule")
57 for time in schedule.getElementsByTagName("start"):
58 start_time = time.getAttribute("time")
59 block_id = time.getAttribute("block")
60 late = time.hasAttribute("late")
61 trip_id = ''.join([route_id, direction_id, service_id,
62 start_time.replace(':', '')])
63 trips.append([route_id, service_id, trip_id, direction_id,
64 block_id])
65 if not late:
66 trips.append([route_id, service_id+'_EARLY',
67 trip_id+'_EARLY', direction_id, block_id])
68 stop_sequence = 0
69 for stop in stops:
70 stop_sequence = stop_sequence + 1
71 stop_id = stop.getAttribute("id")
72 if stop.hasAttribute("time"):
73 time = stop.getAttribute("time")
74 arrival_time = time_addition(start_time, time)
75 arrival_time = arrival_time + ":00"
76 departure_time = arrival_time
77 else:
78 (arrival_time, departure_time) = ('', '')
79 stop_times.append([trip_id, arrival_time, departure_time,
80 stop_id, stop_sequence])
81 if not late:
82 stop_times.append([trip_id+'_EARLY', arrival_time,
83 departure_time, stop_id,
84 stop_sequence])
85 trips_d = []
86 for trip in trips:
87 that = {}
88 that["route_id"] = trip[0]
89 that["service_id"] = trip[1]
90 that["trip_id"] = trip[2]
91 that["direction_id"] = trip[3]
92 that["block_id"] = trip[4]
93 trips_d.append(that)
94 trips_f = file('trips.txt', 'w')
95 trips_w = DictWriter(trips_f, trips[0])
96 trips_w.writerows(trips_d)
97 trips_f.close()
98 stops_d = []
99 for stop in stop_times:
100 that = {}
101 that["trip_id"] = stop[0]
102 that["arrival_time"] = stop[1]
103 that["departure_time"] = stop[2]
104 that["stop_id"] = stop[3]
105 that["stop_sequence"] = stop[4]
106 stops_d.append(that)
107 stops_f = file('stop_times.txt', 'w')
108 stops_w = DictWriter(stops_f, stop_times[0])
109 stops_w.writerows(stops_d)
110 stops_f.close()