Search in sources :

Example 6 with CustomProfile

use of com.graphhopper.routing.weighting.custom.CustomProfile in project graphhopper by graphhopper.

the class RouteResourceCustomModelTest method createConfig.

private static GraphHopperServerConfiguration createConfig() {
    GraphHopperServerConfiguration config = new GraphHopperServerTestConfiguration();
    config.getGraphHopperConfiguration().putObject("graph.flag_encoders", "bike,car,foot,wheelchair,roads").putObject("prepare.min_network_size", 200).putObject("datareader.file", "../core/files/north-bayreuth.osm.gz").putObject("graph.location", DIR).putObject("graph.encoded_values", "max_height,max_weight,max_width,hazmat,toll,surface,track_type").putObject("custom_model_folder", "./src/test/resources/com/graphhopper/application/resources").setProfiles(Arrays.asList(new Profile("wheelchair"), new CustomProfile("roads").setCustomModel(new CustomModel()).setVehicle("roads"), new CustomProfile("car").setCustomModel(new CustomModel()).setVehicle("car"), new CustomProfile("bike").setCustomModel(new CustomModel().setDistanceInfluence(0)).setVehicle("bike"), new Profile("bike_fastest").setWeighting("fastest").setVehicle("bike"), new CustomProfile("truck").setVehicle("car").putHint("custom_model_file", "truck.yml"), new CustomProfile("cargo_bike").setVehicle("bike").putHint("custom_model_file", "cargo_bike.yml"), new CustomProfile("json_bike").setVehicle("bike").putHint("custom_model_file", "json_bike.json"), new Profile("foot_profile").setVehicle("foot").setWeighting("fastest"), new CustomProfile("car_no_unclassified").setCustomModel(new CustomModel(new CustomModel().addToPriority(If("road_class == UNCLASSIFIED", LIMIT, 0)))).setVehicle("car"), new CustomProfile("custom_bike").setCustomModel(new CustomModel().addToSpeed(If("road_class == PRIMARY", LIMIT, 28)).addToPriority(If("max_width < 1.2", MULTIPLY, 0))).setVehicle("bike"), new CustomProfile("custom_bike2").setCustomModel(new CustomModel(new CustomModel().addToPriority(If("road_class == TERTIARY || road_class == TRACK", MULTIPLY, 0)))).setVehicle("bike"), new CustomProfile("custom_bike3").setCustomModel(new CustomModel(new CustomModel().addToSpeed(If("road_class == TERTIARY || road_class == TRACK", MULTIPLY, 10)).addToSpeed(If("true", LIMIT, 40)))).setVehicle("bike"))).setCHProfiles(Arrays.asList(new CHProfile("truck"), new CHProfile("car_no_unclassified")));
    return config;
}
Also used : CHProfile(com.graphhopper.config.CHProfile) GraphHopperServerConfiguration(com.graphhopper.application.GraphHopperServerConfiguration) GraphHopperServerTestConfiguration(com.graphhopper.application.util.GraphHopperServerTestConfiguration) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) CustomModel(com.graphhopper.util.CustomModel)

Example 7 with CustomProfile

use of com.graphhopper.routing.weighting.custom.CustomProfile in project graphhopper by graphhopper.

the class RouteResourceCustomModelLMTest method createConfig.

private static GraphHopperServerConfiguration createConfig() {
    GraphHopperServerConfiguration config = new GraphHopperServerTestConfiguration();
    config.getGraphHopperConfiguration().putObject("graph.flag_encoders", "car,foot").putObject("datareader.file", "../core/files/andorra.osm.pbf").putObject("graph.location", DIR).putObject("graph.encoded_values", "surface").setProfiles(Arrays.asList(// give strange profile names to ensure that we do not mix vehicle and profile:
    new CustomProfile("car_custom").setCustomModel(new CustomModel()).setVehicle("car"), new Profile("foot_profile").setVehicle("foot").setWeighting("fastest"), new CustomProfile("foot_custom").setCustomModel(new CustomModel()).setVehicle("foot"))).setLMProfiles(Arrays.asList(new LMProfile("car_custom"), new LMProfile("foot_custom")));
    return config;
}
Also used : GraphHopperServerConfiguration(com.graphhopper.application.GraphHopperServerConfiguration) GraphHopperServerTestConfiguration(com.graphhopper.application.util.GraphHopperServerTestConfiguration) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) LMProfile(com.graphhopper.config.LMProfile) CustomModel(com.graphhopper.util.CustomModel) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) LMProfile(com.graphhopper.config.LMProfile)

Example 8 with CustomProfile

