Search in sources :

Example 1 with Circle

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

the class GraphEdgeIdFinder method parseStringHints.

/**
     * This method reads string values from the hints about blocked areas and fills the configMap with either the
     * created shapes or the found edges if area is small enough.
     */
public ConfigMap parseStringHints(ConfigMap configMap, HintsMap hints, EdgeFilter filter) {
    final String objectSeparator = ";";
    final String innerObjSep = ",";
    // use shapes if bigger than 1km^2
    final double shapeArea = 1000 * 1000;
    final GHIntHashSet blockedEdges = new GHIntHashSet();
    final List<Shape> blockedShapes = new ArrayList<>();
    // Add blocked circular areas or points
    String blockedCircularAreasStr = hints.get(BLOCK_AREA, "");
    if (!blockedCircularAreasStr.isEmpty()) {
        String[] blockedCircularAreasArr = blockedCircularAreasStr.split(objectSeparator);
        for (int i = 0; i < blockedCircularAreasArr.length; i++) {
            String objectAsString = blockedCircularAreasArr[i];
            String[] splittedObject = objectAsString.split(innerObjSep);
            if (splittedObject.length == 4) {
                final BBox bbox = BBox.parseTwoPoints(objectAsString);
                if (bbox.calculateArea() > shapeArea)
                    blockedShapes.add(bbox);
                else
                    findEdgesInShape(blockedEdges, bbox, filter);
            } else if (splittedObject.length == 3) {
                double lat = Double.parseDouble(splittedObject[0]);
                double lon = Double.parseDouble(splittedObject[1]);
                int radius = Integer.parseInt(splittedObject[2]);
                Circle circle = new Circle(lat, lon, radius);
                if (circle.calculateArea() > shapeArea) {
                    blockedShapes.add(circle);
                } else {
                    findEdgesInShape(blockedEdges, circle, filter);
                }
            } else if (splittedObject.length == 2) {
                double lat = Double.parseDouble(splittedObject[0]);
                double lon = Double.parseDouble(splittedObject[1]);
                findClosestEdge(blockedEdges, lat, lon, filter);
            } else {
                throw new IllegalArgumentException(objectAsString + " at index " + i + " need to be defined as lat,lon " + "or as a circle lat,lon,radius or rectangular lat1,lon1,lat2,lon2");
            }
        }
    }
    configMap.put(BLOCKED_EDGES, blockedEdges);
    configMap.put(BLOCKED_SHAPES, blockedShapes);
    return configMap;
}
Also used : Circle(com.graphhopper.util.shapes.Circle) GHIntHashSet(com.graphhopper.coll.GHIntHashSet) Shape(com.graphhopper.util.shapes.Shape) BBox(com.graphhopper.util.shapes.BBox) ArrayList(java.util.ArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 2 with Circle

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

the class GenericWeightingTest method testBlockedByShape.

@Test
public void testBlockedByShape() {
    EdgeIteratorState edge = graph.getEdgeIteratorState(0, 1);
    ConfigMap cMap = encoder.readStringMap(new PMap());
    GenericWeighting instance = new GenericWeighting(encoder, cMap);
    assertEquals(edgeWeight, instance.calcWeight(edge, false, EdgeIterator.NO_EDGE), 1e-8);
    List<Shape> shapes = new ArrayList<>(1);
    shapes.add(new Circle(0.01, 0.01, 100));
    cMap.put(BLOCKED_SHAPES, shapes);
    instance = new GenericWeighting(encoder, cMap);
    instance.setGraph(graph);
    assertEquals(Double.POSITIVE_INFINITY, instance.calcWeight(edge, false, EdgeIterator.NO_EDGE), 1e-8);
    shapes.clear();
    // Do not match 1,1 of edge
    shapes.add(new Circle(0.1, 0.1, 100));
    cMap.put(BLOCKED_SHAPES, shapes);
    instance = new GenericWeighting(encoder, cMap);
    instance.setGraph(graph);
    assertEquals(edgeWeight, instance.calcWeight(edge, false, EdgeIterator.NO_EDGE), 1e-8);
}
Also used : Circle(com.graphhopper.util.shapes.Circle) Shape(com.graphhopper.util.shapes.Shape) Test(org.junit.Test)

Example 3 with Circle

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

the class Location2IDFullIndex method findClosest.

@Override
public QueryResult findClosest(double queryLat, double queryLon, EdgeFilter edgeFilter) {
    if (isClosed())
        throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed");
    QueryResult res = new QueryResult(queryLat, queryLon);
    Circle circle = null;
    AllEdgesIterator iter = graph.getAllEdges();
    while (iter.next()) {
        if (!edgeFilter.accept(iter))
            continue;
        for (int node, i = 0; i < 2; i++) {
            if (i == 0) {
                node = iter.getBaseNode();
            } else {
                node = iter.getAdjNode();
            }
            double tmpLat = nodeAccess.getLatitude(node);
            double tmpLon = nodeAccess.getLongitude(node);
            double dist = calc.calcDist(tmpLat, tmpLon, queryLat, queryLon);
            if (circle == null || dist < calc.calcDist(circle.getLat(), circle.getLon(), queryLat, queryLon)) {
                res.setClosestEdge(iter.detach(false));
                res.setClosestNode(node);
                res.setQueryDistance(dist);
                if (dist <= 0)
                    break;
                circle = new Circle(tmpLat, tmpLon, dist, calc);
            }
        }
    }
    return res;
}
Also used : Circle(com.graphhopper.util.shapes.Circle) AllEdgesIterator(com.graphhopper.routing.util.AllEdgesIterator)

Example 4 with Circle

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

the class GraphEdgeIdFinderTest method testParseStringHints.

@Test
public void testParseStringHints() {
    FlagEncoder encoder = new CarFlagEncoder();
    EncodingManager em = new EncodingManager(encoder);
    GraphHopperStorage graph = new GraphBuilder(em).create();
    // 0-1-2
    // | |
    // 3-4
    graph.edge(0, 1, 1, true);
    graph.edge(1, 2, 1, true);
    graph.edge(3, 4, 1, true);
    graph.edge(0, 3, 1, true);
    graph.edge(1, 4, 1, true);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.01, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, 0.01, 0.02);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.00, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, 0.00, 0.01);
    LocationIndex locationIndex = new LocationIndexTree(graph, new RAMDirectory()).prepareIndex();
    HintsMap hints = new HintsMap();
    hints.put(Parameters.Routing.BLOCK_AREA, "0.01,0.005,1");
    ConfigMap cMap = new ConfigMap();
    GraphEdgeIdFinder graphFinder = new GraphEdgeIdFinder(graph, locationIndex);
    ConfigMap result = graphFinder.parseStringHints(cMap, hints, new DefaultEdgeFilter(encoder));
    GHIntHashSet blockedEdges = new GHIntHashSet();
    blockedEdges.add(0);
    assertEquals(blockedEdges, result.get(BLOCKED_EDGES, new GHIntHashSet()));
    List<Shape> blockedShapes = new ArrayList<>();
    assertEquals(blockedShapes, result.get(BLOCKED_SHAPES, new ArrayList<>()));
    // big area converts into shapes
    hints.put(Parameters.Routing.BLOCK_AREA, "0,0,1000");
    result = graphFinder.parseStringHints(cMap, hints, new DefaultEdgeFilter(encoder));
    blockedEdges.clear();
    assertEquals(blockedEdges, result.get(BLOCKED_EDGES, new GHIntHashSet()));
    blockedShapes.add(new Circle(0, 0, 1000));
    assertEquals(blockedShapes, result.get(BLOCKED_SHAPES, new ArrayList<>()));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) HintsMap(com.graphhopper.routing.util.HintsMap) Circle(com.graphhopper.util.shapes.Circle) GHIntHashSet(com.graphhopper.coll.GHIntHashSet) Shape(com.graphhopper.util.shapes.Shape) ConfigMap(com.graphhopper.util.ConfigMap) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) ArrayList(java.util.ArrayList) LocationIndex(com.graphhopper.storage.index.LocationIndex) DefaultEdgeFilter(com.graphhopper.routing.util.DefaultEdgeFilter) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) Test(org.junit.Test)

Aggregations

Circle (com.graphhopper.util.shapes.Circle)4 Shape (com.graphhopper.util.shapes.Shape)3 GHIntHashSet (com.graphhopper.coll.GHIntHashSet)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 AllEdgesIterator (com.graphhopper.routing.util.AllEdgesIterator)1 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)1 DefaultEdgeFilter (com.graphhopper.routing.util.DefaultEdgeFilter)1 EncodingManager (com.graphhopper.routing.util.EncodingManager)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 HintsMap (com.graphhopper.routing.util.HintsMap)1 LocationIndex (com.graphhopper.storage.index.LocationIndex)1 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)1 ConfigMap (com.graphhopper.util.ConfigMap)1 BBox (com.graphhopper.util.shapes.BBox)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1