Search in sources :

Example 66 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class RoutingExampleTC method createGraphHopperInstance.

// see RoutingExample for more details
static GraphHopper createGraphHopperInstance(String ghLoc) {
    GraphHopper hopper = new GraphHopper();
    hopper.setOSMFile(ghLoc);
    hopper.setGraphHopperLocation("target/routing-tc-graph-cache");
    // by enabling turn costs for the FlagEncoder, turn restriction constraints like 'no_left_turn' will be taken
    // from OSM
    Profile profile = new Profile("car").setVehicle("car").setWeighting("fastest").setTurnCosts(true).putHint("u_turn_costs", 40);
    hopper.setProfiles(profile);
    // enable CH for our profile. since turn costs are enabled this will take more time and memory to prepare than
    // without turn costs.
    hopper.getCHPreparationHandler().setCHProfiles(new CHProfile(profile.getName()));
    hopper.importOrLoad();
    return hopper;
}
Also used : CHProfile(com.graphhopper.config.CHProfile) GraphHopper(com.graphhopper.GraphHopper) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile)

Example 67 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class LocationIndexExample method graphhopperLocationIndex.

public static void graphhopperLocationIndex(String relDir) {
    GraphHopper hopper = new GraphHopper();
    hopper.setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest"));
    hopper.setOSMFile(relDir + "core/files/andorra.osm.pbf");
    hopper.setGraphHopperLocation("./target/locationindex-graph-cache");
    hopper.importOrLoad();
    LocationIndex index = hopper.getLocationIndex();
    // now you can fetch the closest edge via:
    Snap snap = index.findClosest(42.508552, 1.532936, EdgeFilter.ALL_EDGES);
    EdgeIteratorState edge = snap.getClosestEdge();
    assert edge.getName().equals("Avinguda Meritxell");
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GraphHopper(com.graphhopper.GraphHopper) LocationIndex(com.graphhopper.storage.index.LocationIndex) Snap(com.graphhopper.storage.index.Snap) Profile(com.graphhopper.config.Profile)

Example 68 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class RoutingExample method customizableRouting.

public static void customizableRouting(String ghLoc) {
    GraphHopper hopper = new GraphHopper();
    hopper.setOSMFile(ghLoc);
    hopper.setGraphHopperLocation("target/routing-custom-graph-cache");
    hopper.setProfiles(new CustomProfile("car_custom").setCustomModel(new CustomModel()).setVehicle("car"));
    // The hybrid mode uses the "landmark algorithm" and is up to 15x faster than the flexible mode (Dijkstra).
    // Still it is slower than the speed mode ("contraction hierarchies algorithm") ...
    hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car_custom"));
    hopper.importOrLoad();
    // ... but for the hybrid mode we can customize the route calculation even at request time:
    // 1. a request with default preferences
    GHRequest req = new GHRequest().setProfile("car_custom").addPoint(new GHPoint(42.506472, 1.522475)).addPoint(new GHPoint(42.513108, 1.536005));
    GHResponse res = hopper.route(req);
    if (res.hasErrors())
        throw new RuntimeException(res.getErrors().toString());
    assert Math.round(res.getBest().getTime() / 1000d) == 96;
    // 2. now avoid primary roads and reduce maximum speed, see docs/core/custom-models.md for an in-depth explanation
    // and also the blog posts https://www.graphhopper.com/?s=customizable+routing
    CustomModel model = new CustomModel();
    model.addToPriority(If("road_class == PRIMARY", MULTIPLY, 0.5));
    // unconditional limit to 100km/h
    model.addToPriority(If("true", LIMIT, 100));
    req.setCustomModel(model);
    res = hopper.route(req);
    if (res.hasErrors())
        throw new RuntimeException(res.getErrors().toString());
    assert Math.round(res.getBest().getTime() / 1000d) == 165;
}
Also used : GHRequest(com.graphhopper.GHRequest) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) LMProfile(com.graphhopper.config.LMProfile) GraphHopper(com.graphhopper.GraphHopper) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse)

Example 69 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class RoutingAlgorithmWithOSMTest method testMonacoFoot3D.

@Test
public void testMonacoFoot3D() {
    // most routes have same number of points as testMonaceFoot results but longer distance due to elevation difference
    List<Query> queries = createMonacoFoot();
    queries.get(0).getPoints().get(1).expectedDistance = 1627;
    queries.get(2).getPoints().get(1).expectedDistance = 2250;
    queries.get(3).getPoints().get(1).expectedDistance = 1482;
    // or slightly longer tour with less nodes: list.get(1).setDistance(1, 3610);
    queries.get(1).getPoints().get(1).expectedDistance = 3573;
    queries.get(1).getPoints().get(1).expectedPoints = 149;
    GraphHopper hopper = createHopper(MONACO, new Profile("foot").setVehicle("foot").setWeighting("shortest"));
    hopper.setElevationProvider(new SRTMProvider(DIR));
    hopper.importOrLoad();
    checkQueries(hopper, queries);
}
Also used : GraphHopper(com.graphhopper.GraphHopper) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) SRTMProvider(com.graphhopper.reader.dem.SRTMProvider) Test(org.junit.jupiter.api.Test)

Example 70 with GraphHopper

use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.

the class RoutingAlgorithmWithOSMTest method testKremsBikeRelation.

@Test
public void testKremsBikeRelation() {
    List<Query> queries = new ArrayList<>();
    queries.add(new Query(48.409523, 15.602394, 48.375466, 15.72916, 12491, 159));
    queries.add(new Query(48.410061, 15.63951, 48.411386, 15.604899, 3077, 79));
    queries.add(new Query(48.412294, 15.62007, 48.398306, 15.609667, 3965, 94));
    GraphHopper hopper = createHopper(KREMS, new Profile("bike").setVehicle("bike").setWeighting("fastest"));
    hopper.importOrLoad();
    checkQueries(hopper, queries);
    hopper.getGraphHopperStorage();
    Helper.removeDir(new File(GH_LOCATION));
    hopper = createHopper(KREMS, new Profile("bike").setVehicle("bike").setWeighting("fastest"), new Profile("car").setVehicle("car").setWeighting("fastest"));
    hopper.importOrLoad();
    checkQueries(hopper, queries);
}
Also used : ArrayList(java.util.ArrayList) GraphHopper(com.graphhopper.GraphHopper) File(java.io.File) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test)

Aggregations

GraphHopper (com.graphhopper.GraphHopper)101 Test (org.junit.jupiter.api.Test)62 Profile (com.graphhopper.config.Profile)52 LMProfile (com.graphhopper.config.LMProfile)44 CHProfile (com.graphhopper.config.CHProfile)39 GraphHopperTest (com.graphhopper.GraphHopperTest)25 ArrayList (java.util.ArrayList)24 File (java.io.File)16 Test (org.junit.Test)11 SRTMProvider (com.graphhopper.reader.dem.SRTMProvider)10 GHRequest (com.graphhopper.GHRequest)7 GHResponse (com.graphhopper.GHResponse)7 GraphHopperConfig (com.graphhopper.GraphHopperConfig)6 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)6 EncodingManager (com.graphhopper.routing.util.EncodingManager)6 PMap (com.graphhopper.util.PMap)6 GHPoint (com.graphhopper.util.shapes.GHPoint)5 IOException (java.io.IOException)5 Gpx (com.graphhopper.jackson.Gpx)4 MapMatching (com.graphhopper.matching.MapMatching)4