/
RouteBeanServiceImpl.java
410 lines (337 loc) · 15.5 KB
/
RouteBeanServiceImpl.java
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org>
* Copyright (C) 2011 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onebusaway.transit_data_federation.impl.beans;
import java.util.*;
import org.onebusaway.container.cache.Cacheable;
import org.onebusaway.geospatial.model.EncodedPolylineBean;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.calendar.ServiceDate;
import org.onebusaway.gtfs.services.calendar.CalendarService;
import org.onebusaway.transit_data.model.AgencyBean;
import org.onebusaway.transit_data.model.NameBean;
import org.onebusaway.transit_data.model.NameBeanTypes;
import org.onebusaway.transit_data.model.RouteBean;
import org.onebusaway.transit_data.model.StopBean;
import org.onebusaway.transit_data.model.StopGroupBean;
import org.onebusaway.transit_data.model.StopGroupingBean;
import org.onebusaway.transit_data.model.StopsForRouteBean;
import org.onebusaway.transit_data.model.TransitDataConstants;
import org.onebusaway.transit_data_federation.impl.DirectedGraph;
import org.onebusaway.transit_data_federation.impl.StopGraphComparator;
import org.onebusaway.transit_data_federation.model.StopSequence;
import org.onebusaway.transit_data_federation.model.StopSequenceCollection;
import org.onebusaway.transit_data_federation.model.narrative.RouteCollectionNarrative;
import org.onebusaway.transit_data_federation.services.ExtendedCalendarService;
import org.onebusaway.transit_data_federation.services.RouteService;
import org.onebusaway.transit_data_federation.services.StopSequenceCollectionService;
import org.onebusaway.transit_data_federation.services.StopSequencesService;
import org.onebusaway.transit_data_federation.services.beans.AgencyBeanService;
import org.onebusaway.transit_data_federation.services.beans.RouteBeanService;
import org.onebusaway.transit_data_federation.services.beans.ShapeBeanService;
import org.onebusaway.transit_data_federation.services.beans.StopBeanService;
import org.onebusaway.transit_data_federation.services.blocks.AbstractBlockTripIndex;
import org.onebusaway.transit_data_federation.services.blocks.BlockIndexService;
import org.onebusaway.transit_data_federation.services.blocks.BlockTripIndex;
import org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockTripIndex;
import org.onebusaway.transit_data_federation.services.narrative.NarrativeService;
import org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry;
import org.onebusaway.transit_data_federation.services.transit_graph.RouteCollectionEntry;
import org.onebusaway.transit_data_federation.services.transit_graph.RouteEntry;
import org.onebusaway.transit_data_federation.services.transit_graph.StopEntry;
import org.onebusaway.transit_data_federation.services.transit_graph.TransitGraphDao;
import org.onebusaway.transit_data_federation.services.transit_graph.TripEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
class RouteBeanServiceImpl implements RouteBeanService {
private TransitGraphDao _transitGraphDao;
private NarrativeService _narrativeService;
private AgencyBeanService _agencyBeanService;
private StopBeanService _stopBeanService;
private ShapeBeanService _shapeBeanService;
private RouteService _routeService;
private StopSequencesService _stopSequencesService;
private StopSequenceCollectionService _stopSequenceBlocksService;
private BlockIndexService _blockIndexService;
private CalendarService _calendarService;
private ExtendedCalendarService _extCalendarService;
private List<String> _doNotSortRouteIdList = new ArrayList<>();
@Autowired
public void setTransitGraphDao(TransitGraphDao transitGraphDao) {
_transitGraphDao = transitGraphDao;
}
@Autowired
public void setNarrativeService(NarrativeService narrativeService) {
_narrativeService = narrativeService;
}
@Autowired
public void setAgencyBeanService(AgencyBeanService agencyBeanService) {
_agencyBeanService = agencyBeanService;
}
@Autowired
public void setStopBeanService(StopBeanService stopBeanService) {
_stopBeanService = stopBeanService;
}
@Autowired
public void setShapeBeanService(ShapeBeanService shapeBeanService) {
_shapeBeanService = shapeBeanService;
}
@Autowired
public void setRouteService(RouteService routeService) {
_routeService = routeService;
}
@Autowired
public void setStopSequencesLibrary(StopSequencesService service) {
_stopSequencesService = service;
}
@Autowired
public void setStopSequencesBlocksService(
StopSequenceCollectionService stopSequenceBlocksService) {
_stopSequenceBlocksService = stopSequenceBlocksService;
}
@Autowired
public void setBlockIndexService(BlockIndexService blockIndexService) {
_blockIndexService = blockIndexService;
}
@Autowired
public void setCalendarService(CalendarService calendarService) { _calendarService = calendarService; }
@Autowired
public void setExtCalendarService(ExtendedCalendarService extCalendarService) { _extCalendarService = extCalendarService; }
public void setDoNotSortRouteIdList(String doNotSortRouteIds) {
if (doNotSortRouteIds == null) {
_doNotSortRouteIdList = new ArrayList<>();
} else if (!doNotSortRouteIds.contains(",")) {
_doNotSortRouteIdList = new ArrayList<>();
_doNotSortRouteIdList.add(doNotSortRouteIds);
} else {
_doNotSortRouteIdList = Arrays.asList(doNotSortRouteIds.split(","));
}
}
@Cacheable
public RouteBean getRouteForId(AgencyAndId id) {
RouteCollectionNarrative rc = _narrativeService.getRouteCollectionForId(id);
if (rc == null)
return null;
return getRouteBeanForRouteCollection(id, rc);
}
@Cacheable
public StopsForRouteBean getStopsForRoute(AgencyAndId routeId) {
RouteCollectionEntry routeCollectionEntry = _transitGraphDao.getRouteCollectionForId(routeId);
RouteCollectionNarrative narrative = _narrativeService.getRouteCollectionForId(routeId);
if (routeCollectionEntry == null || narrative == null)
return null;
return getStopsForRouteCollectionAndNarrative(routeCollectionEntry,
narrative, null);
}
// @Cacheable
public StopsForRouteBean getStopsForRouteForServiceDate(AgencyAndId routeId, ServiceDate serviceDate) {
RouteCollectionEntry routeCollectionEntry = _transitGraphDao.getRouteCollectionForId(routeId);
RouteCollectionNarrative narrative = _narrativeService.getRouteCollectionForId(routeId);
if (routeCollectionEntry == null || narrative == null)
return null;
return getStopsForRouteCollectionAndNarrative(routeCollectionEntry,
narrative, serviceDate);
}
/****
* Private Methods
****/
private RouteBean getRouteBeanForRouteCollection(AgencyAndId id,
RouteCollectionNarrative rc) {
RouteBean.Builder bean = RouteBean.builder();
bean.setId(ApplicationBeanLibrary.getId(id));
bean.setShortName(rc.getShortName());
bean.setLongName(rc.getLongName());
bean.setColor(rc.getColor());
bean.setDescription(rc.getDescription());
bean.setTextColor(rc.getTextColor());
bean.setType(rc.getType());
bean.setUrl(rc.getUrl());
AgencyBean agency = _agencyBeanService.getAgencyForId(id.getAgencyId());
bean.setAgency(agency);
return bean.create();
}
private List<StopBean> getStopBeansForRoute(AgencyAndId routeId, ServiceDate serviceDate) {
Collection<AgencyAndId> stopIds;
if (serviceDate == null){
stopIds = _routeService.getStopsForRouteCollection(routeId);
}
else {
stopIds = _routeService.getStopsForRouteCollectionForServiceDate(routeId, serviceDate);
}
List<StopBean> stops = new ArrayList<StopBean>();
for (AgencyAndId stopId : stopIds) {
StopBean stop = _stopBeanService.getStopForId(stopId, null);
stops.add(stop);
}
return stops;
}
private StopsForRouteBean getStopsForRouteCollectionAndNarrative(
RouteCollectionEntry routeCollection, RouteCollectionNarrative narrative, ServiceDate serviceDate) {
StopsForRouteBean result = new StopsForRouteBean();
AgencyAndId routeCollectionId = routeCollection.getId();
result.setRoute(getRouteBeanForRouteCollection(routeCollectionId, narrative));
result.setStops(getStopBeansForRoute(routeCollectionId, serviceDate));
result.setPolylines(getEncodedPolylinesForRoute(routeCollection, serviceDate));
StopGroupingBean directionGrouping = new StopGroupingBean();
directionGrouping.setType(TransitDataConstants.STOP_GROUPING_TYPE_DIRECTION);
List<StopGroupBean> directionGroups = new ArrayList<StopGroupBean>();
directionGrouping.setStopGroups(directionGroups);
directionGrouping.setOrdered(true);
result.addGrouping(directionGrouping);
List<BlockTripIndex> blockIndices = _blockIndexService.getBlockTripIndicesForRouteCollectionId(routeCollectionId);
List<FrequencyBlockTripIndex> frequencyBlockIndices = _blockIndexService.getFrequencyBlockTripIndicesForRouteCollectionId(routeCollectionId);
List<BlockTripEntry> blockTripsUnfiltered = new ArrayList<BlockTripEntry>();
List<BlockTripEntry> blockTrips = new ArrayList<BlockTripEntry>();
getBlockTripsForIndicesMatchingRouteCollection(blockIndices,
routeCollectionId, blockTripsUnfiltered);
getBlockTripsForIndicesMatchingRouteCollection(frequencyBlockIndices,
routeCollectionId, blockTripsUnfiltered);
if (serviceDate == null) {
blockTrips.addAll(blockTripsUnfiltered);//add all the block trips if there is no service date provided
}
else {
for (BlockTripEntry blockTrip : blockTripsUnfiltered) {
TimeZone serviceTimezone = blockTrip.getBlockConfiguration().getServiceIds().getTimeZone();
boolean isActiveTrip = _extCalendarService.areServiceIdsActiveOnServiceDate(blockTrip.getBlockConfiguration().getServiceIds(), serviceDate.getAsDate(serviceTimezone));
if (isActiveTrip) blockTrips.add(blockTrip);
}
}
List<StopSequence> sequences = _stopSequencesService.getStopSequencesForTrips(blockTrips);
List<StopSequenceCollection> blocks = _stopSequenceBlocksService.getStopSequencesAsCollections(sequences);
for (StopSequenceCollection block : blocks) {
NameBean name = new NameBean(NameBeanTypes.DESTINATION,
block.getDescription());
List<StopEntry> stops = getStopsInOrder(routeCollection.getId(), block);
List<String> groupStopIds = new ArrayList<String>();
for (StopEntry stop : stops)
groupStopIds.add(ApplicationBeanLibrary.getId(stop.getId()));
Set<AgencyAndId> shapeIds = getShapeIdsForStopSequenceBlock(block);
List<EncodedPolylineBean> polylines = _shapeBeanService.getMergedPolylinesForShapeIds(shapeIds);
StopGroupBean group = new StopGroupBean();
group.setId(block.getPublicId());
group.setName(name);
group.setStopIds(groupStopIds);
group.setPolylines(polylines);
directionGroups.add(group);
}
sortResult(result);
return result;
}
/**
* A block index potentially includes trips with different routes. We only
* want the trips matching our route collection.
*
* @param <T>
* @param blockIndices
* @param routeCollectionId
* @param resultingTrips
*/
private <T extends AbstractBlockTripIndex> void getBlockTripsForIndicesMatchingRouteCollection(
List<T> blockIndices, AgencyAndId routeCollectionId,
List<BlockTripEntry> resultingTrips) {
for (AbstractBlockTripIndex blockIndex : blockIndices) {
for (BlockTripEntry blockTrip : blockIndex.getTrips()) {
TripEntry trip = blockTrip.getTrip();
AgencyAndId rcId = trip.getRouteCollection().getId();
if (!rcId.equals(routeCollectionId))
continue;
resultingTrips.add(blockTrip);
}
}
}
private List<EncodedPolylineBean> getEncodedPolylinesForRoute(
RouteCollectionEntry routeCollection, ServiceDate serviceDate) {
Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>();
for (RouteEntry route : routeCollection.getChildren()) {
for (TripEntry trip : route.getTrips()) {
if (trip.getShapeId() != null) {
if (serviceDate == null) {
shapeIds.add(trip.getShapeId());
} else {
TimeZone serviceTimezone = trip.getServiceId().getTimeZone();
boolean isActiveTrip = _calendarService.isLocalizedServiceIdActiveOnDate(trip.getServiceId(), serviceDate.getAsDate(serviceTimezone));
if (isActiveTrip)
shapeIds.add(trip.getShapeId());
}
}
}
}
return _shapeBeanService.getMergedPolylinesForShapeIds(shapeIds);
}
private List<StopEntry> getStopsInOrder(AgencyAndId routeId, StopSequenceCollection block) {
List<StopEntry> stopsInDefaultOrder = new ArrayList<>();
DirectedGraph<StopEntry> graph = new DirectedGraph<StopEntry>();
for (StopSequence sequence : block.getStopSequences()) {
StopEntry prev = null;
for (StopEntry stop : sequence.getStops()) {
if (prev != null) {
// We do this to avoid cycles
if (!graph.isConnected(stop, prev)) {
graph.addEdge(prev, stop);
// this tells us if the stop is already used
stopsInDefaultOrder.add(stop);
}
}
prev = stop;
}
}
if (_doNotSortRouteIdList.contains(routeId.toString())) {
// don't sort this as its too complex
return stopsInDefaultOrder;
}
// here we guess at a canonical route pattern via a topological sort order
// works well for simple routes, does poorly for loops
StopGraphComparator c = new StopGraphComparator(graph);
return graph.getTopologicalSort(c);
}
private Set<AgencyAndId> getShapeIdsForStopSequenceBlock(
StopSequenceCollection block) {
Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>();
for (StopSequence sequence : block.getStopSequences()) {
for (BlockTripEntry blockTrip : sequence.getTrips()) {
TripEntry trip = blockTrip.getTrip();
AgencyAndId shapeId = trip.getShapeId();
if (shapeId != null && shapeId.hasValues())
shapeIds.add(shapeId);
}
}
return shapeIds;
}
private void sortResult(StopsForRouteBean result) {
Collections.sort(result.getStops(), new StopBeanIdComparator());
Collections.sort(result.getStopGroupings(),
new Comparator<StopGroupingBean>() {
public int compare(StopGroupingBean o1, StopGroupingBean o2) {
return o1.getType().compareTo(o2.getType());
}
});
for (StopGroupingBean grouping : result.getStopGroupings()) {
Collections.sort(grouping.getStopGroups(),
new Comparator<StopGroupBean>() {
public int compare(StopGroupBean o1, StopGroupBean o2) {
return getName(o1).compareTo(getName(o2));
}
private String getName(StopGroupBean bean) {
StringBuilder b = new StringBuilder();
for (String name : bean.getName().getNames())
b.append(name);
return b.toString();
}
});
}
}
}