Search in sources :

Example 1 with AccumulativeGridSampler

use of org.opentripplanner.common.geometry.AccumulativeGridSampler in project OpenTripPlanner by opentripplanner.

the class SampleGridRenderer method sampleSPT.

/**
 * Sample a SPT using a SPTWalker and an AccumulativeGridSampler.
 */
public static void sampleSPT(final ShortestPathTree spt, ZSampleGrid<WTWD> sampleGrid, final double gridSizeMeters, final double offRoadDistanceMeters, final double offRoadWalkSpeedMps, final double maxWalkDistance, final int maxTimeSec, final double cosLat) {
    AccumulativeMetric<WTWD> accMetric = new WTWDAccumulativeMetric(cosLat, offRoadDistanceMeters, offRoadWalkSpeedMps, gridSizeMeters);
    final AccumulativeGridSampler<WTWD> gridSampler = new AccumulativeGridSampler<WTWD>(sampleGrid, accMetric);
    // At which distance we split edges along the geometry during sampling.
    // For best results, this should be slighly lower than the grid size.
    double walkerSplitDistanceMeters = gridSizeMeters * 0.5;
    SPTWalker johnny = new SPTWalker(spt);
    johnny.walk(new SPTVisitor() {

        @Override
        public final boolean accept(Edge e) {
            return e instanceof StreetEdge;
        }

        @Override
        public final void visit(Edge e, Coordinate c, State s0, State s1, double d0, double d1, double speedAlongEdge) {
            double wd0 = s0.getWalkDistance() + d0;
            double wd1 = s0.getWalkDistance() + d1;
            double t0 = wd0 > maxWalkDistance ? Double.POSITIVE_INFINITY : s0.getActiveTime() + d0 / speedAlongEdge;
            double t1 = wd1 > maxWalkDistance ? Double.POSITIVE_INFINITY : s1.getActiveTime() + d1 / speedAlongEdge;
            if (t0 < maxTimeSec || t1 < maxTimeSec) {
                if (!Double.isInfinite(t0) || !Double.isInfinite(t1)) {
                    WTWD z = new WTWD();
                    z.w = 1.0;
                    z.d = 0.0;
                    if (t0 < t1) {
                        z.wTime = t0;
                        z.wBoardings = s0.getNumBoardings();
                        z.wWalkDist = s0.getWalkDistance() + d0;
                    } else {
                        z.wTime = t1;
                        z.wBoardings = s1.getNumBoardings();
                        z.wWalkDist = s1.getWalkDistance() + d1;
                    }
                    gridSampler.addSamplingPoint(c, z, offRoadWalkSpeedMps);
                }
            }
        }
    }, walkerSplitDistanceMeters);
    gridSampler.close();
}
Also used : SPTVisitor(org.opentripplanner.routing.spt.SPTWalker.SPTVisitor) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) SPTWalker(org.opentripplanner.routing.spt.SPTWalker) Coordinate(com.vividsolutions.jts.geom.Coordinate) AccumulativeGridSampler(org.opentripplanner.common.geometry.AccumulativeGridSampler) State(org.opentripplanner.routing.core.State) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge)

Example 2 with AccumulativeGridSampler

use of org.opentripplanner.common.geometry.AccumulativeGridSampler in project OpenTripPlanner by opentripplanner.

the class IsochroneGenerator method makeGrid.

/**
 * Make a ZSampleGrid from a PointSet and a parallel array of travel times for that PointSet.
 * Those times could come from a ResultSetWithTimes or directly from a PropagatedTimesStore, which has one
 * such array for each of min, avg, and max travel time over the departure time window it represents.
 * If your PointSet is dense enough (e.g. every block in a city) then this should yield a decent surface and
 * isochrones.
 * FIXME code duplication, this is ripped off from TimeSurface and should probably replace the version there as it is more generic.
 * @param walkSpeed the walk speed in meters per second
 * @return a grid suitable for making isochrones, based on an arbitrary PointSet and times to reach all those points.
 */
