Search in sources :

Example 26 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class Examples method routing.

@Test
public void routing() {
    // Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
    GraphHopperWeb gh = new GraphHopperWeb();
    // insert your key here
    gh.setKey(apiKey);
    // change timeout, default is 5 seconds
    gh.setDownloader(new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build());
    // specify at least two coordinates
    GHRequest req = new GHRequest().addPoint(new GHPoint(49.6724, 11.3494)).addPoint(new GHPoint(49.6550, 11.4180));
    // Set vehicle like car, bike, foot, ...
    req.putHint("vehicle", "bike");
    // Optionally enable/disable elevation in output PointList, currently bike and foot support elevation, default is false
    req.putHint("elevation", false);
    // Optionally enable/disable turn instruction information, defaults is true
    req.putHint("instructions", true);
    // Optionally enable/disable path geometry information, default is true
    req.putHint("calc_points", true);
    // note: turn off instructions and calcPoints if you just need the distance or time
    // information to make calculation and transmission faster
    // Optionally set specific locale for instruction information, supports already over 25 languages,
    // defaults to English
    req.setLocale(Locale.GERMAN);
    // Optionally add path details
    req.setPathDetails(Arrays.asList(Parameters.Details.STREET_NAME, Parameters.Details.AVERAGE_SPEED, Parameters.Details.EDGE_ID));
    GHResponse fullRes = gh.route(req);
    if (fullRes.hasErrors())
        throw new RuntimeException(fullRes.getErrors().toString());
    // get best path (you will get more than one path here if you requested algorithm=alternative_route)
    ResponsePath res = fullRes.getBest();
    // get path geometry information (latitude, longitude and optionally elevation)
    PointList pl = res.getPoints();
    // distance of the full path, in meter
    double distance = res.getDistance();
    // time of the full path, in milliseconds
    long millis = res.getTime();
    // get information per turn instruction
    InstructionList il = res.getInstructions();
    for (Instruction i : il) {
    // System.out.println(i.getName());
    }
    // get path details
    List<PathDetail> pathDetails = res.getPathDetails().get(Parameters.Details.STREET_NAME);
    for (PathDetail detail : pathDetails) {
    // System.out.println(detail.getValue());
    }
}
Also used : PointList(com.graphhopper.util.PointList) InstructionList(com.graphhopper.util.InstructionList) Instruction(com.graphhopper.util.Instruction) GHResponse(com.graphhopper.GHResponse) ResponsePath(com.graphhopper.ResponsePath) PathDetail(com.graphhopper.util.details.PathDetail) GHRequest(com.graphhopper.GHRequest) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test)

Example 27 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class OSMReaderTest method testRoadClassInfo.

@Test
public void testRoadClassInfo() {
    GraphHopper gh = new GraphHopper() {

        @Override
        protected File _getOSMFile() {
            return new File(getClass().getResource(file2).getFile());
        }
    }.setOSMFile("dummy").setProfiles(new Profile("profile").setVehicle("car").setWeighting("fastest")).setMinNetworkSize(0).setGraphHopperLocation(dir).importOrLoad();
    GHResponse response = gh.route(new GHRequest(51.2492152, 9.4317166, 52.133, 9.1).setProfile("profile").setPathDetails(Collections.singletonList(RoadClass.KEY)));
    List<PathDetail> list = response.getBest().getPathDetails().get(RoadClass.KEY);
    assertEquals(3, list.size());
    assertEquals(RoadClass.MOTORWAY.toString(), list.get(0).getValue());
    response = gh.route(new GHRequest(51.2492152, 9.4317166, 52.133, 9.1).setProfile("profile").setPathDetails(Collections.singletonList(Toll.KEY)));
    Throwable ex = response.getErrors().get(0);
    assertTrue(ex.getMessage().contains("You requested the details [toll]"), ex.getMessage());
}
Also used : PathDetail(com.graphhopper.util.details.PathDetail) GHRequest(com.graphhopper.GHRequest) GraphHopper(com.graphhopper.GraphHopper) File(java.io.File) GHResponse(com.graphhopper.GHResponse) Profile(com.graphhopper.config.Profile) Test(org.junit.jupiter.api.Test) GraphHopperTest(com.graphhopper.GraphHopperTest)

Example 28 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class GraphHopperTest method simplifyWithInstructionsAndPathDetails.

