Search in sources :

Example 1 with Shape

use of com.graphhopper.util.shapes.Shape 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 Shape

use of com.graphhopper.util.shapes.Shape 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 Shape

use of com.graphhopper.util.shapes.Shape 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 4 with Shape

use of com.graphhopper.util.shapes.Shape 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();
    GraphEdgeIdFinder graphFinder = new GraphEdgeIdFinder(graph, locationIndex);
    GraphEdgeIdFinder.BlockArea blockArea = graphFinder.parseBlockArea("0.01,0.005,1", new DefaultEdgeFilter(encoder), 1000 * 1000);
    GHIntHashSet blockedEdges = new GHIntHashSet();
    blockedEdges.add(0);
    assertEquals(blockedEdges, blockArea.blockedEdges);
    List<Shape> blockedShapes = new ArrayList<>();
    assertEquals(blockedShapes, blockArea.blockedShapes);
    // big area converts into shapes
    graphFinder = new GraphEdgeIdFinder(graph, locationIndex);
    blockArea = graphFinder.parseBlockArea("0,0,1000", new DefaultEdgeFilter(encoder), 1000 * 1000);
    blockedEdges.clear();
    assertEquals(blockedEdges, blockArea.blockedEdges);
    blockedShapes.add(new Circle(0, 0, 1000));
    assertEquals(blockedShapes, blockArea.blockedShapes);
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) Circle(com.graphhopper.util.shapes.Circle) GHIntHashSet(com.graphhopper.coll.GHIntHashSet) Shape(com.graphhopper.util.shapes.Shape) 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

Shape (com.graphhopper.util.shapes.Shape)4 Circle (com.graphhopper.util.shapes.Circle)3 GHIntHashSet (com.graphhopper.coll.GHIntHashSet)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 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 LocationIndex (com.graphhopper.storage.index.LocationIndex)1 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)1 QueryResult (com.graphhopper.storage.index.QueryResult)1 BreadthFirstSearch (com.graphhopper.util.BreadthFirstSearch)1 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)1 BBox (com.graphhopper.util.shapes.BBox)1