/** Makes sure the specified dimensions are identical. */ private static void ensureSameDimension(final int dim1, final int dim2) throws MismatchedDimensionException { if (dim1 != dim2) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$2, dim1, dim2)); } }
/** * Set this coordinate to the specified {@link Point2D}. This coordinate must be * two-dimensional. * * @param point The new coordinate for this point. * @throws MismatchedDimensionException if this coordinate point is not two-dimensional. */ public final void setLocation(final Point2D point) throws MismatchedDimensionException { if (ordinates.length != 2) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.NOT_TWO_DIMENSIONAL_$1, ordinates.length)); } ordinates[0] = point.getX(); ordinates[1] = point.getY(); }
/** * Convenience method for checking object dimension validity. This method is usually invoked for * argument checking. * * @param name The name of the argument to check. * @param dimension The object dimension. * @param expectedDimension The Expected dimension for the object. * @throws MismatchedDimensionException if the object doesn't have the expected dimension. */ static void ensureDimensionMatch( final String name, final int dimension, final int expectedDimension) throws MismatchedDimensionException { if (dimension != expectedDimension) { throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$3, name, dimension, expectedDimension)); } }
/** * Convenience method for checking object dimension validity. * * @param name The name of the argument to check. * @param coordinates The coordinate array to check. * @throws MismatchedDimensionException if the coordinate doesn't have the expected dimension. */ final void ensureDimensionMatch(final String name, final double[] coordinates) throws MismatchedDimensionException { if (coordinates.length != axis.length) { throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$3, name, coordinates.length, axis.length)); } }
/** Ensure the specified point is one-dimensional. */ static void checkDimension(final DirectPosition point) { final int dim = point.getDimension(); if (dim != 1) { throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$2, Integer.valueOf(1), Integer.valueOf(dim))); } } /**
/** * Convenience method for checking object dimension validity. This method is usually invoked for * argument checking. * * @param name The name of the argument to check. * @param envelope The envelope to check. * @param dimension The expected dimension for the object. * @throws MismatchedDimensionException if the envelope doesn't have the expected dimension. */ private static void ensureDimensionMatch( final String name, final Envelope envelope, final int dimension) throws MismatchedDimensionException { final int dim = envelope.getDimension(); if (dimension != dim) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$3, name, dim, dimension)); } }
int dim; if ((dim = getSourceDimensions()) != 2) { throw new MismatchedDimensionException(constructMessage("ptSrc", 2, dim)); throw new MismatchedDimensionException(constructMessage("ptDst", 2, dim));
/** * Sets the envelope to the specified values, which must be the lower corner coordinates * followed by upper corner coordinates. The number of arguments provided shall be twice this * {@linkplain #getDimension envelope dimension}, and minimum shall not be greater than maximum. * * <p><b>Example:</b> (<var>x</var><sub>min</sub>, <var>y</var><sub>min</sub>, * <var>z</var><sub>min</sub>, <var>x</var><sub>max</sub>, <var>y</var><sub>max</sub>, * <var>z</var><sub>max</sub>) * * @param ordinates The new ordinate values. * @since 2.5 */ public void setEnvelope(final double... ordinates) { if ((ordinates.length & 1) != 0) { throw new IllegalArgumentException( Errors.format(ErrorKeys.ODD_ARRAY_LENGTH_$1, ordinates.length)); } final int dimension = ordinates.length >>> 1; final int check = this.ordinates.length >>> 1; if (dimension != check) { throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$3, "ordinates", dimension, check)); } checkCoordinates(ordinates); System.arraycopy(ordinates, 0, this.ordinates, 0, ordinates.length); }
/** Returns the distance between the specified points. */ private static double distance(final DirectPosition source, final DirectPosition target) { final int otherDim = source.getDimension(); final int dimension = target.getDimension(); if (otherDim != dimension) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$2, otherDim, dimension)); } double sum = 0; for (int i = 0; i < dimension; i++) { final double delta = source.getOrdinate(i) - target.getOrdinate(i); sum += delta * delta; } return Math.sqrt(sum / dimension); }
/** * Create a point with the provided ordinates * * @param ordinates * @return getPrimitiveFactory().createPoint(coordinates) * @throws MismatchedDimensionException */ public Point createPoint(double[] ordinates) throws MismatchedDimensionException { if (ordinates == null) throw new NullPointerException("Ordinates required to create a point"); int dimension = this.getCoordinateReferenceSystem().getCoordinateSystem().getDimension(); if (ordinates.length != dimension) throw new MismatchedDimensionException( "Create point requires " + dimension + " ordinates (" + ordinates.length + " provided"); return getPrimitiveFactory().createPoint(ordinates); }
/** * Convenience method for checking coordinate reference system validity. * * @throws IllegalArgumentException if the CRS dimension is not valid. */ protected void checkCoordinateReferenceSystemDimension() throws MismatchedDimensionException { if (crs != null) { final int expected = getDimension(); final int dimension = crs.getCoordinateSystem().getDimension(); if (dimension > expected) { // check dimensions and choose ReferencedEnvelope or ReferencedEnvelope3D // or the factory method ReferencedEnvelope.reference( CoordinateReferenceSystem ) throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$3, crs.getName().getCode(), Integer.valueOf(dimension), Integer.valueOf(expected))); } } }
/** * Gets the derivative of this transform at a point. The default implementation always throw an * exception. Subclasses that implement the {@link MathTransform2D} interface should override * this method. Other subclasses should override {@link #derivative(DirectPosition)} instead. * * @param point The coordinate point where to evaluate the derivative. * @return The derivative at the specified point as a 2×2 matrix. * @throws MismatchedDimensionException if the input dimension is not 2. * @throws TransformException if the derivative can't be evaluated at the specified point. * @see MathTransform2D#derivative(Point2D) */ public Matrix derivative(final Point2D point) throws TransformException { final int dimSource = getSourceDimensions(); if (dimSource != 2) { throw new MismatchedDimensionException(constructMessage("point", 2, dimSource)); } throw new TransformException(Errors.format(ErrorKeys.CANT_COMPUTE_DERIVATIVE)); }
/** * Invoked automatically when the {@code target pt} instruction were executed and a {@code test * tolerance} were previously set. The default implementation compares the transformed source * point with the expected target point. If a mismatch greater than the tolerance error is * found, an exception is thrown. Subclasses may overrides this method in order to performs more * tests. * * @throws TransformException if the source point can't be transformed, or a mistmatch is found. * @throws MismatchedDimensionException if the transformed source point doesn't have the * expected dimension. */ protected void test() throws TransformException, MismatchedDimensionException { final DirectPosition transformedSource = transform.transform(sourcePosition, null); final int sourceDim = transformedSource.getDimension(); final int targetDim = targetPosition.getDimension(); if (sourceDim != targetDim) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$2, sourceDim, targetDim)); } for (int i = 0; i < sourceDim; i++) { // Use '!' for catching NaN. if (!(Math.abs(transformedSource.getOrdinate(i) - targetPosition.getOrdinate(i)) <= tolerance[Math.min(i, tolerance.length - 1)])) { throw new TransformException( "Expected " + targetPosition + " but got " + transformedSource); } } }
/** Makes sure that the specified objects have the same dimension. */ private static void ensureDimensionMatch( final GridEnvelope gridRange, final Envelope envelope, final boolean checkingRange) { if (gridRange != null && envelope != null) { final String label; final int dim1, dim2; if (checkingRange) { label = "gridRange"; dim1 = gridRange.getDimension(); dim2 = envelope.getDimension(); } else { label = "envelope"; dim1 = envelope.getDimension(); dim2 = gridRange.getDimension(); } if (dim1 != dim2) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$3, label, dim1, dim2)); } } }
/** Checks consistency between the base CRS and the "base to derived" transform. */ private static void checkDimensions( final CoordinateReferenceSystem base, final MathTransform baseToDerived, final CoordinateSystem derivedCS) throws MismatchedDimensionException { final int dimSource = baseToDerived.getSourceDimensions(); final int dimTarget = baseToDerived.getTargetDimensions(); int dim1, dim2; if ((dim1 = dimSource) != (dim2 = base.getCoordinateSystem().getDimension()) || (dim1 = dimTarget) != (dim2 = derivedCS.getDimension())) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$2, dim1, dim2)); } }
/** * Convenience method for checking coordinate reference system validity. * * @param crs The coordinate reference system to check. * @param expected the dimension expected. * @throws MismatchedDimensionException if the CRS dimension is not valid. */ static void checkCoordinateReferenceSystemDimension( final CoordinateReferenceSystem crs, final int expected) throws MismatchedDimensionException { if (crs != null) { final int dimension = crs.getCoordinateSystem().getDimension(); if (dimension != expected) { throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$3, crs.getName().getCode(), dimension, expected)); } } }
/** * Creates a new instance for the specified grid range and envelope. * * @param gridRange The valid coordinate range of a grid coverage. * @param userRange The corresponding coordinate range in user coordinate. This envelope must * contains entirely all pixels, i.e. the envelope's upper left corner must coincide with * the upper left corner of the first pixel and the envelope's lower right corner must * coincide with the lower right corner of the last pixel. * @throws MismatchedDimensionException if the grid range and the envelope doesn't have * consistent dimensions. */ public GridToEnvelopeMapper(final GridEnvelope gridRange, final Envelope userRange) throws MismatchedDimensionException { ensureNonNull("gridRange", gridRange); ensureNonNull("userRange", userRange); final int gridDim = gridRange.getDimension(); final int userDim = userRange.getDimension(); if (userDim != gridDim) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$2, gridDim, userDim)); } this.gridRange = gridRange; this.envelope = userRange; }
/** Transforms the specified {@code ptSrc} and stores the result in {@code ptDst}. */ public DirectPosition transform(final DirectPosition ptSrc, DirectPosition ptDst) { if (ptDst == null) { ptDst = new GeneralDirectPosition(2); } else { final int dimension = ptDst.getDimension(); if (dimension != 2) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.MISMATCHED_DIMENSION_$3, "ptDst", dimension, 2)); } } final double[] array = ptSrc.getCoordinate(); transform(array, 0, array, 0, 1); ptDst.setOrdinate(0, array[0]); ptDst.setOrdinate(1, array[1]); return ptDst; }
/** * Constructs two-dimensional envelope defined by an other {@link Envelope}. * * @param envelope The envelope to copy. */ public Envelope2D(final Envelope envelope) { super( envelope.getMinimum(0), envelope.getMinimum(1), envelope.getSpan(0), envelope.getSpan(1)); // TODO: check below should be first, if only Sun could fix RFE #4093999. final int dimension = envelope.getDimension(); if (dimension != 2) { throw new MismatchedDimensionException( Errors.format(ErrorKeys.NOT_TWO_DIMENSIONAL_$1, dimension)); } setCoordinateReferenceSystem(envelope.getCoordinateReferenceSystem()); }
/** Gets the derivative of this transform at a point. */ @Override public Matrix derivative(final DirectPosition point) throws TransformException { final int nSkipped = firstAffectedOrdinate + numTrailingOrdinates; final int transDim = subTransform.getSourceDimensions(); final int pointDim = point.getDimension(); if (pointDim != transDim + nSkipped) { throw new MismatchedDimensionException( Errors.format( ErrorKeys.MISMATCHED_DIMENSION_$3, "point", pointDim, transDim + nSkipped)); } final GeneralDirectPosition subPoint = new GeneralDirectPosition(transDim); for (int i = 0; i < transDim; i++) { subPoint.ordinates[i] = point.getOrdinate(i + firstAffectedOrdinate); } return expand( toGMatrix(subTransform.derivative(subPoint)), firstAffectedOrdinate, numTrailingOrdinates, 0); }