Search in sources :

Example 21 with GHResponse

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);
}
Also used : GraphHopperAPI(com.graphhopper.GraphHopperAPI) PathDetail(com.graphhopper.util.details.PathDetail) GHRequest(com.graphhopper.GHRequest) List(java.util.List) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 22 with GHResponse

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;
}
Also used : GHResponse(com.graphhopper.GHResponse)

Example 23 with GHResponse

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);
    }
}
Also used : ResponsePath(com.graphhopper.ResponsePath) Request(okhttp3.Request) GHRequest(com.graphhopper.GHRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) GHResponse(com.graphhopper.GHResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResponseBody(okhttp3.ResponseBody)

Example 24 with GHResponse

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());
}
Also used : GHLocation(com.graphhopper.gtfs.GHLocation) Instant(java.time.Instant) Request(com.graphhopper.gtfs.Request) GHResponse(com.graphhopper.GHResponse) StopWatch(com.graphhopper.util.StopWatch)

Example 25 with GHResponse

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)));
}
Also used : GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) Test(org.junit.jupiter.api.Test)

Aggregations

GHResponse (com.graphhopper.GHResponse)100 GHRequest (com.graphhopper.GHRequest)86 GHPoint (com.graphhopper.util.shapes.GHPoint)52 Test (org.junit.Test)31 Test (org.junit.jupiter.api.Test)31 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)20 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 ResponsePath (com.graphhopper.ResponsePath)15 EnumSource (org.junit.jupiter.params.provider.EnumSource)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 PathWrapper (com.graphhopper.PathWrapper)9 GraphHopper (com.graphhopper.GraphHopper)7 InstructionList (com.graphhopper.util.InstructionList)7 PathDetail (com.graphhopper.util.details.PathDetail)7 GraphHopperAPI (com.graphhopper.GraphHopperAPI)6 Profile (com.graphhopper.config.Profile)5 Graph (com.graphhopper.storage.Graph)5 NodeAccess (com.graphhopper.storage.NodeAccess)5 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)5