Example #1
0
 /** Transforms a single coordinate point. */
 @Override
 public DirectPosition transform(final DirectPosition ptSrc, DirectPosition ptDst)
     throws MismatchedDimensionException, TransformException {
   final int srcDim = source.getDimension();
   final int tgtDim = target.getDimension();
   if (ptSrc.getDimension() != srcDim) {
     throw new MismatchedDimensionException();
   }
   double[] ordinates = new double[Math.max(srcDim, tgtDim)];
   for (int i = 0; i < srcDim; i++) {
     ordinates[i] = ptSrc.getOrdinate(i);
   }
   source.pj.transform(target.pj, ordinates.length, ordinates, 0, 1);
   if (ptDst != null) {
     if (ptDst.getDimension() != tgtDim) {
       throw new MismatchedDimensionException();
     }
     for (int i = 0; i < tgtDim; i++) {
       ptDst.setOrdinate(i, ordinates[i]);
     }
   } else {
     if (ordinates.length != tgtDim) {
       ordinates = Arrays.copyOf(ordinates, tgtDim);
     }
     ptDst = new SimpleDirectPosition(ordinates);
   }
   return ptDst;
 }
 /** 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();
 }
 /**
  * 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);
     }
   }
 }
 /** 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 {@code true} if the specified object is also a {@linkplain DirectPosition direct
  * position} with equals {@linkplain #getCoordinate coordinate} and {@linkplain
  * #getCoordinateReferenceSystem CRS}.
  *
  * @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 (object instanceof DirectPosition) {
     final DirectPosition that = (DirectPosition) object;
     final int dimension = getDimension();
     if (dimension == that.getDimension()) {
       for (int i = 0; i < dimension; i++) {
         if (!Utilities.equals(this.getOrdinate(i), that.getOrdinate(i))) {
           return false;
         }
       }
       if (Utilities.equals(
           this.getCoordinateReferenceSystem(), that.getCoordinateReferenceSystem())) {
         assert hashCode() == that.hashCode() : this;
         return true;
       }
     }
   }
   return false;
 }