public static ZSampleGrid makeGrid(PointSet pointSet, int[] times, double walkSpeed) {
    // offroad walk distance roughly grid size
    final double D0 = WALK_DISTANCE_GRID_SIZE_RATIO * GRID_SIZE_METERS;
    // off-road walk speed in m/sec
    final double V0 = walkSpeed;
    // Use the first feature as the center of the projection
    Coordinate coordinateOrigin = pointSet.getCoordinate(0);
    final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y));
    double dY = Math.toDegrees(GRID_SIZE_METERS / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M);
    double dX = dY / cosLat;
    ZSampleGrid grid = new SparseMatrixZSampleGrid<WTWD>(16, times.length, dX, dY, coordinateOrigin);
    AccumulativeGridSampler.AccumulativeMetric<WTWD> metric = new SampleGridRenderer.WTWDAccumulativeMetric(cosLat, D0, V0, GRID_SIZE_METERS);
    AccumulativeGridSampler<WTWD> sampler = new AccumulativeGridSampler<>(grid, metric);
    // Iterate over every point in this PointSet, adding it to the ZSampleGrid
    for (int p = 0; p < times.length; p++) {
        int time = times[p];
        WTWD z = new WTWD();
        z.w = 1.0;
        z.d = 0.0;
        z.wTime = time;
        // unused
        z.wBoardings = 0;
        // unused
        z.wWalkDist = 0;
        sampler.addSamplingPoint(pointSet.getCoordinate(p), z, V0);
    }
    sampler.close();
    return grid;
}
Also used : SparseMatrixZSampleGrid(org.opentripplanner.common.geometry.SparseMatrixZSampleGrid) ZSampleGrid(org.opentripplanner.common.geometry.ZSampleGrid) WTWD(org.opentripplanner.analyst.request.SampleGridRenderer.WTWD) Coordinate(com.vividsolutions.jts.geom.Coordinate) AccumulativeGridSampler(org.opentripplanner.common.geometry.AccumulativeGridSampler) SparseMatrixZSampleGrid(org.opentripplanner.common.geometry.SparseMatrixZSampleGrid)

Example 3 with AccumulativeGridSampler

use of org.opentripplanner.common.geometry.AccumulativeGridSampler in project OpenTripPlanner by opentripplanner.

the class PropagatedTimesStore method makeSampleGridForVertices.

/**
 * Create a SampleGrid from only the times stored in this PropagatedTimesStore.
 * This assumes that the target indexes in this router/propagatedTimesStore are vertex indexes, not pointset indexes.
 * This is not really ideal since it includes only intersection nodes, and no points along the road segments.
 * FIXME this may be why we're getting hole-punching failures.
 * TODO: rewrite the isoline code to use only primitive collections and operate on a scalar field.
 */
public SparseMatrixZSampleGrid<WTWD> makeSampleGridForVertices(int[] times, final double gridSizeMeters) {
    SparseMatrixZSampleGrid<WTWD> grid;
    long t0 = System.currentTimeMillis();
    // Off-road max distance MUST be APPROX EQUALS to the grid precision
    // TODO: Loosen this restriction (by adding more closing sample).
    // Change the 0.8 magic factor here with caution. Should be roughly grid size.
    final double offroadWalkDistance = 0.8 * gridSizeMeters;
    // in m/sec
    final double offroadWalkSpeed = 1.00;
    Coordinate coordinateOrigin = graph.getCenter().get();
    final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y));
    double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M);
    double dX = dY / cosLat;
    grid = new SparseMatrixZSampleGrid<WTWD>(16, times.length, dX, dY, coordinateOrigin);
    AccumulativeGridSampler.AccumulativeMetric<SampleGridRenderer.WTWD> metric = new WTWDAccumulativeMetric(cosLat, offroadWalkDistance, offroadWalkSpeed, gridSizeMeters);
    AccumulativeGridSampler<WTWD> sampler = new AccumulativeGridSampler<>(grid, metric);
    // Iterate over every vertex, adding it to the ZSampleGrid if it was reached.
    for (int v = 0; v < times.length; v++) {
        int time = times[v];
        if (time == Integer.MAX_VALUE) {
            // MAX_VALUE is the "unreached" value
            continue;
        }
        WTWD z = new WTWD();
        z.w = 1.0;
        z.d = 0.0;
        z.wTime = time;
        // unused
        z.wBoardings = 0;
        // unused
        z.wWalkDist = 0;
        // FIXME ack, this uses a hashtable and autoboxing!
        Vertex vertex = graph.getVertexById(v);
        // FIXME we should propagate along street geometries here
        if (vertex != null) {
            sampler.addSamplingPoint(vertex.getCoordinate(), z, offroadWalkSpeed);
        }
    }
    sampler.close();
    long t1 = System.currentTimeMillis();
    LOG.info("Made scalar SampleGrid from TimeSurface in {} msec.", (int) (t1 - t0));
    return grid;
}
Also used : WTWDAccumulativeMetric(org.opentripplanner.analyst.request.SampleGridRenderer.WTWDAccumulativeMetric) Vertex(org.opentripplanner.routing.graph.Vertex) WTWD(org.opentripplanner.analyst.request.SampleGridRenderer.WTWD) Coordinate(com.vividsolutions.jts.geom.Coordinate) AccumulativeGridSampler(org.opentripplanner.common.geometry.AccumulativeGridSampler)

