-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rb
163 lines (138 loc) · 4.19 KB
/
build.rb
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
#!/usr/bin/env ruby
require 'csv'
require 'rexml/document'
require 'net/http'
prefix = 'MSC'
stations = 1..26
$route_id = 'MSC'
$service_id = 'EVERYDAY'
# STOPS
stop_list = CSV.read("stops.csv")
stops = CSV.open("gtfs/stops.txt", "wb")
stops << ["stop_id", "stop_name", "stop_lat", "stop_lon", "zone_id", "location_type", "parent_station"]
stations.each do |i|
stop_id = stop_list[i][1]
stop_name = stop_list[i][0]
stop_lat = stop_list[i][2]
stop_lng = stop_list[i][3]
exit_lat = stop_list[i][4]
exit_lng = stop_list[i][5]
if exit_lat == nil && exit_lng == nil
stops << [stop_id, stop_name+" PNR", stop_lat, stop_lng, stop_id, 0, nil]
else
stops << [stop_id+'_STATION', stop_name+" PNR", stop_lat, stop_lng, stop_id, 1, nil]
stops << [stop_id, stop_name+" PNR", stop_lat, stop_lng, stop_id, 0, stop_id+'_STATION']
stops << [stop_id+'_EXIT', stop_name+" PNR", exit_lat, exit_lng, stop_id, 2, stop_id+'_STATION']
end
end
# TIMETABLE
timetable_nb = CSV.read("timetable_nb.csv")
timetable_sb = CSV.read("timetable_sb.csv")
$trips = CSV.open("gtfs/trips.txt", "wb")
$stop_times = CSV.open("gtfs/stop_times.txt", "wb")
$trips << ["trip_id", "route_id", "service_id", "shape_id", "trip_headsign"]
$stop_times << ["trip_id", "stop_sequence", "stop_id", "arrival_time", "departure_time"]
def parse_timetable(timetable, dir)
timetable[0].each_index do |i|
if timetable[0][i] != nil
parse_trip(timetable, i, dir)
end
end
end
def parse_trip(timetable, i, dir)
trip_id = timetable[0][i]
last_stop_name = nil
timetable
.drop(2)
.reject { |r| r[i] == nil }
.each.with_index do |r, index|
stop_id = r[1]
arrival_time = r[i]
departure_time = r[i+1]
last_stop_name = r[0]
$stop_times << [trip_id, index+1, stop_id, arrival_time, departure_time]
end
dir_expanded = case dir
when 'NB'
'Northbound'
when 'SB'
'Southbound'
else
''
end
headsign = $route_id+' to '+last_stop_name+' ('+dir_expanded+')'
$trips << [trip_id, $route_id, $service_id, dir, headsign]
end
parse_timetable(timetable_nb, 'NB')
parse_timetable(timetable_sb, 'SB')
# FARES
fare_table = CSV.read("fares.csv")
fare_rules = CSV.open("gtfs/fare_rules.txt", "wb")
fare_attributes = CSV.open("gtfs/fare_attributes.txt", "wb")
fare_rules << ["fare_id", "origin_id", "destination_id"]
fare_attributes << ["fare_id", "price", "currency_type", "payment_method", "transfers"]
stations.each do |i|
from_code = fare_table[i+1][1]
stations.each do |j|
to_code = fare_table[1][j+1]
if i != j
fare = fare_table[i+1][j+1]
fare_id = sprintf("%s-%s-%s", prefix, from_code, to_code)
fare_rules << [fare_id, from_code, to_code]
fare_attributes << [fare_id, fare, "PHP", 1, 0]
end
end
end
fare_rules.close()
fare_attributes.close()
exit
# SHAPES
$shapes = CSV.open("gtfs/shapes.txt", "wb")
$shapes << ['shape_id', 'shape_pt_lat', 'shape_pt_lon', 'shape_pt_sequence']
def parse_shape_xml(doc)
xml = REXML::Document.new doc
nodes = Hash.new
ways = Hash.new
coords = nil
xml.root.elements.each do |element|
if element.name == 'node'
nodes[element.attributes['id']] = [
element.attributes['lon'],
element.attributes['lat']
]
elsif element.name == 'way'
ways[element.attributes['id']] = element.elements
.select { |x| x.name == 'nd' }
.map { |x| x.attributes['ref'] }
else
coords = element.elements
.select { |x| x.name == 'member' && x.attributes['type'] == 'way' }
.map { |x| ways[x.attributes['ref']] }
.flatten
.map { |x| nodes[x] }
end
end
coords
end
def fetch_shape(name)
uri = URI('http://overpass-api.de/api/interpreter')
query = <<EOF
rel["name"="#{name}"];
(._; way(r));
(._; node(w));
out;
EOF
res = Net::HTTP.post(uri, query)
if res.is_a?(Net::HTTPSuccess)
coords = parse_shape_xml(res.body)
coords.each.with_index do |coord, index|
$shapes << ['SB', coord[1], coord[0], index]
end
coords.reverse.each.with_index do |coord, index|
$shapes << ['NB', coord[1], coord[0], index]
end
else
puts "Failed to get shape data"
end
end
fetch_shape('PNR Metro Commuter Line : Tutuban - Calamba')