use of com.graphhopper.GHRequest in project graphhopper by graphhopper.
the class RouteResourceTest method testPathDetailsRoadClass.
@Test
public void testPathDetailsRoadClass() {
GraphHopperWeb client = new GraphHopperWeb(clientUrl(app, "/route"));
GHRequest request = new GHRequest(42.546757, 1.528645, 42.520573, 1.557999).setProfile("my_car");
request.setPathDetails(Arrays.asList(RoadClass.KEY, Surface.KEY, RoadEnvironment.KEY, "average_speed", RoadClassLink.KEY));
GHResponse rsp = client.route(request);
assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
assertEquals(4, rsp.getBest().getPathDetails().get(RoadClass.KEY).size());
assertEquals(RoadClass.PRIMARY.toString(), rsp.getBest().getPathDetails().get(RoadClass.KEY).get(3).getValue());
assertFalse((Boolean) rsp.getBest().getPathDetails().get(RoadClassLink.KEY).get(0).getValue());
List<PathDetail> roadEnvList = rsp.getBest().getPathDetails().get(RoadEnvironment.KEY);
assertEquals(10, roadEnvList.size());
assertEquals(RoadEnvironment.ROAD.toString(), roadEnvList.get(0).getValue());
assertEquals(RoadEnvironment.TUNNEL.toString(), roadEnvList.get(6).getValue());
}
use of com.graphhopper.GHRequest in project graphhopper by graphhopper.
the class RouteResourceTest method testSnapPreventions.
@Test
public void testSnapPreventions() {
GraphHopperWeb hopper = new GraphHopperWeb(clientUrl(app, "route"));
GHRequest request = new GHRequest(42.511139, 1.53285, 42.508165, 1.532271);
request.setProfile("my_car");
GHResponse rsp = hopper.route(request);
assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
assertEquals(490, rsp.getBest().getDistance(), 2);
request.setSnapPreventions(Collections.singletonList("tunnel"));
rsp = hopper.route(request);
assertEquals(1081, rsp.getBest().getDistance(), 2);
}
use of com.graphhopper.GHRequest in project graphhopper by graphhopper.
the class RouteResourceTest method testPathDetailsNoConnection.
@Test
public void testPathDetailsNoConnection() {
GraphHopperWeb hopper = new GraphHopperWeb(clientUrl(app, "/route"));
GHRequest request = new GHRequest(42.542078, 1.45586, 42.537841, 1.439981);
request.setPathDetails(Collections.singletonList("average_speed"));
request.setProfile("my_car");
GHResponse rsp = hopper.route(request);
assertTrue(rsp.hasErrors(), rsp.getErrors().toString());
}
use of com.graphhopper.GHRequest in project graphhopper by graphhopper.
the class RouteResourceTest method testGraphHopperWebRealExceptions.
@ParameterizedTest(name = "POST = {0}")
@ValueSource(booleans = { false, true })
public void testGraphHopperWebRealExceptions(boolean usePost) {
GraphHopperWeb hopper = new GraphHopperWeb(clientUrl(app, "/route")).setPostRequest(usePost);
// this one actually works
List<GHPoint> points = Arrays.asList(new GHPoint(42.554851, 1.536198), new GHPoint(42.510071, 1.548128));
GHResponse rsp = hopper.route(new GHRequest(points).setProfile("my_car"));
assertEquals(9204, rsp.getBest().getDistance(), 10);
// unknown profile
rsp = hopper.route(new GHRequest(points).setProfile("space_shuttle"));
assertTrue(rsp.hasErrors(), rsp.getErrors().toString());
assertTrue(rsp.getErrors().get(0).getMessage().contains("The requested profile 'space_shuttle' does not exist"), rsp.getErrors().toString());
// unknown profile via web api
Response response = clientTarget(app, "/route?profile=SPACE-SHUTTLE&point=42.554851,1.536198&point=42.510071,1.548128").request().buildGet().invoke();
assertEquals(400, response.getStatus());
String msg = (String) response.readEntity(Map.class).get("message");
assertTrue(msg.contains("The requested profile 'SPACE-SHUTTLE' does not exist"), msg);
// no points
rsp = hopper.route(new GHRequest().setProfile("my_car"));
assertFalse(rsp.getErrors().isEmpty(), "Errors expected but not found.");
Throwable ex = rsp.getErrors().get(0);
assertTrue(ex instanceof IllegalArgumentException, "Wrong exception found: " + ex.getClass().getName() + ", IllegalArgumentException expected.");
assertTrue(ex.getMessage().contains("You have to pass at least one point"), ex.getMessage());
// no points without CH
rsp = hopper.route(new GHRequest().setProfile("my_car").putHint(Parameters.CH.DISABLE, true));
assertFalse(rsp.getErrors().isEmpty(), "Errors expected but not found.");
ex = rsp.getErrors().get(0);
assertTrue(ex instanceof IllegalArgumentException, "Wrong exception found: " + ex.getClass().getName() + ", IllegalArgumentException expected.");
assertTrue(ex.getMessage().contains("You have to pass at least one point"), ex.getMessage());
// points out of bounds
rsp = hopper.route(new GHRequest(0.0, 0.0, 0.0, 0.0).setProfile("my_car"));
assertFalse(rsp.getErrors().isEmpty(), "Errors expected but not found.");
List<Throwable> errs = rsp.getErrors();
for (int i = 0; i < errs.size(); i++) {
assertEquals(((PointOutOfBoundsException) errs.get(i)).getPointIndex(), i);
assertTrue(errs.get(i).getMessage().contains("Point 0 is out of bounds: 0.0,0.0"), errs.get(i).getMessage());
}
// todo: add a check with too few headings, but client-hc does not support headings, #2009
// too many curbsides
rsp = hopper.route(new GHRequest(points).setCurbsides(Arrays.asList("right", "left", "right")).setProfile("my_car"));
assertFalse(rsp.getErrors().isEmpty(), "Errors expected but not found.");
assertTrue(rsp.getErrors().toString().contains("If you pass curbside, you need to pass exactly one curbside for every point"), rsp.getErrors().toString());
// too few point hints
rsp = hopper.route(new GHRequest(points).setPointHints(Collections.singletonList("foo")).setProfile("my_car"));
assertFalse(rsp.getErrors().isEmpty(), "Errors expected but not found.");
assertTrue(rsp.getErrors().toString().contains("If you pass point_hint, you need to pass exactly one hint for every point"), rsp.getErrors().toString());
// unknown vehicle
rsp = hopper.route(new GHRequest(points).putHint("vehicle", "SPACE-SHUTTLE"));
assertFalse(rsp.getErrors().isEmpty(), "Errors expected but not found.");
ex = rsp.getErrors().get(0);
assertTrue(ex instanceof IllegalArgumentException, "Wrong exception found: " + ex.getClass().getName() + ", IllegalArgumentException expected.");
assertTrue(ex.getMessage().contains("Vehicle not supported: `space-shuttle`. Supported are: `car`" + "\nYou should consider using the `profile` parameter instead of specifying a vehicle." + "\nAvailable profiles: [my_car]"), ex.getMessage());
// an IllegalArgumentException from inside the core is written as JSON, unknown profile
response = clientTarget(app, "/route?profile=SPACE-SHUTTLE&point=42.554851,1.536198&point=42.510071,1.548128").request().buildGet().invoke();
assertEquals(400, response.getStatus());
msg = (String) response.readEntity(Map.class).get("message");
assertTrue(msg.contains("The requested profile 'SPACE-SHUTTLE' does not exist"), msg);
}
use of com.graphhopper.GHRequest in project graphhopper by graphhopper.
the class Examples method routing.
@Test
public void routing() {
// Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
GraphHopperWeb gh = new GraphHopperWeb();
// insert your key here
gh.setKey(apiKey);
// change timeout, default is 5 seconds
gh.setDownloader(new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build());
// specify at least two coordinates
GHRequest req = new GHRequest().addPoint(new GHPoint(49.6724, 11.3494)).addPoint(new GHPoint(49.6550, 11.4180));
// Set vehicle like car, bike, foot, ...
req.putHint("vehicle", "bike");
// Optionally enable/disable elevation in output PointList, currently bike and foot support elevation, default is false
req.putHint("elevation", false);
// Optionally enable/disable turn instruction information, defaults is true
req.putHint("instructions", true);
// Optionally enable/disable path geometry information, default is true
req.putHint("calc_points", true);
// note: turn off instructions and calcPoints if you just need the distance or time
// information to make calculation and transmission faster
// Optionally set specific locale for instruction information, supports already over 25 languages,
// defaults to English
req.setLocale(Locale.GERMAN);
// Optionally add path details
req.setPathDetails(Arrays.asList(Parameters.Details.STREET_NAME, Parameters.Details.AVERAGE_SPEED, Parameters.Details.EDGE_ID));
GHResponse fullRes = gh.route(req);
if (fullRes.hasErrors())
throw new RuntimeException(fullRes.getErrors().toString());
// get best path (you will get more than one path here if you requested algorithm=alternative_route)
ResponsePath res = fullRes.getBest();
// get path geometry information (latitude, longitude and optionally elevation)
PointList pl = res.getPoints();
// distance of the full path, in meter
double distance = res.getDistance();
// time of the full path, in milliseconds
long millis = res.getTime();
// get information per turn instruction
InstructionList il = res.getInstructions();
for (Instruction i : il) {
// System.out.println(i.getName());
}
// get path details
List<PathDetail> pathDetails = res.getPathDetails().get(Parameters.Details.STREET_NAME);
for (PathDetail detail : pathDetails) {
// System.out.println(detail.getValue());
}
}
Aggregations