Search in sources :

Example 1 with OsmVertex

use of org.opentripplanner.routing.vertextype.OsmVertex in project OpenTripPlanner by opentripplanner.

the class StreetSpeedSourceTest method testMatching.

@Test
public void testMatching() {
    Graph g = new Graph();
    OsmVertex v1 = new OsmVertex(g, "v1", 0, 0, 5l);
    OsmVertex v2 = new OsmVertex(g, "v2", 0, 0.01, 6l);
    StreetEdge se = new StreetEdge(v1, v2, null, "test", 1000, StreetTraversalPermission.CAR, false);
    se.wayId = 10;
    // create a speed sample
    SegmentSpeedSample s = getSpeedSample();
    Map<Segment, SegmentSpeedSample> speeds = Maps.newHashMap();
    Segment seg = new Segment(10l, 5l, 6l);
    speeds.put(seg, s);
    g.streetSpeedSource = new StreetSpeedSnapshotSource();
    g.streetSpeedSource.setSnapshot(new StreetSpeedSnapshot(speeds));
    // confirm that we get the correct speeds.
    // This also implicitly tests encoding/decoding
    OffsetDateTime odt = OffsetDateTime.of(2015, 6, 1, 8, 5, 0, 0, ZoneOffset.UTC);
    StreetSpeedSnapshot snap = g.streetSpeedSource.getSnapshot();
    double monday8am = snap.getSpeed(se, TraverseMode.CAR, odt.toInstant().toEpochMilli());
    // no data: should use average
    assertEquals(1.33, monday8am, 0.1);
    odt = odt.plusHours(1);
    double monday9am = snap.getSpeed(se, TraverseMode.CAR, odt.toInstant().toEpochMilli());
    assertEquals(6.1, monday9am, 0.1);
    odt = odt.plusHours(1);
    double monday10am = snap.getSpeed(se, TraverseMode.CAR, odt.toInstant().toEpochMilli());
    assertEquals(33.3, monday10am, 0.1);
    se.wayId = 102;
    double wrongStreet = snap.getSpeed(se, TraverseMode.CAR, odt.toInstant().toEpochMilli());
    assertTrue(Double.isNaN(wrongStreet));
}
Also used : Graph(org.opentripplanner.routing.graph.Graph) OffsetDateTime(java.time.OffsetDateTime) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) Test(org.junit.Test)

Example 2 with OsmVertex

use of org.opentripplanner.routing.vertextype.OsmVertex in project OpenTripPlanner by opentripplanner.

the class PlatformLinker method linkEntriesToPlatforms.

void linkEntriesToPlatforms() {
    LOG.info("Start linking platforms");
    List<OsmVertex> endpoints = graph.getVertices().stream().filter(OsmVertex.class::isInstance).map(OsmVertex.class::cast).filter(this::isEndpoint).collect(Collectors.toList());
    LOG.info("Endpoints found: " + endpoints.size());
    List<Area> platforms = osmdb.getWalkableAreas().stream().filter(area -> "platform".equals(area.parent.getTag("public_transport")) && "platform".equals(area.parent.getTag("railway"))).collect(Collectors.toList());
    LOG.info("Platforms found: " + platforms.size());
    for (Area area : platforms) {
        List<OsmVertex> endpointsWithin = new ArrayList<>();
        List<Ring> rings = area.outermostRings;
        for (Ring ring : rings) {
            endpointsWithin.addAll(endpoints.stream().filter(t -> contains(ring, t)).collect(Collectors.toList()));
            for (OSMNode node : ring.nodes) {
                Vertex vertexById = graph.getVertex("osm:node:" + node.getId());
                if (vertexById != null) {
                    endpointsWithin.forEach(e -> makePlatformEdges(area, e, (OsmVertex) vertexById));
                }
            }
        }
    }
    LOG.info("Done linking platforms");
}
Also used : CustomNamer(org.opentripplanner.graph_builder.services.osm.CustomNamer) Arrays(java.util.Arrays) OSMWithTags(org.opentripplanner.openstreetmap.model.OSMWithTags) LoggerFactory(org.slf4j.LoggerFactory) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) StreetEdgeFactory(org.opentripplanner.graph_builder.services.StreetEdgeFactory) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) SphericalDistanceLibrary(org.opentripplanner.common.geometry.SphericalDistanceLibrary) LineString(com.vividsolutions.jts.geom.LineString) OSMNode(org.opentripplanner.openstreetmap.model.OSMNode) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) ArrayList(java.util.ArrayList) Graph(org.opentripplanner.routing.graph.Graph) Coordinate(com.vividsolutions.jts.geom.Coordinate) Logger(org.slf4j.Logger) Vertex(org.opentripplanner.routing.graph.Vertex) I18NString(org.opentripplanner.util.I18NString) Math.min(java.lang.Math.min) Collectors(java.util.stream.Collectors) GeometryUtils(org.opentripplanner.common.geometry.GeometryUtils) List(java.util.List) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge) Math.max(java.lang.Math.max) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) StreetTraversalPermission(org.opentripplanner.routing.edgetype.StreetTraversalPermission) Edge(org.opentripplanner.routing.graph.Edge) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) Vertex(org.opentripplanner.routing.graph.Vertex) ArrayList(java.util.ArrayList) OSMNode(org.opentripplanner.openstreetmap.model.OSMNode) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex)

