Search in sources :

Example 6 with ParameterNotFoundException

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

the class DefaultParameterDescriptorGroup method descriptor.

/**
 * Returns the first parameter in this group for the specified name.
 * This method does not search in sub-groups.
 *
 * @param  name  the name of the parameter to search for.
 * @return the parameter for the given identifier name.
 * @throws ParameterNotFoundException if there is no parameter for the given name.
 */
@Override
@SuppressWarnings("null")
public GeneralParameterDescriptor descriptor(final String name) throws ParameterNotFoundException {
    // Quick search for an exact match.
    ArgumentChecks.ensureNonNull("name", name);
    for (final GeneralParameterDescriptor param : descriptors) {
        if (name.equals(param.getName().getCode())) {
            return param;
        }
    }
    // More costly search before to give up.
    GeneralParameterDescriptor fallback = null, ambiguity = null;
    for (final GeneralParameterDescriptor param : descriptors) {
        if (IdentifiedObjects.isHeuristicMatchForName(param, name)) {
            if (fallback == null) {
                fallback = param;
            } else {
                ambiguity = param;
            }
        }
    }
    if (fallback != null && ambiguity == null) {
        return fallback;
    }
    throw new ParameterNotFoundException(ambiguity != null ? Errors.format(Errors.Keys.AmbiguousName_3, IdentifiedObjects.toString(fallback.getName()), IdentifiedObjects.toString(ambiguity.getName()), name) : Resources.format(Resources.Keys.ParameterNotFound_2, Verifier.getDisplayName(this), name), name);
}
Also used : GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 7 with ParameterNotFoundException

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

the class DefaultParameterValueGroup method parameterIfExist.

/**
 * Returns the value in this group for the specified name if it exists, or {@code null} if none.
 * This method does not create any new {@code ParameterValue} instance.
 *
 * @see #isKnownImplementation()
 */
@Override
ParameterValue<?> parameterIfExist(final String name) throws ParameterNotFoundException {
    // Protect against accidental changes.
    final ParameterValueList values = this.values;
    /*
         * Search for an exact match. By invoking 'descriptor(i)' instead of 'get(i)', we avoid the
         * creation of mandatory ParameterValue which was deferred. If we find a matching name, the
         * ParameterValue will be lazily created (if not already done) by the call to 'get(i)'.
         */
    int index = -1;
    int ambiguity = -1;
    final int size = values.size();
    for (int i = 0; i < size; i++) {
        final GeneralParameterDescriptor descriptor = values.descriptor(i);
        if (descriptor instanceof ParameterDescriptor<?>) {
            if (IdentifiedObjects.isHeuristicMatchForName(descriptor, name)) {
                if (index < 0) {
                    index = i;
                } else {
                    ambiguity = i;
                }
            }
        }
    }
    if (ambiguity < 0) {
        // May lazily create a ParameterValue.
        return (index >= 0) ? (ParameterValue<?>) values.get(index) : null;
    }
    final GeneralParameterDescriptor d1 = values.descriptor(index);
    final GeneralParameterDescriptor d2 = values.descriptor(ambiguity);
    final String message;
    if (d1 == d2) {
        message = Errors.format(Errors.Keys.MultiOccurenceValueAtIndices_3, name, index, ambiguity);
    } else {
        message = Errors.format(Errors.Keys.AmbiguousName_3, IdentifiedObjects.toString(d1.getName()), IdentifiedObjects.toString(d2.getName()), name);
    }
    throw new ParameterNotFoundException(message, name);
}
Also used : GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 8 with ParameterNotFoundException

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

the class DefaultParameterValueGroup method parameter.

