Search in sources :

Example 1 with Instruction

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

the class GraphHopperWebIT method readRoundabout.

@Test
public void readRoundabout() {
    GHRequest req = new GHRequest().addPoint(new GHPoint(52.261434, 13.485718)).addPoint(new GHPoint(52.399067, 13.469238));
    GHResponse res = gh.route(req);
    int counter = 0;
    for (Instruction i : res.getBest().getInstructions()) {
        if (i instanceof RoundaboutInstruction) {
            counter++;
            RoundaboutInstruction ri = (RoundaboutInstruction) i;
            assertEquals("turn_angle was incorrect:" + ri.getTurnAngle(), -1.5, ri.getTurnAngle(), 0.1);
            // This route contains only one roundabout and no (via) point in a roundabout
            assertEquals("exited was incorrect:" + ri.isExited(), ri.isExited(), true);
        }
    }
    assertTrue("no roundabout in route?", counter > 0);
}
Also used : GHRequest(com.graphhopper.GHRequest) RoundaboutInstruction(com.graphhopper.util.RoundaboutInstruction) Instruction(com.graphhopper.util.Instruction) RoundaboutInstruction(com.graphhopper.util.RoundaboutInstruction) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 2 with Instruction

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

the class NaviEngine method calculatePosition.

@WorkerThread
private NaviInstruction calculatePosition(GeoPoint curPos) {
    if (uiJob == UiJob.RecalcPath) {
        return null;
    }
    if (uiJob == UiJob.Finished) {
        return null;
    }
    nearestP.setBaseData(getNearestPoint(instructions.get(0), nearestP.arrPos, curPos));
    if (nearestP.arrPos > 0) {
        // Check dist to line (backward)
        double lat1 = instructions.get(0).getPoints().getLatitude(nearestP.arrPos);
        double lon1 = instructions.get(0).getPoints().getLongitude(nearestP.arrPos);
        double lat2 = instructions.get(0).getPoints().getLatitude(nearestP.arrPos - 1);
        double lon2 = instructions.get(0).getPoints().getLongitude(nearestP.arrPos - 1);
        double lDist = GeoMath.distToLineSegment(curPos.getLatitude(), curPos.getLongitude(), lat1, lon1, lat2, lon2);
        if (lDist < nearestP.distance) {
            nearestP.distance = lDist;
            nearestP.status = PointPosData.Status.CurPosIsBackward;
        }
    }
    if (nearestP.arrPos < instructions.get(0).getPoints().size() - 1) {
        // Check dist to line (forward)
        double lat1 = instructions.get(0).getPoints().getLatitude(nearestP.arrPos);
        double lon1 = instructions.get(0).getPoints().getLongitude(nearestP.arrPos);
        double lat2 = instructions.get(0).getPoints().getLatitude(nearestP.arrPos + 1);
        double lon2 = instructions.get(0).getPoints().getLongitude(nearestP.arrPos + 1);
        double lDist = GeoMath.distToLineSegment(curPos.getLatitude(), curPos.getLongitude(), lat1, lon1, lat2, lon2);
        if (lDist < nearestP.distance) {
            nearestP.distance = lDist;
            nearestP.status = PointPosData.Status.CurPosIsForward;
        }
    } else if (nearestP.arrPos == instructions.get(0).getPoints().size() - 1 && instructions.size() > 1) {
        if (instructions.get(1).getPoints().size() > 0) {
            // Check dist to line (forward to next instruction)
            double lat1 = instructions.get(0).getPoints().getLatitude(nearestP.arrPos);
            double lon1 = instructions.get(0).getPoints().getLongitude(nearestP.arrPos);
            double lat2 = instructions.get(1).getPoints().getLatitude(0);
            double lon2 = instructions.get(1).getPoints().getLongitude(0);
            double lDist = GeoMath.distToLineSegment(curPos.getLatitude(), curPos.getLongitude(), lat1, lon1, lat2, lon2);
            if (lDist < nearestP.distance) {
                nearestP.distance = lDist;
                nearestP.status = PointPosData.Status.CurPosIsForward;
            }
        }
        if (instructions.get(1).getPoints().size() > 1) {
            // Check dist to line (forward next instruction p1+p2)
            double lat1 = instructions.get(1).getPoints().getLatitude(0);
            double lon1 = instructions.get(1).getPoints().getLongitude(0);
            double lat2 = instructions.get(1).getPoints().getLatitude(1);
            double lon2 = instructions.get(1).getPoints().getLongitude(1);
            double lDist = GeoMath.distToLineSegment(curPos.getLatitude(), curPos.getLongitude(), lat1, lon1, lat2, lon2);
            if (lDist < nearestP.distance) {
                nearestP.distance = lDist;
                nearestP.status = PointPosData.Status.CurPosIsForwardNext;
            }
        }
    }
    if (nearestP.isForward()) {
        log("Reset bearing with calculatePosition()");
        nearestP.resetDirectionOk();
    }
    if (!nearestP.isDistanceOk()) {
        Instruction nearestNext = instructions.find(curPos.getLatitude(), curPos.getLongitude(), MAX_WAY_TOLERANCE_METER);
        if (nearestNext == null) {
            GeoPoint closestP = findClosestStreet(curPos);
            nearestNext = instructions.find(closestP.getLatitude(), closestP.getLongitude(), MAX_WAY_TOLERANCE_METER);
        }
        if (nearestNext == null) {
            uiJob = UiJob.RecalcPath;
            recalcFrom = curPos;
            Instruction lastInstruction = instructions.get(instructions.size() - 1);
            int lastPoint = lastInstruction.getPoints().size() - 1;
            double lastPointLat = lastInstruction.getPoints().getLat(lastPoint);
            double lastPointLon = lastInstruction.getPoints().getLon(lastPoint);
            recalcTo = new GeoPoint(lastPointLat, lastPointLon);
            log("NaviTask Start recalc !!!!!!");
            return null;
        } else {
            // Forward to nearest instruction.
            int deleteCounter = 0;
            Instruction lastDeleted = null;
            while (instructions.size() > 0 && !instructions.get(0).equals(nearestNext)) {
                deleteCounter++;
                lastDeleted = instructions.remove(0);
            }
            if (lastDeleted != null) {
                // Because we need the current, and not the next Instruction
                instructions.add(0, lastDeleted);
                deleteCounter--;
            }
            if (deleteCounter == 0) {
                PointPosData newNearestP = getNearestPoint(instructions.get(0), 0, curPos);
                // TODO: Continue-Instruction with DirectionInfo: getContinueInstruction() ?
                log("NaviTask Start update far !!!!!!");
                return getUpdatedInstruction(curPos, newNearestP);
            }
            log("NaviTask Start update skip-mult-" + deleteCounter + " !!!!!!");
            return getNewInstruction();
        }
    } else if (nearestP.isForwardNext()) {
        instructions.remove(0);
        log("NaviTask Start skip-next !!!!!!");
        return getNewInstruction();
    } else {
        log("NaviTask Start update !!!!!!");
        return getUpdatedInstruction(curPos, nearestP);
    }
}
Also used : GeoPoint(org.oscim.core.GeoPoint) Instruction(com.graphhopper.util.Instruction) GeoPoint(org.oscim.core.GeoPoint) WorkerThread(android.support.annotation.WorkerThread)

