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;
}
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);
}
Aggregations