use of org.opentripplanner.analyst.core.SampleSource in project OpenTripPlanner by opentripplanner.
the class SampleFactory method getSample.
@Override
public /**
* implements SampleSource interface
*/
Sample getSample(double lon, double lat) {
Coordinate c = new Coordinate(lon, lat);
// query always returns a (possibly empty) list, but never null
Envelope env = new Envelope(c);
// find scaling factor for equirectangular projection
double xscale = Math.cos(c.y * Math.PI / 180);
env.expandBy(searchRadiusLat / xscale, searchRadiusLat);
@SuppressWarnings("unchecked") Collection<Vertex> vertices = graph.streetIndex.getVerticesForEnvelope(env);
// make sure things are in the radius
final TIntDoubleMap distances = new TIntDoubleHashMap();
for (Vertex v : vertices) {
if (!(v instanceof OsmVertex))
continue;
// figure ersatz distance
double dx = (lon - v.getLon()) * xscale;
double dy = lat - v.getLat();
distances.put(v.getIndex(), dx * dx + dy * dy);
}
List<Vertex> sorted = new ArrayList<Vertex>();
for (Vertex input : vertices) {
if (!(input instanceof OsmVertex && distances.get(input.getIndex()) < searchRadiusLat * searchRadiusLat))
continue;
for (StreetEdge e : Iterables.filter(input.getOutgoing(), StreetEdge.class)) {
if (e.canTraverse(new TraverseModeSet(TraverseMode.WALK))) {
sorted.add(input);
break;
}
}
}
// sort list by distance
Collections.sort(sorted, new Comparator<Vertex>() {
@Override
public int compare(Vertex o1, Vertex o2) {
double d1 = distances.get(o1.getIndex());
double d2 = distances.get(o2.getIndex());
if (d1 < d2)
return -1;
else if (d1 > d2)
return 1;
else
return 0;
}
});
Vertex v0, v1;
if (sorted.isEmpty())
return null;
else if (sorted.size() <= 2) {
v0 = sorted.get(0);
v1 = sorted.size() > 1 ? sorted.get(1) : null;
} else {
int vxi = 0;
// Group them by distance
Vertex[] vx = new Vertex[2];
ArrayList<Vertex> grouped = new ArrayList<>();
// of at least EPSILON. Once we've done that, break ties using labels (which are OSM IDs).
for (int i = 0; i < sorted.size(); i++) {
if (vxi >= 2)
break;
if (grouped.isEmpty()) {
grouped.add(sorted.get(i));
continue;
}
double dlast = distances.get(sorted.get(i - 1).getIndex());
double dthis = distances.get(sorted.get(i).getIndex());
if (dthis - dlast < EPSILON) {
grouped.add(sorted.get(i));
continue;
} else {
// we have a distinct group of vertices
// sort them by OSM IDs
// this seems like it would be slow but keep in mind that it will only do any work
// when there are multiple members of a group, which is relatively rare.
Collections.sort(grouped, (vv1, vv2) -> vv1.getLabel().compareTo(vv2.getLabel()));
// then loop over the list until it's empty or we've found two vertices
int gi = 0;
while (vxi < 2 && gi < grouped.size()) {
vx[vxi++] = grouped.get(gi++);
}
// get ready for the next group
grouped.clear();
}
}
v0 = vx[0];
v1 = vx[1];
}
double d0 = v0 != null ? SphericalDistanceLibrary.distance(v0.getLat(), v0.getLon(), lat, lon) : 0;
double d1 = v1 != null ? SphericalDistanceLibrary.distance(v1.getLat(), v1.getLon(), lat, lon) : 0;
return new Sample(v0, (int) d0, v1, (int) d1);
}
Aggregations