Search in sources :

Example 81 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class NearestServlet method doGet.

@Override
public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
    String pointStr = getParam(httpReq, "point", null);
    boolean enabledElevation = getBooleanParam(httpReq, "elevation", false);
    ObjectNode result = objectMapper.createObjectNode();
    if (pointStr != null && !pointStr.equalsIgnoreCase("")) {
        GHPoint place = GHPoint.parse(pointStr);
        QueryResult qr = index.findClosest(place.lat, place.lon, EdgeFilter.ALL_EDGES);
        if (!qr.isValid()) {
            result.put("error", "Nearest point cannot be found!");
        } else {
            GHPoint3D snappedPoint = qr.getSnappedPoint();
            result.put("type", "Point");
            ArrayNode coord = result.putArray("coordinates");
            coord.add(snappedPoint.lon);
            coord.add(snappedPoint.lat);
            if (hasElevation && enabledElevation)
                coord.add(snappedPoint.ele);
            // Distance from input to snapped point in meters
            result.put("distance", calc.calcDist(place.lat, place.lon, snappedPoint.lat, snappedPoint.lon));
        }
    } else {
        result.put("error", "No lat/lon specified!");
    }
    writeJson(httpReq, httpRes, result);
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GHPoint3D(com.graphhopper.util.shapes.GHPoint3D) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 82 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class GraphHopperWeb method route.

@Override
public GHResponse route(GHRequest request) {
    try {
        String places = "";
        for (GHPoint p : request.getPoints()) {
            places += "point=" + p.lat + "," + p.lon + "&";
        }
        boolean tmpInstructions = request.getHints().getBool("instructions", instructions);
        boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints);
        boolean tmpTurnDescription = request.getHints().getBool("turn_description", turnDescription);
        if (tmpInstructions && !tmpCalcPoints)
            throw new IllegalStateException("Cannot calculate instructions without points (only points without instructions). " + "Use calc_points=false and instructions=false to disable point and instruction calculation");
        boolean tmpElevation = request.getHints().getBool("elevation", elevation);
        String url = routeServiceUrl + "?" + places + "&type=json" + "&instructions=" + tmpInstructions + "&points_encoded=true" + "&calc_points=" + tmpCalcPoints + "&algorithm=" + request.getAlgorithm() + "&locale=" + request.getLocale().toString() + "&elevation=" + tmpElevation;
        if (!request.getVehicle().isEmpty())
            url += "&vehicle=" + request.getVehicle();
        if (!key.isEmpty())
            url += "&key=" + key;
        for (Entry<String, String> entry : request.getHints().toMap().entrySet()) {
            String urlKey = entry.getKey();
            String urlValue = entry.getValue();
            // use lower case conversion for check only!
            if (ignoreSet.contains(urlKey.toLowerCase()))
                continue;
            if (urlValue != null && !urlValue.isEmpty())
                url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue);
        }
        String str = downloader.downloadAsString(url, true);
        JSONObject json = new JSONObject(str);
        GHResponse res = new GHResponse();
        res.addErrors(readErrors(json));
        if (res.hasErrors())
            return res;
        JSONArray paths = json.getJSONArray("paths");
        for (int index = 0; index < paths.length(); index++) {
            JSONObject path = paths.getJSONObject(index);
            PathWrapper altRsp = createPathWrapper(path, tmpCalcPoints, tmpInstructions, tmpElevation, tmpTurnDescription);
            res.add(altRsp);
        }
        return res;
    } catch (Exception ex) {
        throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex);
    }
}
Also used : DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException) JSONObject(org.json.JSONObject) PathWrapper(com.graphhopper.PathWrapper) JSONArray(org.json.JSONArray) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) DetailedRuntimeException(com.graphhopper.util.exceptions.DetailedRuntimeException) PointOutOfBoundsException(com.graphhopper.util.exceptions.PointOutOfBoundsException) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) DetailedIllegalArgumentException(com.graphhopper.util.exceptions.DetailedIllegalArgumentException) JSONException(org.json.JSONException) ConnectionNotFoundException(com.graphhopper.util.exceptions.ConnectionNotFoundException)

Example 83 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class GraphHopperIT method testMonacoNonChMaxWaypointDistance.

