Java Code Examples for org.opengis.geometry.DirectPosition#getCoordinateReferenceSystem()

The following examples show how to use org.opengis.geometry.DirectPosition#getCoordinateReferenceSystem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractDirectPosition.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Sets this direct position to the given position. If the given position is
 * {@code null}, then all coordinate values are set to {@link Double#NaN NaN}.
 *
 * <p>If this position and the given position have a non-null CRS, then the default implementation
 * requires the CRS to be {@linkplain Utilities#equalsIgnoreMetadata equals (ignoring metadata)},
 * otherwise a {@code MismatchedReferenceSystemException} is thrown. However subclass may choose
 * to assign the CRS of this position to the CRS of the given position.</p>
 *
 * @param  position  the new position, or {@code null}.
 * @throws MismatchedDimensionException if the given position doesn't have the expected dimension.
 * @throws MismatchedReferenceSystemException if the given position doesn't use the expected CRS.
 */
public void setLocation(final DirectPosition position)
        throws MismatchedDimensionException, MismatchedReferenceSystemException
{
    final int dimension = getDimension();
    if (position != null) {
        ensureDimensionMatches("position", dimension, position);
        final CoordinateReferenceSystem crs = getCoordinateReferenceSystem();
        if (crs != null) {
            final CoordinateReferenceSystem other = position.getCoordinateReferenceSystem();
            if (other != null && !Utilities.equalsIgnoreMetadata(crs, other)) {
                throw new MismatchedReferenceSystemException(Errors.format(Errors.Keys.MismatchedCRS));
            }
        }
        for (int i=0; i<dimension; i++) {
            setOrdinate(i, position.getOrdinate(i));
        }
    } else {
        for (int i=0; i<dimension; i++) {
            setOrdinate(i, Double.NaN);
        }
    }
}
 
Example 2
Source File: AbstractEnvelope.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the common CRS of specified points.
 *
 * @param  lowerCorner  the first position.
 * @param  upperCorner  the second position.
 * @return their common CRS, or {@code null} if none.
 * @throws MismatchedReferenceSystemException if the two positions don't use equal CRS.
 */
static CoordinateReferenceSystem getCommonCRS(final DirectPosition lowerCorner,
                                              final DirectPosition upperCorner)
        throws MismatchedReferenceSystemException
{
    ensureNonNull("lowerCorner", lowerCorner);
    ensureNonNull("upperCorner", upperCorner);
    final CoordinateReferenceSystem crs1 = lowerCorner.getCoordinateReferenceSystem();
    final CoordinateReferenceSystem crs2 = upperCorner.getCoordinateReferenceSystem();
    if (crs1 == null) {
        return crs2;
    } else {
        if (crs2 != null && !crs1.equals(crs2)) {
            throw new MismatchedReferenceSystemException(Errors.format(Errors.Keys.MismatchedCRS));
        }
        return crs1;
    }
}
 
Example 3
Source File: PositionTransformer.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Transforms a given position from its CRS to the CRS of this {@code PositionTransformer}.
 * If the CRS associated to the given position is {@code null}, then that CRS is assumed to
 * be the default CRS specified at construction time. Otherwise if that CRS is not equal to
 * the {@linkplain #getCoordinateReferenceSystem() CRS associated with this position}, then
 * a coordinates transformations are applied. The result may be stored in this instance.
 *
 * @param  position  a position using an arbitrary CRS, or {@code null}. This object will not be modified.
 * @return the transformed position, either {@code this} or the given position (which may be {@code null}).
 * @throws TransformException if a coordinate transformation was required and failed.
 */
public DirectPosition transform(final DirectPosition position) throws TransformException {
    if (position != null) {
        CoordinateReferenceSystem userCRS = position.getCoordinateReferenceSystem();
        if (userCRS == null) {
            userCRS = defaultCRS;
        }
        /*
         * A projection may be required. Check if it is the same one than the one used last time this method
         * has been invoked. If the specified position uses a new CRS, then get the transformation and save
         * it in case the next call to this method would use again the same transformation.
         */
        if (!Utilities.equalsIgnoreMetadata(lastCRS, userCRS)) {
            setSourceCRS(userCRS);
        }
        if (forward != null) {
            return forward.transform(position, this);
        }
    }
    return position;
}
 
Example 4
Source File: GeohashReferenceSystem.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Encodes the given position into a geohash. The default implementation transforms the given position
 * to the coordinate reference system expected by the enclosing {@link GeohashReferenceSystem}, then
 * delegates to {@link #encode(double, double)}.
 *
 * @param  position  the coordinate to encode.
 * @return geohash encoding of the given position.
 * @throws TransformException if an error occurred while transforming the given coordinate to a geohash reference.
 */
public String encode(DirectPosition position) throws TransformException {
    ArgumentChecks.ensureNonNull("position", position);
    final CoordinateReferenceSystem ps = position.getCoordinateReferenceSystem();
    if (ps != null && !normalizedCRS.equals(ps, ComparisonMode.IGNORE_METADATA)) {
        if (lastOp == null || !Utilities.equalsIgnoreMetadata(lastOp.getSourceCRS(), ps)) try {
            lastOp = CRS.findOperation(ps, normalizedCRS, null);
        } catch (FactoryException e) {
            throw new GazetteerException(e.getLocalizedMessage(), e);
        }
        position = lastOp.getMathTransform().transform(position, null);
    }
    return encode(position.getOrdinate(1), position.getOrdinate(0));
}
 
Example 5
Source File: MilitaryGridReferenceSystemTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Encodes random coordinates, decodes them and verifies that the results are approximately equal
 * to the original coordinates.
 *
 * @throws TransformException if an error occurred while computing the coordinate.
 */
@Test
@DependsOnMethod({
    "testEncodeUTM", "testDecodeUTM",
    "testEncodeUPS", "testDecodeUPS"
})
public void verifyConsistency() throws TransformException {
    final Random random = TestUtilities.createRandomNumberGenerator();
    final MilitaryGridReferenceSystem.Coder coder = coder();
    final DirectPosition2D expected = new DirectPosition2D();
    final DirectPosition2D position = new DirectPosition2D(CommonCRS.WGS84.geographic());
    for (int i=0; i<100; i++) {
        position.x = random.nextDouble() * 180 -  90;       // Latitude  (despite the 'x' field name)
        position.y = random.nextDouble() * 358 - 179;       // Longitude (despite the 'y' field name)
        final String reference = coder.encode(position);
        final DirectPosition r = decode(coder, reference);
        final ProjectedCRS crs = (ProjectedCRS) r.getCoordinateReferenceSystem();
        assertSame(expected, crs.getConversionFromBase().getMathTransform().transform(position, expected));
        final double distance = expected.distance(r.getOrdinate(0), r.getOrdinate(1));
        if (!(distance < 1.5)) {    // Use '!' for catching NaN.
            final String lineSeparator = System.lineSeparator();
            fail("Consistency check failed for φ = " + position.x + " and λ = " + position.y + lineSeparator
               + "MGRS reference = " + reference + lineSeparator
               + "Parsing result = " + r         + lineSeparator
               + "Projected φ, λ = " + expected  + lineSeparator
               + "Distance (m)   = " + distance  + lineSeparator);
        }
    }
}
 
Example 6
Source File: Angle.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the angular value of the axis having the given direction.
 * This helper method is used for subclass constructors expecting a {@link DirectPosition} argument.
 *
 * @param  position  the position from which to get an angular value.
 * @param  positive  axis direction of positive values.
 * @param  negative  axis direction of negative values.
 * @return angular value in degrees.
 * @throws IllegalArgumentException if the given coordinate it not associated to a CRS,
 *         or if no axis oriented toward the given directions is found, or if that axis
 *         does not use {@linkplain Units#isAngular angular units}.
 */
static double valueOf(final DirectPosition position, final AxisDirection positive, final AxisDirection negative) {
    final CoordinateReferenceSystem crs = position.getCoordinateReferenceSystem();
    if (crs == null) {
        throw new IllegalArgumentException(Errors.format(Errors.Keys.UnspecifiedCRS));
    }
    final CoordinateSystem cs = crs.getCoordinateSystem();
    final int dimension = cs.getDimension();
    IncommensurableException cause = null;
    for (int i=0; i<dimension; i++) {
        final CoordinateSystemAxis axis = cs.getAxis(i);
        final AxisDirection dir = axis.getDirection();
        final boolean isPositive = dir.equals(positive);
        if (isPositive || dir.equals(negative)) {
            double value = position.getOrdinate(i);
            if (!isPositive) value = -value;
            final Unit<?> unit = axis.getUnit();
            if (unit != Units.DEGREE) try {
                value = unit.getConverterToAny(Units.DEGREE).convert(value);
            } catch (IncommensurableException e) {
                cause = e;
                break;
            }
            return value;
        }
    }
    throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalCRSType_1,
            Classes.getLeafInterfaces(crs.getClass(), CoordinateReferenceSystem.class)[0]), cause);
}
 
Example 7
Source File: DirectPosition2D.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs a position initialized to the same values than the specified point.
 *
 * @param  position  the position to copy.
 * @throws MismatchedDimensionException if the given position is not two-dimensional.
 *
 * @see #setLocation(Point2D)
 */
public DirectPosition2D(final DirectPosition position) throws MismatchedDimensionException {
    ensureNonNull("position", position);
    ensureDimensionMatches("position", 2, position);
    x   = position.getOrdinate(0);
    y   = position.getOrdinate(1);
    crs = position.getCoordinateReferenceSystem();
}
 
Example 8
Source File: GeneralDirectPosition.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a position initialized to the same values than the specified point.
 * This is a copy constructor.
 *
 * @param point  the position to copy.
 */
public GeneralDirectPosition(final DirectPosition point) {
    coordinates = point.getCoordinate();                            // Should already be cloned.
    crs = point.getCoordinateReferenceSystem();
    ensureDimensionMatches("crs", coordinates.length, crs);
}