Search in sources :

Example 1 with MultiException

use of com.graphhopper.jackson.MultiException 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 2 with MultiException

use of com.graphhopper.jackson.MultiException in project graphhopper by graphhopper.

the class RouteResource method doPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response doPost(@NotNull GHRequest request, @Context HttpServletRequest httpReq) {
    StopWatch sw = new StopWatch().start();
    if (request.getCustomModel() == null) {
        if (Helper.isEmpty(request.getProfile())) {
            // legacy parameter resolution (only used when there is no custom model)
            enableEdgeBasedIfThereAreCurbsides(request.getCurbsides(), request);
            request.setProfile(profileResolver.resolveProfile(request.getHints()).getName());
            removeLegacyParameters(request.getHints());
        }
    } else {
        if (Helper.isEmpty(request.getProfile()))
            // throw a dedicated exception here, otherwise a missing profile is still caught in Router
            throw new IllegalArgumentException("The 'profile' parameter is required when you use the `custom_model` parameter");
    }
    errorIfLegacyParameters(request.getHints());
    GHResponse ghResponse = graphHopper.route(request);
    boolean instructions = request.getHints().getBool(INSTRUCTIONS, true);
    boolean enableElevation = request.getHints().getBool("elevation", false);
    boolean calcPoints = request.getHints().getBool(CALC_POINTS, true);
    boolean pointsEncoded = request.getHints().getBool("points_encoded", true);
    long took = sw.stop().getNanos() / 1_000_000;
    String infoStr = httpReq.getRemoteAddr() + " " + httpReq.getLocale() + " " + httpReq.getHeader("User-Agent");
    String queryString = httpReq.getQueryString() == null ? "" : (httpReq.getQueryString() + " ");
    // todo: vehicle/weighting will always be empty at this point...
    String weightingVehicleLogStr = "weighting: " + request.getHints().getString("weighting", "") + ", vehicle: " + request.getHints().getString("vehicle", "");
    String logStr = queryString + infoStr + " " + request.getPoints().size() + ", took: " + String.format("%.1f", (double) took) + " ms, algo: " + request.getAlgorithm() + ", profile: " + request.getProfile() + ", " + weightingVehicleLogStr + ", custom_model: " + request.getCustomModel();
    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 Response.ok(ResponsePathSerializer.jsonObject(ghResponse, instructions, calcPoints, enableElevation, pointsEncoded, took)).header("X-GH-Took", "" + Math.round(took)).type(MediaType.APPLICATION_JSON).build();
    }
}
Also used : GHResponse(com.graphhopper.GHResponse) MultiException(com.graphhopper.jackson.MultiException)

Aggregations

GHResponse (com.graphhopper.GHResponse)2 MultiException (com.graphhopper.jackson.MultiException)2 GHRequest (com.graphhopper.GHRequest)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1