Search in sources :

Example 46 with GHResponse

use of com.graphhopper.GHResponse in project graphhopper by graphhopper.

the class RouteResource method doGet.

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "application/gpx+xml" })
public Response doGet(@Context HttpServletRequest httpReq, @Context UriInfo uriInfo, @QueryParam(WAY_POINT_MAX_DISTANCE) @DefaultValue("1") double minPathPrecision, @QueryParam(ELEVATION_WAY_POINT_MAX_DISTANCE) Double minPathElevationPrecision, @QueryParam("point") @NotNull List<GHPointParam> pointParams, @QueryParam("type") @DefaultValue("json") String type, @QueryParam(INSTRUCTIONS) @DefaultValue("true") boolean instructions, @QueryParam(CALC_POINTS) @DefaultValue("true") boolean calcPoints, @QueryParam("elevation") @DefaultValue("false") boolean enableElevation, @QueryParam("points_encoded") @DefaultValue("true") boolean pointsEncoded, @QueryParam("profile") String profileName, @QueryParam(ALGORITHM) @DefaultValue("") String algoStr, @QueryParam("locale") @DefaultValue("en") String localeStr, @QueryParam(POINT_HINT) List<String> pointHints, @QueryParam(CURBSIDE) List<String> curbsides, @QueryParam(SNAP_PREVENTION) List<String> snapPreventions, @QueryParam(PATH_DETAILS) List<String> pathDetails, @QueryParam("heading") @NotNull List<Double> headings, @QueryParam("gpx.route") @DefaultValue("true") boolean withRoute, /* default to false for the route part in next API version, see #437 */
@QueryParam("gpx.track") @DefaultValue("true") boolean withTrack, @QueryParam("gpx.waypoints") @DefaultValue("false") boolean withWayPoints, @QueryParam("gpx.trackname") @DefaultValue("GraphHopper Track") String trackName, @QueryParam("gpx.millis") String timeString) {
    List<GHPoint> points = pointParams.stream().map(AbstractParam::get).collect(toList());
    boolean writeGPX = "gpx".equalsIgnoreCase(type);
    instructions = writeGPX || instructions;
    if (enableElevation && !hasElevation)
        throw new IllegalArgumentException("Elevation not supported!");
    StopWatch sw = new StopWatch().start();
    GHRequest request = new GHRequest();
    initHints(request.getHints(), uriInfo.getQueryParameters());
    String weightingVehicleLogStr = "weighting: " + request.getHints().getString("weighting", "") + ", vehicle: " + request.getHints().getString("vehicle", "");
    if (Helper.isEmpty(profileName)) {
        enableEdgeBasedIfThereAreCurbsides(curbsides, request);
        profileName = profileResolver.resolveProfile(request.getHints()).getName();
        removeLegacyParameters(request.getHints());
    }
    errorIfLegacyParameters(request.getHints());
    request.setPoints(points).setProfile(profileName).setAlgorithm(algoStr).setLocale(localeStr).setHeadings(headings).setPointHints(pointHints).setCurbsides(curbsides).setSnapPreventions(snapPreventions).setPathDetails(pathDetails).getHints().putObject(CALC_POINTS, calcPoints).putObject(INSTRUCTIONS, instructions).putObject(WAY_POINT_MAX_DISTANCE, minPathPrecision);
    if (minPathElevationPrecision != null) {
        request.getHints().putObject(ELEVATION_WAY_POINT_MAX_DISTANCE, minPathElevationPrecision);
    }
    GHResponse ghResponse = graphHopper.route(request);
    long took = sw.stop().getNanos() / 1_000_000;
    String infoStr = httpReq.getRemoteAddr() + " " + httpReq.getLocale() + " " + httpReq.getHeader("User-Agent");
    String logStr = httpReq.getQueryString() + " " + infoStr + " " + points + ", took: " + String.format("%.1f", (double) took) + "ms, algo: " + algoStr + ", profile: " + profileName + ", " + weightingVehicleLogStr;
    if (ghResponse.hasErrors()) {
        logger.error(logStr + ", errors:" + ghResponse.getErrors());
        throw new MultiException(ghResponse.getErrors());
    } else {
        logger.info(logStr + ", alternatives: " + ghResponse.getAll().size() + ", distance0: " + ghResponse.getBest().getDistance() + ", weight0: " + ghResponse.getBest().getRouteWeight() + ", time0: " + Math.round(ghResponse.getBest().getTime() / 60000f) + "min" + ", points0: " + ghResponse.getBest().getPoints().size() + ", debugInfo: " + ghResponse.getDebugInfo());
        return writeGPX ? gpxSuccessResponseBuilder(ghResponse, timeString, trackName, enableElevation, withRoute, withTrack, withWayPoints, Constants.VERSION).header("X-GH-Took", "" + Math.round(took)).build() : Response.ok(ResponsePathSerializer.jsonObject(ghResponse, instructions, calcPoints, enableElevation, pointsEncoded, took)).header("X-GH-Took", "" + Math.round(took)).type(MediaType.APPLICATION_JSON).build();
    }
}
Also used : GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) MultiException(com.graphhopper.jackson.MultiException)

