Search in sources :

Example 16 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint in project onebusaway-application-modules by camsys.

the class UTMProjectionTest method assertUTMPoint.

private void assertUTMPoint(double lat, double lon, double x, double y) {
    CoordinatePoint point = new CoordinatePoint(lat, lon);
    int zone = UTMLibrary.getUTMZoneForLongitude(lon);
    UTMProjection projection = new UTMProjection(zone);
    XYPoint p = projection.forward(point);
    assertEquals(x, p.getX(), 0.01);
    assertEquals(y, p.getY(), 0.01);
}
Also used : XYPoint(org.onebusaway.geospatial.model.XYPoint) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) XYPoint(org.onebusaway.geospatial.model.XYPoint) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 17 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint 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;
}
Also used : Min(org.onebusaway.collections.Min) PointAndIndex(org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex) StopTimeEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl) ArrayList(java.util.ArrayList) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) XYPoint(org.onebusaway.geospatial.model.XYPoint)

Example 18 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint 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);
}
Also used : XYPoint(org.onebusaway.geospatial.model.XYPoint) StopTimeEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl) PointAndIndex(org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) XYPoint(org.onebusaway.geospatial.model.XYPoint)

Example 19 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint 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;
}
Also used : XYPoint(org.onebusaway.geospatial.model.XYPoint) PointAndIndex(org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex) StopTimeEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StopEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl)

Example 20 with XYPoint

use of org.onebusaway.geospatial.model.XYPoint in project onebusaway-application-modules by camsys.

the class DistanceAlongShapeLibrary method checkFirstAndLastStop.

/**
 * Special check for an issue with start points where the first stop isn't all
 * that near the start of the shape (the first stop being more of a layover
 * point). If the shape is working against us, the closest point for the first
 * stop can be a point further along the shape, which causes problems.
 */
private void checkFirstAndLastStop(List<StopTimeEntryImpl> stopTimes, List<List<PointAndIndex>> possibleAssignments, ShapePoints shapePoints, UTMProjection projection, List<XYPoint> projectedShapePoints) {
    if (possibleAssignments.size() >= 2) {
        PointAndIndex first = possibleAssignments.get(0).get(0);
        PointAndIndex second = possibleAssignments.get(1).get(0);
        if (first.distanceAlongShape > second.distanceAlongShape) {
            StopTimeEntryImpl firstStopTime = stopTimes.get(0);
            _log.warn("snapping first stop time id=" + firstStopTime.getId() + " to start of shape");
            XYPoint point = projectedShapePoints.get(0);
            StopEntryImpl stop = firstStopTime.getStop();
            XYPoint stopPoint = projection.forward(stop.getStopLocation());
            double d = stopPoint.getDistance(point);
            possibleAssignments.get(0).add(new PointAndIndex(point, 0, d, 0.0));
        }
        int n = possibleAssignments.size();
        PointAndIndex prev = possibleAssignments.get(n - 2).get(0);
        PointAndIndex last = possibleAssignments.get(n - 1).get(0);
        if (prev.distanceAlongShape > last.distanceAlongShape) {
        }
    }
    if (possibleAssignments.size() > 0) {
        /**
         * We snap the last stop to the end of the shape and add it to the set of
         * possible assignments. In the worst case, it will be a higher-scoring
         * assignment and ignored, but it can help in cases where the stop was
         * weirdly assigned.
         */
        PointAndIndex lastSnapped = getLastStopSnappedToEndOfShape(stopTimes, shapePoints, projection, projectedShapePoints);
        possibleAssignments.get(possibleAssignments.size() - 1).add(lastSnapped);
    }
}
Also used : XYPoint(org.onebusaway.geospatial.model.XYPoint) PointAndIndex(org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex) StopTimeEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) XYPoint(org.onebusaway.geospatial.model.XYPoint) StopEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl)

Aggregations

XYPoint (org.onebusaway.geospatial.model.XYPoint)22 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)15 ArrayList (java.util.ArrayList)10 PointAndIndex (org.onebusaway.transit_data_federation.impl.shapes.PointAndIndex)6 Test (org.junit.Test)5 UTMProjection (org.onebusaway.geospatial.services.UTMProjection)5 StopTimeEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.StopTimeEntryImpl)4 List (java.util.List)3 Min (org.onebusaway.collections.Min)3 StopEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl)3 ProjectedPoint (org.onebusaway.transit_data_federation.model.ProjectedPoint)3 ShapePoints (org.onebusaway.transit_data_federation.model.ShapePoints)2 Point (com.vividsolutions.jts.geom.Point)1 Cacheable (org.onebusaway.container.cache.Cacheable)1 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 ShapePointsFactory (org.onebusaway.transit_data_federation.model.ShapePointsFactory)1 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)1 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)1