use of com.graphhopper.GHResponse in project graphhopper by graphhopper.
the class RouteResourceTest method testInitInstructionsWithTurnDescription.
@Test
public void testInitInstructionsWithTurnDescription() {
GraphHopperWeb hopper = new GraphHopperWeb(clientUrl(app, "/route"));
GHRequest request = new GHRequest(42.554851, 1.536198, 42.510071, 1.548128);
request.setProfile("my_car");
GHResponse rsp = hopper.route(request);
assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
assertEquals("Continue onto Carrer Antoni Fiter i Rossell", rsp.getBest().getInstructions().get(3).getName());
request.getHints().putObject("turn_description", false);
rsp = hopper.route(request);
assertFalse(rsp.hasErrors());
assertEquals("Carrer Antoni Fiter i Rossell", rsp.getBest().getInstructions().get(3).getName());
}
use of com.graphhopper.GHResponse 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.GHResponse in project graphhopper by graphhopper.
the class GraphHopperWeb method route.
@Override
public GHResponse route(GHRequest request) {
try {
String places = "";
for (GHPoint p : request.getPoints()) {
places += "point=" + p.lat + "," + p.lon + "&";
}
boolean tmpInstructions = request.getHints().getBool("instructions", instructions);
boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints);
boolean tmpTurnDescription = request.getHints().getBool("turn_description", turnDescription);
if (tmpInstructions && !tmpCalcPoints)
throw new IllegalStateException("Cannot calculate instructions without points (only points without instructions). " + "Use calc_points=false and instructions=false to disable point and instruction calculation");
boolean tmpElevation = request.getHints().getBool("elevation", elevation);
String url = routeServiceUrl + "?" + places + "&type=json" + "&instructions=" + tmpInstructions + "&points_encoded=true" + "&calc_points=" + tmpCalcPoints + "&algorithm=" + request.getAlgorithm() + "&locale=" + request.getLocale().toString() + "&elevation=" + tmpElevation;
if (!request.getVehicle().isEmpty())
url += "&vehicle=" + request.getVehicle();
if (!key.isEmpty())
url += "&key=" + key;
for (Entry<String, String> entry : request.getHints().toMap().entrySet()) {
String urlKey = entry.getKey();
String urlValue = entry.getValue();
// use lower case conversion for check only!
if (ignoreSet.contains(urlKey.toLowerCase()))
continue;
if (urlValue != null && !urlValue.isEmpty())
url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue);
}
String str = downloader.downloadAsString(url, true);
JSONObject json = new JSONObject(str);
GHResponse res = new GHResponse();
res.addErrors(readErrors(json));
if (res.hasErrors())
return res;
JSONArray paths = json.getJSONArray("paths");
for (int index = 0; index < paths.length(); index++) {
JSONObject path = paths.getJSONObject(index);
PathWrapper altRsp = createPathWrapper(path, tmpCalcPoints, tmpInstructions, tmpElevation, tmpTurnDescription);
res.add(altRsp);
}
return res;
} catch (Exception ex) {
throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex);
}
}
use of com.graphhopper.GHResponse in project graphhopper by graphhopper.
the class GraphHopperWebIT method testRetrieveOnlyStreetname.
@Test
public void testRetrieveOnlyStreetname() {
GHRequest req = new GHRequest().addPoint(new GHPoint(52.261434, 13.485718)).addPoint(new GHPoint(52.399067, 13.469238));
GHResponse res = gh.route(req);
assertEquals("Continue onto B 96", res.getBest().getInstructions().get(4).getName());
req.getHints().put("turn_description", false);
res = gh.route(req);
assertEquals("B 96", res.getBest().getInstructions().get(4).getName());
}
use of com.graphhopper.GHResponse in project graphhopper by graphhopper.
the class GraphHopperWebIT method testTimeout.
@Test
public void testTimeout() {
GHRequest req = new GHRequest().addPoint(new GHPoint(49.6724, 11.3494)).addPoint(new GHPoint(49.6550, 11.4180));
GHResponse res = gh.route(req);
assertFalse("errors:" + res.getErrors().toString(), res.hasErrors());
req.getHints().put(GraphHopperWeb.TIMEOUT, 1);
try {
res = gh.route(req);
fail();
} catch (RuntimeException e) {
assertEquals(SocketTimeoutException.class, e.getCause().getClass());
}
}
Aggregations