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