Search in sources :

Example 26 with ParameterDescriptorGroup

use of org.opengis.parameter.ParameterDescriptorGroup in project sis by apache.

the class DefaultMathTransformFactory method createParameterizedTransform.

/**
 * Creates a transform from a group of parameters.
 * The set of expected parameters varies for each operation.
 * The easiest way to provide parameter values is to get an initially empty group for the desired
 * operation by calling {@link #getDefaultParameters(String)}, then to fill the parameter values.
 * Example:
 *
 * {@preformat java
 *     ParameterValueGroup group = factory.getDefaultParameters("Transverse_Mercator");
 *     group.parameter("semi_major").setValue(6378137.000);
 *     group.parameter("semi_minor").setValue(6356752.314);
 *     MathTransform mt = factory.createParameterizedTransform(group, null);
 * }
 *
 * Sometime the {@code "semi_major"} and {@code "semi_minor"} parameter values are not explicitly provided,
 * but rather inferred from the {@linkplain org.apache.sis.referencing.datum.DefaultGeodeticDatum geodetic
 * datum} of the source Coordinate Reference System. If the given {@code context} argument is non-null,
 * then this method will use those contextual information for:
 *
 * <ol>
 *   <li>Inferring the {@code "semi_major"}, {@code "semi_minor"}, {@code "src_semi_major"},
 *       {@code "src_semi_minor"}, {@code "tgt_semi_major"} or {@code "tgt_semi_minor"} parameters values
 *       from the {@linkplain org.apache.sis.referencing.datum.DefaultEllipsoid ellipsoids} associated to
 *       the source or target CRS, if those parameters are not explicitly given and if they are relevant
 *       for the coordinate operation method.</li>
 *   <li>{@linkplain #createConcatenatedTransform Concatenating} the parameterized transform
 *       with any other transforms required for performing units changes and ordinates swapping.</li>
 * </ol>
 *
 * The complete group of parameters, including {@code "semi_major"}, {@code "semi_minor"} or other calculated values,
 * can be obtained by a call to {@link Context#getCompletedParameters()} after {@code createParameterizedTransform(…)}
 * returned. Note that the completed parameters may only have additional parameters compared to the given parameter
 * group; existing parameter values should not be modified.
 *
 * <p>The {@code OperationMethod} instance used by this constructor can be obtained by a call to
 * {@link #getLastMethodUsed()}.</p>
 *
 * @param  parameters  the parameter values. The {@linkplain ParameterDescriptorGroup#getName() parameter group name}
 *                     shall be the name of the desired {@linkplain DefaultOperationMethod operation method}.
 * @param  context     information about the context (for example source and target coordinate systems)
 *                     in which the new transform is going to be used, or {@code null} if none.
 * @return the transform created from the given parameters.
 * @throws NoSuchIdentifierException if there is no method for the given parameter group name.
 * @throws FactoryException if the object creation failed. This exception is thrown
 *         if some required parameter has not been supplied, or has illegal value.
 *
 * @see #getDefaultParameters(String)
 * @see #getAvailableMethods(Class)
 * @see #getLastMethodUsed()
 * @see org.apache.sis.parameter.ParameterBuilder#createGroupForMapProjection(ParameterDescriptor...)
 */
