Search in sources :

Example 1 with OperationNotFoundException

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

the class SubOperationInfo method create.

/**
 * Searches in given list of source components for an operation capable to convert or transform coordinates
 * to the given target CRS. If no such operation can be found, then this method returns {@code null}.
 *
 * @param  caller        the object which is inferring a coordinate operation.
 * @param  sourceIsUsed  flags for keeping trace of which source has been used.
 * @param  sources       all components of the source CRS.
 * @param  target        one component of the target CRS.
 * @return information about a coordinate operation from a source CRS to the given target CRS, or {@code null}.
 * @throws FactoryException if an error occurred while grabbing a coordinate operation.
 */
static SubOperationInfo create(final CoordinateOperationFinder caller, final boolean[] sourceIsUsed, final List<? extends SingleCRS> sources, final SingleCRS target) throws FactoryException {
    OperationNotFoundException failure = null;
    final Class<?> targetType = type(target);
    for (final Class<?>[] sourceTypes : COMPATIBLE_TYPES) {
        if (sourceTypes[0].isAssignableFrom(targetType)) {
            for (final Class<?> sourceType : sourceTypes) {
                int startAtDimension;
                int endAtDimension = 0;
                for (int i = 0; i < sourceIsUsed.length; i++) {
                    final SingleCRS source = sources.get(i);
                    startAtDimension = endAtDimension;
                    endAtDimension += source.getCoordinateSystem().getDimension();
                    if (!sourceIsUsed[i] && sourceType.isAssignableFrom(type(source))) {
                        final CoordinateOperation operation;
                        try {
                            operation = caller.createOperation(source, target);
                        } catch (OperationNotFoundException exception) {
                            if (failure == null) {
                                failure = exception;
                            } else {
                                failure.addSuppressed(exception);
                            }
                            continue;
                        }
                        /*
                             * Found an operation.  Exclude the source component from the list because each source
                             * should be used at most once by SourceComponent. Note that the same source may still
                             * be used again in another context if that source is also an interpolation CRS.
                             *
                             * EXAMPLE: consider a coordinate operation from (GeodeticCRS₁, VerticalCRS₁) source
                             * to (GeodeticCRS₂, VerticalCRS₂) target.  The source GeodeticCRS₁ should be mapped
                             * to exactly one target component (which is GeodeticCRS₂)  and  VerticalCRS₁ mapped
                             * to VerticalCRS₂.  But the operation on vertical coordinates may need GeodeticCRS₁
                             * for doing its work, so GeodeticCRS₁ is needed twice.  However when needed for the
                             * vertical coordinate operation, the GeodeticCRS₁ is used as an "interpolation CRS".
                             * Interpolation CRS are handled in other code paths; it is not the business of this
                             * SourceComponent class to care about them. From the point of view of this class,
                             * GeodeticCRS₁ is used only once.
                             */
                        sourceIsUsed[i] = true;
                        if (failure != null) {
                            Logging.recoverableException(Logging.getLogger(Loggers.COORDINATE_OPERATION), CoordinateOperationFinder.class, "decompose", failure);
                        }
                        return new SubOperationInfo(operation, startAtDimension, endAtDimension);
                    }
                }
            }
        }
    }
    if (failure != null) {
        throw failure;
    }
    return null;
}
Also used : OperationNotFoundException(org.opengis.referencing.operation.OperationNotFoundException) CoordinateOperation(org.opengis.referencing.operation.CoordinateOperation)

Example 2 with OperationNotFoundException

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

the class CoordinateOperationFinderTest method testIncompatibleVerticalCRS.

/**
 * Tests that an exception is thrown on attempt to grab a transformation between incompatible vertical CRS.
 *
 * @throws FactoryException if an exception other than the expected one occurred.
 */
@Test
@DependsOnMethod("testIdentityTransform")
public void testIncompatibleVerticalCRS() throws FactoryException {
    final VerticalCRS sourceCRS = CommonCRS.Vertical.NAVD88.crs();
    final VerticalCRS targetCRS = CommonCRS.Vertical.MEAN_SEA_LEVEL.crs();
    try {
        finder.createOperation(sourceCRS, targetCRS);
        fail("The operation should have failed.");
    } catch (OperationNotFoundException e) {
        final String message = e.getMessage();
        assertTrue(message, message.contains("North American Vertical Datum"));
        assertTrue(message, message.contains("Mean Sea Level"));
    }
}
Also used : OperationNotFoundException(org.opengis.referencing.operation.OperationNotFoundException) VerticalCRS(org.opengis.referencing.crs.VerticalCRS) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 3 with OperationNotFoundException

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

