Search in sources :

Example 1 with SpatialRuleLookup

use of com.graphhopper.routing.util.spatialrules.SpatialRuleLookup in project graphhopper by graphhopper.

the class DefaultModule method createGraphHopper.

/**
     * @return an initialized GraphHopper instance
     */
protected GraphHopper createGraphHopper(CmdArgs args) {
    GraphHopper tmp = new GraphHopperOSM() {

        @Override
        protected void loadOrPrepareLM() {
            if (!getLMFactoryDecorator().isEnabled() || getLMFactoryDecorator().getPreparations().isEmpty())
                return;
            try {
                String location = args.get(Parameters.Landmark.PREPARE + "split_area_location", "");
                Reader reader = location.isEmpty() ? new InputStreamReader(LandmarkStorage.class.getResource("map.geo.json").openStream()) : new FileReader(location);
                JsonFeatureCollection jsonFeatureCollection = new GHJsonBuilder().create().fromJson(reader, JsonFeatureCollection.class);
                if (!jsonFeatureCollection.getFeatures().isEmpty()) {
                    SpatialRuleLookup ruleLookup = new SpatialRuleLookupBuilder().build("country", new SpatialRuleLookupBuilder.SpatialRuleDefaultFactory(), jsonFeatureCollection, getGraphHopperStorage().getBounds(), 0.1, true);
                    for (PrepareLandmarks prep : getLMFactoryDecorator().getPreparations()) {
                        prep.setSpatialRuleLookup(ruleLookup);
                    }
                }
            } catch (IOException ex) {
                logger.error("Problem while reading border map GeoJSON. Skipping this.", ex);
            }
            super.loadOrPrepareLM();
        }
    }.forServer().init(args);
    String location = args.get("spatial_rules.location", "");
    if (!location.isEmpty()) {
        if (!tmp.getEncodingManager().supports(("generic"))) {
            logger.warn("spatial_rules.location was specified but 'generic' encoder is missing to utilize the index");
        } else
            try {
                SpatialRuleLookup index = buildIndex(new FileReader(location), tmp.getGraphHopperStorage().getBounds());
                if (index != null) {
                    logger.info("Set spatial rule lookup with " + index.size() + " rules");
                    ((DataFlagEncoder) tmp.getEncodingManager().getEncoder("generic")).setSpatialRuleLookup(index);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
    }
    tmp.importOrLoad();
    logger.info("loaded graph at:" + tmp.getGraphHopperLocation() + ", data_reader_file:" + tmp.getDataReaderFile() + ", flag_encoders:" + tmp.getEncodingManager() + ", " + tmp.getGraphHopperStorage().toDetailsString());
    return tmp;
}
Also used : GHJsonBuilder(com.graphhopper.json.GHJsonBuilder) InputStreamReader(java.io.InputStreamReader) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) GraphHopper(com.graphhopper.GraphHopper) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) IOException(java.io.IOException) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) FileReader(java.io.FileReader) SpatialRuleLookupBuilder(com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)

Example 2 with SpatialRuleLookup

use of com.graphhopper.routing.util.spatialrules.SpatialRuleLookup in project graphhopper by graphhopper.

the class LandmarkStorageTest method testWithBorderBlocking.

@Test
public void testWithBorderBlocking() {
    AbstractRoutingAlgorithmTester.initBiGraph(ghStorage);
    LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), 2, new FastestWeighting(encoder), TraversalMode.NODE_BASED);
    final SpatialRule ruleRight = new DefaultSpatialRule() {

        @Override
        public String getId() {
            return "right";
        }
    };
    final SpatialRule ruleLeft = new DefaultSpatialRule() {

        @Override
        public String getId() {
            return "left";
        }
    };
    final SpatialRuleLookup lookup = new SpatialRuleLookup() {

        @Override
        public SpatialRule lookupRule(double lat, double lon) {
            if (lon > 0.00105)
                return ruleRight;
            return ruleLeft;
        }

        @Override
        public SpatialRule lookupRule(GHPoint point) {
            return lookupRule(point.lat, point.lon);
        }

        @Override
        public void addRule(SpatialRule rule) {
        }

        @Override
        public int getSpatialId(SpatialRule rule) {
            throw new IllegalStateException();
        }

        @Override
        public BBox getBounds() {
            throw new IllegalStateException();
        }

        @Override
        public int size() {
            return 2;
        }
    };
    storage.setSpatialRuleLookup(lookup);
    storage.setMinimumNodes(2);
    storage.createLandmarks();
    assertEquals(3, storage.getSubnetworksWithLandmarks());
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) SpatialRule(com.graphhopper.routing.util.spatialrules.SpatialRule) DefaultSpatialRule(com.graphhopper.routing.util.spatialrules.countries.DefaultSpatialRule) DefaultSpatialRule(com.graphhopper.routing.util.spatialrules.countries.DefaultSpatialRule) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Aggregations

SpatialRuleLookup (com.graphhopper.routing.util.spatialrules.SpatialRuleLookup)2 GraphHopper (com.graphhopper.GraphHopper)1 GHJsonBuilder (com.graphhopper.json.GHJsonBuilder)1 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)1 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)1 LandmarkStorage (com.graphhopper.routing.lm.LandmarkStorage)1 PrepareLandmarks (com.graphhopper.routing.lm.PrepareLandmarks)1 SpatialRule (com.graphhopper.routing.util.spatialrules.SpatialRule)1 SpatialRuleLookupBuilder (com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)1 DefaultSpatialRule (com.graphhopper.routing.util.spatialrules.countries.DefaultSpatialRule)1 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Test (org.junit.Test)1