Example 3 with Instruction

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

the class NaviEngine method getNewInstruction.

private NaviInstruction getNewInstruction() {
    nearestP.arrPos = 0;
    nearestP.distance = Double.MAX_VALUE;
    uiJob = UiJob.UpdateInstruction;
    if (instructions.size() > 0) {
        Instruction in = instructions.get(0);
        long fullTime = countFullTime(in.getTime());
        GeoPoint curPos = new GeoPoint(in.getPoints().getLat(0), in.getPoints().getLon(0));
        double partDistance = countPartDistance(curPos, in, 0);
        if (partDistance == 0) {
            partDistanceScaler = 1;
        } else {
            partDistanceScaler = in.getDistance() / partDistance;
        }
        Instruction nextIn = null;
        if (instructions.size() > 1) {
            nextIn = instructions.get(1);
        }
        NaviInstruction nIn = new NaviInstruction(in, nextIn, fullTime);
        if (speakDistanceCheck(in.getDistance()) && nearestP.isDirectionOk()) {
            naviVoice.speak(nIn.getVoiceText());
            naviVoiceSpoken = true;
        } else {
            naviVoiceSpoken = false;
        }
        return nIn;
    }
    uiJob = UiJob.Finished;
    return null;
}
Also used : GeoPoint(org.oscim.core.GeoPoint) Instruction(com.graphhopper.util.Instruction)

