Search in sources :

Example 1 with GridTraversor

use of au.gov.amsa.geo.model.GridTraversor 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);
            }
        }
    });
}
Also used : Optional(java.util.Optional) HasPosition(au.gov.amsa.risky.format.HasPosition) Position(au.gov.amsa.util.navigation.Position) GridTraversor(au.gov.amsa.geo.model.GridTraversor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

GridTraversor (au.gov.amsa.geo.model.GridTraversor)1 HasPosition (au.gov.amsa.risky.format.HasPosition)1 Position (au.gov.amsa.util.navigation.Position)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (java.util.Optional)1