Search in sources :

Example 36 with GHPoint

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

the class GraphEdgeIdFinder method findEdgesInShape.

/**
 * This method fills the edgeIds hash with edgeIds found inside the specified shape
 */
public void findEdgesInShape(final GHIntHashSet edgeIds, final Shape shape, EdgeFilter filter) {
    GHPoint center = shape.getCenter();
    QueryResult qr = locationIndex.findClosest(center.getLat(), center.getLon(), filter);
    // TODO: if there is no street close to the center it'll fail although there are roads covered. Maybe we should check edge points or some random points in the Shape instead?
    if (!qr.isValid())
        throw new IllegalArgumentException("Shape " + shape + " does not cover graph");
    if (shape.contains(qr.getSnappedPoint().lat, qr.getSnappedPoint().lon))
        edgeIds.add(qr.getClosestEdge().getEdge());
    BreadthFirstSearch bfs = new BreadthFirstSearch() {

        final NodeAccess na = graph.getNodeAccess();

        final Shape localShape = shape;

        @Override
        protected boolean goFurther(int nodeId) {
            return localShape.contains(na.getLatitude(nodeId), na.getLongitude(nodeId));
        }

        @Override
        protected boolean checkAdjacent(EdgeIteratorState edge) {
            if (localShape.contains(na.getLatitude(edge.getAdjNode()), na.getLongitude(edge.getAdjNode()))) {
                edgeIds.add(edge.getEdge());
                return true;
            }
            return false;
        }
    };
    bfs.start(graph.createEdgeExplorer(filter), qr.getClosestNode());
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) Shape(com.graphhopper.util.shapes.Shape) BreadthFirstSearch(com.graphhopper.util.BreadthFirstSearch) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 37 with GHPoint

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

the class GraphHopperServletIT method testGraphHopperWeb.

@Test
public void testGraphHopperWeb() throws Exception {
    GraphHopperAPI hopper = new com.graphhopper.api.GraphHopperWeb();
    assertTrue(hopper.load(getTestRouteAPIUrl()));
    GHResponse rsp = hopper.route(new GHRequest(42.554851, 1.536198, 42.510071, 1.548128));
    assertFalse(rsp.getErrors().toString(), rsp.hasErrors());
    assertTrue(rsp.getErrors().toString(), rsp.getErrors().isEmpty());
    PathWrapper arsp = rsp.getBest();
    assertTrue("distance wasn't correct:" + arsp.getDistance(), arsp.getDistance() > 9000);
    assertTrue("distance wasn't correct:" + arsp.getDistance(), arsp.getDistance() < 9500);
    rsp = hopper.route(new GHRequest().addPoint(new GHPoint(42.554851, 1.536198)).addPoint(new GHPoint(42.531896, 1.553278)).addPoint(new GHPoint(42.510071, 1.548128)));
    assertTrue(rsp.getErrors().toString(), rsp.getErrors().isEmpty());
    arsp = rsp.getBest();
    assertTrue("distance wasn't correct:" + arsp.getDistance(), arsp.getDistance() > 20000);
    assertTrue("distance wasn't correct:" + arsp.getDistance(), arsp.getDistance() < 21000);
    List<Map<String, Object>> instructions = arsp.getInstructions().createJson();
    assertEquals(26, instructions.size());
    assertEquals("Continue onto la Callisa", instructions.get(0).get("text"));
    assertEquals("At roundabout, take exit 2", instructions.get(4).get("text"));
    assertEquals(true, instructions.get(4).get("exited"));
    assertEquals(false, instructions.get(24).get("exited"));
}
Also used : PathWrapper(com.graphhopper.PathWrapper) GraphHopperAPI(com.graphhopper.GraphHopperAPI) GHRequest(com.graphhopper.GHRequest) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) Map(java.util.Map) Test(org.junit.Test)

Example 38 with GHPoint

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

the class GraphHopperWeb method createPointList.