/**
 * Returns the value in this group for the specified name.
 * This method performs the first applicable action in the following choices:
 *
 * <ul>
 *   <li>If this group contains a parameter value of the given name, then that parameter is returned.</li>
 *   <li>Otherwise if a {@linkplain DefaultParameterDescriptorGroup#descriptor(String) descriptor} of the
 *       given name exists, then a new {@code ParameterValue} instance is
 *       {@linkplain DefaultParameterDescriptor#createValue() created}, added to this group and returned.</li>
 *   <li>Otherwise a {@code ParameterNotFoundException} is thrown.</li>
 * </ul>
 *
 * This convenience method provides a way to get and set parameter values by name.
 * For example the following idiom fetches a floating point value for the <cite>False easting</cite>
 * and <cite>False northing</cite> parameters and set a new value for the <cite>False easting</cite> one:
 *
 * {@preformat java
 *     double easting  = parameter("False easting" ).doubleValue();
 *     double northing = parameter("False northing").doubleValue();
 *     parameter("False easting").setValue(500000.0);
 * }
 *
 * <div class="note"><b>API note:</b> there is no <code>parameter<b><u>s</u></b>(String)</code> method
 * returning a list of parameter values because the ISO 19111 standard fixes the {@code ParameterValue}
 * {@linkplain DefaultParameterDescriptor#getMaximumOccurs() maximum occurrence} to 1.</div>
 *
 * <div class="section">Parameters subgroups</div>
 * This method does not search recursively in subgroups. This is because more than one subgroup
 * may exist for the same {@linkplain ParameterDescriptorGroup descriptor}. The user have to
 * {@linkplain #groups(String) query all subgroups} and select explicitly the appropriate one.
 *
 * @param  name  the name of the parameter to search for.
 * @return the parameter value for the given name.
 * @throws ParameterNotFoundException if there is no parameter value for the given name.
 *
 * @see #getValue(ParameterDescriptor)
 */
@Override
public ParameterValue<?> parameter(final String name) throws ParameterNotFoundException {
    ArgumentChecks.ensureNonNull("name", name);
    ParameterValue<?> value = parameterIfExist(name);
    if (value == null) {
        /*
             * No existing parameter found. Maybe the parameter is optional and not yet created.
             * Get the descriptor of that parameter. If the descriptor is not found, or is not
             * a descriptor for a single parameter (not a group), or the parameter is disabled
             * (maximum occurrence = 0), behaves as if the parameter was not found.
             */
        final GeneralParameterDescriptor descriptor = values.descriptor.descriptor(name);
        if (!(descriptor instanceof ParameterDescriptor<?>) || descriptor.getMaximumOccurs() == 0) {
            throw new ParameterNotFoundException(Resources.format(Resources.Keys.ParameterNotFound_2, Verifier.getDisplayName(values.descriptor), name), name);
        }
        /*
             * Create the optional parameter and add it to our internal list. Note that this is
             * not the only place were a ParameterValue may be created,  so do not extract just
             * this call to 'createValue()' in a user-overrideable method.
             */
        value = ((ParameterDescriptor<?>) descriptor).createValue();
        values.addUnchecked(value);
    }
    return value;
}
Also used : GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 9 with ParameterNotFoundException

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

the class EPSGDataAccess method fillParameterValues.

/**
 * Sets the values of all parameters in the given group.
 *
 * @param  method      the EPSG code for the operation method.
 * @param  operation   the EPSG code for the operation (conversion or transformation).
 * @param  parameters  the parameter values to fill.
 * @throws SQLException if a SQL statement failed.
 */