Example 3 with OsmVertex

use of org.opentripplanner.routing.vertextype.OsmVertex in project OpenTripPlanner by opentripplanner.

the class PlatformLinker method isEndpoint.

private boolean isEndpoint(OsmVertex ov) {
    boolean isCandidate = false;
    Vertex start = null;
    for (Edge e : ov.getIncoming()) {
        if (e instanceof StreetEdge && !(e instanceof AreaEdge)) {
            StreetEdge se = (StreetEdge) e;
            if (Arrays.asList(1, 2, 3).contains(se.getPermission().code)) {
                isCandidate = true;
                start = se.getFromVertex();
                break;
            }
        }
    }
    if (isCandidate && start != null) {
        boolean isEndpoint = true;
        for (Edge se : ov.getOutgoing()) {
            if (!se.getToVertex().getCoordinate().equals(start.getCoordinate()) && !(se instanceof AreaEdge)) {
                isEndpoint = false;
            }
        }
        return isEndpoint;
    }
    return false;
}
Also used : OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) Vertex(org.opentripplanner.routing.graph.Vertex) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge) Edge(org.opentripplanner.routing.graph.Edge) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge)

Example 4 with OsmVertex

use of org.opentripplanner.routing.vertextype.OsmVertex in project OpenTripPlanner by opentripplanner.

the class StreetSpeedSourceTest method testConcurrency.

@Test
public void testConcurrency() {
    Graph g = new Graph();
    StreetSpeedSnapshotSource ssss = new StreetSpeedSnapshotSource();
    OsmVertex v1 = new OsmVertex(g, "v1", 0, 0, 5l);
    OsmVertex v2 = new OsmVertex(g, "v2", 0, 0.01, 6l);
    StreetEdge se = new StreetEdge(v1, v2, null, "test", 1000, StreetTraversalPermission.CAR, false);
    se.wayId = 10;
    Map<Segment, SegmentSpeedSample> ss2 = Maps.newHashMap();
    Segment seg = new Segment(10l, 5l, 6l);
    ss2.put(seg, getSpeedSample());
    StreetSpeedSnapshot ssOrig = new StreetSpeedSnapshot(ss2);
    ssss.setSnapshot(ssOrig);
    StreetSpeedSnapshot snap = ssss.getSnapshot();
    assertEquals(ssOrig, snap);
    // should be match
    assertFalse(Double.isNaN(snap.getSpeed(se, TraverseMode.CAR, System.currentTimeMillis())));
    // should not have changed
    assertEquals(snap, ssss.getSnapshot());
    Map<Segment, SegmentSpeedSample> ss1 = Maps.newHashMap();
    seg = new Segment(10l, 4l, 6l);
    ss1.put(seg, getSpeedSample());
    StreetSpeedSnapshot ssNew = new StreetSpeedSnapshot(ss1);
    ssss.setSnapshot(ssNew);
    snap = ssss.getSnapshot();
    assertEquals(ssNew, snap);
    // should be no match; the segment in the index does not match the street edge
    assertTrue(Double.isNaN(snap.getSpeed(se, TraverseMode.CAR, System.currentTimeMillis())));
}
Also used : Graph(org.opentripplanner.routing.graph.Graph) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) OsmVertex(org.opentripplanner.routing.vertextype.OsmVertex) Test(org.junit.Test)

Example 5 with OsmVertex

use of org.opentripplanner.routing.vertextype.OsmVertex 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

StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)5 OsmVertex (org.opentripplanner.routing.vertextype.OsmVertex)5 Graph (org.opentripplanner.routing.graph.Graph)4 Edge (org.opentripplanner.routing.graph.Edge)3 Vertex (org.opentripplanner.routing.graph.Vertex)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 LineString (com.vividsolutions.jts.geom.LineString)2 Test (org.junit.Test)2 GeometryUtils (org.opentripplanner.common.geometry.GeometryUtils)2 SphericalDistanceLibrary (org.opentripplanner.common.geometry.SphericalDistanceLibrary)2 AreaEdge (org.opentripplanner.routing.edgetype.AreaEdge)2 Iterables (com.google.common.collect.Iterables)1 CoordinateSequence (com.vividsolutions.jts.geom.CoordinateSequence)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 Math.max (java.lang.Math.max)1 Math.min (java.lang.Math.min)1 OffsetDateTime (java.time.OffsetDateTime)1