Search in sources :

Example 6 with ConcatenatedOperation

use of org.opengis.referencing.operation.ConcatenatedOperation in project sis by apache.

the class AbstractCoordinateOperation method formatTo.

/**
 * Formats this coordinate operation in Well Known Text (WKT) version 2 format.
 *
 * @param  formatter  the formatter to use.
 * @return {@code "CoordinateOperation"}.
 *
 * @see <a href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html#113">WKT 2 specification §17</a>
 */
@Override
protected String formatTo(final Formatter formatter) {
    super.formatTo(formatter);
    formatter.newLine();
    /*
         * If the WKT is a component of a ConcatenatedOperation, do not format the source CRS since it is identical
         * to the target CRS of the previous step, or to the source CRS of the enclosing "ConcatenatedOperation" if
         * this step is the first step.
         *
         * This decision is SIS-specific since the WKT 2 specification does not define concatenated operations.
         * This choice may change in any future SIS version.
         */
    final FormattableObject enclosing = formatter.getEnclosingElement(1);
    final boolean isSubOperation = (enclosing instanceof PassThroughOperation);
    final boolean isComponent = (enclosing instanceof ConcatenatedOperation);
    if (!isSubOperation && !isComponent) {
        append(formatter, getSourceCRS(), WKTKeywords.SourceCRS);
        append(formatter, getTargetCRS(), WKTKeywords.TargetCRS);
    }
    final OperationMethod method = getMethod();
    if (method != null) {
        formatter.append(DefaultOperationMethod.castOrCopy(method));
        ParameterValueGroup parameters;
        try {
            parameters = getParameterValues();
        } catch (UnsupportedOperationException e) {
            final IdentifiedObject c = getParameterDescriptors();
            formatter.setInvalidWKT(c != null ? c : this, e);
            parameters = null;
        }
        if (parameters != null) {
            /*
                 * Format the parameter values. Apache SIS uses the EPSG geodetic dataset as the main source of
                 * parameter definitions. When a parameter is defined by both OGC and EPSG with different names,
                 * the Formatter class is responsible for choosing an appropriate name. But when the difference
                 * is more fundamental, we may have duplication. For example in the "Molodensky" operation, OGC
                 * uses source and target axis lengths while EPSG uses only difference between those lengths.
                 * In this case, OGC and EPSG parameters are defined separately and are redundant. To simplify
                 * the CoordinateOperation WKT, we omit non-EPSG parameters when we have determined that we are
                 * about to describe an EPSG operation. We could generalize this filtering to any authority, but
                 * we don't because few authorities are as complete as EPSG, so other authorities are more likely
                 * to mix EPSG or someone else components with their own. Note also that we don't apply filtering
                 * on MathTransform WKT neither for more reliable debugging.
                 */
            final boolean filter = // NOT method.getName()
            WKTUtilities.isEPSG(parameters.getDescriptor(), false) && Constants.EPSG.equalsIgnoreCase(Citations.getCodeSpace(formatter.getNameAuthority()));
            formatter.newLine();
            formatter.indent(+1);
            for (final GeneralParameterValue param : parameters.values()) {
                if (!filter || WKTUtilities.isEPSG(param.getDescriptor(), true)) {
                    WKTUtilities.append(param, formatter);
                }
            }
            formatter.indent(-1);
        }
    }
    if (!isSubOperation && !(this instanceof ConcatenatedOperation)) {
        append(formatter, getInterpolationCRS(), WKTKeywords.InterpolationCRS);
        final double accuracy = getLinearAccuracy();
        if (accuracy > 0) {
            formatter.append(new FormattableObject() {

                @Override
                protected String formatTo(final Formatter formatter) {
                    formatter.append(accuracy);
                    return WKTKeywords.OperationAccuracy;
                }
            });
        }
    }
    if (formatter.getConvention().majorVersion() == 1) {
        formatter.setInvalidWKT(this, null);
    }
    if (isComponent) {
        formatter.setInvalidWKT(this, null);
        return "CoordinateOperationStep";
    }
    return WKTKeywords.CoordinateOperation;
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Formatter(org.apache.sis.io.wkt.Formatter) InternationalString(org.opengis.util.InternationalString) FormattableObject(org.apache.sis.io.wkt.FormattableObject) OperationMethod(org.opengis.referencing.operation.OperationMethod) PassThroughOperation(org.opengis.referencing.operation.PassThroughOperation) ConcatenatedOperation(org.opengis.referencing.operation.ConcatenatedOperation) IdentifiedObject(org.opengis.referencing.IdentifiedObject) AbstractIdentifiedObject(org.apache.sis.referencing.AbstractIdentifiedObject)

Example 7 with ConcatenatedOperation

use of org.opengis.referencing.operation.ConcatenatedOperation in project sis by apache.

the class DefaultConcatenatedOperation method initialize.

/**
 * Performs the part of {@code DefaultConcatenatedOperations} construction that requires an iteration over
 * the sequence of coordinate operations. This method performs the following processing:
 *
 * <ul>
 *   <li>Verify the validity of the {@code operations} argument.</li>
 *   <li>Add the single operations in the {@code flattened} array.</li>
 *   <li>Set the {@link #transform} field to the concatenated transform.</li>
 *   <li>Set the {@link #coordinateOperationAccuracy} field, but only if {@code setAccuracy} is {@code true}.</li>
 * </ul>
 *
 * This method invokes itself recursively if there is nested {@code ConcatenatedOperation} instances
 * in the given list. This should not happen according ISO 19111 standard, but we try to be safe.
 *
 * <div class="section">How coordinate operation accuracy is determined</div>
 * If {@code setAccuracy} is {@code true}, then this method copies accuracy information found in the single
 * {@link Transformation} instance. This method ignores instances of other kinds for the following reason:
 * some {@link Conversion} instances declare an accuracy, which is typically close to zero. If a concatenated
 * operation contains such conversion together with a transformation with unknown accuracy, then we do not want
 * to declare "0 meter" as the concatenated operation accuracy; it would be a false information.
 * An other reason is that a concatenated operation typically contains an arbitrary amount of conversions,
 * but only one transformation. So considering only transformations usually means to pickup only one operation
 * in the given {@code operations} list, which make things clearer.
 *
 * <div class="note"><b>Note:</b>
 * according ISO 19111, the accuracy attribute is allowed only for transformations. However this restriction
 * is not enforced everywhere. For example the EPSG database declares an accuracy of 0 meter for conversions,
 * which is conceptually exact. In this class we are departing from strict interpretation of the specification
 * since we are adding accuracy informations to a concatenated operation. This departure should be considered
 * as a convenience feature only; accuracies are really relevant in transformations only.</div>
 *
 * @param  properties   the properties specified at construction time, or {@code null} if unknown.
 * @param  operations   the operations to concatenate.
 * @param  flattened    the destination list in which to add the {@code SingleOperation} instances.
 * @param  mtFactory    the math transform factory to use, or {@code null} for not performing concatenation.
 * @param  setSource    {@code true} for setting the {@link #sourceCRS} on the very first CRS (regardless if null or not).
 * @param  setAccuracy  {@code true} for setting the {@link #coordinateOperationAccuracy} field.
 * @param  setDomain    {@code true} for setting the {@link #domainOfValidity} field.
 * @return the last target CRS, regardless if null or not.
 * @throws FactoryException if the factory can not concatenate the math transforms.
 */
private CoordinateReferenceSystem initialize(final Map<String, ?> properties, final CoordinateOperation[] operations, final List<CoordinateOperation> flattened, final MathTransformFactory mtFactory, boolean setSource, boolean setAccuracy, boolean setDomain) throws FactoryException {
    CoordinateReferenceSystem previous = null;
    for (int i = 0; i < operations.length; i++) {
        final CoordinateOperation op = operations[i];
        ArgumentChecks.ensureNonNullElement("operations", i, op);
        /*
             * Verify consistency of user argument: for each coordinate operation, the number of dimensions of the
             * source CRS shall be equals to the number of dimensions of the target CRS in the previous operation.
             */
        final CoordinateReferenceSystem next = op.getSourceCRS();
        if (previous != null && next != null) {
            final int dim1 = previous.getCoordinateSystem().getDimension();
            final int dim2 = next.getCoordinateSystem().getDimension();
            if (dim1 != dim2) {
                throw new IllegalArgumentException(Errors.getResources(properties).getString(Errors.Keys.MismatchedDimension_3, "operations[" + i + "].sourceCRS", dim1, dim2));
            }
        }
        if (setSource) {
            setSource = false;
            // Take even if null.
            sourceCRS = next;
        }
        // For next iteration cycle.
        previous = op.getTargetCRS();
        /*
             * Now that we have verified the CRS dimensions, we should be able to concatenate the transforms.
             * If an operation is a nested ConcatenatedOperation (not allowed by ISO 19111, but we try to be
             * safe), we will first try to use the ConcatenatedOperation.transform as a whole.  Only if that
             * concatenated operation does not provide a transform we will concatenate its components.  Note
             * however that we traverse nested concatenated operations unconditionally at least for checking
             * its consistency.
             */
        MathTransform step = op.getMathTransform();
        if (op instanceof ConcatenatedOperation) {
            final List<? extends CoordinateOperation> children = ((ConcatenatedOperation) op).getOperations();
            @SuppressWarnings("SuspiciousToArrayCall") final CoordinateOperation[] asArray = children.toArray(new CoordinateOperation[children.size()]);
            initialize(properties, asArray, flattened, (step == null) ? mtFactory : null, false, setAccuracy, setDomain);
        } else if (!step.isIdentity()) {
            flattened.add(op);
        }
        if (mtFactory != null && step != null) {
            transform = (transform != null) ? mtFactory.createConcatenatedTransform(transform, step) : step;
        }
        /*
             * Optionally copy the coordinate operation accuracy from the transformation (or from a concatenated
             * operation on the assumption that its accuracy was computed by the same algorithm than this method).
             * See javadoc for a rational about why we take only transformations in account. If more than one
             * transformation is found, clear the collection and abandon the attempt to set the accuracy information.
             * Instead the user will get a better result by invoking PositionalAccuracyConstant.getLinearAccuracy(…)
             * since that method conservatively computes the sum of all linear accuracy.
             */
        if (setAccuracy && (op instanceof Transformation || op instanceof ConcatenatedOperation) && (PositionalAccuracyConstant.getLinearAccuracy(op) != 0)) {
            if (coordinateOperationAccuracy == null) {
                coordinateOperationAccuracy = op.getCoordinateOperationAccuracy();
            } else {
                coordinateOperationAccuracy = null;
                setAccuracy = false;
            }
        }
        /*
             * Optionally copy the domain of validity, provided that it is the same for all component.
             * Current implementation does not try to compute the intersection of all components.
             */
        if (setDomain) {
            final Extent domain = op.getDomainOfValidity();
            if (domain != null) {
                if (domainOfValidity == null) {
                    domainOfValidity = domain;
                } else if (!domain.equals(domainOfValidity)) {
                    domainOfValidity = null;
                    setDomain = false;
                }
            }
        }
    }
    return previous;
}
Also used : Transformation(org.opengis.referencing.operation.Transformation) MathTransform(org.opengis.referencing.operation.MathTransform) Extent(org.opengis.metadata.extent.Extent) CoordinateOperation(org.opengis.referencing.operation.CoordinateOperation) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ConcatenatedOperation(org.opengis.referencing.operation.ConcatenatedOperation)

Aggregations

ConcatenatedOperation (org.opengis.referencing.operation.ConcatenatedOperation)7 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)4 IdentifiedObject (org.opengis.referencing.IdentifiedObject)3 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)2 CompoundCRS (org.opengis.referencing.crs.CompoundCRS)2 OperationMethod (org.opengis.referencing.operation.OperationMethod)2 PassThroughOperation (org.opengis.referencing.operation.PassThroughOperation)2 SingleOperation (org.opengis.referencing.operation.SingleOperation)2 Transformation (org.opengis.referencing.operation.Transformation)2 Length (javax.measure.quantity.Length)1 FormattableObject (org.apache.sis.io.wkt.FormattableObject)1 Formatter (org.apache.sis.io.wkt.Formatter)1 DefaultAbsoluteExternalPositionalAccuracy (org.apache.sis.metadata.iso.quality.DefaultAbsoluteExternalPositionalAccuracy)1 DefaultConformanceResult (org.apache.sis.metadata.iso.quality.DefaultConformanceResult)1 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)1 IdentifiedObjectFinder (org.apache.sis.referencing.factory.IdentifiedObjectFinder)1 NoSuchAuthorityFactoryException (org.apache.sis.referencing.factory.NoSuchAuthorityFactoryException)1 Extent (org.opengis.metadata.extent.Extent)1 PositionalAccuracy (org.opengis.metadata.quality.PositionalAccuracy)1 QuantitativeResult (org.opengis.metadata.quality.QuantitativeResult)1