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);
}
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);
}
}
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;
}
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);
}
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?");
}
Aggregations