-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
335 lines (262 loc) · 7.46 KB
/
merge.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package bifrost
import (
"fmt"
"sync"
"time"
)
// ConnectStopsToVertices connects stops to street graph using knn and the Bifrost parameters.
func (b *Bifrost) ConnectStopsToVertices() {
t := time.Now()
fmt.Println("Building kd-tree took", time.Since(t))
t = time.Now()
fmt.Println("Connecting stops to street graph")
locks := make([]sync.Mutex, len(b.Data.Vertices))
for i, stop := range b.Data.Vertices {
if stop.Stop == nil {
continue // only connect stops
}
nearest := b.Data.WalkableVertexTree.KNN(&stop, 30)
for _, point := range nearest {
streetVert := point.(*GeoPoint)
if !b.fastDistWithin(&stop, streetVert, b.MaxStopsConnectionSeconds) {
break
}
dist := b.DistanceMs(&stop, streetVert, VehicleTypeWalking)
if dist > b.MaxStopsConnectionSeconds {
break
}
fromKey := uint64(i)
toKey := streetVert.VertKey
fromLock := &locks[fromKey]
fromLock.Lock()
b.Data.StreetGraph[fromKey] = append(b.Data.StreetGraph[fromKey], Arc{
Target: toKey,
WalkDistance: dist,
})
fromLock.Unlock()
toLock := &locks[toKey]
toLock.Lock()
b.Data.StreetGraph[toKey] = append(b.Data.StreetGraph[toKey], Arc{
Target: fromKey,
WalkDistance: dist,
})
toLock.Unlock()
}
}
fmt.Println("Connecting stops to street graph took", time.Since(t))
}
func (b *Bifrost) MergeData(other *RoutingData) {
b.Data = MergeData(b.Data, other)
}
// MergeData merges two RoutingData structs. It only concatenates the vertices and edges. Use ConnectStopsToVertices
// to connect stops to the street graph. IMPORTANT: This algorithm may change and re-use the data from both structs.
// Also note, that using multiple transit feeds may break things like the stops index due to duplicate stop ids.
// Multiple street graphs are not supported as there is no way of connecting them.
// todo: fix stops index for multiple transit feeds
// todo: add support for multiple street graphs
func MergeData(a *RoutingData, b *RoutingData) *RoutingData {
if a == nil {
return b
}
if b == nil {
return a
}
a.EnsureSliceLengths()
b.EnsureSliceLengths()
maxTripDayLength := a.MaxTripDayLength
if b.MaxTripDayLength > maxTripDayLength {
maxTripDayLength = b.MaxTripDayLength
}
bVertexOffset := uint64(len(a.Vertices))
bTripOffset := uint32(len(a.Trips))
bRouteOffset := uint32(len(a.Routes))
bServiceOffset := uint32(len(a.Services))
bGtfsRouteOffset := uint32(len(a.RouteInformation))
result := &RoutingData{
MaxTripDayLength: maxTripDayLength,
Services: append(a.Services, b.Services...),
Routes: mergeRoutes(a.Routes, b.Routes, bVertexOffset, bTripOffset),
StopToRoutes: mergeStopToRoutes(a.StopToRoutes, b.StopToRoutes, bRouteOffset),
Trips: mergeTrips(a.Trips, b.Trips, bServiceOffset),
StreetGraph: mergeStreetGraph(a.StreetGraph, b.StreetGraph, bVertexOffset),
Reorders: mergeReorders(a.Reorders, b.Reorders, bRouteOffset),
Vertices: append(a.Vertices, b.Vertices...),
StopsIndex: mergeStopsIndex(a.StopsIndex, b.StopsIndex, bVertexOffset),
NodesIndex: mergeNodesIndex(a.NodesIndex, b.NodesIndex, bVertexOffset),
GtfsRouteIndex: mergeGtfsRouteIndex(a.GtfsRouteIndex, b.GtfsRouteIndex, bGtfsRouteOffset),
RouteInformation: append(a.RouteInformation, b.RouteInformation...),
TripInformation: append(a.TripInformation, b.TripInformation...),
TripToRoute: mergeTripToRoute(a.TripToRoute, b.TripToRoute, bRouteOffset),
}
result.RebuildVertexTree()
return result
}
func (r *RoutingData) EnsureSliceLengths() {
vertexCount := len(r.Vertices)
if len(r.StopToRoutes) == 0 {
r.StopToRoutes = make([][]StopRoutePair, vertexCount)
}
if len(r.StopToRoutes) != vertexCount {
panic(fmt.Sprintf("stop to routes length mismatch: %d != %d", len(r.StopToRoutes), vertexCount))
}
if len(r.StreetGraph) == 0 {
r.StreetGraph = make([][]Arc, vertexCount)
}
if len(r.StreetGraph) != vertexCount {
panic(fmt.Sprintf("street graph length mismatch: %d != %d", len(r.StreetGraph), vertexCount))
}
routeCount := len(r.Routes)
if len(r.GtfsRouteIndex) == 0 {
r.GtfsRouteIndex = make([]uint32, routeCount)
}
if len(r.GtfsRouteIndex) != routeCount {
panic(fmt.Sprintf("gtfs route index length mismatch: %d != %d", len(r.GtfsRouteIndex), routeCount))
}
tripCount := len(r.Trips)
if len(r.TripToRoute) == 0 {
r.TripToRoute = make([]uint32, tripCount)
}
if len(r.TripToRoute) != tripCount {
panic(fmt.Sprintf("trip to route length mismatch: %d != %d", len(r.TripToRoute), tripCount))
}
}
func mergeRoutes(a []*Route, b []*Route, bVertexOffset uint64, bTripOffset uint32) []*Route {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
// shift all trips and routes in b
for _, route := range b {
for j, stop := range route.Stops {
route.Stops[j] = stop + bVertexOffset
}
for j, trip := range route.Trips {
route.Trips[j] = trip + bTripOffset
}
}
routes := make([]*Route, len(a)+len(b))
copy(routes, a)
copy(routes[len(a):], b)
return routes
}
func mergeStopToRoutes(a [][]StopRoutePair, b [][]StopRoutePair, bRouteOffset uint32) [][]StopRoutePair {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
// shift all trips and routes in b
for _, stopToRoutes := range b {
for _, stopRoutePair := range stopToRoutes {
stopRoutePair.Route += bRouteOffset
// StopKeyInRoute not shifted
}
}
stopToRoutes := make([][]StopRoutePair, len(a)+len(b))
copy(stopToRoutes, a)
copy(stopToRoutes[len(a):], b)
return stopToRoutes
}
func mergeTrips(a []*Trip, b []*Trip, bServiceOffset uint32) []*Trip {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
// shift all services in b
for _, trip := range b {
trip.Service += bServiceOffset
// stopovers not shifted
}
trips := make([]*Trip, len(a)+len(b))
copy(trips, a)
copy(trips[len(a):], b)
return trips
}
func mergeStreetGraph(a [][]Arc, b [][]Arc, bVertexOffset uint64) [][]Arc {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
// shift all vertices in b
for _, arcs := range b {
for _, arc := range arcs {
arc.Target += bVertexOffset
}
}
streetGraph := make([][]Arc, len(a)+len(b))
copy(streetGraph, a)
copy(streetGraph[len(a):], b)
return streetGraph
}
func mergeReorders(a, b map[uint64][]uint32, bRouteOffset uint32) map[uint64][]uint32 {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
for k, v := range b {
// k= uint64(routeKey)<<32 | uint64(stopSeqKey)
routeKey := uint32(k >> 32)
stopSeqKey := uint32(k) & 0xffffffff
routeKey += bRouteOffset
k = uint64(routeKey)<<32 | uint64(stopSeqKey)
a[k] = v // write to a, so a contains the merged reorders
}
return a
}
func mergeStopsIndex(a, b map[string]uint64, bVertexOffset uint64) map[string]uint64 {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
for k, v := range b {
a[k] = v + bVertexOffset
}
return a
}
func mergeNodesIndex(a, b map[int64]uint64, bVertexOffset uint64) map[int64]uint64 {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
for k, v := range b {
a[k] = v + bVertexOffset
}
return a
}
func mergeGtfsRouteIndex(a, b []uint32, bGtfsRouteOffset uint32) []uint32 {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
for i, v := range b {
a[i] = v + bGtfsRouteOffset
}
return a
}
func mergeTripToRoute(a, b []uint32, bRouteOffset uint32) []uint32 {
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
for i, v := range b {
a[i] = v + bRouteOffset
}
return a
}