use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.
the class GraphHopperWebIT method readFinishInstruction.
@Test
public void readFinishInstruction() {
GHRequest req = new GHRequest().addPoint(new GHPoint(52.261434, 13.485718)).addPoint(new GHPoint(52.399067, 13.469238));
GHResponse res = gh.route(req);
InstructionList instructions = res.getBest().getInstructions();
String finishInstructionName = instructions.get(instructions.size() - 1).getName();
assertEquals("Arrive at destination", finishInstructionName);
}
use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.
the class SimpleRouteSerializer method toJSON.
@Override
public Map<String, Object> toJSON(GHResponse rsp, boolean calcPoints, boolean pointsEncoded, boolean includeElevation, boolean enableInstructions) {
Map<String, Object> json = new HashMap<String, Object>();
if (rsp.hasErrors()) {
json.put("message", getMessage(rsp.getErrors().get(0)));
List<Map<String, Object>> errorHintList = new ArrayList<>();
for (Throwable t : rsp.getErrors()) {
Map<String, Object> map = new HashMap<>();
map.put("message", getMessage(t));
map.put("details", t.getClass().getName());
if (t instanceof GHException) {
map.putAll(((GHException) t).getDetails());
}
errorHintList.add(map);
}
json.put("hints", errorHintList);
} else {
Map<String, Object> jsonInfo = new HashMap<String, Object>();
json.put("info", jsonInfo);
json.put("hints", rsp.getHints().toMap());
// If you replace GraphHopper with your own brand name, this is fine.
// Still it would be highly appreciated if you mention us in your about page!
jsonInfo.put("copyrights", Arrays.asList("GraphHopper", "OpenStreetMap contributors"));
List<Map<String, Object>> jsonPathList = new ArrayList<Map<String, Object>>();
for (PathWrapper ar : rsp.getAll()) {
Map<String, Object> jsonPath = new HashMap<String, Object>();
jsonPath.put("distance", Helper.round(ar.getDistance(), 3));
jsonPath.put("weight", Helper.round6(ar.getRouteWeight()));
jsonPath.put("time", ar.getTime());
jsonPath.put("transfers", ar.getNumChanges());
if (!ar.getDescription().isEmpty())
jsonPath.put("description", ar.getDescription());
if (calcPoints) {
jsonPath.put("points_encoded", pointsEncoded);
PointList points = ar.getPoints();
if (points.getSize() >= 2) {
BBox maxBounds2D = new BBox(maxBounds.minLon, maxBounds.maxLon, maxBounds.minLat, maxBounds.maxLat);
jsonPath.put("bbox", ar.calcRouteBBox(maxBounds2D).toGeoJson());
}
jsonPath.put("points", createPoints(points, pointsEncoded, includeElevation));
if (enableInstructions) {
InstructionList instructions = ar.getInstructions();
jsonPath.put("instructions", instructions.createJson());
}
jsonPath.put("legs", ar.getLegs());
jsonPath.put("details", ar.getPathDetails());
jsonPath.put("ascend", ar.getAscend());
jsonPath.put("descend", ar.getDescend());
}
jsonPath.put("snapped_waypoints", createPoints(ar.getWaypoints(), pointsEncoded, includeElevation));
if (ar.getFare() != null) {
jsonPath.put("fare", NumberFormat.getCurrencyInstance(Locale.ROOT).format(ar.getFare()));
}
jsonPathList.add(jsonPath);
}
json.put("paths", jsonPathList);
}
return json;
}
use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.
the class RouteResourceTest method testGraphHopperWeb.
@Test
public void testGraphHopperWeb() {
GraphHopperWeb hopper = new GraphHopperWeb(clientUrl(app, "/route"));
GHResponse rsp = hopper.route(new GHRequest(42.554851, 1.536198, 42.510071, 1.548128).setProfile("my_car"));
assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
assertTrue(rsp.getErrors().isEmpty(), rsp.getErrors().toString());
ResponsePath res = rsp.getBest();
assertTrue(res.getDistance() > 9000, "distance wasn't correct:" + res.getDistance());
assertTrue(res.getDistance() < 9500, "distance wasn't correct:" + res.getDistance());
rsp = hopper.route(new GHRequest().setProfile("my_car").addPoint(new GHPoint(42.554851, 1.536198)).addPoint(new GHPoint(42.531896, 1.553278)).addPoint(new GHPoint(42.510071, 1.548128)));
assertTrue(rsp.getErrors().isEmpty(), rsp.getErrors().toString());
res = rsp.getBest();
assertTrue(res.getDistance() > 20000, "distance wasn't correct:" + res.getDistance());
assertTrue(res.getDistance() < 21000, "distance wasn't correct:" + res.getDistance());
InstructionList instructions = res.getInstructions();
assertEquals(24, instructions.size());
assertEquals("Continue onto la Callisa", instructions.get(0).getTurnDescription(null));
assertEquals("At roundabout, take exit 2", instructions.get(4).getTurnDescription(null));
assertEquals(true, instructions.get(4).getExtraInfoJSON().get("exited"));
assertEquals(false, instructions.get(22).getExtraInfoJSON().get("exited"));
}
use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.
the class GraphHopperWebIT method doNotReadFinishInstruction.
@Test
public void doNotReadFinishInstruction() {
GHRequest req = new GHRequest().addPoint(new GHPoint(52.261434, 13.485718)).addPoint(new GHPoint(52.399067, 13.469238));
req.getHints().put("turn_description", false);
GHResponse res = gh.route(req);
InstructionList instructions = res.getBest().getInstructions();
String finishInstructionName = instructions.get(instructions.size() - 1).getName();
assertEquals("", finishInstructionName);
}
use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.
the class RouteResourceClientHCTest method doNotReadFinishInstruction.
@ParameterizedTest
@EnumSource(value = TestParam.class)
public void doNotReadFinishInstruction(TestParam p) {
GraphHopperWeb gh = createGH(p);
GHRequest req = new GHRequest().addPoint(new GHPoint(42.507065, 1.529846)).addPoint(new GHPoint(42.510383, 1.533392)).putHint("vehicle", "car").putHint("turn_description", false);
GHResponse res = gh.route(req);
InstructionList instructions = res.getBest().getInstructions();
String finishInstructionName = instructions.get(instructions.size() - 1).getName();
assertEquals("", finishInstructionName);
}
Aggregations