Search in sources :

Example 1 with Rectangle

use of com.github.davidmoten.rtree.geometry.Rectangle in project risky by amsa-code.

the class DriftingLayer method render.

@Override
public void render(Graphics2D g, WmsRequest request) {
    log.info("request=" + request);
    log.info("drawing " + queue.size() + " positions");
    final Projector projector = WmsUtil.getProjector(request);
    Position a = projector.toPosition(0, 0);
    Position b = projector.toPosition(request.getWidth(), request.getHeight());
    Rectangle r = Geometries.rectangle(a.getLon(), b.getLat(), b.getLon(), a.getLat());
    Optional<VesselPosition> last = Optional.empty();
    Optional<Point> lastPoint = Optional.empty();
    // Iterable<VesselPosition> positions = tree
    // .search(r)
    // .map(new Func1<Entry<VesselPosition,
    // com.github.davidmoten.rtree.geometry.Point>, VesselPosition>() {
    // 
    // @Override
    // public VesselPosition call(
    // Entry<VesselPosition, com.github.davidmoten.rtree.geometry.Point>
    // entry) {
    // return entry.value();
    // }
    // 
    // }).toBlocking().toIterable();
    ConcurrentLinkedQueue<VesselPosition> positions = queue;
    Point startPoint = null;
    for (VesselPosition p : positions) {
        // expecting positions to be in mmsi, time order
        Point point = projector.toPoint(p.lat(), p.lon());
        if (last.isPresent() && p.id().equals(last.get().id()) && p.data().isPresent() && !p.data().get().equals(p.time()) && isOkMovement(p, last.get())) {
            // join the last position with this one with a line
            g.setColor(Color.gray);
            g.drawLine(lastPoint.get().x, lastPoint.get().y, point.x, point.y);
        }
        if (p.data().get().equals(p.time()) || (last.isPresent() && !isOkMovement(p, last.get()))) {
            g.setColor(Color.red);
            g.drawRect(point.x, point.y, 1, 1);
            startPoint = point;
        } else if (startPoint != null) {
            // draw intermediate point
            g.setColor(Color.darkGray);
            g.drawRect(point.x, point.y, 1, 1);
            // redraw startPoint so that a slightly moving drift doesn't
            // overdraw the startPoint with the color of an intermediate
            // point
            g.setColor(Color.red);
            g.drawRect(startPoint.x, startPoint.y, 1, 1);
        }
        last = Optional.of(p);
        lastPoint = Optional.of(point);
    }
    log.info("drawn");
}
Also used : Projector(com.github.davidmoten.grumpy.projection.Projector) VesselPosition(au.gov.amsa.navigation.VesselPosition) Position(com.github.davidmoten.grumpy.core.Position) Rectangle(com.github.davidmoten.rtree.geometry.Rectangle) Point(java.awt.Point) VesselPosition(au.gov.amsa.navigation.VesselPosition)

Example 2 with Rectangle

use of com.github.davidmoten.rtree.geometry.Rectangle in project risky by amsa-code.

the class CollisionDetector method toCollisionCandidatesForPosition.

private static Observable<CollisionCandidate> toCollisionCandidatesForPosition(State state) {
    final VesselPosition p = state.last().get();
    final Optional<VesselPosition> next = state.nextPosition();
    // use the spatial index to get positions physically near the latest
    // position report
    // setup a region around the latest position report to search with a
    // decent delta).
    double longitudeDelta = longitudeDelta(p.lat());
    Rectangle searchRegion = rectangle(p.lon() - longitudeDelta, p.lat() - LATITUDE_DELTA, p.lon() + longitudeDelta, p.lat() + LATITUDE_DELTA);
    // find nearby vessels within time constraints and cache them
    Observable<VesselPosition> near = state.tree().search(searchRegion).map(toVesselPosition).filter(aroundInTime(p, MAX_TIME_INTERVAL_MS));
    final Observable<TreeSet<VesselPosition>> othersByVessel = near.filter(not(isVessel(p.id()))).groupBy(byId()).flatMap(toSortedSet());
    Observable<CollisionCandidate> collisionCandidates = othersByVessel.flatMap(toCollisionCandidates2(p, next));
    return collisionCandidates;
}
Also used : TreeSet(java.util.TreeSet) Rectangle(com.github.davidmoten.rtree.geometry.Rectangle)

Aggregations

Rectangle (com.github.davidmoten.rtree.geometry.Rectangle)2 VesselPosition (au.gov.amsa.navigation.VesselPosition)1 Position (com.github.davidmoten.grumpy.core.Position)1 Projector (com.github.davidmoten.grumpy.projection.Projector)1 Point (java.awt.Point)1 TreeSet (java.util.TreeSet)1