use of au.gov.amsa.util.navigation.Position in project risky by amsa-code.
the class DistanceTravelledCalculator method getCellDistances.
@VisibleForTesting
static final Observable<CellAndDistance> getCellDistances(final Position a, final Position b, final Options options) {
return Observable.create(new OnSubscribe<CellAndDistance>() {
@Override
public void call(Subscriber<? super CellAndDistance> subscriber) {
try {
GridTraversor grid = new GridTraversor(options);
boolean keepGoing = true;
Position p1 = a;
Position destination = b;
int count = 0;
while (keepGoing) {
Position p2 = grid.nextPoint(p1, destination);
double distanceNm = p1.getDistanceToKm(p2) / 1.852;
// report cell and distance
Optional<Cell> cell = Cell.cellAt(p1.getLat(), p1.getLon(), options);
if (cell.isPresent())
subscriber.onNext(new CellAndDistance(cell.get(), distanceNm));
keepGoing = p2.getLat() != destination.getLat() || p2.getLon() != destination.getLon();
keepGoing = keepGoing && !subscriber.isUnsubscribed();
p1 = p2;
count++;
checkCount(p1, destination, count, options);
}
subscriber.onCompleted();
} catch (Throwable t) {
// TODO resolve all problems so that this will revert to a
// call to onError
log.warn(t.getMessage(), t);
subscriber.onCompleted();
// subscriber.onError(t);
}
}
});
}
use of au.gov.amsa.util.navigation.Position in project risky by amsa-code.
the class GridTraversor method nextPointCrossingLatitude.
private Position nextPointCrossingLatitude(Position a, Position b, double leftLon, double rightLon, double targetLat, double bearingDegrees) {
LongitudePair lonCrossingCandidates = a.getLongitudeOnGreatCircle(b, targetLat);
if (lonCrossingCandidates != null) {
double lonCrossing;
// choose candidate closest in longitude to point a along path to b
boolean candidate1ok = leftLon <= lonCrossingCandidates.getLon1() && lonCrossingCandidates.getLon1() <= rightLon && !(lonCrossingCandidates.getLon1() == a.getLon() && targetLat == a.getLat());
boolean candidate2ok = leftLon <= lonCrossingCandidates.getLon2() && lonCrossingCandidates.getLon2() <= rightLon && !(lonCrossingCandidates.getLon2() == a.getLon() && targetLat == a.getLat());
if (candidate1ok && candidate2ok) {
// choose the best of the candidates
// no units used in calculations because only doing comparisons
// so don't care if it's km or nautical miles
double distance1ToB = new Position(targetLat, lonCrossingCandidates.getLon1()).getDistanceToKm(b);
double distance2ToB = new Position(targetLat, lonCrossingCandidates.getLon2()).getDistanceToKm(b);
double distanceAToB = a.getDistanceToKm(b);
if (distance1ToB > distanceAToB)
candidate1ok = false;
if (distance2ToB > distanceAToB)
candidate2ok = false;
if (candidate1ok && candidate2ok) {
if (distance1ToB < distance2ToB) {
candidate1ok = false;
} else
candidate2ok = false;
}
if (candidate1ok)
lonCrossing = lonCrossingCandidates.getLon1();
else if (candidate2ok)
lonCrossing = lonCrossingCandidates.getLon2();
else
// neither of the candidates are on the way to b!
return null;
} else if (candidate1ok) {
lonCrossing = lonCrossingCandidates.getLon1();
} else if (candidate2ok)
lonCrossing = lonCrossingCandidates.getLon2();
else
return null;
// check that lonCrossing is on the segment a to b
double bearingDegreesTest = bearingDegrees(a, Position.create(targetLat, lonCrossing));
double diff = Position.getBearingDifferenceDegrees(bearingDegrees, bearingDegreesTest);
if (Math.abs(diff) > 90)
return null;
return Position.create(targetLat, lonCrossing);
} else
return null;
}
use of au.gov.amsa.util.navigation.Position in project risky by amsa-code.
the class GridTraversorTest method testGridNextPointOnBottomEdgeStartingInsideCell.
/**
**********************************************************************
* Test going from (-30.8,140.2) to (-31.7,142.6) on a single degree grid
* and back again!
***********************************************************************
*/
@Test
public void testGridNextPointOnBottomEdgeStartingInsideCell() {
GridTraversor grid = createGrid();
Position a = Position.create(-30.8, 140.2);
Position b = Position.create(-31.7, 142.6);
assertEquals(114.303575, a.getBearingDegrees(b), PRECISION);
Position p = grid.nextPoint(a, b);
assertEquals(-31, p.getLat(), PRECISION);
assertEquals(140.719351, p.getLon(), PRECISION);
}
use of au.gov.amsa.util.navigation.Position in project risky by amsa-code.
the class GridTraversorTest method testGridNextPointOnDestinationStartingBottomEdge.
@Test
public void testGridNextPointOnDestinationStartingBottomEdge() {
GridTraversor grid = createGrid();
Position a = Position.create(-31.7, 142.6);
Position b = Position.create(-30.8, 140.2);
Position p = grid.nextPoint(a, b);
Position p2 = grid.nextPoint(p, b);
Position p3 = grid.nextPoint(p2, b);
System.out.println("p3lat=" + p3.getLat());
Position p4 = grid.nextPoint(p3, b);
assertEquals(-30.8, p4.getLat(), PRECISION);
assertEquals(140.2, p4.getLon(), PRECISION);
}
use of au.gov.amsa.util.navigation.Position in project risky by amsa-code.
the class GridTraversorTest method testGridNextPointOnRightEdgeStartingLeftEdge.
@Test
public void testGridNextPointOnRightEdgeStartingLeftEdge() {
GridTraversor grid = createGrid();
Position a = Position.create(-30.8, 140.2);
Position b = Position.create(-31.7, 142.6);
Position p = grid.nextPoint(a, b);
Position p2 = grid.nextPoint(p, b);
Position p3 = grid.nextPoint(p2, b);
assertEquals(-31.480781, p3.getLat(), PRECISION);
assertEquals(142.0, p3.getLon(), PRECISION);
}
Aggregations