use of com.github.davidmoten.grumpy.core.Position 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");
}
use of com.github.davidmoten.grumpy.core.Position in project risky by amsa-code.
the class ShapefileUtil method findRegionCrossingPoint.
public static TimedPosition findRegionCrossingPoint(Shapefile region, Fix fix1, Fix fix2) {
Coordinate[] coords = new Coordinate[] { new Coordinate(fix1.lon(), fix1.lat()), new Coordinate(fix2.lon(), fix2.lat()) };
LineString line = new GeometryFactory().createLineString(coords);
for (PreparedGeometry g : region.geometries()) {
if (g.crosses(line)) {
Geometry intersection = g.getGeometry().intersection(line);
// expecting just one point
Coordinate coord = intersection.getCoordinate();
double longitude = coord.x;
double latitude = coord.y;
Position a = Position.create(fix1.lat(), fix1.lon());
Position b = Position.create(fix2.lat(), fix2.lon());
Position c = Position.create(latitude, longitude);
double ac = a.getDistanceToKm(c);
double bc = b.getDistanceToKm(c);
if (ac == 0) {
return new TimedPosition(fix1.lat(), fix1.lon(), fix1.time());
} else if (bc == 0) {
return new TimedPosition(fix2.lat(), fix2.lon(), fix2.time());
} else {
// predict the timestamp based on distance from a and b
long diff = fix2.time() - fix1.time();
long t = Math.round(fix1.time() + ac * diff / (ac + bc));
return new TimedPosition(latitude, longitude, t);
}
}
}
throw new RuntimeException("crossing not found");
}
Aggregations