Example 4 with AccumulativeGridSampler

use of org.opentripplanner.common.geometry.AccumulativeGridSampler in project OpenTripPlanner by opentripplanner.

the class TimeSurface method makeSampleGridWithoutSPT.

/**
 * Create the SampleGrid from whatever values are already in the TimeSurface, rather than looking at the SPT.
 * This is not really ideal since it includes only intersection nodes, and no points along the road segments.
 */
public void makeSampleGridWithoutSPT() {
    long t0 = System.currentTimeMillis();
    // Todo: set dynamically and make sure this matches isoline builder params
    final double gridSizeMeters = 300;
    // Off-road max distance MUST be APPROX EQUALS to the grid precision
    // TODO: Loosen this restriction (by adding more closing sample).
    // Change the 0.8 magic factor here with caution.
    // offroad walk distance roughly grid size
    final double D0 = 0.8 * gridSizeMeters;
    // off-road walk speed in m/sec
    final double V0 = 1.00;
    Coordinate coordinateOrigin = new Coordinate();
    final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y));
    double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M);
    double dX = dY / cosLat;
    sampleGrid = new SparseMatrixZSampleGrid<WTWD>(16, this.times.size(), dX, dY, coordinateOrigin);
    AccumulativeGridSampler.AccumulativeMetric<WTWD> metric = new SampleGridRenderer.WTWDAccumulativeMetric(cosLat, D0, V0, gridSizeMeters);
    AccumulativeGridSampler<WTWD> sampler = new AccumulativeGridSampler<WTWD>(sampleGrid, metric);
    // TODO propagation along street geometries could happen at this stage, rather than when the SPT is still available.
    for (TObjectIntIterator<Vertex> iter = times.iterator(); iter.hasNext(); ) {
        iter.advance();
        Vertex vertex = iter.key();
        int time = iter.value();
        WTWD z = new WTWD();
        z.w = 1.0;
        z.d = 0.0;
        z.wTime = time;
        // unused
        z.wBoardings = 0;
        // unused
        z.wWalkDist = 0;
        sampler.addSamplingPoint(vertex.getCoordinate(), z, V0);
    }
    sampler.close();
    long t1 = System.currentTimeMillis();
    LOG.info("Made scalar SampleGrid from TimeSurface in {} msec.", (int) (t1 - t0));
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) WTWD(org.opentripplanner.analyst.request.SampleGridRenderer.WTWD) Coordinate(com.vividsolutions.jts.geom.Coordinate) AccumulativeGridSampler(org.opentripplanner.common.geometry.AccumulativeGridSampler)

Example 5 with AccumulativeGridSampler

use of org.opentripplanner.common.geometry.AccumulativeGridSampler in project OpenTripPlanner by opentripplanner.

the class TNPropagatedTimesStore method makeSampleGridForVertices.

// FIXME the following function requires a reference to the LinkedPointSet or the TransportNetwork. It should be elsewhere (PointSetTimeRange?)
/**
 * Create a SampleGrid from only the times stored in this PropagatedTimesStore.
 * This assumes that the target indexes in this router/propagatedTimesStore are vertex indexes, not pointset indexes.
 * This is not really ideal since it includes only intersection nodes, and no points along the road segments.
 * FIXME this may be why we're getting hole-punching failures.
 * TODO: rewrite the isoline code to use only primitive collections and operate on a scalar field.
 */
