use of org.opengis.referencing.crs.SingleCRS in project sis by apache.
the class ServicesForMetadata method addElements.
/**
* Initializes a horizontal, vertical and temporal extent with the values inferred from the given envelope.
*
* @param envelope the source envelope.
* @param target the target extent where to store envelope information.
* @throws TransformException if a coordinate transformation was required and failed.
*/
@Override
public void addElements(final Envelope envelope, final DefaultExtent target) throws TransformException {
final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
final SingleCRS horizontalCRS = CRS.getHorizontalComponent(crs);
final VerticalCRS verticalCRS = CRS.getVerticalComponent(crs, true);
final TemporalCRS temporalCRS = CRS.getTemporalComponent(crs);
if (horizontalCRS == null && verticalCRS == null && temporalCRS == null) {
throw new TransformException(dimensionNotFound(Resources.Keys.MissingSpatioTemporalDimension_1, crs));
}
if (horizontalCRS != null) {
final DefaultGeographicBoundingBox extent = new DefaultGeographicBoundingBox();
extent.setInclusion(Boolean.TRUE);
setBounds(envelope, extent);
target.getGeographicElements().add(extent);
}
if (verticalCRS != null) {
final DefaultVerticalExtent extent = new DefaultVerticalExtent();
setVerticalExtent(envelope, extent, crs, verticalCRS);
target.getVerticalElements().add(extent);
}
if (temporalCRS != null) {
final DefaultTemporalExtent extent = new DefaultTemporalExtent();
setTemporalExtent(envelope, extent, crs, temporalCRS);
target.getTemporalElements().add(extent);
}
}
use of org.opengis.referencing.crs.SingleCRS in project sis by apache.
the class ServicesForMetadata method setBounds.
/**
* Sets the geographic, vertical and temporal extents with the values inferred from the given envelope.
* If the given {@code target} has more geographic or vertical extents than needed (0 or 1), then the
* extraneous extents are removed.
*
* @param envelope the source envelope.
* @param target the target spatiotemporal extent where to store envelope information.
* @throws TransformException if no temporal component can be extracted from the given envelope.
*/
@Override
public void setBounds(final Envelope envelope, final DefaultSpatialTemporalExtent target) throws TransformException {
final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
final SingleCRS horizontalCRS = CRS.getHorizontalComponent(crs);
final VerticalCRS verticalCRS = CRS.getVerticalComponent(crs, true);
final TemporalCRS temporalCRS = CRS.getTemporalComponent(crs);
if (horizontalCRS == null && verticalCRS == null && temporalCRS == null) {
throw new TransformException(dimensionNotFound(Resources.Keys.MissingSpatioTemporalDimension_1, crs));
}
/*
* Try to set the geographic bounding box first, because this operation may fail with a
* TransformException while the other operations (vertical and temporal) should not fail.
* So doing the geographic part first help us to get a "all or nothing" behavior.
*/
DefaultGeographicBoundingBox box = null;
boolean useExistingBox = (horizontalCRS != null);
final Collection<GeographicExtent> spatialExtents = target.getSpatialExtent();
final Iterator<GeographicExtent> it = spatialExtents.iterator();
while (it.hasNext()) {
final GeographicExtent extent = it.next();
if (extent instanceof GeographicBoundingBox) {
if (useExistingBox && (extent instanceof DefaultGeographicBoundingBox)) {
box = (DefaultGeographicBoundingBox) extent;
useExistingBox = false;
} else {
it.remove();
}
}
}
if (horizontalCRS != null) {
if (box == null) {
box = new DefaultGeographicBoundingBox();
spatialExtents.add(box);
}
GeographicCRS normalizedCRS = ReferencingUtilities.toNormalizedGeographicCRS(crs);
if (normalizedCRS == null) {
normalizedCRS = CommonCRS.defaultGeographic();
}
setGeographicExtent(envelope, box, crs, normalizedCRS);
}
/*
* Other dimensions (vertical and temporal).
*/
if (verticalCRS != null) {
VerticalExtent e = target.getVerticalExtent();
if (!(e instanceof DefaultVerticalExtent)) {
e = new DefaultVerticalExtent();
target.setVerticalExtent(e);
}
setVerticalExtent(envelope, (DefaultVerticalExtent) e, crs, verticalCRS);
} else {
target.setVerticalExtent(null);
}
if (temporalCRS != null) {
setTemporalExtent(envelope, target, crs, temporalCRS);
} else {
target.setExtent(null);
}
}
use of org.opengis.referencing.crs.SingleCRS in project sis by apache.
the class CC_Conversion method setBaseCRS.
/**
* Temporarily stores the {@code baseCRS} associated to the given {@code Conversion}. This temporary storage is
* needed because {@code org.apache.sis.referencing.crs.AbstractDerivedCRS} does not have any explicit field for
* {@code baseCRS}. Instead the base CRS is stored in {@link Conversion#getSourceCRS()}, but we can set this
* property only after the {@code DerivedCRS} coordinate system has been unmarshalled.
*
* See {@code AbstractDerivedCRS.afterUnmarshal(Unmarshaller, Object parent)} for more information.
*
* @param conversion the conversion to which to associate a base CRS.
* @param crs the base CRS to associate to the given conversion.
* @return the previous base CRS, or {@code null} if none.
*/
public static SingleCRS setBaseCRS(final Conversion conversion, final SingleCRS crs) {
/*
* Implementation note: we store the base CRS in the marshalling context because:
*
* - we want to keep each thread isolated (using ThreadLocal), and
* - we want to make sure that the reference is disposed even if the unmarshaller throws an exception.
* This is guaranteed because the Context is disposed by Apache SIS in "try … finally" constructs.
*/
final PropertyType<?, ?> wrapper = Context.getWrapper(Context.current());
if (wrapper instanceof CC_Conversion) {
final CC_Conversion c = (CC_Conversion) wrapper;
if (c.getElement() == conversion) {
// For making sure that we do not confuse with another conversion.
final SingleCRS previous = c.baseCRS;
c.baseCRS = crs;
return previous;
}
}
return null;
}
use of org.opengis.referencing.crs.SingleCRS in project sis by apache.
the class DefinitionVerifier method diffCode.
/**
* Returns a code indicating in which part the two given CRS differ. The given iterators usually iterate over
* exactly one element, but may iterate over more elements if the CRS were instance of {@code CompoundCRS}.
* The returned value is one of {@link #METHOD}, {@link #CONVERSION}, {@link #CS}, {@link #DATUM},
* {@link #PRIME_MERIDIAN} or {@link #OTHER} constants.
*/
private static int diffCode(final Iterator<SingleCRS> authoritative, final Iterator<SingleCRS> given) {
while (authoritative.hasNext() && given.hasNext()) {
final SingleCRS crsA = authoritative.next();
final SingleCRS crsG = given.next();
if (!Utilities.equalsApproximatively(crsA, crsG)) {
if (crsA instanceof GeneralDerivedCRS && crsG instanceof GeneralDerivedCRS) {
final Conversion cnvA = ((GeneralDerivedCRS) crsA).getConversionFromBase();
final Conversion cnvG = ((GeneralDerivedCRS) crsG).getConversionFromBase();
if (!Utilities.equalsApproximatively(cnvA, cnvG)) {
return Utilities.equalsApproximatively(cnvA.getMethod(), cnvG.getMethod()) ? CONVERSION : METHOD;
}
}
if (!Utilities.equalsApproximatively(crsA.getCoordinateSystem(), crsG.getCoordinateSystem())) {
return CS;
}
final Datum datumA = crsA.getDatum();
final Datum datumG = crsG.getDatum();
if (!Utilities.equalsApproximatively(datumA, datumG)) {
if ((datumA instanceof GeodeticDatum) && (datumG instanceof GeodeticDatum) && !Utilities.equalsApproximatively(((GeodeticDatum) datumA).getPrimeMeridian(), ((GeodeticDatum) datumG).getPrimeMeridian())) {
return PRIME_MERIDIAN;
}
return DATUM;
}
break;
}
}
return OTHER;
}
use of org.opengis.referencing.crs.SingleCRS in project sis by apache.
the class CRSTest method testComponentsOfProjectedCRS.
/**
* Tests getting the horizontal and vertical components of a three-dimensional projected CRS.
*
* @since 0.8
*/
@Test
public void testComponentsOfProjectedCRS() {
final ProjectedCRS volumetric = HardCodedConversions.mercator3D();
assertFalse("isHorizontalCRS", CRS.isHorizontalCRS(volumetric));
assertNull("getTemporalComponent", CRS.getTemporalComponent(volumetric));
assertNull("getVerticalComponent", CRS.getVerticalComponent(volumetric, false));
assertEqualsIgnoreMetadata(HardCodedCRS.ELLIPSOIDAL_HEIGHT, CRS.getVerticalComponent(volumetric, true));
final SingleCRS horizontal = CRS.getHorizontalComponent(volumetric);
assertInstanceOf("getHorizontalComponent", ProjectedCRS.class, horizontal);
assertEquals("dimension", 2, horizontal.getCoordinateSystem().getDimension());
assertTrue("isHorizontalCRS", CRS.isHorizontalCRS(horizontal));
}
Aggregations