/** * Return true if the two bounding box are in overlaps relation. * i.e. the two box intersect but are not contained in each other. * * @param boundingBox1 An envelope. * @param boundingBox2 An envelope. * * @return True if the two envelope are overlaping. */ public static boolean overlaps(final GeneralEnvelope boundingBox1, final GeneralEnvelope boundingBox2) { if ((boundingBox1.contains(boundingBox2, true) || boundingBox2.contains(boundingBox1, true))) return false; if (boundingBox1.intersects(boundingBox2, true)) return true; return false; }
/** * Returns {@code true} if the outer envelope contains the inner one. */ @Override boolean contains(GeneralEnvelope outer, GeneralEnvelope inner) { return outer.contains(inner); }
/** * Return the shortest orthodromic distance between a boundingBox and a point. * if the boundingBox contains the point it return 0. * * @param boundingBox A bounding box * @param point A point in the same CRS that the boundingBox. * @param units The units in which the distance will be expressed. * * @return the shortest distance between the bounding box and the point. */ public static double bboxToPointDistance(final GeneralEnvelope boundingBox, final GeneralDirectPosition point, final String units) { if (boundingBox.contains(point)) return 0; final List<Line2D> border = getBorder(boundingBox); double distance = Double.MAX_VALUE; for (Line2D l: border) { final double tempDistance = lineToPointDistance(l, point, units); if (tempDistance < distance) distance = tempDistance; } return distance; }
/** * Return true if the line crosses the envelope. * i.e. one point is inside the box, one is outside. * * @param boundingBox An envelope. * @param line A line2D * * @return True if the line crosses the envelope. */ public static boolean crosses(final GeneralEnvelope boundingBox, final Line2D line) { final CoordinateReferenceSystem crs = boundingBox.getCoordinateReferenceSystem(); final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); tempPoint1.setCoordinateReferenceSystem(crs); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); tempPoint2.setCoordinateReferenceSystem(crs); // for this case we look if the line have a point inside and a point outside if ((boundingBox.contains(tempPoint1) && !boundingBox.contains(tempPoint2)) || (!boundingBox.contains(tempPoint1) && boundingBox.contains(tempPoint2)) ) return true; return false; }
/** * Return true if the intersection between the envelope and the line is not empty. * * @param boundingBox An envelope. * @param line A line2D * * @return True f the intersection between the envelope and the point is empty. */ public static boolean intersect(final GeneralEnvelope boundingBox, final Line2D line) { final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); if (boundingBox.contains(tempPoint1) || boundingBox.contains(tempPoint2)) return true; for (Line2D l: getBorder(boundingBox)) { if (l.intersectsLine(line)) return true; } return false; }
/** * Return true if the intersection between the envelope and the line is empty. * * @param boundingBox An envelope. * @param line A line2D * * @return True f the intersection between the envelope and the point is empty. */ public static boolean disjoint(final GeneralEnvelope boundingBox, final Line2D line) { final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); if (!boundingBox.contains(tempPoint1) && !boundingBox.contains(tempPoint2)) { final List<Line2D> border = getBorder(boundingBox); for (Line2D l : border) { if (l.intersectsLine(line)) { return false; } } return true; } return false; }
/** * Return true if the bounding box contain the line. * i.e. the two point of the line are inside the box. * * @param boundingBox An envelope. * @param line A line2D * * @return True if the envelope contain the line. */ public static boolean contains(final GeneralEnvelope boundingBox, final Line2D line) { final CoordinateReferenceSystem crs = boundingBox.getCoordinateReferenceSystem(); final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); tempPoint1.setCoordinateReferenceSystem(crs); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); tempPoint2.setCoordinateReferenceSystem(crs); if ((boundingBox.contains(tempPoint1) && boundingBox.contains(tempPoint2))) return true; return false; }
/** * Return the shortest distance between a line and a boundingBox. * If the line intersect or is contains by the bounding box it return 0; * * @param line A line2D. * @param boundingBox A bounding box in the same CRS than the line * @param units The units in which the distance will be expressed. * @return The shortest distance between the bounding box and the line. */ public static double lineToBBoxDistance(final Line2D line, final GeneralEnvelope boundingBox, final String units) { final CoordinateReferenceSystem crs = boundingBox.getCoordinateReferenceSystem(); final GeneralDirectPosition tempPoint1 = new GeneralDirectPosition(line.getX1(), line.getY1()); tempPoint1.setCoordinateReferenceSystem(crs); final GeneralDirectPosition tempPoint2 = new GeneralDirectPosition(line.getX2(), line.getY2()); tempPoint2.setCoordinateReferenceSystem(crs); if (boundingBox.contains(tempPoint1) || boundingBox.contains(tempPoint2)) return 0; final List<Line2D> border = getBorder(boundingBox); double distance = Double.MAX_VALUE; for (Line2D l: border) { if (l.intersectsLine(line)) return 0; double tempDistance = lineTolineDistance(l, line, units); if (tempDistance < distance) distance = tempDistance; } return distance; }
assert contains(position) || isEmpty() || hasNaN(position) : position;
assert contains(position) || isEmpty() || hasNaN(position) : position;
assert contains(envelope) || isEmpty() || hasNaN(envelope) : this;
assert contains(envelope) || isEmpty() || hasNaN(envelope) : this;
assertPositionEquals(e1.getLowerCorner(), e2.getLowerCorner()); assertPositionEquals(e1.getUpperCorner(), e2.getUpperCorner()); assertTrue (e1.contains(e2, true )); assertFalse (e1.contains(e2, false)); assertNotSame(e1, e2); assertEquals (e1, e2); assertTrue (e1.contains(e2, true )); assertFalse(e1.contains(e2, false)); assertFalse(e1.equals (e2)); assertTrue (e1.equals (e2, EPS, true )); assertTrue (e1.contains(e2, true )); assertFalse(e1.contains(e2, false)); assertFalse(e1.equals (e2)); assertFalse(e1.equals (e2, EPS, true ));