use of com.graphhopper.routing.weighting.custom.CustomProfile in project graphhopper by graphhopper.

the class GraphHopper method checkProfilesConsistency.

private void checkProfilesConsistency() {
    EncodingManager encodingManager = getEncodingManager();
    for (Profile profile : profilesByName.values()) {
        if (!encodingManager.hasEncoder(profile.getVehicle())) {
            throw new IllegalArgumentException("Unknown vehicle '" + profile.getVehicle() + "' in profile: " + profile + ". Make sure all vehicles used in 'profiles' exist in 'graph.flag_encoders'");
        }
        FlagEncoder encoder = encodingManager.getEncoder(profile.getVehicle());
        if (profile.isTurnCosts() && !encoder.supportsTurnCosts()) {
            throw new IllegalArgumentException("The profile '" + profile.getName() + "' was configured with " + "'turn_costs=true', but the corresponding vehicle '" + profile.getVehicle() + "' does not support turn costs." + "\nYou need to add `|turn_costs=true` to the vehicle in `graph.flag_encoders`");
        }
        try {
            createWeighting(profile, new PMap());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Could not create weighting for profile: '" + profile.getName() + "'.\n" + "Profile: " + profile + "\n" + "Error: " + e.getMessage());
        }
        if (profile instanceof CustomProfile) {
            CustomModel customModel = ((CustomProfile) profile).getCustomModel();
            if (customModel == null)
                throw new IllegalArgumentException("custom model for profile '" + profile.getName() + "' was empty");
            if (!CustomWeighting.NAME.equals(profile.getWeighting()))
                throw new IllegalArgumentException("profile '" + profile.getName() + "' has a custom model but " + "weighting=" + profile.getWeighting() + " was defined");
        }
    }
    Set<String> chProfileSet = new LinkedHashSet<>(chPreparationHandler.getCHProfiles().size());
    for (CHProfile chProfile : chPreparationHandler.getCHProfiles()) {
        boolean added = chProfileSet.add(chProfile.getProfile());
        if (!added) {
            throw new IllegalArgumentException("Duplicate CH reference to profile '" + chProfile.getProfile() + "'");
        }
        if (!profilesByName.containsKey(chProfile.getProfile())) {
            throw new IllegalArgumentException("CH profile references unknown profile '" + chProfile.getProfile() + "'");
        }
    }
    Map<String, LMProfile> lmProfileMap = new LinkedHashMap<>(lmPreparationHandler.getLMProfiles().size());
    for (LMProfile lmProfile : lmPreparationHandler.getLMProfiles()) {
        LMProfile previous = lmProfileMap.put(lmProfile.getProfile(), lmProfile);
        if (previous != null) {
            throw new IllegalArgumentException("Multiple LM profiles are using the same profile '" + lmProfile.getProfile() + "'");
        }
        if (!profilesByName.containsKey(lmProfile.getProfile())) {
            throw new IllegalArgumentException("LM profile references unknown profile '" + lmProfile.getProfile() + "'");
        }
        if (lmProfile.usesOtherPreparation() && !profilesByName.containsKey(lmProfile.getPreparationProfile())) {
            throw new IllegalArgumentException("LM profile references unknown preparation profile '" + lmProfile.getPreparationProfile() + "'");
        }
    }
    for (LMProfile lmProfile : lmPreparationHandler.getLMProfiles()) {
        if (lmProfile.usesOtherPreparation() && !lmProfileMap.containsKey(lmProfile.getPreparationProfile())) {
            throw new IllegalArgumentException("Unknown LM preparation profile '" + lmProfile.getPreparationProfile() + "' in LM profile '" + lmProfile.getProfile() + "' cannot be used as preparation_profile");
        }
        if (lmProfile.usesOtherPreparation() && lmProfileMap.get(lmProfile.getPreparationProfile()).usesOtherPreparation()) {
            throw new IllegalArgumentException("Cannot use '" + lmProfile.getPreparationProfile() + "' as preparation_profile for LM profile '" + lmProfile.getProfile() + "', because it uses another profile for preparation itself.");
        }
    }
}
Also used : CHProfile(com.graphhopper.config.CHProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) 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)

Example 9 with CustomProfile

use of com.graphhopper.routing.weighting.custom.CustomProfile in project graphhopper by graphhopper.

the class GraphHopperTest method testImportCloseAndLoad.