@Test
public void simplifyWithInstructionsAndPathDetails() {
    final String profile = "profile";
    GraphHopper hopper = new GraphHopper();
    hopper.getEncodingManagerBuilder().setEnableInstructions(true).add(new OSMMaxSpeedParser()).add(new CarFlagEncoder());
    hopper.setOSMFile(BAYREUTH).setProfiles(new Profile(profile).setVehicle("car").setWeighting("fastest")).setGraphHopperLocation(GH_LOCATION);
    hopper.importOrLoad();
    GHRequest req = new GHRequest().addPoint(new GHPoint(50.026932, 11.493201)).addPoint(new GHPoint(50.016895, 11.4923)).addPoint(new GHPoint(50.003464, 11.49157)).setProfile(profile).setPathDetails(Arrays.asList("street_name", "max_speed"));
    req.putHint("elevation", true);
    GHResponse rsp = hopper.route(req);
    assertFalse(rsp.hasErrors(), rsp.getErrors().toString());
    ResponsePath path = rsp.getBest();
    // check path was simplified (without it would be more like 58)
    assertEquals(41, path.getPoints().size());
    // check instructions
    InstructionList instructions = path.getInstructions();
    int totalLength = 0;
    for (Instruction instruction : instructions) {
        totalLength += instruction.getLength();
    }
    assertEquals(40, totalLength);
    assertInstruction(instructions.get(0), "KU 11", "[0, 4[", 4, 4);
    assertInstruction(instructions.get(1), "B 85", "[4, 16[", 12, 12);
    // via instructions have length = 0, but the point list must not be empty!
    assertInstruction(instructions.get(2), "", "[16, 17[", 0, 1);
    assertInstruction(instructions.get(3), "B 85", "[16, 32[", 16, 16);
    assertInstruction(instructions.get(4), "", "[32, 34[", 2, 2);
    assertInstruction(instructions.get(5), "KU 18", "[34, 37[", 3, 3);
    assertInstruction(instructions.get(6), "St 2189", "[37, 38[", 1, 1);
    assertInstruction(instructions.get(7), "", "[38, 40[", 2, 2);
    // finish instructions have length = 0, but the point list must not be empty!
    assertInstruction(instructions.get(8), "", "[40, 41[", 0, 1);
    // check max speeds
    List<PathDetail> speeds = path.getPathDetails().get("max_speed");
    assertDetail(speeds.get(0), "null [0, 4]");
    assertDetail(speeds.get(1), "70.0 [4, 6]");
    assertDetail(speeds.get(2), "100.0 [6, 31]");
    assertDetail(speeds.get(3), "80.0 [31, 32]");
    assertDetail(speeds.get(4), "null [32, 37]");
    assertDetail(speeds.get(5), "50.0 [37, 38]");
    assertDetail(speeds.get(6), "null [38, 40]");
    // check street_names
    List<PathDetail> streetNames = path.getPathDetails().get("street_name");
    assertDetail(streetNames.get(0), "KU 11 [0, 4]");
    assertDetail(streetNames.get(1), "B 85 [4, 32]");
    assertDetail(streetNames.get(2), " [32, 34]");
    assertDetail(streetNames.get(3), "KU 18 [34, 37]");
    assertDetail(streetNames.get(4), "St 2189 [37, 38]");
    assertDetail(streetNames.get(5), " [38, 40]");
}
Also used : CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) GHPoint(com.graphhopper.util.shapes.GHPoint) PathDetail(com.graphhopper.util.details.PathDetail) OSMMaxSpeedParser(com.graphhopper.routing.util.parsers.OSMMaxSpeedParser) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class PathSimplification method assertConsistencyOfPathDetails.

private static void assertConsistencyOfPathDetails(Map<String, List<PathDetail>> pathDetails) {
    for (Map.Entry<String, List<PathDetail>> pdEntry : pathDetails.entrySet()) {
        List<PathDetail> list = pdEntry.getValue();
        if (list.isEmpty())
            continue;
        PathDetail prevPD = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            if (prevPD.getLast() != list.get(i).getFirst())
                throw new IllegalStateException("PathDetail list " + pdEntry.getKey() + " is inconsistent due to entries " + prevPD + " vs. " + list.get(i));
            prevPD = list.get(i);
        }
    }
}
Also used : PathDetail(com.graphhopper.util.details.PathDetail) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 30 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class TripFromLabel method shift.

private Map<String, List<PathDetail>> shift(Map<String, List<PathDetail>> pathDetailss, int previousPointsCount) {
    return Maps.transformEntries(pathDetailss, (s, pathDetails) -> pathDetails.stream().map(p -> {
        PathDetail pathDetail = new PathDetail(p.getValue());
        pathDetail.setFirst(p.getFirst() + previousPointsCount);
        pathDetail.setLast(p.getLast() + previousPointsCount);
        return pathDetail;
    }).collect(Collectors.toList()));
}
Also used : PathDetail(com.graphhopper.util.details.PathDetail)

Aggregations

PathDetail (com.graphhopper.util.details.PathDetail)30 Test (org.junit.jupiter.api.Test)14 PathDetailsBuilderFactory (com.graphhopper.util.details.PathDetailsBuilderFactory)10 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)8 AbstractGraphStorageTester.assertPList (com.graphhopper.storage.AbstractGraphStorageTester.assertPList)8 GHPoint (com.graphhopper.util.shapes.GHPoint)8 GHRequest (com.graphhopper.GHRequest)7 GHResponse (com.graphhopper.GHResponse)7 ResponsePath (com.graphhopper.ResponsePath)5 List (java.util.List)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)3 PathDetailsFromEdges (com.graphhopper.util.details.PathDetailsFromEdges)3 GTFSFeed (com.conveyal.gtfs.GTFSFeed)2 Stop (com.conveyal.gtfs.model.Stop)2 StopTime (com.conveyal.gtfs.model.StopTime)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Iterables (com.google.common.collect.Iterables)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2