-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtfs.go
272 lines (232 loc) · 9.39 KB
/
gtfs.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
package gtfs
import (
"encoding/csv"
"errors"
"io"
"reflect"
)
// From agency.txt
type Agency struct {
Id string `gtfs_name:"agency_id" gtfs_required:"false"`
Name string `gtfs_name:"agency_name" gtfs_required:"true"`
Url string `gtfs_name:"agency_url" gtfs_required:"true"`
Timezone string `gtfs_name:"agency_timezone" gtfs_required:"true"`
Language string `gtfs_name:"agency_lang" gtfs_required:"false"`
Phone string `gtfs_name:"agency_phone" gtfs_required:"false"`
FareUrl string `gtfs_name:"agency_fare_url" gtfs_required:"false"`
Email string `gtfs_name:"agency_email" gtfs_required:"false"`
}
func (a *Agency) String() string {
return a.Name + " " + a.Url
}
// From stops.txt
type Stop struct {
Id string `gtfs_name:"stop_id" gtfs_required:"true"`
Code string `gtfs_name:"stop_code" gtfs_required:"false"`
Name string `gtfs_name:"stop_name" gtfs_required:"true"`
Description string `gtfs_name:"stop_desc" gtfs_required:"false"`
Latitude string `gtfs_name:"stop_lat" gtfs_required:"true"`
Longitude string `gtfs_name:"stop_lon" gtfs_required:"true"`
ZoneId string `gtfs_name:"zone_id" gtfs_required:"false"`
Url string `gtfs_name:"stop_url" gtfs_required:"false"`
LocationType string `gtfs_name:"location_type" gtfs_required:"false"`
ParentStation string `gtfs_name:"parent_station" gtfs_required:"false"`
Timezone string `gtfs_name:"stop_timezone" gtfs_required:"false"`
WheelchairBoarding string `gtfs_name:"wheelchair_boarding" gtfs_required:"false"`
}
func (s *Stop) String() string {
return s.Id + " " + s.Name
}
// From routes.txt
type Route struct {
Id string `gtfs_name:"route_id" gtfs_required:"true"`
AgencyId string `gtfs_name:"agency_id" gtfs_required:"false"`
ShortName string `gtfs_name:"route_short_name" gtfs_required:"true"`
LongName string `gtfs_name:"route_long_name" gtfs_required:"true"`
Description string `gtfs_name:"route_desc" gtfs_required:"false"`
Type string `gtfs_name:"route_type" gtfs_required:"true"`
Url string `gtfs_name:"route_url" gtfs_required:"false"`
Color string `gtfs_name:"route_color" gtfs_required:"false"`
TextColor string `gtfs_name:"route_text_color" gtfs_required:"false"`
SortOrder string `gtfs_name:"route_sort_order" gtfs_required:"false"`
}
func (r *Route) String() string {
return r.Id + " " + r.ShortName + " " + r.LongName
}
// From trips.txt
type Trip struct {
RouteId string `gtfs_name:"route_id" gtfs_required:"true"`
ServiceId string `gtfs_name:"service_id" gtfs_required:"true"`
Id string `gtfs_name:"trip_id" gtfs_required:"true"`
HeadSign string `gtfs_name:"trip_headsign" gtfs_required:"false"`
ShortName string `gtfs_name:"trip_short_name" gtfs_required:"false"`
DirectionId string `gtfs_name:"direction_id" gtfs_required:"false"`
BlockId string `gtfs_name:"block_id" gtfs_required:"false"`
ShapeId string `gtfs_name:"shape_id" gtfs_required:"false"`
WheelchairAccessible string `gtfs_name:"wheelchair_accessible" gtfs_required:"false"`
BikesAllowed string `gtfs_name:"bikes_allowed" gtfs_required:"false"`
}
func (t *Trip) String() string {
return t.Id + " " + t.RouteId + " " + t.ServiceId
}
// From stop_times.txt
type StopTime struct {
TripId string `gtfs_name:"trip_id" gtfs_required:"true"`
ArrivalTime string `gtfs_name:"arrival_time" gtfs_required:"false"`
DepartureTime string `gtfs_name:"departure_time" gtfs_required:"false"`
StopId string `gtfs_name:"stop_id" gtfs_required:"true"`
StopSequence string `gtfs_name:"stop_sequence" gtfs_required:"true"`
StopHeadSign string `gtfs_name:"stop_headsign" gtfs_required:"false"`
PickupType string `gtfs_name:"pickup_type" gtfs_required:"false"`
DropOffType string `gtfs_name:"drop_off_type" gtfs_required:"false"`
ShapeDistTraveled string `gtfs_name:"shape_dist_traveled" gtfs_required:"false"`
TimePoint string `gtfs_name:"timepoint" gtfs_required:"false"`
}
func (st *StopTime) String() string {
return st.TripId + " " + st.StopId
}
// From calendar.txt
type Service struct {
ServiceId string `gtfs_name:"service_id" gtfs_required:"true"`
Monday string `gtfs_name:"monday" gtfs_required:"true"`
Tuesday string `gtfs_name:"tuesday" gtfs_required:"true"`
Wednesday string `gtfs_name:"wednesday" gtfs_required:"true"`
Thursday string `gtfs_name:"thursday" gtfs_required:"true"`
Friday string `gtfs_name:"friday" gtfs_required:"true"`
Saturday string `gtfs_name:"saturday" gtfs_required:"true"`
Sunday string `gtfs_name:"sunday" gtfs_required:"true"`
StartDate string `gtfs_name:"start_date" gtfs_required:"true"`
EndDate string `gtfs_name:"end_date" gtfs_required:"true"`
}
func (s *Service) String() string {
return s.ServiceId + " from " + s.StartDate + " to " + s.EndDate
}
// From calendar_dates.txt
type ServiceException struct {
ServiceId string `gtfs_name:"service_id" gtfs_required:"true"`
Date string `gtfs_name:"date" gtfs_required:"true"`
ExceptionType string `gtfs_name:"exception_type" gtfs_required:"true"`
}
func (s *ServiceException) String() string {
return s.ServiceId + " on " + s.Date + " exception " + s.ExceptionType
}
// From fare_attributes.txt
type Fare struct {
FareId string `gtfs_name:"fare_id" gtfs_required:"true"`
Price string `gtfs_name:"price" gtfs_required:"true"`
CurrencyType string `gtfs_name:"currency_type" gtfs_required:"true"`
PaymentMethod string `gtfs_name:"payment_method" gtfs_required:"true"`
Transfers string `gtfs_name:"transfers" gtfs_required:"true"`
AgencyId string `gtfs_name:"agency_id" gtfs_required:"false"`
TransferDuration string `gtfs_name:"transfer_duration" gtfs_required:"false"`
}
func (f *Fare) String() string {
return f.FareId + " " + f.Price + f.CurrencyType
}
// From fare_rules.txt
type FareRule struct {
FareId string `gtfs_name:"fare_id" gtfs_required:"true"`
RouteId string `gtfs_name:"route_id" gtfs_required:"false"`
OriginId string `gtfs_name:"origin_id" gtfs_required:"false"`
DestinationId string `gtfs_name:"destination_id" gtfs_required:"false"`
ContainsId string `gtfs_name:"contains_id" gtfs_required:"false"`
}
func (f *FareRule) String() string {
return f.FareId + " " + f.RouteId + " " + f.OriginId + " " + f.DestinationId
}
// From shapes.txt
type ShapePoint struct {
Id string `gtfs_name:"shape_id" gtfs_required:"true"`
PtLatitude string `gtfs_name:"shape_pt_lat" gtfs_required:"true"`
PtLongitude string `gtfs_name:"shape_pt_lon" gtfs_required:"true"`
PtSequence string `gtfs_name:"shape_pt_sequence" gtfs_required:"true"`
DistTraveled string `gtfs_name:"shape_dist_traveled" gtfs_required:"false"`
}
func (s *ShapePoint) String() string {
return s.Id + " " + s.PtSequence + " " + s.PtLatitude + " " + s.PtLongitude
}
// From frequencies.txt
type Frequency struct {
TripId string `gtfs_name:"trip_id" gtfs_required:"true"`
StartTime string `gtfs_name:"start_time" gtfs_required:"true"`
EndTime string `gtfs_name:"end_time" gtfs_required:"true"`
HeadwaySecs string `gtfs_name:"headway_secs" gtfs_required:"true"`
ExactTimes string `gtfs_name:"exact_times" gtfs_required:"false"`
}
func (f *Frequency) String() string {
return f.TripId + " " + f.StartTime + " " + f.EndTime
}
// From transfers.txt
type Transfer struct {
FromStopId string `gtfs_name:"from_stop_id" gtfs_required:"true"`
ToStopId string `gtfs_name:"to_stop_id" gtfs_required:"true"`
TransferType string `gtfs_name:"transfer_type" gtfs_required:"true"`
MinimumTransferTime string `gtfs_name:"min_transfer_time" gtfs_required:"false"`
}
func (t *Transfer) String() string {
return t.FromStopId + " to " + t.ToStopId + " " + t.TransferType
}
func getFieldIndexForStruct(t reflect.Type, name string) (int, error) {
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Tag.Get("gtfs_name") == name {
return i, nil
}
}
return -1, errors.New("Field not found " + name)
}
func getIfFieldRequired(t reflect.Type, name string) bool {
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Tag.Get("gtfs_name") == name {
if f.Tag.Get("gtfs_required") == "true" {
return true
} else {
return false
}
}
}
return false
}
func Decode(r io.Reader, rowtype interface{}) ([]interface{}, error) {
c := csv.NewReader(r)
columns, err := c.Read()
if err != nil {
return nil, err
}
if len(columns) == 0 {
return nil, errors.New("No fields")
}
fields := make([]int, len(columns))
required := make([]bool, len(columns))
t := reflect.TypeOf(rowtype).Elem()
for i := 0; i < len(columns); i++ {
fields[i], err = getFieldIndexForStruct(t, columns[i])
if err != nil {
return nil, err
}
required[i] = getIfFieldRequired(t, columns[i])
}
output := make([]interface{}, 0)
for {
row, err := c.Read()
if err == nil {
o := reflect.New(t)
for i := 0; i < len(row); i++ {
value := row[i]
if required[i] {
if value == "" {
return nil, errors.New("Row is missing required field " + columns[i])
}
}
o.Elem().Field(fields[i]).SetString(value)
}
output = append(output, o.Interface())
} else if err == io.EOF {
return output, nil
} else {
return nil, err
}
}
return nil, errors.New("Not implemented")
}