public Object getProperty(Object object, QName name) { DirectPosition dp = (DirectPosition) object; if (name.getLocalPart().equals("dimension")) { return dp.getDimension(); } return null; } }
/** Formats the specified position. */ static String toString(final DirectPosition position) { final StringBuilder buffer = new StringBuilder(Classes.getShortClassName(position)).append('['); final int dimension = position.getDimension(); for (int i = 0; i < dimension; i++) { if (i != 0) { buffer.append(", "); } buffer.append(position.getOrdinate(i)); } return buffer.append(']').toString(); }
/** 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))); } } /**
/** * Constructs a string for the specified point. This is used for formatting error messages. * * @param point The coordinate point to format. * @param locale The locale for formatting numbers. * @return The coordinate point as a string, without '(' or ')' characters. */ static String toString(final DirectPosition point, final Locale locale) { final StringBuffer buffer = new StringBuffer(); final FieldPosition dummy = new FieldPosition(0); final NumberFormat format = NumberFormat.getNumberInstance(locale); final int dimension = point.getDimension(); for (int i = 0; i < dimension; i++) { if (i != 0) { buffer.append(", "); } format.format(point.getOrdinate(i), buffer, dummy); } return buffer.toString(); } }
/** Returns a hash value for the given coordinate. */ static int hashCode(final DirectPosition position) { final int dimension = position.getDimension(); int code = 1; for (int i = 0; i < dimension; i++) { final long bits = Double.doubleToLongBits(position.getOrdinate(i)); code = 31 * code + ((int) (bits) ^ (int) (bits >>> 32)); } final CoordinateReferenceSystem crs = position.getCoordinateReferenceSystem(); if (crs != null) { code += crs.hashCode(); } return code; }
/** 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); }
/** * Copies the values from {@code ptSrc} to {@code ptDst}. Overrides the super-class method for * performance reason. * * @since 2.2 */ @Override public DirectPosition transform(final DirectPosition ptSrc, final DirectPosition ptDst) { if (ptSrc.getDimension() == dimension) { if (ptDst == null) { return new GeneralDirectPosition(ptSrc); } if (ptDst.getDimension() == dimension) { for (int i = 0; i < dimension; i++) { ptDst.setOrdinate(i, ptSrc.getOrdinate(i)); } return ptDst; } } try { // The super class will take care of throwing the MismatchedDimensionException. return super.transform(ptSrc, ptDst); } catch (TransformException e) { throw new AssertionError(e); // Should never happen. } }
@Override public void expandToInclude(DirectPosition pt) { double x = pt.getOrdinate(0); double y = pt.getOrdinate(1); double z = pt.getDimension() >= 3 ? pt.getOrdinate(2) : Double.NaN; expandToInclude(x, y, z); } /**
/** * Set this coordinate to the specified direct position. If the specified position contains a * {@linkplain CoordinateReferenceSystem coordinate reference system}, then the CRS for this * position will be set to the CRS of the specified position. * * @param position The new position for this point. * @throws MismatchedDimensionException if this point doesn't have the expected dimension. */ public void setLocation(final DirectPosition position) throws MismatchedDimensionException { AbstractDirectPosition.ensureDimensionMatch("position", position.getDimension(), 1); setCoordinateReferenceSystem(position.getCoordinateReferenceSystem()); ordinate = position.getOrdinate(0); }
/** * Set this coordinate to the specified direct position. If the specified position contains a * {@linkplain CoordinateReferenceSystem coordinate reference system}, then the CRS for this * position will be set to the CRS of the specified position. * * @param position The new position for this point. * @throws MismatchedDimensionException if this point doesn't have the expected dimension. * @since 2.2 */ public final void setLocation(final DirectPosition position) throws MismatchedDimensionException { ensureDimensionMatch("position", position.getDimension(), ordinates.length); setCoordinateReferenceSystem(position.getCoordinateReferenceSystem()); for (int i = 0; i < ordinates.length; i++) { ordinates[i] = position.getOrdinate(i); } }
/** * Set this coordinate to the specified direct position. If the specified position contains a * {@linkplain CoordinateReferenceSystem coordinate reference system}, then the CRS for this * position will be set to the CRS of the specified position. * * @param position The new position for this point. * @throws MismatchedDimensionException if this point doesn't have the expected dimension. */ public void setLocation(final DirectPosition position) throws MismatchedDimensionException { AbstractDirectPosition.ensureDimensionMatch("position", position.getDimension(), 2); setCoordinateReferenceSystem(position.getCoordinateReferenceSystem()); x = position.getOrdinate(0); y = position.getOrdinate(1); }
/** * Sets this direct position to the given position. If the given position is {@code null}, then * all ordinate values are set to {@linkplain Double#NaN NaN}. * * @param position The new position. * @since 2.5 */ public void setPosition(final DirectPosition position) { final int dimension = getDimension(); if (position != null) { ensureDimensionMatch("position", position.getDimension(), dimension); for (int i = 0; i < dimension; i++) { setOrdinate(i, position.getOrdinate(i)); } } else { for (int i = 0; i < dimension; i++) { setOrdinate(i, Double.NaN); } } }
/** * Check if two coordinate points are equals, in the limits of the specified tolerance vector. * * @param expected The expected coordinate point. * @param actual The actual coordinate point. * @param tolerance The tolerance vector. If this vector length is smaller than the number of * dimension of <code>actual</code>, then the last tolerance value will be reused for all * extra dimensions. */ private static void assertPositionEquals( final DirectPosition expected, final DirectPosition actual, final double[] tolerance) { final int dimension = actual.getDimension(); final int lastToleranceIndex = tolerance.length - 1; assertEquals( "The coordinate point doesn't have the expected dimension", expected.getDimension(), dimension); for (int i = 0; i < dimension; i++) { assertEquals( "Mismatch for ordinate " + i + " (zero-based):", expected.getOrdinate(i), actual.getOrdinate(i), tolerance[Math.min(i, lastToleranceIndex)]); } }
/** * Set this coordinate to the specified direct position. If the specified position contains a * {@linkplain CoordinateReferenceSystem coordinate reference system}, then the CRS for this * position will be set to the CRS of the specified position. * * @param position The new position for this point. * @throws MismatchedDimensionException if this point doesn't have the expected dimension. */ public void setLocation(final DirectPosition position) throws MismatchedDimensionException { AbstractDirectPosition.ensureDimensionMatch("position", position.getDimension(), 3); setCoordinateReferenceSystem(position.getCoordinateReferenceSystem()); x = position.getOrdinate(0); y = position.getOrdinate(1); z = position.getOrdinate(2); }
/** * 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; }
/** * Create a Point from a ISO Geometry DirectPosition. * * @param position * @param factory Optional GeometryFactory * @return Point */ public static Point toGeometry(DirectPosition position, GeometryFactory factory) { if (factory == null) { factory = new GeometryFactory(); } Coordinate coordinate = new Coordinate(position.getOrdinate(0), position.getOrdinate(1)); if (position.getDimension() == 3) { coordinate.z = position.getOrdinate(2); } return factory.createPoint(coordinate); }
/** * Returns a string representation of this mapped position. * * @todo Consider using a {@link java.text.NumberFormat} instance. */ public String toString() { final TableWriter table = new TableWriter(null, " "); table.write(Vocabulary.format(VocabularyKeys.SOURCE_POINT)); table.write(':'); int dimension = source.getDimension(); for (int i = 0; i < dimension; i++) { table.nextColumn(); table.write(String.valueOf(source.getOrdinate(i))); } table.nextLine(); table.write(Vocabulary.format(VocabularyKeys.TARGET_POINT)); table.write(':'); dimension = target.getDimension(); for (int i = 0; i < dimension; i++) { table.nextColumn(); table.write(String.valueOf(target.getOrdinate(i))); } return table.toString(); } }
/** 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; }
/** 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); }
/** * Compares this point with the specified object for equality. If the given object implements * the {@link DirectPosition} interface, then the comparison is performed as specified in its * {@link DirectPosition#equals} contract. * * @param object The object to compare with this position. * @return {@code true} if the given object is equals to this position. */ @Override public boolean equals(final Object object) { /* * If the other object implements the DirectPosition interface, performs * the comparaison as specified in DirectPosition.equals(Object) contract. */ if (object instanceof DirectPosition) { final DirectPosition other = (DirectPosition) object; if (other.getDimension() == 3 && Utilities.equals(other.getOrdinate(0), x) && Utilities.equals(other.getOrdinate(1), y) && Utilities.equals(other.getOrdinate(2), z) && Utilities.equals(other.getCoordinateReferenceSystem(), crs)) { assert hashCode() == other.hashCode() : this; return true; } return false; } return false; }