use of org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl in project onebusaway-application-modules by camsys.
the class UnitTestingSupport method stopTime.
public static StopTimeEntryImpl stopTime(int id, StopEntryImpl stop, TripEntryImpl trip, int arrivalTime, int departureTime, double shapeDistTraveled, int shapeIndex) {
StopTimeEntryImpl stopTime = new StopTimeEntryImpl();
stopTime.setId(id);
stopTime.setStop(stop);
stopTime.setArrivalTime(arrivalTime);
stopTime.setDepartureTime(departureTime);
stopTime.setShapeDistTraveled(shapeDistTraveled);
stopTime.setShapePointIndex(shapeIndex);
if (trip != null)
addStopTime(trip, stopTime);
return stopTime;
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl in project onebusaway-application-modules by camsys.
the class DistanceAlongShapeLibrary method computeBestAssignment.
private List<PointAndIndex> computeBestAssignment(ShapePoints shapePoints, List<StopTimeEntryImpl> stopTimes, List<List<PointAndIndex>> possibleAssignments, UTMProjection projection, List<XYPoint> projectedShapePoints) throws InvalidStopToShapeMappingException {
checkFirstAndLastStop(stopTimes, possibleAssignments, shapePoints, projection, projectedShapePoints);
int startIndex = 0;
int assingmentCount = 1;
/**
* We iterate over each stop, examining its possible assignments. If we find
* a region of stops where the first and last stop have a single assignment
* but the stops in-between have multiple assignments, we compute the best
* assignments for that section. We also handle the edge-cases where the
* multiple potential assignments occur at the start or end of the route.
*/
for (int index = 0; index < possibleAssignments.size(); index++) {
List<PointAndIndex> possibleAssignment = possibleAssignments.get(index);
int count = possibleAssignment.size();
if (count == 0) {
constructErrorForPotentialAssignmentCount(shapePoints, stopTimes, count);
}
boolean hasRegion = index > startIndex;
boolean hasSingleAssignmentFollowingMultipleAssignments = count == 1 && assingmentCount > 1;
boolean hasMultipleAssignmentsAndLastPoint = count > 1 && index == possibleAssignments.size() - 1;
if (hasRegion && (hasSingleAssignmentFollowingMultipleAssignments || hasMultipleAssignmentsAndLastPoint)) {
List<PointAndIndex> currentAssignment = new ArrayList<PointAndIndex>(index - startIndex + 1);
Min<Assignment> bestAssignments = new Min<Assignment>();
recursivelyConstructAssignments(possibleAssignments, currentAssignment, startIndex, startIndex, index + 1, bestAssignments);
if (bestAssignments.isEmpty()) {
constructError(shapePoints, stopTimes, possibleAssignments, projection);
} else {
List<PointAndIndex> bestAssignment = bestAssignments.getMinElement().assigment;
for (int bestIndex = 0; bestIndex < bestAssignment.size(); bestIndex++) {
possibleAssignments.set(startIndex + bestIndex, Arrays.asList(bestAssignment.get(bestIndex)));
}
}
}
if (count == 1) {
startIndex = index;
assingmentCount = 1;
} else {
assingmentCount *= count;
if (assingmentCount > _maximumNumberOfPotentialAssignments) {
constructErrorForPotentialAssignmentCount(shapePoints, stopTimes, assingmentCount);
}
}
}
List<PointAndIndex> bestAssignment = new ArrayList<PointAndIndex>();
for (List<PointAndIndex> possibleAssignment : possibleAssignments) {
if (possibleAssignment.size() != 1) {
String msg = "expected just one assignment at this point, found " + possibleAssignment.size() + "; " + "shapePoint=" + shapePoints.getShapeId() + ", " + "\npossibleAssignments=\n";
for (PointAndIndex pa : possibleAssignment) {
msg += "PointAndIndex(index=" + pa.index + ", point=" + pa.point + ", distanceAlongShape=" + pa.distanceAlongShape + ", distanceFromTarget=" + pa.distanceFromTarget + "), ";
}
msg += "\nstopTime=\n";
for (StopTimeEntryImpl st : stopTimes) {
msg += "StopTimeEntry(Stop(" + st.getStop().getId() + ":" + st.getStop().getStopLat() + ", " + st.getStop().getStopLon() + ")" + ", trip=" + st.getTrip().getId() + "), ";
}
_log.error(msg);
if (!_lenientStopShapeAssignment)
throw new IllegalStateException(msg);
}
bestAssignment.add(possibleAssignment.get(0));
}
return bestAssignment;
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl in project onebusaway-application-modules by camsys.
the class DistanceAlongShapeLibrary method getLastStopSnappedToEndOfShape.
private PointAndIndex getLastStopSnappedToEndOfShape(List<StopTimeEntryImpl> stopTimes, ShapePoints shapePoints, UTMProjection projection, List<XYPoint> projectedShapePoints) {
int i = stopTimes.size() - 1;
StopTimeEntryImpl lastStopTime = stopTimes.get(i);
int lastShapePointIndex = projectedShapePoints.size() - 1;
XYPoint lastShapePoint = projectedShapePoints.get(lastShapePointIndex);
XYPoint stopLocation = projection.forward(lastStopTime.getStop().getStopLocation());
double existingDistanceAlongShape = shapePoints.getDistTraveledForIndex(lastShapePointIndex);
double extraDistanceAlongShape = lastShapePoint.getDistance(stopLocation);
double distanceAlongShape = existingDistanceAlongShape + extraDistanceAlongShape;
double d = lastShapePoint.getDistance(stopLocation);
return new PointAndIndex(lastShapePoint, lastShapePointIndex, d, distanceAlongShape);
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl in project onebusaway-application-modules by camsys.
the class DistanceAlongShapeLibrary method computePotentialAssignments.
private List<List<PointAndIndex>> computePotentialAssignments(UTMProjection projection, List<XYPoint> projectedShapePoints, double[] shapePointDistance, List<StopTimeEntryImpl> stopTimes) {
List<List<PointAndIndex>> possibleAssignments = new ArrayList<List<PointAndIndex>>();
for (StopTimeEntryImpl stopTime : stopTimes) {
StopEntryImpl stop = stopTime.getStop();
XYPoint stopPoint = projection.forward(stop.getStopLocation());
List<PointAndIndex> assignments = _shapePointsLibrary.computePotentialAssignments(projectedShapePoints, shapePointDistance, stopPoint, 0, projectedShapePoints.size());
possibleAssignments.add(assignments);
}
return possibleAssignments;
}
use of org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl in project onebusaway-application-modules by camsys.
the class DistanceAlongShapeLibrary method constructErrorForPotentialAssignmentCount.
private void constructErrorForPotentialAssignmentCount(ShapePoints shapePoints, List<StopTimeEntryImpl> stopTimes, long count) throws InvalidStopToShapeMappingException {
if (count == 0) {
_log.error("We were attempting to compute the distance along a particular trip for each stop time of that " + "trip by snapping them to the shape for that trip. However, we could not find an assignment for each " + "stop time of the trip, which usually indicates that there is something wrong with the underlying " + "shape data. For more information on errors of this kind, see:\n" + " https://github.com/OneBusAway/onebusaway-application-modules/wiki/Stop-to-Shape-Matching");
} else {
_log.error("We were attempting to compute the distance along a particular trip for each stop time of that " + "trip by snapping them to the shape for that trip. However, we found WAY TOO MANY potential " + "assignments, which usually indicates that there is something wrong with the underlying shape data. " + "For more information on errors of this kind, see:\n" + " https://github.com/OneBusAway/onebusaway-application-modules/wiki/Stop-to-Shape-Matching");
}
StopTimeEntryImpl first = stopTimes.get(0);
TripEntryImpl trip = first.getTrip();
StopTimeEntryImpl last = stopTimes.get(stopTimes.size() - 1);
_log.error("error constructing stop-time distances along shape for trip=" + trip.getId() + " shape=" + trip.getShapeId() + " firstStopTime=" + first.getId() + " lastStopTime=" + last.getId());
if (_shapeIdsWeHavePrinted.add(trip.getShapeId())) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < shapePoints.getSize(); i++) {
b.append(shapePoints.getLatForIndex(i));
b.append(' ');
b.append(shapePoints.getLonForIndex(i));
b.append(' ');
b.append(shapePoints.getDistTraveledForIndex(i));
b.append('\n');
}
_log.error("shape points:\n" + b.toString());
}
throw new InvalidStopToShapeMappingException(first.getTrip());
}
Aggregations