use of com.graphhopper.routing.Path in project graphhopper by graphhopper.
the class PathMerger method doWork.
public void doWork(PathWrapper altRsp, List<Path> paths, Translation tr) {
int origPoints = 0;
long fullTimeInMillis = 0;
double fullWeight = 0;
double fullDistance = 0;
boolean allFound = true;
InstructionList fullInstructions = new InstructionList(tr);
PointList fullPoints = PointList.EMPTY;
List<String> description = new ArrayList<String>();
for (int pathIndex = 0; pathIndex < paths.size(); pathIndex++) {
Path path = paths.get(pathIndex);
description.addAll(path.getDescription());
fullTimeInMillis += path.getTime();
fullDistance += path.getDistance();
fullWeight += path.getWeight();
if (enableInstructions) {
InstructionList il = path.calcInstructions(tr);
if (!il.isEmpty()) {
if (fullPoints.isEmpty()) {
PointList pl = il.get(0).getPoints();
// do a wild guess about the total number of points to avoid reallocation a bit
fullPoints = new PointList(il.size() * Math.min(10, pl.size()), pl.is3D());
}
for (Instruction i : il) {
if (simplifyResponse) {
origPoints += i.getPoints().size();
douglasPeucker.simplify(i.getPoints());
}
fullInstructions.add(i);
fullPoints.add(i.getPoints());
}
// if not yet reached finish replace with 'reached via'
if (pathIndex + 1 < paths.size()) {
ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1));
newInstr.setViaCount(pathIndex + 1);
fullInstructions.replaceLast(newInstr);
}
}
} else if (calcPoints) {
PointList tmpPoints = path.calcPoints();
if (fullPoints.isEmpty())
fullPoints = new PointList(tmpPoints.size(), tmpPoints.is3D());
if (simplifyResponse) {
origPoints = tmpPoints.getSize();
douglasPeucker.simplify(tmpPoints);
}
fullPoints.add(tmpPoints);
}
allFound = allFound && path.isFound();
}
if (!fullPoints.isEmpty()) {
String debug = altRsp.getDebugInfo() + ", simplify (" + origPoints + "->" + fullPoints.getSize() + ")";
altRsp.addDebugInfo(debug);
if (fullPoints.is3D)
calcAscendDescend(altRsp, fullPoints);
}
if (enableInstructions)
altRsp.setInstructions(fullInstructions);
if (!allFound)
altRsp.addError(new ConnectionNotFoundException("Connection between locations not found", Collections.<String, Object>emptyMap()));
altRsp.setDescription(description).setPoints(fullPoints).setRouteWeight(fullWeight).setDistance(fullDistance).setTime(fullTimeInMillis);
}
use of com.graphhopper.routing.Path in project graphhopper by graphhopper.
the class InstructionListTest method testEmptyList.
@Test
public void testEmptyList() {
Graph g = new GraphBuilder(carManager).create();
Path p = new Dijkstra(g, new ShortestWeighting(carEncoder), tMode).calcPath(0, 1);
InstructionList il = p.calcInstructions(usTR);
assertEquals(0, il.size());
assertEquals(0, il.createStartPoints().size());
}
use of com.graphhopper.routing.Path in project graphhopper by graphhopper.
the class InstructionListTest method testInstructionsWithTimeAndPlace.
@Test
public void testInstructionsWithTimeAndPlace() {
Graph g = new GraphBuilder(carManager).create();
// 4-5
// |
// 3-2
// |
// 1
NodeAccess na = g.getNodeAccess();
na.setNode(1, 15.0, 10);
na.setNode(2, 15.1, 10);
na.setNode(3, 15.1, 9.9);
na.setNode(4, 15.2, 9.9);
na.setNode(5, 15.2, 10);
g.edge(1, 2, 7000, true).setName("1-2").setFlags(flagsForSpeed(carManager, 70));
g.edge(2, 3, 8000, true).setName("2-3").setFlags(flagsForSpeed(carManager, 80));
g.edge(3, 4, 9000, true).setName("3-4").setFlags(flagsForSpeed(carManager, 90));
g.edge(4, 5, 10000, true).setName("4-5").setFlags(flagsForSpeed(carManager, 100));
Path p = new Dijkstra(g, new ShortestWeighting(carEncoder), tMode).calcPath(1, 5);
InstructionList wayList = p.calcInstructions(usTR);
assertEquals(5, wayList.size());
List<GPXEntry> gpxList = wayList.createGPXList();
assertEquals(34000, p.getDistance(), 1e-1);
assertEquals(34000, sumDistances(wayList), 1e-1);
assertEquals(5, gpxList.size());
assertEquals(1604120, p.getTime());
assertEquals(1604120, gpxList.get(gpxList.size() - 1).getTime());
assertEquals(Instruction.CONTINUE_ON_STREET, wayList.get(0).getSign());
assertEquals(15, wayList.get(0).getFirstLat(), 1e-3);
assertEquals(10, wayList.get(0).getFirstLon(), 1e-3);
assertEquals(Instruction.TURN_LEFT, wayList.get(1).getSign());
assertEquals(15.1, wayList.get(1).getFirstLat(), 1e-3);
assertEquals(10, wayList.get(1).getFirstLon(), 1e-3);
assertEquals(Instruction.TURN_RIGHT, wayList.get(2).getSign());
assertEquals(15.1, wayList.get(2).getFirstLat(), 1e-3);
assertEquals(9.9, wayList.get(2).getFirstLon(), 1e-3);
assertEquals(Instruction.TURN_RIGHT, wayList.get(3).getSign());
assertEquals(15.2, wayList.get(3).getFirstLat(), 1e-3);
assertEquals(9.9, wayList.get(3).getFirstLon(), 1e-3);
String gpxStr = wayList.createGPX("test", 0);
verifyGPX(gpxStr);
assertTrue(gpxStr, gpxStr.contains("<trkpt lat=\"15.0\" lon=\"10.0\"><time>1970-01-01T00:00:00Z</time>"));
assertTrue(gpxStr, gpxStr.contains("<extensions>") && gpxStr.contains("</extensions>"));
assertTrue(gpxStr, gpxStr.contains("<rtept lat=\"15.1\" lon=\"10.0\">"));
assertTrue(gpxStr, gpxStr.contains("<gh:distance>8000.0</gh:distance>"));
assertTrue(gpxStr, gpxStr.contains("<desc>turn left onto 2-3</desc>"));
assertTrue(gpxStr, gpxStr.contains("<gh:sign>-2</gh:sign>"));
assertTrue(gpxStr, gpxStr.contains("<gh:direction>N</gh:direction>"));
assertTrue(gpxStr, gpxStr.contains("<gh:azimuth>0.0</gh:azimuth>"));
assertFalse(gpxStr, gpxStr.contains("NaN"));
}
use of com.graphhopper.routing.Path in project graphhopper by graphhopper.
the class InstructionListTest method testFind.
@Test
public void testFind() {
Graph g = new GraphBuilder(carManager).create();
// n-4-5 (n: pillar node)
// |
// 3-2
// |
// 1
NodeAccess na = g.getNodeAccess();
na.setNode(1, 15.0, 10);
na.setNode(2, 15.1, 10);
na.setNode(3, 15.1, 9.9);
PointList waypoint = new PointList();
waypoint.add(15.2, 9.9);
na.setNode(4, 15.2, 10);
na.setNode(5, 15.2, 10.1);
g.edge(1, 2, 10000, true).setName("1-2");
g.edge(2, 3, 10000, true).setName("2-3");
g.edge(3, 4, 10000, true).setName("3-4").setWayGeometry(waypoint);
g.edge(4, 5, 10000, true).setName("4-5");
Path p = new Dijkstra(g, new ShortestWeighting(carEncoder), tMode).calcPath(1, 5);
InstructionList wayList = p.calcInstructions(usTR);
// query on first edge, get instruction for second edge
assertEquals("2-3", wayList.find(15.05, 10, 1000).getName());
// query east of first edge, get instruction for second edge
assertEquals("2-3", wayList.find(15.05, 10.001, 1000).getName());
// query south-west of node 3, get instruction for third edge
assertEquals("3-4", wayList.find(15.099, 9.9, 1000).getName());
// query north-west of pillar node n , get instruction for fourth edge
assertEquals("4-5", wayList.find(15.21, 9.85, 100000).getName());
}
use of com.graphhopper.routing.Path in project graphhopper by graphhopper.
the class InstructionListTest method testWayList2.
@Test
public void testWayList2() {
Graph g = new GraphBuilder(carManager).create();
// 2
// \. 5
// \/
// 4
// /
// 3
NodeAccess na = g.getNodeAccess();
na.setNode(2, 10.3, 10.15);
na.setNode(3, 10.0, 10.08);
na.setNode(4, 10.1, 10.10);
na.setNode(5, 10.2, 10.13);
g.edge(3, 4, 100, true).setName("3-4");
g.edge(4, 5, 100, true).setName("4-5");
EdgeIteratorState iter = g.edge(2, 4, 100, true);
iter.setName("2-4");
PointList list = new PointList();
list.add(10.20, 10.05);
iter.setWayGeometry(list);
Path p = new Dijkstra(g, new ShortestWeighting(carEncoder), tMode).calcPath(2, 3);
InstructionList wayList = p.calcInstructions(usTR);
List<String> tmpList = pick("text", wayList.createJson());
assertEquals(Arrays.asList("Continue onto 2-4", "Turn slight right onto 3-4", "Finish!"), tmpList);
p = new Dijkstra(g, new ShortestWeighting(carEncoder), tMode).calcPath(3, 5);
wayList = p.calcInstructions(usTR);
tmpList = pick("text", wayList.createJson());
assertEquals(Arrays.asList("Continue onto 3-4", "Continue onto 4-5", "Finish!"), tmpList);
}
Aggregations