public SparseMatrixZSampleGrid<WTWD> makeSampleGridForVertices(int[] times, final double gridSizeMeters) {
    SparseMatrixZSampleGrid<WTWD> grid;
    long t0 = System.currentTimeMillis();
    // Off-road max distance MUST be APPROX EQUALS to the grid precision
    // TODO: Loosen this restriction (by adding more closing sample).
    // Change the 0.8 magic factor here with caution. Should be roughly grid size.
    final double offroadWalkDistance = 0.8 * gridSizeMeters;
    // in m/sec
    final double offroadWalkSpeed = 1.00;
    // FIXME new Coordinate(transitLayer.centerLon, transitLayer.centerLat);
    Coordinate coordinateOrigin = null;
    final double cosLat = FastMath.cos(toRadians(coordinateOrigin.y));
    double dY = Math.toDegrees(gridSizeMeters / SphericalDistanceLibrary.RADIUS_OF_EARTH_IN_M);
    double dX = dY / cosLat;
    grid = new SparseMatrixZSampleGrid<WTWD>(16, times.length, dX, dY, coordinateOrigin);
    AccumulativeGridSampler.AccumulativeMetric<WTWD> metric = new WTWDAccumulativeMetric(cosLat, offroadWalkDistance, offroadWalkSpeed, gridSizeMeters);
    AccumulativeGridSampler<WTWD> sampler = new AccumulativeGridSampler<>(grid, metric);
    // Iterate over every vertex, adding it to the ZSampleGrid if it was reached.
    // FIXME streetLayer.vertexStore.getCursor();
    Vertex vertex = null;
    for (int v = 0; v < times.length; v++) {
        int time = times[v];
        if (time == Integer.MAX_VALUE) {
            // MAX_VALUE is the "unreached" value
            continue;
        }
        WTWD z = new WTWD();
        z.w = 1.0;
        z.d = 0.0;
        z.wTime = time;
        // unused
        z.wBoardings = 0;
        // unused
        z.wWalkDist = 0;
    // vertex.seek(v); FIXME need street layer to get vertex cursor
    // // FIXME we should propagate along street geometries here
    // if (vertex != null) {
    // sampler.addSamplingPoint(vertex.getCoordinate(), z, offroadWalkSpeed);
    // }
    }
    sampler.close();
    long t1 = System.currentTimeMillis();
    LOG.info("Made scalar SampleGrid from TimeSurface in {} msec.", (int) (t1 - t0));
    return grid;
}
Also used : WTWDAccumulativeMetric(org.opentripplanner.analyst.request.SampleGridRenderer.WTWDAccumulativeMetric) Vertex(org.opentripplanner.routing.graph.Vertex) WTWD(org.opentripplanner.analyst.request.SampleGridRenderer.WTWD) Coordinate(com.vividsolutions.jts.geom.Coordinate) AccumulativeGridSampler(org.opentripplanner.common.geometry.AccumulativeGridSampler)

Aggregations

Coordinate (com.vividsolutions.jts.geom.Coordinate)5 AccumulativeGridSampler (org.opentripplanner.common.geometry.AccumulativeGridSampler)5 WTWD (org.opentripplanner.analyst.request.SampleGridRenderer.WTWD)4 Vertex (org.opentripplanner.routing.graph.Vertex)3 WTWDAccumulativeMetric (org.opentripplanner.analyst.request.SampleGridRenderer.WTWDAccumulativeMetric)2 SparseMatrixZSampleGrid (org.opentripplanner.common.geometry.SparseMatrixZSampleGrid)1 ZSampleGrid (org.opentripplanner.common.geometry.ZSampleGrid)1 State (org.opentripplanner.routing.core.State)1 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)1 Edge (org.opentripplanner.routing.graph.Edge)1 SPTWalker (org.opentripplanner.routing.spt.SPTWalker)1 SPTVisitor (org.opentripplanner.routing.spt.SPTWalker.SPTVisitor)1 StreetVertex (org.opentripplanner.routing.vertextype.StreetVertex)1