Search in sources :

Example 6 with Sample

use of org.opentripplanner.analyst.core.Sample in project OpenTripPlanner by opentripplanner.

the class SampleFactory method findClosest.

/**
 * DistanceToPoint.computeDistance() uses a LineSegment, which has a closestPoint method.
 * That finds the true distance every time rather than once the closest segment is known,
 * and does not allow for equi-rectangular projection/scaling.
 *
 * Here we want to compare squared distances to all line segments until we find the best one,
 * then do the precise calculations.
 */
public Sample findClosest(List<Edge> edges, Coordinate pt, double xscale) {
    Candidate c = new Candidate();
    // track the best geometry
    Candidate best = new Candidate();
    for (Edge edge : edges) {
        /* LineString.getCoordinates() uses PackedCoordinateSequence.toCoordinateArray() which
             * necessarily builds new Coordinate objects.CoordinateSequence.getOrdinate() reads them 
             * directly. */
        c.edge = edge;
        LineString ls = (LineString) (edge.getGeometry());
        // one of the two a sample gets linked to is effectively random.
        if (!edge.getFromVertex().getLabel().startsWith("osm:node:") || (edge instanceof StreetEdge && ((StreetEdge) edge).isBack()))
            continue;
        CoordinateSequence coordSeq = ls.getCoordinateSequence();
        int numCoords = coordSeq.size();
        for (int seg = 0; seg < numCoords - 1; seg++) {
            c.seg = seg;
            double x0 = coordSeq.getX(seg);
            double y0 = coordSeq.getY(seg);
            double x1 = coordSeq.getX(seg + 1);
            double y1 = coordSeq.getY(seg + 1);
            // use bounding rectangle to find a lower bound on (squared) distance ?
            // this would mean more squaring or roots.
            c.frac = GeometryUtils.segmentFraction(x0, y0, x1, y1, pt.x, pt.y, xscale);
            // project to get closest point
            // note: no need to multiply anything by xscale; the fraction is scaleless.
            c.x = x0 + c.frac * (x1 - x0);
            c.y = y0 + c.frac * (y1 - y0);
            // find ersatz distance to edge (do not take root)
            double dx = (c.x - pt.x) * xscale;
            double dy = c.y - pt.y;
            c.dist2 = dx * dx + dy * dy;
            // replace best segments
            if (c.dist2 < best.dist2) {
                best.setFrom(c);
            }
        }
    // end loop over segments
    }
    // if at least one vertex was found make a sample
    if (best.edge != null) {
        Vertex v0 = best.edge.getFromVertex();
        // Vertex v1 = best.edge.getToVertex();
        Vertex v1 = v0;
        double d = best.distanceTo(pt);
        if (d > searchRadiusM)
            return null;
        double d0 = d + best.distanceAlong();
        // double d1 = d + best.distanceToEnd();
        double d1 = d0;
        Sample s = new Sample(v0, (int) d0, v1, (int) d1);
        // System.out.println(s.toString());
        return s;
    }
    return null;
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Vertex(org.opentripplanner.routing.graph.Vertex) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) LineString(com.vividsolutions.jts.geom.LineString) Sample(org.opentripplanner.analyst.core.Sample) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge)

Example 7 with Sample

use of org.opentripplanner.analyst.core.Sample in project OpenTripPlanner by opentripplanner.

the class BatchProcessor method linkIntoGraph.

/**
 * Generate samples for (i.e. non-invasively link into the Graph) only those individuals that
 * were not rejected by filters. Other Individuals will have null samples, indicating that they
 * should be skipped.
 */
private void linkIntoGraph(Population p) {
    LOG.info("linking population {} to the graph...", p);
    int n = 0, nonNull = 0;
    for (Individual i : p) {
        Sample s = sampleFactory.getSample(i.lon, i.lat);
        i.sample = s;
        n += 1;
        if (s != null)
            nonNull += 1;
    }
    LOG.info("successfully linked {} individuals out of {}", nonNull, n);
}
Also used : Sample(org.opentripplanner.analyst.core.Sample)

Aggregations

Sample (org.opentripplanner.analyst.core.Sample)7 LineString (com.vividsolutions.jts.geom.LineString)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 CoordinateSequence (com.vividsolutions.jts.geom.CoordinateSequence)2 SampleFactory (org.opentripplanner.analyst.request.SampleFactory)2 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)2 Edge (org.opentripplanner.routing.graph.Edge)2 Vertex (org.opentripplanner.routing.graph.Vertex)2 OsmVertex (org.opentripplanner.routing.vertextype.OsmVertex)2 Iterables (com.google.common.collect.Iterables)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 TIntDoubleMap (gnu.trove.map.TIntDoubleMap)1 TIntDoubleHashMap (gnu.trove.map.hash.TIntDoubleHashMap)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 IsochroneData (org.opentripplanner.analyst.core.IsochroneData)1 SampleSource (org.opentripplanner.analyst.core.SampleSource)1 GeometryUtils (org.opentripplanner.common.geometry.GeometryUtils)1 RecursiveGridIsolineBuilder (org.opentripplanner.common.geometry.RecursiveGridIsolineBuilder)1