private void testImportCloseAndLoad(boolean ch, boolean lm, boolean sort, boolean custom) {
    final String vehicle = "foot";
    final String profileName = "profile";
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile(MONACO).setStoreOnFlush(true).setSortGraph(sort);
    Profile profile = new Profile(profileName).setVehicle(vehicle).setWeighting("fastest");
    if (custom) {
        JsonFeature area51Feature = new JsonFeature();
        area51Feature.setGeometry(new GeometryFactory().createPolygon(new Coordinate[] { new Coordinate(7.4174, 43.7345), new Coordinate(7.4198, 43.7355), new Coordinate(7.4207, 43.7344), new Coordinate(7.4174, 43.7345) }));
        CustomModel customModel = new CustomModel().setDistanceInfluence(0);
        customModel.getPriority().add(Statement.If("in_area51", Statement.Op.MULTIPLY, 0.1));
        customModel.getAreas().put("area51", area51Feature);
        profile = new CustomProfile(profileName).setCustomModel(customModel).setVehicle(vehicle);
    }
    hopper.setProfiles(profile);
    if (ch) {
        hopper.getCHPreparationHandler().setCHProfiles(new CHProfile(profileName));
    }
    if (lm) {
        hopper.getLMPreparationHandler().setLMProfiles(new LMProfile(profileName));
    }
    hopper.importAndClose();
    hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile(MONACO).setProfiles(profile).setStoreOnFlush(true).setAllowWrites(false);
    if (ch) {
        hopper.getCHPreparationHandler().setCHProfiles(new CHProfile(profileName));
    }
    if (lm) {
        hopper.getLMPreparationHandler().setLMProfiles(new LMProfile(profileName));
    }
    hopper.importOrLoad();
    if (ch) {
        GHRequest req = new GHRequest(43.727687, 7.418737, 43.74958, 7.436566).setProfile(profileName);
        req.putHint(CH.DISABLE, false);
        req.putHint(Landmark.DISABLE, true);
        GHResponse rsp = hopper.route(req);
        ResponsePath bestPath = rsp.getBest();
        long sum = rsp.getHints().getLong("visited_nodes.sum", 0);
        assertNotEquals(sum, 0);
        assertTrue(sum < 155, "Too many nodes visited " + sum);
        assertEquals(3535, bestPath.getDistance(), 1);
        assertEquals(115, bestPath.getPoints().size());
    }
    if (lm) {
        GHRequest req = new GHRequest(43.727687, 7.418737, 43.74958, 7.436566).setProfile(profileName).setAlgorithm(Parameters.Algorithms.ASTAR_BI);
        req.putHint(CH.DISABLE, true);
        req.putHint(Landmark.DISABLE, false);
        GHResponse rsp = hopper.route(req);
        ResponsePath bestPath = rsp.getBest();
        long sum = rsp.getHints().getLong("visited_nodes.sum", 0);
        assertNotEquals(sum, 0);
        assertTrue(sum < 125, "Too many nodes visited " + sum);
        assertEquals(3535, bestPath.getDistance(), 1);
        assertEquals(115, bestPath.getPoints().size());
    }
    // flexible
    GHRequest req = new GHRequest(43.727687, 7.418737, 43.74958, 7.436566).setProfile(profileName);
    req.putHint(CH.DISABLE, true);
    req.putHint(Landmark.DISABLE, true);
    GHResponse rsp = hopper.route(req);
    ResponsePath bestPath = rsp.getBest();
    long sum = rsp.getHints().getLong("visited_nodes.sum", 0);
    assertNotEquals(sum, 0);
    assertTrue(sum > 120, "Too few nodes visited " + sum);
    assertEquals(3535, bestPath.getDistance(), 1);
    assertEquals(115, bestPath.getPoints().size());
    hopper.close();
}
Also used : CHProfile(com.graphhopper.config.CHProfile) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Coordinate(org.locationtech.jts.geom.Coordinate) LMProfile(com.graphhopper.config.LMProfile)

Aggregations

CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)9 LMProfile (com.graphhopper.config.LMProfile)6 Profile (com.graphhopper.config.Profile)6 CHProfile (com.graphhopper.config.CHProfile)4 CustomModel (com.graphhopper.util.CustomModel)4 GraphHopperServerConfiguration (com.graphhopper.application.GraphHopperServerConfiguration)2 GraphHopperServerTestConfiguration (com.graphhopper.application.util.GraphHopperServerTestConfiguration)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 GHRequest (com.graphhopper.GHRequest)1 GHResponse (com.graphhopper.GHResponse)1 GraphHopper (com.graphhopper.GraphHopper)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 CustomWeighting (com.graphhopper.routing.weighting.custom.CustomWeighting)1 PMap (com.graphhopper.util.PMap)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1