Search in sources :

Example 1 with Transformation

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

the class PositionalAccuracyConstant method getLinearAccuracy.

/**
 * Convenience method returning the accuracy in meters for the specified operation.
 * This method tries each of the following procedures and returns the first successful one:
 *
 * <ul>
 *   <li>If at least one {@link QuantitativeResult} is found with a linear unit, then the largest
 *       accuracy estimate is converted to {@linkplain Units#METRE metres} and returned.</li>
 *   <li>Otherwise, if the operation is a {@link Conversion}, then returns 0 since a conversion
 *       is by definition accurate up to rounding errors.</li>
 *   <li>Otherwise, if the operation is a {@link Transformation}, then checks if the datum shift
 *       were applied with the help of Bursa-Wolf parameters. This procedure looks for SIS-specific
 *       {@link #DATUM_SHIFT_APPLIED} and {@link #DATUM_SHIFT_OMITTED DATUM_SHIFT_OMITTED} constants.</li>
 *   <li>Otherwise, if the operation is a {@link ConcatenatedOperation}, returns the sum of the accuracy
 *       of all components. This is a conservative scenario where we assume that errors cumulate linearly.
 *       Note that this is not necessarily the "worst case" scenario since the accuracy could be worst
 *       if the math transforms are highly non-linear.</li>
 * </ul>
 *
 * If the above is modified, please update {@code AbstractCoordinateOperation.getLinearAccuracy()} javadoc.
 *
 * @param  operation  the operation to inspect for accuracy.
 * @return the accuracy estimate (always in meters), or NaN if unknown.
 *
 * @see org.apache.sis.referencing.operation.AbstractCoordinateOperation#getLinearAccuracy()
 */
public static double getLinearAccuracy(final CoordinateOperation operation) {
    double accuracy = Double.NaN;
    final Collection<PositionalAccuracy> accuracies = operation.getCoordinateOperationAccuracy();
    for (final PositionalAccuracy metadata : accuracies) {
        for (final Result result : metadata.getResults()) {
            if (result instanceof QuantitativeResult) {
                final QuantitativeResult quantity = (QuantitativeResult) result;
                final Collection<? extends Record> records = quantity.getValues();
                if (records != null) {
                    final Unit<?> unit = quantity.getValueUnit();
                    if (Units.isLinear(unit)) {
                        final Unit<Length> unitOfLength = unit.asType(Length.class);
                        for (final Record record : records) {
                            for (final Object value : record.getAttributes().values()) {
                                if (value instanceof Number) {
                                    double v = ((Number) value).doubleValue();
                                    v = unitOfLength.getConverterTo(Units.METRE).convert(v);
                                    if (v >= 0 && !(v <= accuracy)) {
                                        // '!' is for replacing the NaN value.
                                        accuracy = v;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (Double.isNaN(accuracy)) {
        /*
             * No quantitative (linear) accuracy were found. If the coordinate operation is actually
             * a conversion, the accuracy is up to rounding error (i.e. conceptually 0) by definition.
             */
        if (operation instanceof Conversion) {
            return 0;
        }
        /*
             * If the coordinate operation is actually a transformation, checks if Bursa-Wolf parameters
             * were available for the datum shift. This is SIS-specific. See field javadoc for a rational
             * about the return values chosen.
             */
        if (operation instanceof Transformation) {
            if (accuracies.contains(DATUM_SHIFT_APPLIED)) {
                return DATUM_SHIFT_ACCURACY;
            }
            if (accuracies.contains(DATUM_SHIFT_OMITTED)) {
                return UNKNOWN_ACCURACY;
            }
        }
        /*
             * If the coordinate operation is a compound of other coordinate operations, returns the sum of their accuracy,
             * skipping unknown ones. Making the sum is a conservative approach (not exactly the "worst case" scenario,
             * since it could be worst if the transforms are highly non-linear).
             */
        if (operation instanceof ConcatenatedOperation) {
            for (final CoordinateOperation op : ((ConcatenatedOperation) operation).getOperations()) {
                final double candidate = Math.abs(getLinearAccuracy(op));
                if (!Double.isNaN(candidate)) {
                    if (Double.isNaN(accuracy)) {
                        accuracy = candidate;
                    } else {
                        accuracy += candidate;
                    }
                }
            }
        }
    }
    return accuracy;
}
Also used : Transformation(org.opengis.referencing.operation.Transformation) PositionalAccuracy(org.opengis.metadata.quality.PositionalAccuracy) DefaultAbsoluteExternalPositionalAccuracy(org.apache.sis.metadata.iso.quality.DefaultAbsoluteExternalPositionalAccuracy) CoordinateOperation(org.opengis.referencing.operation.CoordinateOperation) Conversion(org.opengis.referencing.operation.Conversion) Result(org.opengis.metadata.quality.Result) QuantitativeResult(org.opengis.metadata.quality.QuantitativeResult) DefaultConformanceResult(org.apache.sis.metadata.iso.quality.DefaultConformanceResult) QuantitativeResult(org.opengis.metadata.quality.QuantitativeResult) Length(javax.measure.quantity.Length) Record(org.opengis.util.Record) ConcatenatedOperation(org.opengis.referencing.operation.ConcatenatedOperation)

Example 2 with Transformation

use of org.opengis.referencing.operation.Transformation 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)2 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)2 Transformation (org.opengis.referencing.operation.Transformation)2 Length (javax.measure.quantity.Length)1 DefaultAbsoluteExternalPositionalAccuracy (org.apache.sis.metadata.iso.quality.DefaultAbsoluteExternalPositionalAccuracy)1 DefaultConformanceResult (org.apache.sis.metadata.iso.quality.DefaultConformanceResult)1 Extent (org.opengis.metadata.extent.Extent)1 PositionalAccuracy (org.opengis.metadata.quality.PositionalAccuracy)1 QuantitativeResult (org.opengis.metadata.quality.QuantitativeResult)1 Result (org.opengis.metadata.quality.Result)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 Conversion (org.opengis.referencing.operation.Conversion)1 MathTransform (org.opengis.referencing.operation.MathTransform)1 Record (org.opengis.util.Record)1