private void fillParameterValues(final Integer method, final Integer operation, final ParameterValueGroup parameters) throws FactoryException, SQLException {
    try (ResultSet result = executeQuery("Coordinate_Operation Parameter Value", "SELECT CP.PARAMETER_NAME," + " CV.PARAMETER_VALUE," + " CV.PARAM_VALUE_FILE_REF," + " CV.UOM_CODE" + " FROM ([Coordinate_Operation Parameter Value] AS CV" + " INNER JOIN [Coordinate_Operation Parameter] AS CP" + " ON CV.PARAMETER_CODE = CP.PARAMETER_CODE)" + " INNER JOIN [Coordinate_Operation Parameter Usage] AS CU" + " ON (CP.PARAMETER_CODE = CU.PARAMETER_CODE)" + " AND (CV.COORD_OP_METHOD_CODE = CU.COORD_OP_METHOD_CODE)" + " WHERE CV.COORD_OP_METHOD_CODE = ?" + " AND CV.COORD_OP_CODE = ?" + " ORDER BY CU.SORT_ORDER", method, operation)) {
        while (result.next()) {
            final String name = getString(operation, result, 1);
            final double value = getOptionalDouble(result, 2);
            final Unit<?> unit;
            String reference;
            if (Double.isNaN(value)) {
                /*
                     * If no numeric values were provided in the database, then the values should be
                     * in some external file. It may be a file in the $SIS_DATA/DatumChanges directory.
                     */
                reference = getString(operation, result, 3);
                unit = null;
            } else {
                reference = null;
                final String unitCode = getOptionalString(result, 4);
                unit = (unitCode != null) ? owner.createUnit(unitCode) : null;
            }
            final ParameterValue<?> param;
            try {
                param = parameters.parameter(name);
            } catch (ParameterNotFoundException exception) {
                /*
                     * Wrap the unchecked ParameterNotFoundException into the checked NoSuchIdentifierException,
                     * which is a FactoryException subclass.  Note that in principle, NoSuchIdentifierException is for
                     * MathTransforms rather than parameters. However we are close in spirit here since we are setting
                     * up MathTransform's parameters. Using NoSuchIdentifierException allows CoordinateOperationSet to
                     * know that the failure is probably caused by a MathTransform not yet supported in Apache SIS
                     * (or only partially supported) rather than some more serious failure in the database side.
                     * Callers can use this information in order to determine if they should try the next coordinate
                     * operation or propagate the exception.
                     */
                throw (NoSuchIdentifierException) new NoSuchIdentifierException(error().getString(Errors.Keys.CanNotSetParameterValue_1, name), name).initCause(exception);
            }
            try {
                if (reference != null) {
                    param.setValue(reference);
                } else if (unit != null) {
                    param.setValue(value, unit);
                } else {
                    param.setValue(value);
                }
            } catch (RuntimeException exception) {
                // Catch InvalidParameterValueException, ArithmeticException and others.
                throw new FactoryDataException(error().getString(Errors.Keys.CanNotSetParameterValue_1, name), exception);
            }
        }
    }
}
Also used : FactoryDataException(org.apache.sis.referencing.factory.FactoryDataException) ResultSet(java.sql.ResultSet) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 10 with ParameterNotFoundException

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

the class CC_GeneralOperationParameter method merge.

/**
 * Returns a descriptor with the given properties, completed with information not found in GML.
 * Those extra information are given by the {@code complete} descriptor.
 *
 * @param  caller         the public source class to report if a log message need to be emitted.
 * @param  properties     properties as declared in the GML document, to be used if {@code complete} is incompatible.
 * @param  merged         more complete properties, to be used if {@code complete} is compatible.
 * @param  minimumOccurs  value to assign to {@link DefaultParameterDescriptorGroup#getMinimumOccurs()}.
 * @param  maximumOccurs  value to assign to {@link DefaultParameterDescriptorGroup#getMaximumOccurs()}.
 * @param  provided       parameter descriptors declared in the GML document. This array will be overwritten.
 * @param  complete       more complete parameter descriptors.
 * @param  canSubstitute  {@code true} if this method is allowed to return {@code complete}.
 * @return the parameter descriptor group to use (may be the {@code complete} instance).
 *
 * @see <a href="http://issues.apache.org/jira/browse/SIS-290">SIS-290</a>
 */
