-
Notifications
You must be signed in to change notification settings - Fork 104
/
SubsectionTripTransformStrategy.java
287 lines (246 loc) · 8.73 KB
/
SubsectionTripTransformStrategy.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
/**
* Copyright (C) 2012 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.gtfs_transformer.updates;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.ShapePoint;
import org.onebusaway.gtfs.model.Stop;
import org.onebusaway.gtfs.model.StopTime;
import org.onebusaway.gtfs.model.StopLocation;
import org.onebusaway.gtfs.model.Trip;
import org.onebusaway.gtfs.services.GtfsMutableRelationalDao;
import org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy;
import org.onebusaway.gtfs_transformer.services.TransformContext;
public class SubsectionTripTransformStrategy implements GtfsTransformStrategy {
private Map<String, List<SubsectionOperation>> _operationsByRouteId = new HashMap<String, List<SubsectionOperation>>();
@Override
public String getName() {
return this.getClass().getSimpleName();
}
public void addOperation(SubsectionOperation operation) {
if (operation.getFromStopId() == null && operation.getToStopId() == null) {
throw new IllegalArgumentException(
"must specify at least fromStopId or toStopId");
}
List<SubsectionOperation> operations = _operationsByRouteId.get(operation.getRouteId());
if (operations == null) {
operations = new ArrayList<SubsectionOperation>();
_operationsByRouteId.put(operation.getRouteId(), operations);
}
operations.add(operation);
}
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
List<Trip> tripsToAdd = new ArrayList<Trip>();
List<StopTime> stopTimesToAdd = new ArrayList<StopTime>();
List<Trip> tripsToRemove = new ArrayList<Trip>();
List<StopTime> stopTimesToRemove = new ArrayList<StopTime>();
Set<AgencyAndId> newShapeIds = new HashSet<AgencyAndId>();
for (Trip trip : dao.getAllTrips()) {
String routeId = trip.getRoute().getId().getId();
List<SubsectionOperation> operations = _operationsByRouteId.get(routeId);
if (operations == null) {
continue;
}
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
Map<String, Integer> stopToIndex = getStopIndices(stopTimes);
boolean removeOriginalTrip = false;
for (SubsectionOperation operation : operations) {
Integer fromIndex = stopToIndex.get(operation.getFromStopId());
Integer toIndex = stopToIndex.get(operation.getToStopId());
if (fromIndex == null && toIndex == null) {
if (operation.removeUnmatchedTrips) {
removeOriginalTrip = true;
}
continue;
}
removeOriginalTrip = true;
Trip newTrip = new Trip(trip);
String id = newTrip.getId().getId();
if (fromIndex != null) {
id += "-" + operation.getFromStopId();
}
if (toIndex != null) {
id += "-" + operation.getToStopId();
}
if (fromIndex == null) {
fromIndex = 0;
} else if (!operation.isIncludeFromStop()) {
fromIndex++;
}
if (toIndex == null) {
toIndex = stopTimes.size() - 1;
} else if (!operation.isIncludeToStop()) {
toIndex--;
}
newTrip.setId(new AgencyAndId("1", id));
tripsToAdd.add(newTrip);
List<StopTime> newStopTimes = new ArrayList<StopTime>();
for (int i = fromIndex; i <= toIndex; ++i) {
StopTime stopTime = new StopTime(stopTimes.get(i));
stopTime.setId(0);
stopTime.setTrip(newTrip);
newStopTimes.add(stopTime);
}
updateShape(dao, newTrip, newStopTimes, newShapeIds);
stopTimesToAdd.addAll(newStopTimes);
}
if (removeOriginalTrip) {
tripsToRemove.add(trip);
stopTimesToRemove.addAll(stopTimes);
}
}
for (StopTime stopTime : stopTimesToRemove) {
dao.removeEntity(stopTime);
}
for (Trip trip : tripsToRemove) {
dao.removeEntity(trip);
}
for (Trip trip : tripsToAdd) {
dao.saveEntity(trip);
}
for (StopTime stopTime : stopTimesToAdd) {
dao.saveEntity(stopTime);
}
((GtfsRelationalDaoImpl) dao).clearAllCaches();
Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>(dao.getAllShapeIds());
for (Trip trip : dao.getAllTrips()) {
shapeIds.remove(trip.getShapeId());
}
for (AgencyAndId shapeId : shapeIds) {
for (ShapePoint point : dao.getShapePointsForShapeId(shapeId)) {
dao.removeEntity(point);
}
}
}
private void updateShape(GtfsMutableRelationalDao dao, Trip trip,
List<StopTime> stopTimes, Set<AgencyAndId> newShapeIds) {
if (stopTimes.size() < 2) {
trip.setShapeId(null);
return;
}
AgencyAndId shapeId = trip.getShapeId();
if (shapeId == null || !shapeId.hasValues()) {
return;
}
List<ShapePoint> points = dao.getShapePointsForShapeId(shapeId);
if (points.isEmpty()) {
return;
}
StopLocation firstStop = stopTimes.get(0).getStop();
StopLocation lastStop = stopTimes.get(stopTimes.size() - 1).getStop();
String id = shapeId.getId() + "-" + firstStop.getId().getId() + "-"
+ lastStop.getId().getId();
AgencyAndId newShapeId = new AgencyAndId("1", id);
trip.setShapeId(newShapeId);
if (!newShapeIds.add(newShapeId)) {
return;
}
if (!(firstStop instanceof Stop)) {
//TODO Correct error type
throw new Error(firstStop + " must be stop");
}
if (!(lastStop instanceof Stop)) {
//TODO Correct error type
throw new Error(firstStop + " must be stop");
}
int shapePointFrom = getClosestShapePointToStop(points, (Stop) firstStop);
int shapePointTo = getClosestShapePointToStop(points, (Stop) lastStop);
for (int index = shapePointFrom; index <= shapePointTo; ++index) {
ShapePoint point = new ShapePoint(points.get(index));
point.setId(0);
point.setShapeId(newShapeId);
dao.saveEntity(point);
}
}
private int getClosestShapePointToStop(List<ShapePoint> points, Stop stop) {
int minIndex = -1;
double minDistance = Double.POSITIVE_INFINITY;
for (int i = 0; i < points.size(); ++i) {
ShapePoint point = points.get(i);
double dx = point.getLon() - stop.getLon();
double dy = point.getLat() - stop.getLat();
double d = Math.sqrt(dx * dx + dy * dy);
if (d < minDistance) {
minIndex = i;
minDistance = d;
}
}
return minIndex;
}
private Map<String, Integer> getStopIndices(List<StopTime> stopTimes) {
Map<String, Integer> indices = new HashMap<String, Integer>();
int index = 0;
for (StopTime stopTime : stopTimes) {
String id = stopTime.getStop().getId().getId();
if (!indices.containsKey(id)) {
indices.put(id, index);
}
index++;
}
return indices;
}
public static class SubsectionOperation {
private String fromStopId;
private boolean includeFromStop = true;
private String toStopId;
private boolean includeToStop = true;
private String routeId;
private boolean removeUnmatchedTrips = false;
public String getFromStopId() {
return fromStopId;
}
public void setFromStopId(String fromStopId) {
this.fromStopId = fromStopId;
}
public boolean isIncludeFromStop() {
return includeFromStop;
}
public void setIncludeFromStop(boolean includeFromStop) {
this.includeFromStop = includeFromStop;
}
public String getToStopId() {
return toStopId;
}
public void setToStopId(String toStopId) {
this.toStopId = toStopId;
}
public boolean isIncludeToStop() {
return includeToStop;
}
public void setIncludeToStop(boolean includeToStop) {
this.includeToStop = includeToStop;
}
public String getRouteId() {
return routeId;
}
public void setRouteId(String routeId) {
this.routeId = routeId;
}
public boolean isRemoveUnmatchedTrips() {
return removeUnmatchedTrips;
}
public void setRemoveUnmatchedTrips(boolean removeUnmatchedTrips) {
this.removeUnmatchedTrips = removeUnmatchedTrips;
}
}
}