Search in sources :

Example 6 with InstructionList

use of com.graphhopper.util.InstructionList in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method readFinishInstruction.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void readFinishInstruction(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.507065, 1.529846)).addPoint(new GHPoint(42.510383, 1.533392)).putHint("vehicle", "car");
    GHResponse res = gh.route(req);
    InstructionList instructions = res.getBest().getInstructions();
    String finishInstructionName = instructions.get(instructions.size() - 1).getName();
    assertEquals("Arrive at destination", finishInstructionName);
}
Also used : InstructionList(com.graphhopper.util.InstructionList) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with InstructionList

use of com.graphhopper.util.InstructionList 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 8 with InstructionList

use of com.graphhopper.util.InstructionList in project PocketMaps by junjunguo.

the class TargetDirComputer method createTargetdirResponse.

public GHResponse createTargetdirResponse(double fromLat, double fromLon, double toLat, double toLon) {
    GHResponse resp = new GHResponse();
    PathWrapper pr = new PathWrapper();
    PointList pl = new PointList();
    pl.add(fromLat, fromLon);
    pl.add(toLat, toLon);
    pr.setPoints(pl);
    double distance = GeoMath.fastDistance(fromLat, fromLon, toLat, toLon);
    distance = distance * GeoMath.METER_PER_DEGREE;
    pr.setDistance(distance);
    double distKm = distance / 1000.0;
    // 50km == 1h
    double hours = distKm / 50;
    if (Variable.getVariable().getTravelMode() == Variable.TravelMode.Bike) {
        hours = hours * 3.0;
    }
    if (Variable.getVariable().getTravelMode() == Variable.TravelMode.Foot) {
        hours = hours * 9.0;
    }
    long timeMs = (long) (hours * 60.0 * 60.0 * 1000.0);
    pr.setTime(timeMs);
    InstructionList insL = new InstructionList(createEmptyTranslator());
    int sign = Instruction.CONTINUE_ON_STREET;
    String name = "direction to target";
    InstructionAnnotation ia = InstructionAnnotation.EMPTY;
    Instruction ins = new Instruction(sign, name, ia, pl);
    ins.setDistance(distance);
    ins.setTime(timeMs);
    insL.add(ins);
    pr.setInstructions(insL);
    resp.add(pr);
    return resp;
}
Also used : PointList(com.graphhopper.util.PointList) PathWrapper(com.graphhopper.PathWrapper) InstructionList(com.graphhopper.util.InstructionList) Instruction(com.graphhopper.util.Instruction) GHResponse(com.graphhopper.GHResponse) InstructionAnnotation(com.graphhopper.util.InstructionAnnotation)

Aggregations

InstructionList (com.graphhopper.util.InstructionList)8 GHResponse (com.graphhopper.GHResponse)7 GHRequest (com.graphhopper.GHRequest)6 GHPoint (com.graphhopper.util.shapes.GHPoint)6 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)3 PointList (com.graphhopper.util.PointList)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 PathWrapper (com.graphhopper.PathWrapper)2 ResponsePath (com.graphhopper.ResponsePath)2 Instruction (com.graphhopper.util.Instruction)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 InstructionAnnotation (com.graphhopper.util.InstructionAnnotation)1 PathDetail (com.graphhopper.util.details.PathDetail)1 GHException (com.graphhopper.util.exceptions.GHException)1 BBox (com.graphhopper.util.shapes.BBox)1