@Test
public void testMonacoNonChMaxWaypointDistance() {
    GHPoint from = new GHPoint(43.741069, 7.426854);
    GHPoint to = new GHPoint(43.727697, 7.419199);
    GHRequest req = new GHRequest().addPoint(from).addPoint(to).setVehicle(vehicle).setWeighting("fastest");
    // Fail since points are too far
    hopper.setNonChMaxWaypointDistance(1000);
    GHResponse rsp = hopper.route(req);
    assertTrue(rsp.hasErrors());
    // Suceed since points are not far anymore
    hopper.setNonChMaxWaypointDistance(Integer.MAX_VALUE);
    rsp = hopper.route(req);
    assertFalse(rsp.hasErrors());
}
Also used : GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 84 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class GraphHopperIT method checkMultiVehiclesWithCH.

private void checkMultiVehiclesWithCH(GraphHopper tmpHopper) {
    String str = tmpHopper.getEncodingManager().toString();
    GHResponse rsp = tmpHopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826).setVehicle("car"));
    PathWrapper arsp = rsp.getBest();
    assertFalse("car routing for " + str + " should not have errors:" + rsp.getErrors(), rsp.hasErrors());
    assertEquals(207, arsp.getTime() / 1000f, 1);
    assertEquals(2838, arsp.getDistance(), 1);
    rsp = tmpHopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826).setVehicle("bike"));
    arsp = rsp.getBest();
    assertFalse("bike routing for " + str + " should not have errors:" + rsp.getErrors(), rsp.hasErrors());
    assertEquals(494, arsp.getTime() / 1000f, 1);
    assertEquals(2192, arsp.getDistance(), 1);
    rsp = tmpHopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826).setVehicle("foot"));
    assertTrue("only bike and car were imported. foot request should fail", rsp.hasErrors());
    GHRequest req = new GHRequest().addPoint(new GHPoint(43.741069, 7.426854), 0.).addPoint(new GHPoint(43.744445, 7.429483), 190.).setVehicle("bike").setWeighting("fastest");
    rsp = hopper.route(req);
    assertTrue("heading not allowed for CH enabled graph", rsp.hasErrors());
}
Also used : GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 85 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class GraphHopperIT method testMonacoNonChMaxWaypointDistanceMultiplePoints.

@Test
public void testMonacoNonChMaxWaypointDistanceMultiplePoints() {
    GHPoint from = new GHPoint(43.741069, 7.426854);
    GHPoint via = new GHPoint(43.744445, 7.429483);
    GHPoint to = new GHPoint(43.727697, 7.419199);
    GHRequest req = new GHRequest().addPoint(from).addPoint(via).addPoint(to).setVehicle(vehicle).setWeighting("fastest");
    // Fail since points are too far
    hopper.setNonChMaxWaypointDistance(1000);
    GHResponse rsp = hopper.route(req);
    assertTrue(rsp.hasErrors());
    PointDistanceExceededException exception = (PointDistanceExceededException) rsp.getErrors().get(0);
    assertEquals(2, exception.getDetails().get("to"));
    // Suceed since points are not far anymore
    hopper.setNonChMaxWaypointDistance(Integer.MAX_VALUE);
    rsp = hopper.route(req);
    assertFalse(rsp.hasErrors());
}
Also used : PointDistanceExceededException(com.graphhopper.util.exceptions.PointDistanceExceededException) GHPoint(com.graphhopper.util.shapes.GHPoint)

Aggregations

GHPoint (com.graphhopper.util.shapes.GHPoint)100 Test (org.junit.Test)52 GHRequest (com.graphhopper.GHRequest)25 GHResponse (com.graphhopper.GHResponse)23 QueryResult (com.graphhopper.storage.index.QueryResult)16 PathWrapper (com.graphhopper.PathWrapper)10 DistanceCalcEarth (com.graphhopper.util.DistanceCalcEarth)5 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)4 EncodingManager (com.graphhopper.routing.util.EncodingManager)4 PointList (com.graphhopper.util.PointList)4 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)4 BBox (com.graphhopper.util.shapes.BBox)4 ArrayList (java.util.ArrayList)4 GHPoint3D (com.graphhopper.util.shapes.GHPoint3D)3 IntArrayList (com.carrotsearch.hppc.IntArrayList)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ReaderWay (com.graphhopper.reader.ReaderWay)2 EdgeFilter (com.graphhopper.routing.util.EdgeFilter)2 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)2 Graph (com.graphhopper.storage.Graph)2