/** * Constructs a position from the specified {@link Point2D}. * * @param point The point to copy. */ public DirectPosition2D(final Point2D point) { super(point.getX(), point.getY()); if (point instanceof DirectPosition) { setCoordinateReferenceSystem(((DirectPosition) point).getCoordinateReferenceSystem()); } }
/** * Constructs a position initialized to the same values than the specified point. * * @param point The position to copy. * @since 2.2 */ public GeneralDirectPosition(final DirectPosition point) { ordinates = point.getCoordinate(); // Should already be cloned. crs = point.getCoordinateReferenceSystem(); }
/** * Returns the common CRS of specified points. * * @param minDP The first position. * @param maxDP The second position. * @return Their common CRS, or {@code null} if none. * @throws MismatchedReferenceSystemException if the two positions don't use the same CRS. */ static CoordinateReferenceSystem getCoordinateReferenceSystem( final DirectPosition minDP, final DirectPosition maxDP) throws MismatchedReferenceSystemException { final CoordinateReferenceSystem crs1 = minDP.getCoordinateReferenceSystem(); final CoordinateReferenceSystem crs2 = maxDP.getCoordinateReferenceSystem(); if (crs1 == null) { return crs2; } else { if (crs2 != null && !crs1.equals(crs2)) { throw new MismatchedReferenceSystemException( Errors.format(ErrorKeys.MISMATCHED_COORDINATE_REFERENCE_SYSTEM)); } return crs1; } }
/** 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 CRS of the specified point. If the CRS of the previous point is known, it can be * specified. This method will then ensure that the two CRS are compatibles. */ private static CoordinateReferenceSystem getCoordinateReferenceSystem( final DirectPosition point, CoordinateReferenceSystem previousCRS) throws MismatchedReferenceSystemException { final CoordinateReferenceSystem candidate = point.getCoordinateReferenceSystem(); if (candidate != null) { if (previousCRS == null) { return candidate; } /* * We use strict 'equals' instead of 'equalsIgnoreCase' because if the metadata * are not identical, we have no easy way to choose which CRS is the "main" one. */ if (!previousCRS.equals(candidate)) { throw new MismatchedReferenceSystemException( Errors.format(ErrorKeys.MISMATCHED_COORDINATE_REFERENCE_SYSTEM)); } } return previousCRS; }
private void ensureCompatibleReferenceSystem(DirectPosition location) { if (crs != null) { final CoordinateReferenceSystem other = location.getCoordinateReferenceSystem(); if (other != null) { if (!CRS.equalsIgnoreMetadata(crs, other)) { throw new MismatchedReferenceSystemException( Errors.format(ErrorKeys.MISMATCHED_COORDINATE_REFERENCE_SYSTEM)); } } } }
public MockDirectPosition(DirectPosition position) { assert position.getCoordinateReferenceSystem() == crs; coordinates = position.getCoordinate(); }
/** * Returns reduced coordinates of vertices so the first vertex has [0,0] coordinats. * * @return The List of reduced vertives */ protected List<DirectPosition> reduce() { // Coordinate[] redCoords = new Coordinate[coordinates.length]; ArrayList<DirectPosition> redCoords = new ArrayList<DirectPosition>(); for (int i = 0; i < vertices.length; i++) { redCoords.add( new DirectPosition2D( vertices[i].getCoordinateReferenceSystem(), vertices[i].getCoordinate()[0] - vertices[0].getCoordinate()[0], vertices[i].getCoordinate()[1] - vertices[0].getCoordinate()[1])); } return redCoords; }
/** * Make sure that the specified location uses the same CRS as this one. * * @param location * @throws MismatchedReferenceSystemException if the CRS are incompatible. */ protected void ensureCompatibleReferenceSystem(DirectPosition location) { if (crs != null) { final CoordinateReferenceSystem other = location.getCoordinateReferenceSystem(); if (other != null) { if (!CRS.equalsIgnoreMetadata(crs, other)) { throw new MismatchedReferenceSystemException( Errors.format(ErrorKeys.MISMATCHED_COORDINATE_REFERENCE_SYSTEM)); } } } } /** Returns the coordinate reference system associated with this envelope. */
/** * 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); } }
public Point createPoint(Position position) throws MismatchedReferenceSystemException, MismatchedDimensionException { if (position == null) { throw new NullPointerException(); } setCoordinateReferenceSystem(position.getDirectPosition().getCoordinateReferenceSystem()); DirectPosition copy = (DirectPosition) getPositionFactory() .createDirectPosition(position.getDirectPosition().getCoordinate()); return getPrimitiveFactory().createPoint(copy); }
/** * 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); }
/** * 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); }
/** * Returns the CircumCicle of the this TINTriangle. * * @return Returns the CircumCicle of the this TINTriangle */ protected Circle getCircumCicle() { // DirectPosition center = new DirectPosition2D(); List<DirectPosition> reducedVertices = reduce(); CoordinateReferenceSystem crs = ((DirectPosition) reducedVertices.get(1)).getCoordinateReferenceSystem(); double x1 = ((DirectPosition) reducedVertices.get(1)).getCoordinate()[0]; double y1 = ((DirectPosition) reducedVertices.get(1)).getCoordinate()[1]; double x2 = ((DirectPosition) reducedVertices.get(2)).getCoordinate()[0]; double y2 = ((DirectPosition) reducedVertices.get(2)).getCoordinate()[1]; // Calculation of Circumcicle center double t = (0.5 * (((x1 * x1) + (y1 * y1)) - (x1 * x2) - (y1 * y2))) / ((y1 * x2) - (x1 * y2)); // t = Math.abs(t); DirectPosition2D center = new DirectPosition2D( crs, (x2 / 2) - (t * y2) + p0.getCoordinate()[0], (y2 / 2) + (t * x2) + p0.getCoordinate()[1]); return new Circle(center.getDirectPosition(), center.distance(new DirectPosition2D(p0))); }
/** * 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; }
/** * 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; } }
/** * 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; }
/** * Calculate the destination position for the quad vertex as source position using the * difference between nearest source and destination point pair. * * @param x the original coordinate. * @param vertices List of the MappedPosition. * @return MappedPosition from the original and new coordinate, so the difference between them * is the same as for the nearest one MappedPosition. It is used for calculating destination * quad. */ protected MappedPosition generateCoordFromNearestOne( DirectPosition x, List<MappedPosition> vertices) { MappedPosition nearestOne = nearestMappedCoordinate(x, vertices); double dstX = x.getCoordinate()[0] + (nearestOne.getTarget().getCoordinate()[0] - nearestOne.getSource().getCoordinate()[0]); double dstY = x.getCoordinate()[1] + (nearestOne.getTarget().getCoordinate()[1] - nearestOne.getSource().getCoordinate()[1]); DirectPosition dst = new DirectPosition2D( nearestOne.getTarget().getCoordinateReferenceSystem(), dstX, dstY); return new MappedPosition(x, dst); }