the class CoordinateSystemTransform method create.

/**
 * Implementation of {@link DefaultMathTransformFactory#createCoordinateSystemChange(CoordinateSystem,
 * CoordinateSystem, Ellipsoid)}, defined here for reducing the {@code DefaultMathTransformFactory}
 * weight in the common case where the conversions handled by this class are not needed.
 */
static MathTransform create(final MathTransformFactory factory, final CoordinateSystem source, final CoordinateSystem target) throws FactoryException {
    int passthrough = 0;
    CoordinateSystemTransform kernel = null;
    if (source instanceof CartesianCS) {
        if (target instanceof SphericalCS) {
            kernel = CartesianToSpherical.INSTANCE;
        } else if (target instanceof PolarCS) {
            kernel = CartesianToPolar.INSTANCE;
        } else if (target instanceof CylindricalCS) {
            kernel = CartesianToPolar.INSTANCE;
            passthrough = 1;
        }
    } else if (target instanceof CartesianCS) {
        if (source instanceof SphericalCS) {
            kernel = SphericalToCartesian.INSTANCE;
        } else if (source instanceof PolarCS) {
            kernel = PolarToCartesian.INSTANCE;
        } else if (source instanceof CylindricalCS) {
            kernel = PolarToCartesian.INSTANCE;
            passthrough = 1;
        }
    }
    Exception cause = null;
    try {
        if (kernel == null) {
            return factory.createAffineTransform(CoordinateSystems.swapAndScaleAxes(source, target));
        } else if (source.getDimension() == kernel.getSourceDimensions() + passthrough && target.getDimension() == kernel.getTargetDimensions() + passthrough) {
            final MathTransform tr = (passthrough == 0) ? kernel.completeTransform(factory) : kernel.passthrough(factory);
            final MathTransform before = factory.createAffineTransform(CoordinateSystems.swapAndScaleAxes(source, CoordinateSystems.replaceAxes(source, AxesConvention.NORMALIZED)));
            final MathTransform after = factory.createAffineTransform(CoordinateSystems.swapAndScaleAxes(CoordinateSystems.replaceAxes(target, AxesConvention.NORMALIZED), target));
            return factory.createConcatenatedTransform(before, factory.createConcatenatedTransform(tr, after));
        }
    } catch (IllegalArgumentException | IncommensurableException e) {
        cause = e;
    }
    throw new OperationNotFoundException(Resources.format(Resources.Keys.CoordinateOperationNotFound_2, WKTUtilities.toType(CoordinateSystem.class, source.getClass()), WKTUtilities.toType(CoordinateSystem.class, target.getClass())), cause);
}
Also used : CartesianCS(org.opengis.referencing.cs.CartesianCS) SphericalCS(org.opengis.referencing.cs.SphericalCS) PolarCS(org.opengis.referencing.cs.PolarCS) CylindricalCS(org.opengis.referencing.cs.CylindricalCS) OperationNotFoundException(org.opengis.referencing.operation.OperationNotFoundException) MathTransform(org.opengis.referencing.operation.MathTransform) IncommensurableException(javax.measure.IncommensurableException) OperationNotFoundException(org.opengis.referencing.operation.OperationNotFoundException) IncommensurableException(javax.measure.IncommensurableException) FactoryException(org.opengis.util.FactoryException)

Aggregations

OperationNotFoundException (org.opengis.referencing.operation.OperationNotFoundException)3 IncommensurableException (javax.measure.IncommensurableException)1 DependsOnMethod (org.apache.sis.test.DependsOnMethod)1 Test (org.junit.Test)1 VerticalCRS (org.opengis.referencing.crs.VerticalCRS)1 CartesianCS (org.opengis.referencing.cs.CartesianCS)1 CylindricalCS (org.opengis.referencing.cs.CylindricalCS)1 PolarCS (org.opengis.referencing.cs.PolarCS)1 SphericalCS (org.opengis.referencing.cs.SphericalCS)1 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)1 MathTransform (org.opengis.referencing.operation.MathTransform)1 FactoryException (org.opengis.util.FactoryException)1