Search in sources :

Example 1 with LMProfile

use of com.graphhopper.config.LMProfile in project graphhopper by graphhopper.

the class GraphHopper method loadOrPrepareLM.

/**
 * For landmarks it is required to always call this method: either it creates the landmark data or it loads it.
 */
protected void loadOrPrepareLM(boolean closeEarly) {
    for (LMProfile profile : lmPreparationHandler.getLMProfiles()) if (!getLMProfileVersion(profile.getProfile()).isEmpty() && !getLMProfileVersion(profile.getProfile()).equals("" + profilesByName.get(profile.getProfile()).getVersion()))
        throw new IllegalArgumentException("LM preparation of " + profile.getProfile() + " already exists in storage and doesn't match configuration");
    // we load landmark storages that already exist and prepare the other ones
    List<LMConfig> lmConfigs = createLMConfigs(lmPreparationHandler.getLMProfiles());
    List<LandmarkStorage> loaded = lmPreparationHandler.load(lmConfigs, ghStorage);
    List<LMConfig> loadedConfigs = loaded.stream().map(LandmarkStorage::getLMConfig).collect(Collectors.toList());
    List<LMConfig> configsToPrepare = lmConfigs.stream().filter(c -> !loadedConfigs.contains(c)).collect(Collectors.toList());
    List<PrepareLandmarks> prepared = prepareLM(closeEarly, configsToPrepare);
    // we map all profile names for which there is LM support to the according LM storages
    landmarks = new LinkedHashMap<>();
    for (LMProfile lmp : lmPreparationHandler.getLMProfiles()) {
        // cross-querying
        String prepProfile = lmp.usesOtherPreparation() ? lmp.getPreparationProfile() : lmp.getProfile();
        Optional<LandmarkStorage> loadedLMS = loaded.stream().filter(lms -> lms.getLMConfig().getName().equals(prepProfile)).findFirst();
        Optional<PrepareLandmarks> preparedLMS = prepared.stream().filter(pl -> pl.getLandmarkStorage().getLMConfig().getName().equals(prepProfile)).findFirst();
        if (loadedLMS.isPresent() && preparedLMS.isPresent())
            throw new IllegalStateException("LM should be either loaded or prepared, but not both: " + prepProfile);
        else if (preparedLMS.isPresent()) {
            setLMProfileVersion(lmp.getProfile(), profilesByName.get(lmp.getProfile()).getVersion());
            landmarks.put(lmp.getProfile(), preparedLMS.get().getLandmarkStorage());
        } else
            loadedLMS.ifPresent(landmarkStorage -> landmarks.put(lmp.getProfile(), landmarkStorage));
    }
}
Also used : com.graphhopper.routing.ev(com.graphhopper.routing.ev) RoundTrip(com.graphhopper.util.Parameters.Algorithms.RoundTrip) com.graphhopper.routing(com.graphhopper.routing) LoggerFactory(org.slf4j.LoggerFactory) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) DirectoryStream(java.nio.file.DirectoryStream) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) LMConfig(com.graphhopper.routing.lm.LMConfig) Path(java.nio.file.Path) DateFormat(java.text.DateFormat) DefaultTagParserFactory(com.graphhopper.routing.util.parsers.DefaultTagParserFactory) PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) Landmark(com.graphhopper.util.Parameters.Landmark) Helper(com.graphhopper.util.Helper) PrepareRoutingSubnetworks(com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) com.graphhopper.reader.dem(com.graphhopper.reader.dem) UncheckedIOException(java.io.UncheckedIOException) GHUtility.readCountries(com.graphhopper.util.GHUtility.readCountries) com.graphhopper.routing.util(com.graphhopper.routing.util) LMProfile(com.graphhopper.config.LMProfile) java.util(java.util) CountryRuleFactory(com.graphhopper.routing.util.countryrules.CountryRuleFactory) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Routing(com.graphhopper.util.Parameters.Routing) PrepareJob(com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks.PrepareJob) CHPreparationHandler(com.graphhopper.routing.ch.CHPreparationHandler) Profile(com.graphhopper.config.Profile) OSMReader(com.graphhopper.reader.osm.OSMReader) com.graphhopper.storage(com.graphhopper.storage) DateRangeParser(com.graphhopper.reader.osm.conditional.DateRangeParser) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) LocationIndex(com.graphhopper.storage.index.LocationIndex) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) CHProfile(com.graphhopper.config.CHProfile) File(java.io.File) TagParserFactory(com.graphhopper.routing.util.parsers.TagParserFactory) LMPreparationHandler(com.graphhopper.routing.lm.LMPreparationHandler) Paths(java.nio.file.Paths) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) Weighting(com.graphhopper.routing.weighting.Weighting) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) BufferedReader(java.io.BufferedReader) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) LMProfile(com.graphhopper.config.LMProfile) LMConfig(com.graphhopper.routing.lm.LMConfig)

Example 2 with LMProfile

use of com.graphhopper.config.LMProfile in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testLoadingCustomProfiles.

