Search in sources :

Example 1 with PrepareLandmarks

use of com.graphhopper.routing.lm.PrepareLandmarks 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 PrepareLandmarks

use of com.graphhopper.routing.lm.PrepareLandmarks in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testMultipleLMPreparationsInParallel.

@Test
public void testMultipleLMPreparationsInParallel() {
    HashMap<String, Integer> landmarkCount = new HashMap<String, Integer>();
    // try all parallelization modes
    for (int threadCount = 1; threadCount < 6; threadCount++) {
        EncodingManager em = new EncodingManager(Arrays.asList(new CarFlagEncoder(), new MotorcycleFlagEncoder(), new MountainBikeFlagEncoder(), new RacingBikeFlagEncoder(), new FootFlagEncoder()), 8);
        GraphHopper tmpGH = new GraphHopperOSM().setStoreOnFlush(false).setCHEnabled(false).setEncodingManager(em).setGraphHopperLocation(ghLoc).setDataReaderFile(testOsm);
        tmpGH.getLMFactoryDecorator().addWeighting("fastest").setEnabled(true).setPreparationThreads(threadCount);
        tmpGH.importOrLoad();
        assertEquals(5, tmpGH.getLMFactoryDecorator().getPreparations().size());
        for (PrepareLandmarks prepLM : tmpGH.getLMFactoryDecorator().getPreparations()) {
            assertTrue("Preparation wasn't run! [" + threadCount + "]", prepLM.isPrepared());
            String name = AbstractWeighting.weightingToFileName(prepLM.getWeighting());
            Integer singleThreadShortcutCount = landmarkCount.get(name);
            if (singleThreadShortcutCount == null)
                landmarkCount.put(name, prepLM.getSubnetworksWithLandmarks());
            else
                assertEquals((int) singleThreadShortcutCount, prepLM.getSubnetworksWithLandmarks());
            String keyError = Parameters.Landmark.PREPARE + "error." + name;
            String valueError = tmpGH.getGraphHopperStorage().getProperties().get(keyError);
            assertTrue("Properties for " + name + " should NOT contain error " + valueError + " [" + threadCount + "]", valueError.isEmpty());
            String key = Parameters.Landmark.PREPARE + "date." + name;
            String value = tmpGH.getGraphHopperStorage().getProperties().get(key);
            assertTrue("Properties for " + name + " did NOT contain finish date [" + threadCount + "]", !value.isEmpty());
        }
        tmpGH.close();
    }
}
Also used : HashMap(java.util.HashMap) GraphHopper(com.graphhopper.GraphHopper) GHPoint(com.graphhopper.util.shapes.GHPoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) Test(org.junit.Test)

Aggregations

GraphHopper (com.graphhopper.GraphHopper)2 PrepareLandmarks (com.graphhopper.routing.lm.PrepareLandmarks)2 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 SpatialRuleLookup (com.graphhopper.routing.util.spatialrules.SpatialRuleLookup)1 SpatialRuleLookupBuilder (com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)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 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1