Example 4 with Instruction

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

the class InstructionListSerializer method serialize.

@Override
public void serialize(InstructionList instructions, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    List<Map<String, Object>> instrList = new ArrayList<>(instructions.size());
    int pointsIndex = 0;
    for (Instruction instruction : instructions) {
        Map<String, Object> instrJson = new HashMap<>();
        instrList.add(instrJson);
        instrJson.put("text", Helper.firstBig(instruction.getTurnDescription(instructions.getTr())));
        instrJson.put("street_name", instruction.getName());
        instrJson.put("time", instruction.getTime());
        instrJson.put("distance", Helper.round(instruction.getDistance(), 3));
        instrJson.put("sign", instruction.getSign());
        instrJson.putAll(instruction.getExtraInfoJSON());
        int tmpIndex = pointsIndex + instruction.getLength();
        instrJson.put("interval", Arrays.asList(pointsIndex, tmpIndex));
        pointsIndex = tmpIndex;
    }
    jsonGenerator.writeObject(instrList);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Instruction(com.graphhopper.util.Instruction) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with Instruction

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

the class RouteResourceClientHCTest method readRoundabout.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void readRoundabout(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.509644, 1.532958)).addPoint(new GHPoint(42.510383, 1.533392)).putHint("vehicle", "car");
    GHResponse res = gh.route(req);
    int counter = 0;
    for (Instruction i : res.getBest().getInstructions()) {
        if (i instanceof RoundaboutInstruction) {
            counter++;
            RoundaboutInstruction ri = (RoundaboutInstruction) i;
            assertEquals(-5, ri.getTurnAngle(), 0.1, "turn_angle was incorrect:" + ri.getTurnAngle());
            // This route contains only one roundabout and no (via) point in a roundabout
            assertTrue(ri.isExited(), "exited was incorrect:" + ri.isExited());
        }
    }
    assertTrue(counter > 0, "no roundabout in route?");
}
Also used : GHRequest(com.graphhopper.GHRequest) RoundaboutInstruction(com.graphhopper.util.RoundaboutInstruction) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) Instruction(com.graphhopper.util.Instruction) RoundaboutInstruction(com.graphhopper.util.RoundaboutInstruction) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Instruction (com.graphhopper.util.Instruction)9 GHRequest (com.graphhopper.GHRequest)3 GHResponse (com.graphhopper.GHResponse)3 GHPoint (com.graphhopper.util.shapes.GHPoint)3 GeoPoint (org.oscim.core.GeoPoint)3 RoundaboutInstruction (com.graphhopper.util.RoundaboutInstruction)2 Location (android.location.Location)1 WorkerThread (android.support.annotation.WorkerThread)1 ResponsePath (com.graphhopper.ResponsePath)1 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)1 InstructionList (com.graphhopper.util.InstructionList)1 PointList (com.graphhopper.util.PointList)1 PathDetail (com.graphhopper.util.details.PathDetail)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 EnumSource (org.junit.jupiter.params.provider.EnumSource)1