@Test
public void testLoadingCustomProfiles() {
    CustomModel customModel = new CustomModel().setDistanceInfluence(123);
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(ghLoc).setOSMFile(testOsm).setProfiles(new CustomProfile("car").setCustomModel(customModel));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car"));
    hopper.importOrLoad();
    hopper.close();
    // load without problem
    hopper = new GraphHopper().setProfiles(new CustomProfile("car").setCustomModel(customModel));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car"));
    hopper.setGraphHopperLocation(ghLoc);
    assertTrue(hopper.load());
    hopper.close();
    // do not load changed CustomModel
    customModel.setDistanceInfluence(100);
    hopper = new GraphHopper().setProfiles(new CustomProfile("car").setCustomModel(customModel));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car"));
    hopper.setGraphHopperLocation(ghLoc);
    try {
        assertFalse(hopper.load());
        fail("load should fail");
    } catch (Exception ex) {
        assertEquals("LM preparation of car already exists in storage and doesn't match configuration", ex.getMessage());
    } finally {
        hopper.close();
    }
}
Also used : CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test)

Example 3 with LMProfile

use of com.graphhopper.config.LMProfile in project graphhopper by graphhopper.

the class GraphHopperOSMTest method testLoadingLMAndCHProfiles.

@Test
public void testLoadingLMAndCHProfiles() {
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(ghLoc).setOSMFile(testOsm).setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest"));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car"));
    hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));
    hopper.importOrLoad();
    hopper.close();
    // load without problem
    hopper = new GraphHopper().setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest"));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car"));
    hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));
    hopper.setGraphHopperLocation(ghLoc);
    assertTrue(hopper.load());
    hopper.close();
    // problem: changed weighting in profile although LM preparation was enabled
    hopper = new GraphHopper().setProfiles(new Profile("car").setVehicle("car").setWeighting("shortest"));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car"));
    hopper.setGraphHopperLocation(ghLoc);
    // do not load CH
    try {
        assertFalse(hopper.load());
        fail("load should fail");
    } catch (Exception ex) {
        assertEquals("LM preparation of car already exists in storage and doesn't match configuration", ex.getMessage());
    } finally {
        hopper.close();
    }
    // problem: changed weighting in profile although CH preparation was enabled
    hopper = new GraphHopper().setProfiles(new Profile("car").setVehicle("car").setWeighting("shortest"));
    hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));
    hopper.setGraphHopperLocation(ghLoc);
    // do not load LM
    try {
        assertFalse(hopper.load());
        fail("load should fail");
    } catch (Exception ex) {
        assertEquals("CH preparation of car already exists in storage and doesn't match configuration", ex.getMessage());
    } finally {
        hopper.close();
    }
}
Also used : CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test)

Example 4 with LMProfile

use of com.graphhopper.config.LMProfile in project graphhopper by graphhopper.

the class GraphHopperProfileTest method lmPreparationProfileChain_error.

@Test
public void lmPreparationProfileChain_error() {
    final GraphHopper hopper = createHopper();
    hopper.setProfiles(new Profile("profile1").setVehicle("car"), new Profile("profile2").setVehicle("bike"), new Profile("profile3").setVehicle("foot"));
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("profile1"), new LMProfile("profile2").setPreparationProfile("profile1"), new LMProfile("profile3").setPreparationProfile("profile2"));
    assertIllegalArgument(hopper::load, "Cannot use 'profile2' as preparation_profile for LM profile 'profile3', because it uses another profile for preparation itself.");
}
Also used : LMProfile(com.graphhopper.config.LMProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test)

Example 5 with LMProfile

use of com.graphhopper.config.LMProfile in project graphhopper by graphhopper.

the class ProfileResolverTest method defaultVehicle.

@Test
public void defaultVehicle() {
    ProfileResolver profileResolver = new ProfileResolver(EncodingManager.create("car,foot,bike"), Arrays.asList(new Profile("my_bike").setVehicle("bike"), new Profile("your_car").setVehicle("car")), Collections.<CHProfile>emptyList(), Collections.<LMProfile>emptyList());
    // without specifying the vehicle we get an error, because there are multiple matches
    assertMultiMatchError(profileResolver, new PMap(), "There are multiple profiles matching your request");
    // use vehicle to specify profile
    assertEquals("your_car", profileResolver.resolveProfile(new PMap().putObject("vehicle", "car")).getName());
    assertEquals("my_bike", profileResolver.resolveProfile(new PMap().putObject("vehicle", "bike")).getName());
}
Also used : PMap(com.graphhopper.util.PMap) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test) CHProfileSelectorTest(com.graphhopper.routing.ch.CHProfileSelectorTest) LMProfileSelectorTest(com.graphhopper.routing.lm.LMProfileSelectorTest)

Aggregations

LMProfile (com.graphhopper.config.LMProfile)48 Profile (com.graphhopper.config.Profile)41 CHProfile (com.graphhopper.config.CHProfile)33 Test (org.junit.jupiter.api.Test)24 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)15 PMap (com.graphhopper.util.PMap)12 GraphHopper (com.graphhopper.GraphHopper)10 GraphHopperServerConfiguration (com.graphhopper.application.GraphHopperServerConfiguration)6 GraphHopperServerTestConfiguration (com.graphhopper.application.util.GraphHopperServerTestConfiguration)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 CHProfileSelectorTest (com.graphhopper.routing.ch.CHProfileSelectorTest)4 LMProfileSelectorTest (com.graphhopper.routing.lm.LMProfileSelectorTest)4 File (java.io.File)4 GraphHopperConfig (com.graphhopper.GraphHopperConfig)3 Gpx (com.graphhopper.jackson.Gpx)3 EdgeMatch (com.graphhopper.matching.EdgeMatch)3 MapMatching (com.graphhopper.matching.MapMatching)3 MatchResult (com.graphhopper.matching.MatchResult)3 ProfileResolver (com.graphhopper.routing.ProfileResolver)3 GHPoint (com.graphhopper.util.shapes.GHPoint)3