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
|
package http
import (
"math"
"strconv"
"strings"
"time"
"github.com/mpolden/atb/atb"
"github.com/mpolden/atb/entur"
)
// BusStops represents a list of bus stops.
type BusStops struct {
Stops []BusStop `json:"stops"`
nodeIDs map[int]*BusStop
}
// BusStop represents a single bus stop.
type BusStop struct {
URL string `json:"url"`
StopID int `json:"stopId"`
NodeID int `json:"nodeId"`
Description string `json:"description"`
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
MobileCode string `json:"mobileCode"`
MobileName string `json:"mobileName"`
}
// GeoJSON represents the top-level object of the GeoJSON format.
type GeoJSON struct {
Type string `json:"type"`
Geometry `json:"geometry"`
Properties map[string]interface{} `json:"properties"`
}
// Geometry represents the geometry object of the GeoJSON format.
type Geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
}
// GeoJSONCollection represents a collection of GeoJSON feature objects.
type GeoJSONCollection struct {
Type string `json:"type"`
Features []GeoJSON `json:"features"`
}
// Departures represents a list of departures, from a given bus stop.
type Departures struct {
URL string `json:"url"`
TowardsCentrum *bool `json:"isGoingTowardsCentrum,omitempty"`
Departures []Departure `json:"departures"`
}
// Departure represents a single departure in a given direction.
type Departure struct {
LineID string `json:"line"`
RegisteredDepartureTime string `json:"registeredDepartureTime,omitempty"`
ScheduledDepartureTime string `json:"scheduledDepartureTime"`
Destination string `json:"destination"`
IsRealtimeData bool `json:"isRealtimeData"`
TowardsCentrum *bool `json:"isGoingTowardsCentrum,omitempty"`
}
// Error represents an error in the API, which is returned to the user.
type Error struct {
err error
Status int `json:"status"`
Message string `json:"message"`
}
func convertBusStop(s atb.BusStop) (BusStop, error) {
nodeID, err := strconv.Atoi(s.NodeID)
if err != nil {
return BusStop{}, err
}
longitude, err := strconv.Atoi(s.Longitude)
if err != nil {
return BusStop{}, err
}
lat, lon := ConvertCoordinates(s.Latitude, longitude)
return BusStop{
StopID: s.StopID,
NodeID: nodeID,
Description: s.Description,
Longitude: ceilN(lon, 6),
Latitude: ceilN(lat, 6),
MobileCode: s.MobileCode,
MobileName: s.MobileName,
}, nil
}
func convertBusStops(s atb.BusStops) (BusStops, error) {
stops := make([]BusStop, 0, len(s.Stops))
for _, stop := range s.Stops {
converted, err := convertBusStop(stop)
if err != nil {
return BusStops{}, err
}
stops = append(stops, converted)
}
return BusStops{Stops: stops}, nil
}
// ConvertTime converts time from AtBs format to ISO 8601.
func ConvertTime(src string) (string, error) {
t, err := time.Parse("02.01.2006 15:04", src)
if err != nil {
return "", err
}
return t.Format("2006-01-02T15:04:05.000"), nil
}
// IsRealtime returns a boolean indicating whether stationForecast is realtime.
func IsRealtime(stationForecast string) bool {
return strings.EqualFold(stationForecast, "prev")
}
func convertForecast(f atb.Forecast) (Departure, error) {
registeredDeparture, err := ConvertTime(f.RegisteredDepartureTime)
if err != nil {
return Departure{}, err
}
scheduledDeparture, err := ConvertTime(f.ScheduledDepartureTime)
if err != nil {
return Departure{}, err
}
return Departure{
LineID: f.LineID,
Destination: f.Destination,
RegisteredDepartureTime: registeredDeparture,
ScheduledDepartureTime: scheduledDeparture,
IsRealtimeData: IsRealtime(f.StationForecast),
}, nil
}
// IsTowardsCentrum returns a boolean indicating whether a bus stop, identified
// by nodeID, is going to the centrum.
func IsTowardsCentrum(nodeID int) bool {
return (nodeID/1000)%2 == 1
}
// ConvertCoordinates converts latitude and longitude from EPSG:3785 to
// EPSG:4326.
func ConvertCoordinates(latitude, longitude int) (float64, float64) {
const earthRadius = 6378137
originShift := (2 * math.Pi * earthRadius) / 2
lat := (float64(latitude) / originShift) * 180
lon := (float64(longitude) / originShift) * 180
lat = 180 / math.Pi * (2*math.Atan(
math.Exp(lat*math.Pi/180)) - math.Pi/2)
return lat, lon
}
func ceilN(f float64, n int) float64 {
shift := math.Pow(10, float64(n))
return math.Ceil(f*shift) / shift
}
func convertForecasts(f atb.Forecasts) (Departures, error) {
towardsCentrum := false
if len(f.Nodes) > 0 {
nodeID, err := strconv.Atoi(f.Nodes[0].NodeID)
if err != nil {
return Departures{}, err
}
towardsCentrum = IsTowardsCentrum(nodeID)
}
departures := make([]Departure, 0, len(f.Forecasts))
for _, forecast := range f.Forecasts {
departure, err := convertForecast(forecast)
if err != nil {
return Departures{}, err
}
departures = append(departures, departure)
}
return Departures{
TowardsCentrum: &towardsCentrum,
Departures: departures,
}, nil
}
func convertDepartures(enturDepartures []entur.Departure) Departures {
departures := make([]Departure, 0, len(enturDepartures))
const timeLayout = "2006-01-02T15:04:05.000"
for _, d := range enturDepartures {
scheduledDepartureTime := d.ScheduledDepartureTime.Format(timeLayout)
registeredDepartureTime := ""
if !d.RegisteredDepartureTime.IsZero() {
registeredDepartureTime = d.RegisteredDepartureTime.Format(timeLayout)
}
towardsCentrum := d.Inbound
departure := Departure{
LineID: d.Line,
ScheduledDepartureTime: scheduledDepartureTime,
RegisteredDepartureTime: registeredDepartureTime,
Destination: d.Destination,
IsRealtimeData: d.IsRealtime,
TowardsCentrum: &towardsCentrum,
}
departures = append(departures, departure)
}
return Departures{
Departures: departures,
}
}
// GeoJSON converts BusStop into the GeoJSON format.
func (s *BusStop) GeoJSON() GeoJSON {
geometry := Geometry{
Type: "Point",
Coordinates: []float64{s.Longitude, s.Latitude},
}
properties := map[string]interface{}{
"name": s.Description,
"busstop": s,
}
return GeoJSON{
Type: "Feature",
Geometry: geometry,
Properties: properties,
}
}
// GeoJSON converts BusStops into a GeoJSON feature collection.
func (s *BusStops) GeoJSON() GeoJSONCollection {
features := make([]GeoJSON, 0, len(s.Stops))
for i := range s.Stops {
geoJSON := s.Stops[i].GeoJSON()
features = append(features, geoJSON)
}
return GeoJSONCollection{
Type: "FeatureCollection",
Features: features,
}
}
|