Search in sources :

Example 1 with JsonFeature

use of com.graphhopper.json.geo.JsonFeature in project graphhopper by graphhopper.

the class SpatialRuleLookupBuilder method build.

/**
     * This method connects the rules with the jsonFeatureCollection via their ISO_A3 property and the rules its
     * getId method.
     *
     * @param jsonProperty the key that should be used to fetch the ID that is passed to SpatialRuleFactory#createSpatialRule
     * @return the index or null if the specified bounds does not intersect with the calculated ones from the rules.
     */
public SpatialRuleLookup build(String jsonProperty, SpatialRuleFactory ruleFactory, JsonFeatureCollection jsonFeatureCollection, BBox bounds, double resolution, boolean exact) {
    // TODO filter out polyons that don't intersect with the given BBox, will be implicitly done later anyway
    BBox polygonBounds = BBox.createInverse(false);
    List<SpatialRule> rules = new ArrayList<>();
    Set<String> ids = new HashSet<>();
    int unknownCounter = 0;
    for (JsonFeature jsonFeature : jsonFeatureCollection.getFeatures()) {
        Geometry geometry = jsonFeature.getGeometry();
        if (!geometry.isPolygon())
            continue;
        List<Polygon> borders = geometry.asPolygon().getPolygons();
        String id = (String) jsonFeature.getProperty(jsonProperty);
        if (id == null || id.isEmpty()) {
            id = "_unknown_id_" + unknownCounter;
            unknownCounter++;
        }
        if (ids.contains(id))
            throw new RuntimeException("The id " + id + " was already used. Either leave the json property '" + jsonProperty + "' empty or use an unique id.");
        ids.add(id);
        SpatialRule spatialRule = ruleFactory.createSpatialRule(id, borders);
        if (spatialRule == SpatialRule.EMPTY)
            continue;
        rules.add(spatialRule);
        for (Polygon polygon : borders) {
            polygonBounds.update(polygon.getMinLat(), polygon.getMinLon());
            polygonBounds.update(polygon.getMaxLat(), polygon.getMaxLon());
        }
    }
    if (rules.isEmpty())
        return null;
    if (!polygonBounds.isValid()) {
        throw new IllegalStateException("No associated polygons found in JsonFeatureCollection for rules " + rules);
    }
    // Only create a SpatialRuleLookup if there are rules defined in the given bounds
    BBox calculatedBounds = polygonBounds.calculateIntersection(bounds);
    if (calculatedBounds == null)
        return null;
    SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(calculatedBounds, resolution, exact);
    for (SpatialRule spatialRule : rules) {
        spatialRuleLookup.addRule(spatialRule);
    }
    return spatialRuleLookup;
}
Also used : DefaultSpatialRule(com.graphhopper.routing.util.spatialrules.countries.DefaultSpatialRule) JsonFeature(com.graphhopper.json.geo.JsonFeature) Geometry(com.graphhopper.json.geo.Geometry) BBox(com.graphhopper.util.shapes.BBox)

Example 2 with JsonFeature

use of com.graphhopper.json.geo.JsonFeature in project graphhopper by graphhopper.

the class GraphHopperAPITest method testConcurrentGraphChange.

@Test
public void testConcurrentGraphChange() throws InterruptedException {
    final GraphHopperStorage graph = new GraphBuilder(encodingManager).create();
    initGraph(graph);
    graph.edge(1, 2, 10, true);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger checkPointCounter = new AtomicInteger(0);
    final GraphHopper graphHopper = new GraphHopper() {

        @Override
        protected ChangeGraphHelper createChangeGraphHelper(Graph graph, LocationIndex locationIndex) {
            return new ChangeGraphHelper(graph, locationIndex) {

                @Override
                public long applyChanges(EncodingManager em, Collection<JsonFeature> features) {
                    // force sleep inside the lock and let the main thread run until the lock barrier
                    latch.countDown();
                    try {
                        Thread.sleep(400);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    checkPointCounter.incrementAndGet();
                    return super.applyChanges(em, features);
                }
            };
        }
    }.setStoreOnFlush(false).setEncodingManager(encodingManager).setCHEnabled(false).loadGraph(graph);
    GHResponse rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10));
    assertFalse(rsp.toString(), rsp.hasErrors());
    assertEquals(1800, rsp.getBest().getTime());
    final List<JsonFeature> list = new ArrayList<>();
    Map<String, Object> properties = new HashMap<>();
    properties.put("speed", 5);
    list.add(new JsonFeature("1", "bbox", new BBox(10.399, 10.4, 42.0, 42.001), null, properties));
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.submit(new Runnable() {

        @Override
        public void run() {
            graphHopper.changeGraph(list);
            checkPointCounter.incrementAndGet();
        }
    });
    latch.await();
    assertEquals(0, checkPointCounter.get());
    rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10));
    assertFalse(rsp.toString(), rsp.hasErrors());
    assertEquals(8400, rsp.getBest().getTime());
    executorService.shutdown();
    executorService.awaitTermination(3, TimeUnit.SECONDS);
    assertEquals(2, checkPointCounter.get());
}
Also used : ChangeGraphHelper(com.graphhopper.storage.change.ChangeGraphHelper) GraphBuilder(com.graphhopper.storage.GraphBuilder) EncodingManager(com.graphhopper.routing.util.EncodingManager) CountDownLatch(java.util.concurrent.CountDownLatch) LocationIndex(com.graphhopper.storage.index.LocationIndex) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) JsonFeature(com.graphhopper.json.geo.JsonFeature) Graph(com.graphhopper.storage.Graph) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BBox(com.graphhopper.util.shapes.BBox) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 3 with JsonFeature

