use of org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation in project onebusaway-application-modules by camsys.
the class ScheduledBlockLocationServiceImpl method getScheduledBlockLocationWhenAtStopTime.
private ScheduledBlockLocation getScheduledBlockLocationWhenAtStopTime(BlockStopTimeEntry blockStopTime, BlockStopTimeEntry previousBlockStopTime, StopTimeEntry stopTime, int scheduleTime, int stopTimeIndex) {
StopEntry stop = stopTime.getStop();
ScheduledBlockLocation result = new ScheduledBlockLocation();
int shapePointIndex = stopTime.getShapePointIndex();
PointAndOrientation po = getLocationAlongShape(blockStopTime.getTrip(), blockStopTime.getDistanceAlongBlock(), shapePointIndex, shapePointIndex + 1);
if (po != null) {
result.setLocation(po.getPoint());
result.setOrientation(po.getOrientation());
} else {
CoordinatePoint location = new CoordinatePoint(stop.getStopLat(), stop.getStopLon());
result.setLocation(location);
result.setOrientation(0);
}
result.setClosestStop(blockStopTime);
result.setClosestStopTimeOffset(0);
result.setNextStop(blockStopTime);
result.setNextStopTimeOffset(0);
result.setScheduledTime(scheduleTime);
result.setDistanceAlongBlock(blockStopTime.getDistanceAlongBlock());
result.setActiveTrip(blockStopTime.getTrip());
result.setInService(true);
result.setStopTimeIndex(stopTimeIndex);
// If there is more than 1 stop, grab the previous stop
if (blockStopTime.hasPreviousStop()) {
result.setPreviousStop(previousBlockStopTime);
}
return result;
}
use of org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation in project onebusaway-application-modules by camsys.
the class ScheduledBlockLocationServiceImpl method getScheduledBlockLocationBeforeStartOfBlock.
private ScheduledBlockLocation getScheduledBlockLocationBeforeStartOfBlock(List<BlockStopTimeEntry> stopTimes, int scheduleTime) {
/**
* The first block stop time
*/
BlockStopTimeEntry blockStopTime = stopTimes.get(0);
StopTimeEntry stopTime = blockStopTime.getStopTime();
double distanceAlongBlock = Double.NaN;
boolean inService = false;
/**
* If we have more than one stop time in the block (we'd hope!), then we
* attempt to interpolate the distance along the block
*/
if (stopTimes.size() > 1) {
BlockStopTimeEntry secondBlockStopTime = stopTimes.get(1);
StopTimeEntry secondStopTime = secondBlockStopTime.getStopTime();
distanceAlongBlock = InterpolationLibrary.interpolatePair(stopTime.getDepartureTime(), blockStopTime.getDistanceAlongBlock(), secondStopTime.getArrivalTime(), secondBlockStopTime.getDistanceAlongBlock(), scheduleTime);
if (distanceAlongBlock >= 0)
inService = true;
else
distanceAlongBlock = 0.0;
}
PointAndOrientation po = null;
if (!Double.isNaN(distanceAlongBlock))
po = getLocationAlongShape(blockStopTime.getTrip(), distanceAlongBlock, 0, nextShapePointIndex(stopTime));
ScheduledBlockLocation result = new ScheduledBlockLocation();
if (po != null) {
result.setLocation(po.getPoint());
result.setOrientation(po.getOrientation());
}
result.setClosestStop(blockStopTime);
result.setClosestStopTimeOffset(stopTime.getArrivalTime() - scheduleTime);
result.setPreviousStop(null);
result.setNextStop(blockStopTime);
result.setNextStopTimeOffset(stopTime.getArrivalTime() - scheduleTime);
result.setScheduledTime(scheduleTime);
result.setDistanceAlongBlock(distanceAlongBlock);
result.setActiveTrip(blockStopTime.getTrip());
result.setInService(inService);
result.setStopTimeIndex(0);
return result;
}
use of org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation in project onebusaway-application-modules by camsys.
the class GenerateNarrativesTask method computeStopDirection.
private String computeStopDirection(NarrativeProviderImpl provider, Map<AgencyAndId, List<ProjectedPoint>> shapePointCache, Stop stop, StopEntry stopEntry) {
String direction = translateGtfsDirection(stop.getDirection());
if (direction != null)
return direction;
Collection<PointAndOrientation> orientations = getAllOrientationsForStop(provider, stopEntry);
DoubleArrayList ys = new DoubleArrayList();
DoubleArrayList xs = new DoubleArrayList();
for (PointAndOrientation po : orientations) {
double orientation = Math.toRadians(po.getOrientation());
double x = Math.cos(orientation);
double y = Math.sin(orientation);
xs.add(x);
ys.add(y);
}
if (ys.isEmpty())
return null;
if (ys.size() == 1) {
double theta = Math.atan2(ys.get(0), xs.get(0));
return getAngleAsDirection(theta);
}
double yMu = Descriptive.mean(ys);
double xMu = Descriptive.mean(xs);
/**
* Check for undefined case where angles are directly opposite
*/
if (yMu == 0.0 && xMu == 0.0)
return null;
double thetaMu = Math.atan2(yMu, xMu);
double yVariance = Descriptive.sampleVariance(ys, yMu);
double xVariance = Descriptive.sampleVariance(xs, xMu);
double yStdDev = Descriptive.sampleStandardDeviation(ys.size(), yVariance);
double xStdDev = Descriptive.sampleStandardDeviation(xs.size(), xVariance);
if (yStdDev > _stopDirectionStandardDeviationThreshold || xStdDev > _stopDirectionStandardDeviationThreshold) {
return null;
}
DoubleArrayList normalizedThetas = new DoubleArrayList();
for (PointAndOrientation po : orientations) {
double orientation = Math.toRadians(po.getOrientation());
double delta = orientation - thetaMu;
delta = normalizeDelta(delta);
orientation = thetaMu + delta;
normalizedThetas.add(orientation);
}
normalizedThetas.sort();
double thetaMedian = Descriptive.median(normalizedThetas);
return getAngleAsDirection(thetaMedian);
}
use of org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation in project onebusaway-application-modules by camsys.
the class GenerateNarrativesTask method getAllOrientationsForStop.
private Collection<PointAndOrientation> getAllOrientationsForStop(NarrativeProviderImpl provider, StopEntry stop) {
List<BlockStopTimeIndex> stopTimeIndices = _blockIndexService.getStopTimeIndicesForStop(stop);
List<PointAndOrientation> pos = new ArrayList<PointAndOrientation>();
Map<ShapeIdAndDistance, PointAndOrientation> orientationsByKey = new HashMap<ShapeIdAndDistance, PointAndOrientation>();
for (BlockStopTimeIndex stopTimeIndex : stopTimeIndices) {
for (BlockStopTimeEntry blockStopTime : stopTimeIndex.getStopTimes()) {
StopTimeEntry stopTime = blockStopTime.getStopTime();
TripEntry trip = stopTime.getTrip();
AgencyAndId shapeId = trip.getShapeId();
if (shapeId == null)
continue;
ShapePoints shapePoints = provider.getShapePointsForId(shapeId);
if (shapePoints == null)
continue;
int shapePointIndex = stopTime.getShapePointIndex();
if (shapePointIndex == -1)
continue;
ShapeIdAndDistance key = new ShapeIdAndDistance(shapeId, stopTime.getShapeDistTraveled());
PointAndOrientation orientation = orientationsByKey.get(key);
if (orientation == null) {
int indexFrom = Math.max(0, shapePointIndex - 5);
int indexTo = Math.min(shapePoints.getSize(), shapePointIndex + 5);
ShapePointIndex shapePointIndexMethod = new DistanceTraveledShapePointIndex(stopTime.getShapeDistTraveled(), indexFrom, indexTo);
orientation = shapePointIndexMethod.getPointAndOrientation(shapePoints);
if (orientation == null)
continue;
orientationsByKey.put(key, orientation);
}
pos.add(orientation);
}
}
return orientationsByKey.values();
}
use of org.onebusaway.transit_data_federation.impl.shapes.PointAndOrientation in project onebusaway-application-modules by camsys.
the class ScheduledBlockLocationServiceImpl method getScheduledBlockLocationBetweenStopTimes.
private ScheduledBlockLocation getScheduledBlockLocationBetweenStopTimes(List<BlockStopTimeEntry> stopTimes, int scheduleTime, int stopTimeIndex) {
BlockStopTimeEntry blockBefore = stopTimes.get(stopTimeIndex - 1);
BlockStopTimeEntry blockAfter = stopTimes.get(stopTimeIndex);
StopTimeEntry before = blockBefore.getStopTime();
StopTimeEntry after = blockAfter.getStopTime();
ScheduledBlockLocation result = new ScheduledBlockLocation();
result.setScheduledTime(scheduleTime);
result.setInService(true);
result.setStopTimeIndex(stopTimeIndex);
int fromTime = before.getDepartureTime();
int toTime = after.getArrivalTime();
int fromTimeOffset = fromTime - scheduleTime;
int toTimeOffset = toTime - scheduleTime;
if (Math.abs(fromTimeOffset) < Math.abs(toTimeOffset)) {
result.setClosestStop(blockBefore);
result.setClosestStopTimeOffset(fromTimeOffset);
} else {
result.setClosestStop(blockAfter);
result.setClosestStopTimeOffset(toTimeOffset);
}
result.setPreviousStop(blockBefore);
result.setNextStop(blockAfter);
result.setNextStopTimeOffset(toTimeOffset);
double ratio = (scheduleTime - fromTime) / ((double) (toTime - fromTime));
double fromDistance = blockBefore.getDistanceAlongBlock();
double toDistance = blockAfter.getDistanceAlongBlock();
double distanceAlongBlock = ratio * (toDistance - fromDistance) + fromDistance;
result.setDistanceAlongBlock(distanceAlongBlock);
int shapePointIndexFrom = -1;
int shapePointIndexTo = -1;
/**
* Are we between trips? Where is the transition point?
*/
if (!before.getTrip().equals(after.getTrip())) {
if (distanceAlongBlock >= blockAfter.getTrip().getDistanceAlongBlock()) {
result.setActiveTrip(blockAfter.getTrip());
shapePointIndexFrom = 0;
shapePointIndexTo = nextShapePointIndex(after);
} else {
result.setActiveTrip(blockBefore.getTrip());
shapePointIndexFrom = before.getShapePointIndex();
shapePointIndexTo = Integer.MAX_VALUE;
}
} else {
result.setActiveTrip(blockBefore.getTrip());
shapePointIndexFrom = before.getShapePointIndex();
shapePointIndexTo = nextShapePointIndex(after);
}
BlockTripEntry activeTrip = result.getActiveTrip();
PointAndOrientation po = getLocationAlongShape(activeTrip, distanceAlongBlock, shapePointIndexFrom, shapePointIndexTo);
if (po != null) {
result.setLocation(po.getPoint());
result.setOrientation(po.getOrientation());
return result;
}
StopEntry beforeStop = before.getStop();
StopEntry afterStop = after.getStop();
double latFrom = beforeStop.getStopLat();
double lonFrom = beforeStop.getStopLon();
double latTo = afterStop.getStopLat();
double lonTo = afterStop.getStopLon();
double lat = (latTo - latFrom) * ratio + latFrom;
double lon = (lonTo - lonFrom) * ratio + lonFrom;
CoordinatePoint location = new CoordinatePoint(lat, lon);
result.setLocation(location);
double orientation = SphericalGeometryLibrary.getOrientation(latFrom, lonFrom, latTo, lonTo);
result.setOrientation(orientation);
return result;
}
Aggregations