use of com.graphhopper.GHResponse in project graphhopper by graphhopper.
the class GraphHopperServletIT method testPathDetails.
@Test
public void testPathDetails() throws Exception {
GraphHopperAPI hopper = new com.graphhopper.api.GraphHopperWeb();
assertTrue(hopper.load(getTestRouteAPIUrl()));
GHRequest request = new GHRequest(42.554851, 1.536198, 42.510071, 1.548128);
request.setPathDetails(Arrays.asList("average_speed", "edge_id", "time"));
GHResponse rsp = hopper.route(request);
assertFalse(rsp.getErrors().toString(), rsp.hasErrors());
assertTrue(rsp.getErrors().toString(), rsp.getErrors().isEmpty());
Map<String, List<PathDetail>> pathDetails = rsp.getBest().getPathDetails();
assertFalse(pathDetails.isEmpty());
assertTrue(pathDetails.containsKey("average_speed"));
assertTrue(pathDetails.containsKey("edge_id"));
assertTrue(pathDetails.containsKey("time"));
List<PathDetail> averageSpeedList = pathDetails.get("average_speed");
assertEquals(9, averageSpeedList.size());
assertEquals(30.0, averageSpeedList.get(0).getValue());
assertEquals(14, averageSpeedList.get(0).getLength());
assertEquals(60.0, averageSpeedList.get(1).getValue());
assertEquals(5, averageSpeedList.get(1).getLength());
List<PathDetail> edgeIdDetails = pathDetails.get("edge_id");
assertEquals(77, edgeIdDetails.size());
assertEquals(3759L, edgeIdDetails.get(0).getValue());
assertEquals(2, edgeIdDetails.get(0).getLength());
assertEquals(881L, edgeIdDetails.get(1).getValue());
assertEquals(8, edgeIdDetails.get(1).getLength());
long expectedTime = rsp.getBest().getTime();
long actualTime = 0;
List<PathDetail> timeDetails = pathDetails.get("time");
for (PathDetail pd : timeDetails) {
actualTime += (Long) pd.getValue();
}
assertEquals(expectedTime, actualTime);
}
use of com.graphhopper.GHResponse in project massim by agentcontest.
the class CityMap method existsRoute.
/**
* Checks if a route exists without creating the actual route object.
* @param from starting location
* @param to destination
* @return true if such a route exists
*/
private boolean existsRoute(Location from, Location to) {
GHResponse rsp = queryGH(from, to);
rsp.getErrors().forEach(error -> Log.log(Log.Level.ERROR, "GH: " + error.getMessage()));
return !rsp.hasErrors() && rsp.getBest().getPoints().size() > 0;
}
use of com.graphhopper.GHResponse in project graphhopper by graphhopper.
the class GraphHopperWeb method route.
public GHResponse route(GHRequest ghRequest) {
ResponseBody rspBody = null;
try {
boolean tmpElevation = ghRequest.getHints().getBool("elevation", elevation);
boolean tmpTurnDescription = ghRequest.getHints().getBool("turn_description", true);
// do not include in request
ghRequest.getHints().remove("turn_description");
Request okRequest = postRequest ? createPostRequest(ghRequest) : createGetRequest(ghRequest);
rspBody = getClientForRequest(ghRequest).newCall(okRequest).execute().body();
JsonNode json = objectMapper.reader().readTree(rspBody.byteStream());
GHResponse res = new GHResponse();
res.addErrors(ResponsePathDeserializer.readErrors(objectMapper, json));
if (res.hasErrors())
return res;
JsonNode paths = json.get("paths");
for (JsonNode path : paths) {
ResponsePath altRsp = ResponsePathDeserializer.createResponsePath(objectMapper, path, tmpElevation, tmpTurnDescription);
res.add(altRsp);
}
return res;
} catch (Exception ex) {
throw new RuntimeException("Problem while fetching path " + ghRequest.getPoints() + ": " + ex.getMessage(), ex);
} finally {
Helper.close(rspBody);
}
}
use of com.graphhopper.GHResponse in project graphhopper by graphhopper.
the class PtRouteResource method route.
@GET
@Produces(MediaType.APPLICATION_JSON)
public ObjectNode route(@QueryParam("point") @Size(min = 2, max = 2) List<GHLocationParam> requestPoints, @QueryParam("pt.earliest_departure_time") @NotNull OffsetDateTimeParam departureTimeParam, @QueryParam("pt.profile_duration") DurationParam profileDuration, @QueryParam("pt.arrive_by") @DefaultValue("false") boolean arriveBy, @QueryParam("locale") String localeStr, @QueryParam("pt.ignore_transfers") Boolean ignoreTransfers, @QueryParam("pt.profile") Boolean profileQuery, @QueryParam("pt.limit_solutions") Integer limitSolutions, @QueryParam("pt.limit_trip_time") DurationParam limitTripTime, @QueryParam("pt.limit_street_time") DurationParam limitStreetTime, @QueryParam("pt.access_profile") String accessProfile, @QueryParam("pt.egress_profile") String egressProfile) {
StopWatch stopWatch = new StopWatch().start();
List<GHLocation> points = requestPoints.stream().map(AbstractParam::get).collect(toList());
Instant departureTime = departureTimeParam.get().toInstant();
Request request = new Request(points, departureTime);
request.setArriveBy(arriveBy);
Optional.ofNullable(profileQuery).ifPresent(request::setProfileQuery);
Optional.ofNullable(profileDuration.get()).ifPresent(request::setMaxProfileDuration);
Optional.ofNullable(ignoreTransfers).ifPresent(request::setIgnoreTransfers);
Optional.ofNullable(localeStr).ifPresent(s -> request.setLocale(Helper.getLocale(s)));
Optional.ofNullable(limitSolutions).ifPresent(request::setLimitSolutions);
Optional.ofNullable(limitTripTime.get()).ifPresent(request::setLimitTripTime);
Optional.ofNullable(limitStreetTime.get()).ifPresent(request::setLimitStreetTime);
Optional.ofNullable(accessProfile).ifPresent(request::setAccessProfile);
Optional.ofNullable(egressProfile).ifPresent(request::setEgressProfile);
GHResponse route = ptRouter.route(request);
return ResponsePathSerializer.jsonObject(route, true, true, false, false, stopWatch.stop().getMillis());
}
use of com.graphhopper.GHResponse in project graphhopper by graphhopper.
the class HeadingRoutingTest method headingTest1.
@Test
public void headingTest1() {
// Test enforce start direction
GraphHopperStorage graph = createSquareGraph();
Router router = createRouter(graph);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
GHRequest req = new GHRequest().setPoints(Arrays.asList(start, end)).setHeadings(Arrays.asList(180., Double.NaN)).setProfile("profile").setPathDetails(Collections.singletonList("edge_key"));
GHResponse response = router.route(req);
assertFalse(response.hasErrors(), response.getErrors().toString());
assertArrayEquals(new int[] { 4, 5, 8, 3, 2 }, calcNodes(graph, response.getAll().get(0)));
}
Aggregations