Search in sources :

Example 16 with TIntDoubleMap

use of gnu.trove.map.TIntDoubleMap 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);
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) Iterables(com.google.common.collect.Iterables) java.util(java.util) Envelope(com.vividsolutions.jts.geom.Envelope) Vertex(org.opentripplanner.routing.graph.Vertex) TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Sample(org.opentripplanner.analyst.core.Sample) SphericalDistanceLibrary(org.opentripplanner.common.geometry.SphericalDistanceLibrary) LineString(com.vividsolutions.jts.geom.LineString) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) GeometryUtils(org.opentripplanner.common.geometry.GeometryUtils) TIntDoubleMap(gnu.trove.map.TIntDoubleMap) Graph(org.opentripplanner.routing.graph.Graph) SampleSource(org.opentripplanner.analyst.core.SampleSource) TraverseMode(org.opentripplanner.routing.core.TraverseMode) CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Edge(org.opentripplanner.routing.graph.Edge) Vertex(org.opentripplanner.routing.graph.Vertex) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) Sample(org.opentripplanner.analyst.core.Sample) TIntDoubleMap(gnu.trove.map.TIntDoubleMap) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) TraverseModeSet(org.opentripplanner.routing.core.TraverseModeSet) Envelope(com.vividsolutions.jts.geom.Envelope) Coordinate(com.vividsolutions.jts.geom.Coordinate) TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex)

Aggregations

TIntDoubleMap (gnu.trove.map.TIntDoubleMap)16 TIntDoubleHashMap (gnu.trove.map.hash.TIntDoubleHashMap)12 TIntDoubleIterator (gnu.trove.iterator.TIntDoubleIterator)6 Iterables (com.google.common.collect.Iterables)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 Envelope (com.vividsolutions.jts.geom.Envelope)2 LineString (com.vividsolutions.jts.geom.LineString)2 PosNegRWExample (edu.cmu.ml.proppr.examples.PosNegRWExample)2 PprExample (edu.cmu.ml.proppr.examples.PprExample)2 Test (org.junit.Test)2 GeometryUtils (org.opentripplanner.common.geometry.GeometryUtils)2 SphericalDistanceLibrary (org.opentripplanner.common.geometry.SphericalDistanceLibrary)2 TraverseMode (org.opentripplanner.routing.core.TraverseMode)2 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)2 CoordinateSequence (com.vividsolutions.jts.geom.CoordinateSequence)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 SpatialIndex (com.vividsolutions.jts.index.SpatialIndex)1 LinearLocation (com.vividsolutions.jts.linearref.LinearLocation)1 LocationIndexedLine (com.vividsolutions.jts.linearref.LocationIndexedLine)1 DprExample (edu.cmu.ml.proppr.examples.DprExample)1