/** * Constructs two-dimensional envelope defined by a {@link Rectangle2D}. The coordinate * reference system is initially undefined. * * @param rect The rectangle to copy. */ public GeneralEnvelope(final Rectangle2D rect) { ensureNonNull("rect", rect); ordinates = new double[] {rect.getMinX(), rect.getMinY(), rect.getMaxX(), rect.getMaxY()}; checkCoordinates(ordinates); }
/** * Constructs a envelope defined by two positions. * * @param minDP Minimum ordinate values. * @param maxDP Maximum ordinate values. * @throws MismatchedDimensionException if the two positions don't have the same dimension. * @throws IllegalArgumentException if an ordinate value in the minimum point is not less than * or equal to the corresponding ordinate value in the maximum point. */ public GeneralEnvelope(final double[] minDP, final double[] maxDP) throws IllegalArgumentException { ensureNonNull("minDP", minDP); ensureNonNull("maxDP", maxDP); ensureSameDimension(minDP.length, maxDP.length); ordinates = new double[minDP.length + maxDP.length]; System.arraycopy(minDP, 0, ordinates, 0, minDP.length); System.arraycopy(maxDP, 0, ordinates, minDP.length, maxDP.length); checkCoordinates(ordinates); }
/** * Sets this envelope to the same coordinate values than the specified envelope. * * @param envelope The new envelope to copy coordinates from. * @throws MismatchedDimensionException if the specified envelope doesn't have the expected * number of dimensions. * @since 2.2 */ public void setEnvelope(final GeneralEnvelope envelope) throws MismatchedDimensionException { ensureNonNull("envelope", envelope); AbstractDirectPosition.ensureDimensionMatch( "envelope", envelope.getDimension(), getDimension()); System.arraycopy(envelope.ordinates, 0, ordinates, 0, ordinates.length); if (envelope.crs != null) { crs = envelope.crs; assert crs.getCoordinateSystem().getDimension() == getDimension() : crs; assert !envelope.getClass().equals(getClass()) || equals(envelope) : envelope; } }
/** * Constructs a new envelope with the same data than the specified geographic bounding box. The * coordinate reference system is set to {@linkplain DefaultGeographicCRS#WGS84 WGS84}. * * @param box The bounding box to copy. * @since 2.4 */ public GeneralEnvelope(final GeographicBoundingBox box) { ensureNonNull("box", box); ordinates = new double[] { box.getWestBoundLongitude(), box.getSouthBoundLatitude(), box.getEastBoundLongitude(), box.getNorthBoundLatitude() }; crs = DefaultGeographicCRS.WGS84; }
/** * Sets this envelope to the intersection if this envelope with the specified one. * * <p>This method assumes that the specified envelope uses the same CRS than this envelope. For * performance reason, it will no be verified unless J2SE assertions are enabled. * * @param envelope the {@code Envelope} to intersect to this envelope. * @throws MismatchedDimensionException if the specified envelope doesn't have the expected * dimension. */ public void intersect(final Envelope envelope) throws MismatchedDimensionException { ensureNonNull("envelope", envelope); final int dim = ordinates.length / 2; AbstractDirectPosition.ensureDimensionMatch("envelope", envelope.getDimension(), dim); assert equalsIgnoreMetadata(crs, envelope.getCoordinateReferenceSystem()) : envelope; for (int i = 0; i < dim; i++) { double min = Math.max(ordinates[i], envelope.getMinimum(i)); double max = Math.min(ordinates[i + dim], envelope.getMaximum(i)); if (min > max) { // Make an empty envelope (min==max) // while keeping it legal (min<=max). min = max = 0.5 * (min + max); } ordinates[i] = min; ordinates[i + dim] = max; } }
/** * Adds a point to this envelope. The resulting envelope is the smallest envelope that contains * both the original envelope and the specified point. After adding a point, a call to {@link * #contains} with the added point as an argument will return {@code true}, except if one of the * point's ordinates was {@link Double#NaN} (in which case the corresponding ordinate have been * ignored). * * <p>This method assumes that the specified point uses the same CRS than this envelope. For * performance reason, it will no be verified unless J2SE assertions are enabled. * * @param position The point to add. * @throws MismatchedDimensionException if the specified point doesn't have the expected * dimension. */ public void add(final DirectPosition position) throws MismatchedDimensionException { ensureNonNull("position", position); final int dim = ordinates.length / 2; AbstractDirectPosition.ensureDimensionMatch("position", position.getDimension(), dim); assert equalsIgnoreMetadata(crs, position.getCoordinateReferenceSystem()) : position; for (int i = 0; i < dim; i++) { final double value = position.getOrdinate(i); if (value < ordinates[i] || Double.isNaN(ordinates[i])) ordinates[i] = value; if (value > ordinates[i + dim] || Double.isNaN(ordinates[i + dim])) ordinates[i + dim] = value; } assert isEmpty() || contains(position); }
/** * Tests if a specified coordinate is inside the boundary of this envelope. * * <p>This method assumes that the specified point uses the same CRS than this envelope. For * performance reason, it will no be verified unless J2SE assertions are enabled. * * @param position The point to text. * @return {@code true} if the specified coordinates are inside the boundary of this envelope; * {@code false} otherwise. * @throws MismatchedDimensionException if the specified point doesn't have the expected * dimension. */ public boolean contains(final DirectPosition position) throws MismatchedDimensionException { ensureNonNull("position", position); final int dim = ordinates.length / 2; AbstractDirectPosition.ensureDimensionMatch("point", position.getDimension(), dim); assert equalsIgnoreMetadata(crs, position.getCoordinateReferenceSystem()) : position; for (int i = 0; i < dim; i++) { final double value = position.getOrdinate(i); if (!(value >= ordinates[i])) return false; if (!(value <= ordinates[i + dim])) return false; // Use '!' in order to take 'NaN' in account. } return true; }
/** * Constructs a new envelope with the same data than the specified envelope. * * @param envelope The envelope to copy. */ public GeneralEnvelope(final Envelope envelope) { ensureNonNull("envelope", envelope); if (envelope instanceof GeneralEnvelope) { final GeneralEnvelope e = (GeneralEnvelope) envelope; ordinates = e.ordinates.clone(); crs = e.crs; } else { crs = envelope.getCoordinateReferenceSystem(); final int dimension = envelope.getDimension(); ordinates = new double[2 * dimension]; for (int i = 0; i < dimension; i++) { ordinates[i] = envelope.getMinimum(i); ordinates[i + dimension] = envelope.getMaximum(i); } checkCoordinates(ordinates); } }
ensureNonNull("envelope", envelope); final int dimension = getDimension(); if (envelope.getDimension() != dimension) {
final CoordinateReferenceSystem crs) throws IllegalArgumentException { ensureNonNull("gridRange", gridRange); ensureNonNull("gridToCRS", gridToCRS); final int dimRange = gridRange.getDimension(); final int dimSource = gridToCRS.getSourceDimensions();
ensureNonNull("envelope", envelope); final int dim = ordinates.length / 2; AbstractDirectPosition.ensureDimensionMatch("envelope", envelope.getDimension(), dim);
/** * Adds an envelope object to this envelope. The resulting envelope is the union of the two * {@code Envelope} objects. * * <p>This method assumes that the specified envelope uses the same CRS than this envelope. For * performance reason, it will no be verified unless J2SE assertions are enabled. * * @param envelope the {@code Envelope} to add to this envelope. * @throws MismatchedDimensionException if the specified envelope doesn't have the expected * dimension. */ public void add(final Envelope envelope) throws MismatchedDimensionException { ensureNonNull("envelope", envelope); final int dim = ordinates.length / 2; AbstractDirectPosition.ensureDimensionMatch("envelope", envelope.getDimension(), dim); assert equalsIgnoreMetadata(crs, envelope.getCoordinateReferenceSystem()) : envelope; for (int i = 0; i < dim; i++) { final double min = envelope.getMinimum(i); final double max = envelope.getMaximum(i); if (min < ordinates[i]) ordinates[i] = min; if (max > ordinates[i + dim]) ordinates[i + dim] = max; } assert isEmpty() || contains(envelope, true); }
ensureNonNull("envelope", envelope); final int dim = ordinates.length / 2; AbstractDirectPosition.ensureDimensionMatch("envelope", envelope.getDimension(), dim);