Example 47 with GHResponse

use of com.graphhopper.GHResponse in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method testCannotFindPointException.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void testCannotFindPointException(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.49058, 1.602974)).addPoint(new GHPoint(42.510383, 1.533392)).putHint("vehicle", "car");
    GHResponse res = gh.route(req);
    assertTrue(res.hasErrors(), "no errors found?");
    assertTrue(res.getErrors().get(0) instanceof PointNotFoundException);
}
Also used : PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 48 with GHResponse

use of com.graphhopper.GHResponse in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method testNoPoints.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void testNoPoints(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.509225, 1.534728)).addPoint(new GHPoint(42.512602, 1.551558)).putHint("vehicle", "car");
    req.putHint("instructions", false);
    req.putHint("calc_points", false);
    GHResponse rsp = gh.route(req);
    assertFalse(rsp.hasErrors(), "errors:" + rsp.getErrors().toString());
    ResponsePath res = rsp.getBest();
    assertEquals(0, res.getPoints().size());
    isBetween(1750, 1800, res.getDistance());
}
Also used : ResponsePath(com.graphhopper.ResponsePath) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 49 with GHResponse

use of com.graphhopper.GHResponse in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method readRoundabout.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void readRoundabout(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.509644, 1.532958)).addPoint(new GHPoint(42.510383, 1.533392)).putHint("vehicle", "car");
    GHResponse res = gh.route(req);
    int counter = 0;
    for (Instruction i : res.getBest().getInstructions()) {
        if (i instanceof RoundaboutInstruction) {
            counter++;
            RoundaboutInstruction ri = (RoundaboutInstruction) i;
            assertEquals(-5, ri.getTurnAngle(), 0.1, "turn_angle was incorrect:" + ri.getTurnAngle());
            // This route contains only one roundabout and no (via) point in a roundabout
            assertTrue(ri.isExited(), "exited was incorrect:" + ri.isExited());
        }
    }
    assertTrue(counter > 0, "no roundabout in route?");
}
Also used : GHRequest(com.graphhopper.GHRequest) RoundaboutInstruction(com.graphhopper.util.RoundaboutInstruction) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) Instruction(com.graphhopper.util.Instruction) RoundaboutInstruction(com.graphhopper.util.RoundaboutInstruction) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 50 with GHResponse

use of com.graphhopper.GHResponse in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method testAlternativeRoute.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void testAlternativeRoute(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.505041, 1.521864)).addPoint(new GHPoint(42.509074, 1.537936)).putHint("vehicle", "car").setAlgorithm("alternative_route").putHint("instructions", true).putHint("calc_points", true).putHint("ch.disable", true);
    GHResponse res = gh.route(req);
    assertFalse(res.hasErrors(), "errors:" + res.getErrors().toString());
    List<ResponsePath> paths = res.getAll();
    assertEquals(2, paths.size());
    ResponsePath path = paths.get(0);
    assertEquals(35, path.getPoints().size());
    assertEquals(1689, path.getDistance(), 1);
    assertTrue(path.getInstructions().toString().contains("Avinguda de Tarragona"), path.getInstructions().toString());
    path = paths.get(1);
    assertEquals(30, path.getPoints().size());
    assertEquals(1759, path.getDistance(), 1);
    assertTrue(path.getInstructions().toString().contains("Avinguda Prat de la Creu"), path.getInstructions().toString());
}
Also used : ResponsePath(com.graphhopper.ResponsePath) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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