Java Code Examples for org.opengis.referencing.crs.GeographicCRS#getCoordinateSystem()

The following examples show how to use org.opengis.referencing.crs.GeographicCRS#getCoordinateSystem() . 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: CommonCRSTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the {@link CommonCRS#normalizedGeographic()} method.
 */
@Test
@DependsOnMethod("testGeographic")
public void testNormalizedGeographic() {
    final GeographicCRS geographic = CommonCRS.WGS84.geographic();
    final GeographicCRS normalized = CommonCRS.WGS84.normalizedGeographic();
    Validators.validate(normalized);
    assertSame(geographic.getDatum(), normalized.getDatum());
    /*
     * Compare axes. Note that axes in different order have different EPSG codes.
     */
    final CoordinateSystem φλ = geographic.getCoordinateSystem();
    final CoordinateSystem λφ = normalized.getCoordinateSystem();
    assertEqualsIgnoreMetadata(φλ.getAxis(1), λφ.getAxis(0));       // Longitude
    assertEqualsIgnoreMetadata(φλ.getAxis(0), λφ.getAxis(1));       // Latitude
    assertSame("Cached value", normalized, CommonCRS.WGS84.normalizedGeographic());
}
 
Example 2
Source File: CommonCRSTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the {@link CommonCRS#geographic3D()} method.
 */
@Test
@DependsOnMethod("testGeographic")
public void testGeographic3D() {
    final GeographicCRS crs = CommonCRS.WGS72.geographic3D();
    Validators.validate(crs);
    assertEquals ("WGS 72", crs.getName().getCode());
    assertSame   (CommonCRS.WGS72.geographic().getDatum(), crs.getDatum());
    assertNotSame(CommonCRS.WGS84.geographic().getDatum(), crs.getDatum());

    final EllipsoidalCS cs = crs.getCoordinateSystem();
    final String name = cs.getName().getCode();
    assertTrue(name, name.startsWith("Ellipsoidal 3D"));
    assertEquals("dimension", 3, cs.getDimension());
    assertAxisDirectionsEqual(name, cs, AxisDirection.NORTH, AxisDirection.EAST, AxisDirection.UP);
    assertSame("Cached value", crs, CommonCRS.WGS72.geographic3D());
}
 
Example 3
Source File: CRSBuilder.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Possibly sets {@link #datum}, {@link #coordinateSystem} and {@link #referenceSystem}
 * to predefined objects matching the axes defined in the netCDF file.
 */
@Override void setPredefinedComponents(final Decoder decoder) throws FactoryException {
    super.setPredefinedComponents(decoder);
    if (isPredefinedCS(Units.DEGREE)) {
        GeographicCRS crs;
        if (is3D()) {
            crs = defaultCRS.geographic3D();
            if (isLongitudeFirst) {
                crs = DefaultGeographicCRS.castOrCopy(crs).forConvention(AxesConvention.RIGHT_HANDED);
            }
        } else if (isLongitudeFirst) {
            crs = defaultCRS.normalizedGeographic();
        } else {
            crs = defaultCRS.geographic();
        }
        referenceSystem  = crs;
        coordinateSystem = crs.getCoordinateSystem();
        datum            = crs.getDatum();
    } else {
        datum = defaultCRS.datum();
        final Integer epsg = epsgCandidateCS(Units.DEGREE);
        if (epsg != null) try {
            coordinateSystem = decoder.getCSAuthorityFactory().createEllipsoidalCS(epsg.toString());
        } catch (NoSuchAuthorityCodeException e) {
            recoverableException(e);
        }
    }
}
 
Example 4
Source File: IdentifiedObjectFinderTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the {@link IdentifiedObjectFinder#findSingleton(IdentifiedObject)} method.
 *
 * @throws FactoryException if the creation of a CRS failed.
 */
@Test
public void testFindSingleton() throws FactoryException {
    final GeographicCRS CRS84 = factory.createGeographicCRS("CRS:84");
    final IdentifiedObjectFinder finder = factory.newIdentifiedObjectFinder();
    assertEquals("Newly created finder should default to full scan.",
            IdentifiedObjectFinder.Domain.VALID_DATASET, finder.getSearchDomain());

    finder.setSearchDomain(IdentifiedObjectFinder.Domain.DECLARATION);
    assertSame("Should find without the need for scan, since we can use the CRS:84 identifier.",
               CRS84, finder.findSingleton(CRS84));

    finder.setSearchDomain(IdentifiedObjectFinder.Domain.VALID_DATASET);
    assertSame("Allowing scanning should not make any difference for this CRS84 instance.",
               CRS84, finder.findSingleton(CRS84));
    /*
     * Same test than above, using a CRS without identifier.
     * The intent is to force a full scan.
     */
    final CoordinateReferenceSystem search = new DefaultGeographicCRS(
            Collections.singletonMap(DefaultGeographicCRS.NAME_KEY, CRS84.getName()),
            CRS84.getDatum(), CRS84.getCoordinateSystem());
    assertEqualsIgnoreMetadata(CRS84, search);              // Required condition for next test.

    finder.setSearchDomain(IdentifiedObjectFinder.Domain.DECLARATION);
    assertNull("Should not find WGS84 without a full scan, since it does not contains the CRS:84 identifier.",
               finder.findSingleton(search));

    finder.setSearchDomain(IdentifiedObjectFinder.Domain.VALID_DATASET);
    assertSame("A full scan should allow us to find WGS84, since it is equals ignoring metadata to CRS:84.",
               CRS84, finder.findSingleton(search));
}
 
Example 5
Source File: ServicesForMetadata.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Implementation of the public {@code setBounds(…, DefaultGeographicBoundingBox, …)} methods for
 * the horizontal extent. If the {@code crs} argument is null, then it is caller's responsibility
 * to ensure that the given envelope is two-dimensional.
 *
 * <p>If {@code findOpCaller} is non-null, then this method will be executed in optional mode:
 * some failures will cause this method to return {@code null} instead than throwing an exception.
 * Note that {@link TransformException} may still be thrown but not directly by this method.
 * Warning may be logged, but in such case this method presumes that public caller is the named
 * method from {@link Envelopes} — typically {@link Envelopes#findOperation(Envelope, Envelope)}.</p>
 *
 * @param  envelope       the source envelope.
 * @param  target         the target bounding box, or {@code null} for creating it automatically.
 * @param  crs            the envelope CRS, or {@code null} if unknown.
 * @param  normalizedCRS  the horizontal component of the given CRS, or null if the {@code crs} argument is null.
 * @param  findOpCaller   non-null for replacing some (not all) exceptions by {@code null} return value.
 * @return the bounding box or {@code null} on failure. Never {@code null} if {@code findOpCaller} argument is {@code null}.
 * @throws TransformException if the given envelope can not be transformed.
 */
private static DefaultGeographicBoundingBox setGeographicExtent(Envelope envelope, DefaultGeographicBoundingBox target,
        final CoordinateReferenceSystem crs, final GeographicCRS normalizedCRS, final String findOpCaller) throws TransformException
{
    if (normalizedCRS != null) {
        // No need to check for dimension, since GeodeticCRS can not have less than 2.
        final CoordinateSystem cs1 = crs.getCoordinateSystem();
        final CoordinateSystem cs2 = normalizedCRS.getCoordinateSystem();
        if (!Utilities.equalsIgnoreMetadata(cs2.getAxis(0), cs1.getAxis(0)) ||
            !Utilities.equalsIgnoreMetadata(cs2.getAxis(1), cs1.getAxis(1)))
        {
            final CoordinateOperation operation;
            final CoordinateOperationFactory factory = CoordinateOperations.factory();
            try {
                operation = factory.createOperation(crs, normalizedCRS);
            } catch (FactoryException e) {
                if (findOpCaller != null) {
                    // See javadoc for the assumption that optional mode is used by Envelopes.findOperation(…).
                    Logging.recoverableException(Logging.getLogger(Modules.REFERENCING), Envelopes.class, findOpCaller, e);
                    return null;
                }
                throw new TransformException(Resources.format(Resources.Keys.CanNotTransformEnvelopeToGeodetic), e);
            }
            envelope = Envelopes.transform(operation, envelope);
        }
    }
    /*
     * At this point, the envelope should use (longitude, latitude) coordinates in degrees.
     * The envelope may cross the anti-meridian if the envelope implementation is an Apache SIS one.
     * For other implementations, the longitude range may be conservatively expanded to [-180 … 180]°.
     */
    double westBoundLongitude, eastBoundLongitude;
    double southBoundLatitude, northBoundLatitude;
    if (envelope instanceof AbstractEnvelope) {
        final AbstractEnvelope ae = (AbstractEnvelope) envelope;
        westBoundLongitude = ae.getLower(0);
        eastBoundLongitude = ae.getUpper(0);            // Cross anti-meridian if eastBoundLongitude < westBoundLongitude.
        southBoundLatitude = ae.getLower(1);
        northBoundLatitude = ae.getUpper(1);
    } else {
        westBoundLongitude = envelope.getMinimum(0);
        eastBoundLongitude = envelope.getMaximum(0);    // Expanded to [-180 … 180]° if it was crossing the anti-meridian.
        southBoundLatitude = envelope.getMinimum(1);
        northBoundLatitude = envelope.getMaximum(1);
    }
    /*
     * The envelope transformation at the beginning of this method intentionally avoided to apply datum shift.
     * This implies that the prime meridian has not been changed and may be something else than Greenwich.
     * We need to take it in account manually.
     *
     * Note that there is no need to normalize the longitudes back to the [-180 … +180]° range after the rotation, or
     * to verify if the longitude span is 360°. Those verifications will be done automatically by target.setBounds(…).
     */
    if (normalizedCRS != null) {
        final double rotation = CRS.getGreenwichLongitude(normalizedCRS);
        westBoundLongitude += rotation;
        eastBoundLongitude += rotation;
    }
    /*
     * In the particular case where this method is invoked (indirectly) for Envelopes.findOperation(…) purposes,
     * replace NaN values by the whole world.  We do that only for Envelopes.findOperation(…) since we know that
     * the geographic bounding box will be used for choosing a CRS, and a conservative approach is to select the
     * CRS valid in the widest area. If this method is invoked for other usages, then we keep NaN values because
     * we don't know the context (union, intersection, something else?).
     */
    if (findOpCaller != null) {
        if (Double.isNaN(southBoundLatitude)) southBoundLatitude = Latitude.MIN_VALUE;
        if (Double.isNaN(northBoundLatitude)) northBoundLatitude = Latitude.MAX_VALUE;
        if (Double.isNaN(eastBoundLongitude) || Double.isNaN(westBoundLongitude)) {
            // Conservatively set the two bounds because may be spanning the anti-meridian.
            eastBoundLongitude = Longitude.MIN_VALUE;
            westBoundLongitude = Longitude.MAX_VALUE;
        }
    }
    if (target == null) {
        target = new DefaultGeographicBoundingBox();
    }
    target.setBounds(westBoundLongitude, eastBoundLongitude, southBoundLatitude, northBoundLatitude);
    target.setInclusion(Boolean.TRUE);
    return target;
}