static ParameterDescriptorGroup merge(final Class<?> caller, final Map<String, ?> properties, final Map<String, ?> merged, final int minimumOccurs, final int maximumOccurs, final GeneralParameterDescriptor[] provided, final ParameterDescriptorGroup complete, boolean canSubstitute) {
    boolean isCompatible = true;
    final Set<GeneralParameterDescriptor> included = new HashSet<>(Containers.hashMapCapacity(provided.length));
    for (int i = 0; i < provided.length; i++) {
        final GeneralParameterDescriptor p = provided[i];
        try {
            /*
                 * Replace the descriptors provided in the GML document by descriptors from the 'complete' instance,
                 * if possible. Keep trace of the complete descriptors that we found in this process.
                 */
            GeneralParameterDescriptor predefined = complete.descriptor(p.getName().getCode());
            if (predefined != null) {
                // Safety in case 'complete' is a user's implementation.
                canSubstitute &= (provided[i] = merge(p, predefined)) == predefined;
                if (!included.add(predefined)) {
                    // Broken hashCode/equals, or object mutated.
                    throw new CorruptedObjectException(predefined);
                }
                continue;
            }
        } catch (ParameterNotFoundException e) {
            /*
                 * Log at Level.WARNING for the first parameter (canSubstitute == true) and at Level.FINE
                 * for all other (canSubstitute == false).  We do not use CC_GeneralOperationParameter as
                 * the source class because this is an internal class. We rather use the first public class
                 * in the caller hierarchy, which is either DefaultParameterValueGroup or DefaultOperationMethod.
                 */
            Context.warningOccured(Context.current(), caller, (caller == DefaultParameterValueGroup.class) ? "setValues" : "setDescriptors", e, canSubstitute);
        }
        /*
             * If a parameter was not found in the 'complete' descriptor, we will not be able to use that descriptor.
             * But we may still be able to use its properties (name, alias, identifier) provided that the parameter
             * not found was optional.
             */
        isCompatible &= p.getMinimumOccurs() == 0;
        canSubstitute = false;
    }
    if (isCompatible) {
        /*
             * At this point, we determined that all mandatory parameters in the GML document exist in the 'complete'
             * descriptor. However the converse is not necessarily true. Verify that all parameters missing in the GML
             * document were optional.
             */
        for (final GeneralParameterDescriptor descriptor : complete.descriptors()) {
            if (!included.contains(descriptor) && descriptor.getMinimumOccurs() != 0 && !CC_OperationMethod.isImplicitParameter(descriptor)) {
                canSubstitute = false;
                isCompatible = false;
                break;
            }
        }
    }
    if (canSubstitute) {
        return complete;
    } else {
        return new DefaultParameterDescriptorGroup(isCompatible ? merged : properties, minimumOccurs, maximumOccurs, provided);
    }
}
Also used : DefaultParameterDescriptorGroup(org.apache.sis.parameter.DefaultParameterDescriptorGroup) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) CorruptedObjectException(org.apache.sis.util.CorruptedObjectException) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException) HashSet(java.util.HashSet)

Aggregations

ParameterNotFoundException (org.opengis.parameter.ParameterNotFoundException)16 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)7 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)6 ParameterDescriptorGroup (org.opengis.parameter.ParameterDescriptorGroup)4 Test (org.junit.Test)3 Constructor (java.lang.reflect.Constructor)2 PrivilegedAction (java.security.PrivilegedAction)2 ArrayList (java.util.ArrayList)2 Parser (org.apache.sis.io.wkt.Parser)2 FactoryException (org.opengis.util.FactoryException)2 NoSuchIdentifierException (org.opengis.util.NoSuchIdentifierException)2 ResultSet (java.sql.ResultSet)1 ParseException (java.text.ParseException)1 HashSet (java.util.HashSet)1 NoSuchElementException (java.util.NoSuchElementException)1 Angle (javax.measure.quantity.Angle)1 UnmodifiableArrayList (org.apache.sis.internal.util.UnmodifiableArrayList)1 DefaultParameterDescriptorGroup (org.apache.sis.parameter.DefaultParameterDescriptorGroup)1 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)1 FactoryDataException (org.apache.sis.referencing.factory.FactoryDataException)1