Search in sources :

Example 1 with CoordinateOperationContext

use of org.apache.sis.referencing.operation.CoordinateOperationContext in project sis by apache.

the class CRS method findOperations.

/**
 * Finds mathematical operations that transform or convert coordinates from the given source to the
 * given target coordinate reference system. If at least one operation exists, they are returned in
 * preference order: the operation having the widest intersection between its
 * {@linkplain AbstractCoordinateOperation#getDomainOfValidity() domain of validity}
 * and the given area of interest are returned first.
 *
 * @param  sourceCRS       the CRS of source coordinates.
 * @param  targetCRS       the CRS of target coordinates.
 * @param  areaOfInterest  the area of interest, or {@code null} if none.
 * @return mathematical operations from {@code sourceCRS} to {@code targetCRS}.
 * @throws OperationNotFoundException if no operation was found between the given pair of CRS.
 * @throws FactoryException if the operation can not be created for another reason.
 *
 * @see DefaultCoordinateOperationFactory#createOperations(CoordinateReferenceSystem, CoordinateReferenceSystem, CoordinateOperationContext)
 *
 * @since 1.0
 */
public static List<CoordinateOperation> findOperations(final CoordinateReferenceSystem sourceCRS, final CoordinateReferenceSystem targetCRS, final GeographicBoundingBox areaOfInterest) throws FactoryException {
    ArgumentChecks.ensureNonNull("sourceCRS", sourceCRS);
    ArgumentChecks.ensureNonNull("targetCRS", targetCRS);
    final CoordinateOperationContext context = CoordinateOperationContext.fromBoundingBox(areaOfInterest);
    final DefaultCoordinateOperationFactory factory = CoordinateOperations.factory();
    try {
        return factory.createOperations(sourceCRS, targetCRS, context);
    } catch (UnavailableFactoryException e) {
        if (AuthorityFactories.failure(e)) {
            throw e;
        } else
            try {
                return Collections.singletonList(factory.createOperation(sourceCRS, targetCRS, context));
            } catch (FactoryException ex) {
                ex.addSuppressed(e);
                throw ex;
            }
    }
}
Also used : FactoryException(org.opengis.util.FactoryException) UnavailableFactoryException(org.apache.sis.referencing.factory.UnavailableFactoryException) DefaultCoordinateOperationFactory(org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory) UnavailableFactoryException(org.apache.sis.referencing.factory.UnavailableFactoryException) CoordinateOperationContext(org.apache.sis.referencing.operation.CoordinateOperationContext)

Example 2 with CoordinateOperationContext

use of org.apache.sis.referencing.operation.CoordinateOperationContext in project sis by apache.

the class CRS method findOperation.

/**
 * Finds a mathematical operation that transforms or converts coordinates from the given source to the
 * given target coordinate reference system. If an estimation of the geographic area containing the points
 * to transform is known, it can be specified for helping this method to find a better suited operation.
 * If no area of interest is specified, then the current default is the widest
 * {@linkplain AbstractCoordinateOperation#getDomainOfValidity() domain of validity}.
 * A future Apache SIS version may also take the country of current locale in account.
 *
 * <div class="note"><b>Note:</b>
 * the area of interest is just one aspect that may affect the coordinate operation.
 * Other aspects are the time of interest (because some coordinate operations take in account the
 * plate tectonics movement) or the desired accuracy. For more control on the coordinate operation
 * to create, see {@link CoordinateOperationContext}.</div>
 *
 * After the caller received a {@code CoordinateOperation} instance, the following methods can be invoked
 * for checking if the operation suits the caller's needs:
 *
 * <ul>
 *   <li>{@link #getGeographicBoundingBox(CoordinateOperation)}
 *       for checking if the operation is valid in the caller's area of interest.</li>
 *   <li>{@link #getLinearAccuracy(CoordinateOperation)}
 *       for checking if the operation has sufficient accuracy for caller's purpose.</li>
 * </ul>
 *
 * If the source and target CRS are equivalent, then this method returns an operation backed by an
 * {@linkplain org.apache.sis.referencing.operation.transform.AbstractMathTransform#isIdentity() identity}
 * transform. If there is no known operation between the given pair of CRS, then this method throws an
 * {@link OperationNotFoundException}.
 *
 * @param  sourceCRS       the CRS of source coordinates.
 * @param  targetCRS       the CRS of target coordinates.
 * @param  areaOfInterest  the area of interest, or {@code null} if none.
 * @return the mathematical operation from {@code sourceCRS} to {@code targetCRS}.
 * @throws OperationNotFoundException if no operation was found between the given pair of CRS.
 * @throws FactoryException if the operation can not be created for another reason.
 *
 * @see DefaultCoordinateOperationFactory#createOperation(CoordinateReferenceSystem, CoordinateReferenceSystem, CoordinateOperationContext)
 *
 * @since 0.7
 */
public static CoordinateOperation findOperation(final CoordinateReferenceSystem sourceCRS, final CoordinateReferenceSystem targetCRS, final GeographicBoundingBox areaOfInterest) throws FactoryException {
    ArgumentChecks.ensureNonNull("sourceCRS", sourceCRS);
    ArgumentChecks.ensureNonNull("targetCRS", targetCRS);
    final CoordinateOperationContext context = CoordinateOperationContext.fromBoundingBox(areaOfInterest);
    /*
         * In principle we should just delegate to factory.createOperation(…). However this operation may fail
         * if a connection to the EPSG database has been found, but the EPSG tables do not yet exist in that
         * database and
         */
    final DefaultCoordinateOperationFactory factory = CoordinateOperations.factory();
    try {
        return factory.createOperation(sourceCRS, targetCRS, context);
    } catch (UnavailableFactoryException e) {
        if (AuthorityFactories.failure(e)) {
            throw e;
        } else
            try {
                // Above method call replaced the EPSG factory by a fallback. Try again.
                return factory.createOperation(sourceCRS, targetCRS, context);
            } catch (FactoryException ex) {
                ex.addSuppressed(e);
                throw ex;
            }
    }
}
Also used : FactoryException(org.opengis.util.FactoryException) UnavailableFactoryException(org.apache.sis.referencing.factory.UnavailableFactoryException) DefaultCoordinateOperationFactory(org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory) UnavailableFactoryException(org.apache.sis.referencing.factory.UnavailableFactoryException) CoordinateOperationContext(org.apache.sis.referencing.operation.CoordinateOperationContext)

Aggregations

UnavailableFactoryException (org.apache.sis.referencing.factory.UnavailableFactoryException)2 CoordinateOperationContext (org.apache.sis.referencing.operation.CoordinateOperationContext)2 DefaultCoordinateOperationFactory (org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory)2 FactoryException (org.opengis.util.FactoryException)2