private ArrayNode createPointList(List<GHPoint> list) {
    ArrayNode outList = objectMapper.createArrayNode();
    for (GHPoint p : list) {
        ArrayNode entry = objectMapper.createArrayNode();
        entry.add(p.lon);
        entry.add(p.lat);
        outList.add(entry);
    }
    return outList;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 39 with GHPoint

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

the class GraphHopperWeb method createGetRequest.

Request createGetRequest(GHRequest ghRequest) {
    boolean tmpInstructions = ghRequest.getHints().getBool(INSTRUCTIONS, instructions);
    boolean tmpCalcPoints = ghRequest.getHints().getBool(CALC_POINTS, calcPoints);
    String tmpOptimize = ghRequest.getHints().getString("optimize", optimize);
    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 = ghRequest.getHints().getBool("elevation", elevation);
    String places = "";
    for (GHPoint p : ghRequest.getPoints()) {
        places += "point=" + round6(p.lat) + "," + round6(p.lon) + "&";
    }
    String type = ghRequest.getHints().getString("type", "json");
    String url = routeServiceUrl + "?" + places + "&profile=" + ghRequest.getProfile() + "&type=" + type + "&instructions=" + tmpInstructions + "&points_encoded=true" + "&calc_points=" + tmpCalcPoints + "&algorithm=" + ghRequest.getAlgorithm() + "&locale=" + ghRequest.getLocale().toString() + "&elevation=" + tmpElevation + "&optimize=" + tmpOptimize;
    for (String details : ghRequest.getPathDetails()) {
        url += "&" + Parameters.Details.PATH_DETAILS + "=" + details;
    }
    // append *all* point hints only if at least *one* is not empty
    for (String checkEmptyHint : ghRequest.getPointHints()) {
        if (!checkEmptyHint.isEmpty()) {
            for (String hint : ghRequest.getPointHints()) {
                url += "&" + Parameters.Routing.POINT_HINT + "=" + encodeURL(hint);
            }
            break;
        }
    }
    // append *all* curbsides only if at least *one* is not empty
    for (String checkEitherSide : ghRequest.getCurbsides()) {
        if (!checkEitherSide.isEmpty()) {
            for (String curbside : ghRequest.getCurbsides()) {
                url += "&" + Parameters.Routing.CURBSIDE + "=" + encodeURL(curbside);
            }
            break;
        }
    }
    for (String snapPrevention : ghRequest.getSnapPreventions()) {
        url += "&" + Parameters.Routing.SNAP_PREVENTION + "=" + encodeURL(snapPrevention);
    }
    if (!key.isEmpty()) {
        url += "&key=" + encodeURL(key);
    }
    for (Map.Entry<String, Object> entry : ghRequest.getHints().toMap().entrySet()) {
        String urlKey = entry.getKey();
        String urlValue = entry.getValue().toString();
        // use lower case conversion for check only!
        if (ignoreSet.contains(toLowerCase(urlKey))) {
            continue;
        }
        if (urlValue != null && !urlValue.isEmpty()) {
            url += "&" + encodeURL(urlKey) + "=" + encodeURL(urlValue);
        }
    }
    return new Request.Builder().url(url).build();
}
Also used : GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 40 with GHPoint

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

the class Examples method matrix.

@Test
public void matrix() {
    // Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
    GraphHopperMatrixWeb matrixClient = new GraphHopperMatrixWeb();
    // for very large matrices you need:
    // GraphHopperMatrixWeb matrixClient = new GraphHopperMatrixWeb(new GHMatrixBatchRequester());
    matrixClient.setKey(apiKey);
    GHMRequest ghmRequest = new GHMRequest();
    ghmRequest.addOutArray("distances");
    ghmRequest.addOutArray("times");
    ghmRequest.putHint("vehicle", "car");
    // init points for a symmetric matrix
    // List<GHPoint> allPoints = Arrays.asList(new GHPoint(49.6724, 11.3494), new GHPoint(49.6550, 11.4180));
    // ghmRequest.addAllPoints(allPoints);
    // or init e.g. a one-to-many matrix:
    ghmRequest.addFromPoint(new GHPoint(49.6724, 11.3494));
    for (GHPoint to : Arrays.asList(new GHPoint(49.6724, 11.3494), new GHPoint(49.6550, 11.4180))) {
        ghmRequest.addToPoint(to);
    }
    MatrixResponse response = matrixClient.route(ghmRequest);
    if (response.hasErrors())
        throw new RuntimeException(response.getErrors().toString());
// get time from first to second point:
// System.out.println(response.getTime(0, 1));
}
Also used : GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test)

Aggregations

GHPoint (com.graphhopper.util.shapes.GHPoint)206 Test (org.junit.jupiter.api.Test)62 GHRequest (com.graphhopper.GHRequest)57 GHResponse (com.graphhopper.GHResponse)50 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)50 Test (org.junit.Test)45 LMProfile (com.graphhopper.config.LMProfile)27 Profile (com.graphhopper.config.Profile)27 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)27 CHProfile (com.graphhopper.config.CHProfile)26 Snap (com.graphhopper.storage.index.Snap)17 QueryResult (com.graphhopper.storage.index.QueryResult)15 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)13 ResponsePath (com.graphhopper.ResponsePath)11 ArrayList (java.util.ArrayList)11 EnumSource (org.junit.jupiter.params.provider.EnumSource)11 PathWrapper (com.graphhopper.PathWrapper)8 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)8 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)8 IntArrayList (com.carrotsearch.hppc.IntArrayList)6