public MathTransform createParameterizedTransform(ParameterValueGroup parameters, final Context context) throws NoSuchIdentifierException, FactoryException {
    OperationMethod method = null;
    RuntimeException failure = null;
    MathTransform transform;
    try {
        ArgumentChecks.ensureNonNull("parameters", parameters);
        final ParameterDescriptorGroup descriptor = parameters.getDescriptor();
        final String methodName = descriptor.getName().getCode();
        String methodIdentifier = IdentifiedObjects.toString(IdentifiedObjects.getIdentifier(descriptor, Citations.EPSG));
        if (methodIdentifier == null) {
            methodIdentifier = methodName;
        }
        /*
             * Get the MathTransformProvider of the same name or identifier than the given parameter group.
             * We give precedence to EPSG identifier because operation method names are sometime ambiguous
             * (e.g. "Lambert Azimuthal Equal Area (Spherical)"). If we fail to find the method by its EPSG code,
             * we will try searching by method name. As a side effect, this second attempt will produce a better
             * error message if the method is really not found.
             */
        try {
            method = getOperationMethod(methodIdentifier);
        } catch (NoSuchIdentifierException exception) {
            if (methodIdentifier.equals(methodName)) {
                throw exception;
            }
            method = getOperationMethod(methodName);
            Logging.recoverableException(Logging.getLogger(Loggers.COORDINATE_OPERATION), DefaultMathTransformFactory.class, "createParameterizedTransform", exception);
        }
        if (!(method instanceof MathTransformProvider)) {
            throw new NoSuchIdentifierException(// For now, handle like an unknown operation.
            Errors.format(Errors.Keys.UnsupportedImplementation_1, Classes.getClass(method)), methodName);
        }
        /*
             * Will catch only exceptions that may be the result of improper parameter usage (e.g. a value out
             * of range). Do not catch exceptions caused by programming errors (e.g. null pointer exception).
             */
        try {
            /*
                 * If the user's parameters do not contain semi-major and semi-minor axis lengths, infer
                 * them from the ellipsoid. We have to do that because those parameters are often omitted,
                 * since the standard place where to provide this information is in the ellipsoid object.
                 */
            if (context != null) {
                failure = context.completeParameters(this, method, parameters);
                parameters = context.parameters;
                method = context.provider;
            }
            transform = ((MathTransformProvider) method).createMathTransform(this, parameters);
        } catch (IllegalArgumentException | IllegalStateException exception) {
            throw new InvalidGeodeticParameterException(exception.getLocalizedMessage(), exception);
        }
        /*
             * Cache the transform that we just created and make sure that the number of dimensions
             * is compatible with the OperationMethod instance. Then make final adjustment for axis
             * directions and units of measurement.
             */
        transform = unique(transform);
        method = DefaultOperationMethod.redimension(method, transform.getSourceDimensions(), transform.getTargetDimensions());
        if (context != null) {
            transform = swapAndScaleAxes(transform, context);
        }
    } catch (FactoryException e) {
        if (failure != null) {
            e.addSuppressed(failure);
        }
        throw e;
    } finally {
        // May be null in case of failure, which is intended.
        lastMethod.set(method);
        if (context != null) {
            context.provider = null;
        /*
                 * For now we conservatively reset the provider information to null. But if we choose to
                 * make that information public in a future SIS version, then we would remove this code.
                 */
        }
    }
    return transform;
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.util.FactoryException) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) DefaultOperationMethod(org.apache.sis.referencing.operation.DefaultOperationMethod) OperationMethod(org.opengis.referencing.operation.OperationMethod) InvalidGeodeticParameterException(org.apache.sis.referencing.factory.InvalidGeodeticParameterException) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException)

Example 27 with ParameterDescriptorGroup

use of org.opengis.parameter.ParameterDescriptorGroup in project sis by apache.

the class NormalizedProjection method getParameterDescriptors.

/**
 * Returns a description of the non-linear internal parameters of this {@code NormalizedProjection}.
 * The returned group contains at least a descriptor for the {@link #eccentricity} parameter.
 * Subclasses may add more parameters.
 *
 * <p>This method is for inspecting the parameter values of this non-linear kernel only,
 * not for inspecting the {@linkplain #getContextualParameters() contextual parameters}.
 * Inspecting the kernel parameter values is usually for debugging purpose only.</p>
 *
 * @return a description of the internal parameters.
 */
@Debug
@Override
public ParameterDescriptorGroup getParameterDescriptors() {
    Class<?> type = getClass();
    while (!Modifier.isPublic(type.getModifiers())) {
        type = type.getSuperclass();
    }
    ParameterDescriptorGroup group;
    synchronized (DESCRIPTORS) {
        group = DESCRIPTORS.get(type);
        if (group == null) {
            final ParameterBuilder builder = new ParameterBuilder().setRequired(true);
            if (type.getName().startsWith(Modules.CLASSNAME_PREFIX)) {
                builder.setCodeSpace(Citations.SIS, Constants.SIS);
            }
            final String[] names = getInternalParameterNames();
            final ParameterDescriptor<?>[] parameters = new ParameterDescriptor<?>[names.length + 1];
            parameters[0] = MapProjection.ECCENTRICITY;
            for (int i = 1; i < parameters.length; i++) {
                parameters[i] = builder.addName(names[i - 1]).create(Double.class, null);
            }
            group = builder.addName(CharSequences.camelCaseToSentence(type.getSimpleName()) + " (radians domain)").createGroup(1, 1, parameters);
            DESCRIPTORS.put(type, group);
        }
    }
    return group;
}
Also used : ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) ParameterBuilder(org.apache.sis.parameter.ParameterBuilder) Debug(org.apache.sis.util.Debug)

Aggregations

ParameterDescriptorGroup (org.opengis.parameter.ParameterDescriptorGroup)27 DefaultParameterDescriptorGroup (org.apache.sis.parameter.DefaultParameterDescriptorGroup)13 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)13 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 DependsOnMethod (org.apache.sis.test.DependsOnMethod)6 ParameterDescriptor (org.opengis.parameter.ParameterDescriptor)5 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)5 DefaultOperationMethod (org.apache.sis.referencing.operation.DefaultOperationMethod)4 ParameterNotFoundException (org.opengis.parameter.ParameterNotFoundException)4 OperationMethod (org.opengis.referencing.operation.OperationMethod)3 ArrayList (java.util.ArrayList)2 IdentityHashMap (java.util.IdentityHashMap)2 FormattableObject (org.apache.sis.io.wkt.FormattableObject)2 ParameterBuilder (org.apache.sis.parameter.ParameterBuilder)2 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)2 FactoryException (org.opengis.util.FactoryException)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 UnmodifiableArrayList (org.apache.sis.internal.util.UnmodifiableArrayList)1