use of com.graphhopper.json.geo.JsonFeature in project graphhopper by graphhopper.

the class DataFlagEncoderTest method testSpatialId.

@Test
public void testSpatialId() {
    List<SpatialRule> rules = Collections.<SpatialRule>singletonList(new GermanySpatialRule());
    final BBox bbox = new BBox(0, 1, 0, 1);
    JsonFeatureCollection jsonFeatures = new JsonFeatureCollection() {

        @Override
        public List<JsonFeature> getFeatures() {
            Geometry geometry = new GeoJsonPolygon().addPolygon(new Polygon(new double[] { 0, 0, 1, 1 }, new double[] { 0, 1, 1, 0 }));
            Map<String, Object> properties = new HashMap<>();
            properties.put("ISO_A3", "DEU");
            return Collections.singletonList(new JsonFeature("x", "Polygon", bbox, geometry, properties));
        }
    };
    SpatialRuleLookup index = new SpatialRuleLookupBuilder().build(rules, jsonFeatures, bbox, 1, false);
    DataFlagEncoder encoder = new DataFlagEncoder(new PMap().put("spatial_rules", index.size()));
    encoder.setSpatialRuleLookup(index);
    EncodingManager em = new EncodingManager(encoder);
    ReaderWay way = new ReaderWay(27l);
    way.setTag("highway", "track");
    way.setTag("estimated_center", new GHPoint(0.005, 0.005));
    ReaderWay way2 = new ReaderWay(28l);
    way2.setTag("highway", "track");
    way2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
    ReaderWay livingStreet = new ReaderWay(29l);
    livingStreet.setTag("highway", "living_street");
    livingStreet.setTag("estimated_center", new GHPoint(0.005, 0.005));
    ReaderWay livingStreet2 = new ReaderWay(30l);
    livingStreet2.setTag("highway", "living_street");
    livingStreet2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
    Graph graph = new GraphBuilder(em).create();
    EdgeIteratorState e1 = graph.edge(0, 1, 1, true);
    EdgeIteratorState e2 = graph.edge(0, 2, 1, true);
    EdgeIteratorState e3 = graph.edge(0, 3, 1, true);
    EdgeIteratorState e4 = graph.edge(0, 4, 1, true);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.00, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, -0.01, -0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, -0.01, -0.01);
    e1.setFlags(encoder.handleWayTags(way, 1, 0));
    e2.setFlags(encoder.handleWayTags(way2, 1, 0));
    e3.setFlags(encoder.handleWayTags(livingStreet, 1, 0));
    e4.setFlags(encoder.handleWayTags(livingStreet2, 1, 0));
    assertEquals(index.getSpatialId(new GermanySpatialRule()), encoder.getSpatialId(e1.getFlags()));
    assertEquals(index.getSpatialId(SpatialRule.EMPTY), encoder.getSpatialId(e2.getFlags()));
    assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, encoder.getAccessValue(e1.getFlags()));
    assertEquals(AccessValue.ACCESSIBLE, encoder.getAccessValue(e2.getFlags()));
    assertEquals(5, encoder.getMaxspeed(e3, -1, false), .1);
    assertEquals(-1, encoder.getMaxspeed(e4, -1, false), .1);
}
Also used : GeoJsonPolygon(com.graphhopper.json.geo.GeoJsonPolygon) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) ReaderWay(com.graphhopper.reader.ReaderWay) JsonFeature(com.graphhopper.json.geo.JsonFeature) Geometry(com.graphhopper.json.geo.Geometry) Graph(com.graphhopper.storage.Graph) BBox(com.graphhopper.util.shapes.BBox) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) GraphBuilder(com.graphhopper.storage.GraphBuilder) GeoJsonPolygon(com.graphhopper.json.geo.GeoJsonPolygon) GHPoint(com.graphhopper.util.shapes.GHPoint) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) Test(org.junit.Test)

Aggregations

JsonFeature (com.graphhopper.json.geo.JsonFeature)3 BBox (com.graphhopper.util.shapes.BBox)3 Geometry (com.graphhopper.json.geo.Geometry)2 Graph (com.graphhopper.storage.Graph)2 GraphBuilder (com.graphhopper.storage.GraphBuilder)2 Test (org.junit.Test)2 GeoJsonPolygon (com.graphhopper.json.geo.GeoJsonPolygon)1 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)1 ReaderWay (com.graphhopper.reader.ReaderWay)1 EncodingManager (com.graphhopper.routing.util.EncodingManager)1 DefaultSpatialRule (com.graphhopper.routing.util.spatialrules.countries.DefaultSpatialRule)1 GermanySpatialRule (com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule)1 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)1 ChangeGraphHelper (com.graphhopper.storage.change.ChangeGraphHelper)1 LocationIndex (com.graphhopper.storage.index.LocationIndex)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1