Search in sources :

Example 11 with ParameterNotFoundException

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

the class MathTransformParser method parseParameters.

/**
 * Parses a sequence of {@code "PARAMETER"} elements.
 *
 * @param  element             the parent element containing the parameters to parse.
 * @param  parameters          the group where to store the parameter values.
 * @param  defaultUnit         the default unit (for arbitrary quantity, including angular), or {@code null}.
 * @param  defaultAngularUnit  the default angular unit, or {@code null} if none. This is determined by the context,
 *                             especially when {@link GeodeticObjectParser} parses a {@code ProjectedCRS} element.
 * @throws ParseException if the {@code "PARAMETER"} element can not be parsed.
 */
final void parseParameters(final Element element, final ParameterValueGroup parameters, final Unit<?> defaultUnit, final Unit<Angle> defaultAngularUnit) throws ParseException {
    final Unit<?> defaultSI = (defaultUnit != null) ? defaultUnit.getSystemUnit() : null;
    Element param = element;
    try {
        while ((param = element.pullElement(OPTIONAL, WKTKeywords.Parameter)) != null) {
            final String name = param.pullString("name");
            Unit<?> unit = parseUnit(param);
            param.pullElement(OPTIONAL, ID_KEYWORDS);
            /*
                 * DEPARTURE FROM ISO 19162: the specification recommends that we use the identifier instead
                 * than the parameter name. However we do not yet have a "get parameter by ID" in Apache SIS
                 * or in GeoAPI interfaces. This was not considered necessary since SIS is lenient (hopefully
                 * without introducing ambiguity) regarding parameter names, but we may revisit in a future
                 * version if it become no longer the case. See https://issues.apache.org/jira/browse/SIS-210
                 */
            final ParameterValue<?> parameter = parameters.parameter(name);
            final ParameterDescriptor<?> descriptor = parameter.getDescriptor();
            final Class<?> valueClass = descriptor.getValueClass();
            final boolean isNumeric = Number.class.isAssignableFrom(valueClass);
            if (isNumeric && unit == null) {
                unit = descriptor.getUnit();
                if (unit != null) {
                    final Unit<?> si = unit.getSystemUnit();
                    if (si.equals(defaultSI)) {
                        unit = defaultUnit;
                    } else if (si.equals(Units.RADIAN)) {
                        unit = defaultAngularUnit;
                    }
                }
            }
            if (unit != null) {
                parameter.setValue(param.pullDouble("doubleValue"), unit);
            } else if (isNumeric) {
                if (Numbers.isInteger(valueClass)) {
                    parameter.setValue(param.pullInteger("intValue"));
                } else {
                    parameter.setValue(param.pullDouble("doubleValue"));
                }
            } else if (valueClass == Boolean.class) {
                parameter.setValue(param.pullBoolean("booleanValue"));
            } else {
                parameter.setValue(param.pullString("stringValue"));
            }
            param.close(ignoredElements);
        }
    } catch (ParameterNotFoundException e) {
        throw new UnparsableObjectException(errorLocale, Errors.Keys.UnexpectedParameter_1, new String[] { e.getParameterName() }, param.offset).initCause(e);
    } catch (InvalidParameterValueException e) {
        throw (ParseException) new ParseException(e.getLocalizedMessage(), param.offset).initCause(e);
    }
}
Also used : InvalidParameterValueException(org.opengis.parameter.InvalidParameterValueException) ParseException(java.text.ParseException) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 12 with ParameterNotFoundException

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

the class UnmodifiableParameterValueGroup method groups.

/**
 * Returns all subgroups with the specified name.
 */
@Override
public List<ParameterValueGroup> groups(final String name) throws ParameterNotFoundException {
    ArgumentChecks.ensureNonNull("name", name);
    final List<ParameterValueGroup> groups = new ArrayList<>(4);
    for (final GeneralParameterValue value : values) {
        if (value instanceof ParameterValueGroup) {
            if (IdentifiedObjects.isHeuristicMatchForName(value.getDescriptor(), name)) {
                groups.add((ParameterValueGroup) value);
            }
        }
    }
    if (groups.isEmpty()) {
        if (!(descriptor.descriptor(name) instanceof ParameterDescriptorGroup)) {
            throw new ParameterNotFoundException(Resources.format(Resources.Keys.ParameterNotFound_2, Verifier.getDisplayName(descriptor), name), name);
        }
    }
    return groups;
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) ArrayList(java.util.ArrayList) UnmodifiableArrayList(org.apache.sis.internal.util.UnmodifiableArrayList) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 13 with ParameterNotFoundException

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

the class DefaultParameterValueGroup method groups.

/**
 * Returns all subgroups with the specified name.
 *
 * <p>This method do not create new groups: if the requested group is optional (i.e.
 * <code>{@linkplain DefaultParameterDescriptor#getMinimumOccurs() minimumOccurs} == 0</code>)
 * and no value were defined previously, then this method returns an empty set.</p>
 *
 * @param  name  the name of the parameter to search for.
 * @return the set of all parameter group for the given name.
 * @throws ParameterNotFoundException if no descriptor was found for the given name.
 */
@Override
public List<ParameterValueGroup> groups(final String name) throws ParameterNotFoundException {
    ArgumentChecks.ensureNonNull("name", name);
    // Protect against accidental changes.
    final ParameterValueList values = this.values;
    final List<ParameterValueGroup> groups = new ArrayList<>(4);
    final int size = values.size();
    for (int i = 0; i < size; i++) {
        final GeneralParameterDescriptor descriptor = values.descriptor(i);
        if (descriptor instanceof ParameterDescriptorGroup) {
            if (IdentifiedObjects.isHeuristicMatchForName(descriptor, name)) {
                groups.add((ParameterValueGroup) values.get(i));
            }
        }
    }
    /*
         * No groups were found. Check if the group actually exists (i.e. is declared in the
         * descriptor). If it doesn't exists, then an exception is thrown. If it exists (i.e.
         * it is simply an optional group not yet defined), then returns an empty list.
         */
    if (groups.isEmpty()) {
        final ParameterDescriptorGroup descriptor = values.descriptor;
        if (!(descriptor.descriptor(name) instanceof ParameterDescriptorGroup)) {
            throw new ParameterNotFoundException(Resources.format(Resources.Keys.ParameterNotFound_2, Verifier.getDisplayName(descriptor), name), name);
        }
    }
    return groups;
}
Also used : ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) ArrayList(java.util.ArrayList) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 14 with ParameterNotFoundException

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

the class DefaultParameterValueGroup method addGroup.

/**
 * Creates a new subgroup of the specified name, and adds it to the list of subgroups.
 * The argument shall be the name of a {@linkplain DefaultParameterDescriptorGroup descriptor group}
 * which is a child of this group.
 *
 * <div class="note"><b>API note:</b>
 * There is no {@code removeGroup(String)} method. To remove a group, users shall inspect the
 * {@link #values()} list, decide which occurrences to remove if there is many of them for the
 * same name, and whether to iterate recursively into sub-groups or not.</div>
 *
 * @param  name  the name of the parameter group to create.
 * @return a newly created parameter group for the given name.
 * @throws ParameterNotFoundException if no descriptor was found for the given name.
 * @throws InvalidParameterCardinalityException if this parameter group already contains the
 *         {@linkplain ParameterDescriptorGroup#getMaximumOccurs() maximum number of occurrences}
 *         of subgroups of the given name.
 */
@Override
public ParameterValueGroup addGroup(final String name) throws ParameterNotFoundException, InvalidParameterCardinalityException {
    // Protect against accidental changes.
    final ParameterValueList values = this.values;
    final ParameterDescriptorGroup descriptor = values.descriptor;
    final GeneralParameterDescriptor child = descriptor.descriptor(name);
    if (!(child instanceof ParameterDescriptorGroup)) {
        throw new ParameterNotFoundException(Resources.format(Resources.Keys.ParameterNotFound_2, descriptor.getName(), name), name);
    }
    final ParameterValueGroup value = ((ParameterDescriptorGroup) child).createValue();
    values.add(value);
    return value;
}
Also used : ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException)

Example 15 with ParameterNotFoundException

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

the class GeodeticObjectFactory method createFromWKT.

/**
 * Creates a Coordinate Reference System object from a <cite>Well Known Text</cite> (WKT).
 * This method understands both version 1 (a.k.a. OGC 01-009) and version 2 (a.k.a. ISO 19162)
 * of the WKT format.
 *
 * <div class="note"><b>Example:</b> below is a slightly simplified WKT 2 string for a Mercator projection.
 * For making this example smaller, some optional {@code UNIT[…]} and {@code ORDER[…]} elements have been omitted.
 *
 * {@preformat wkt
 *   ProjectedCRS["SIRGAS 2000 / Brazil Mercator",
 *     BaseGeodCRS["SIRGAS 2000",
 *       Datum["Sistema de Referencia Geocentrico para las Americas 2000",
 *         Ellipsoid["GRS 1980", 6378137, 298.257222101]]],
 *     Conversion["Petrobras Mercator",
 *       Method["Mercator (variant B)", Id["EPSG",9805]],
 *       Parameter["Latitude of 1st standard parallel", -2],
 *       Parameter["Longitude of natural origin", -43],
 *       Parameter["False easting", 5000000],
 *       Parameter["False northing", 10000000]],
 *     CS[cartesian,2],
 *       Axis["easting (E)", east],
 *       Axis["northing (N)", north],
 *       LengthUnit["metre", 1],
 *     Id["EPSG",5641]]
 * }
 * </div>
 *
 * If the given text contains non-fatal anomalies
 * (unknown or unsupported WKT elements, inconsistent unit definitions, unparsable axis abbreviations, <i>etc.</i>),
 * warnings may be reported in a {@linkplain java.util.logging.Logger logger} named {@code "org.apache.sis.io.wkt"}.
 * However this parser does not verify if the overall parsed object matches the EPSG (or other authority) definition,
 * since this geodetic object factory is not an {@linkplain GeodeticAuthorityFactory authority factory}.
 * For such verification, see the {@link org.apache.sis.referencing.CRS#fromWKT(String)} convenience method.
 *
 * <div class="section">Usage and performance considerations</div>
 * The default implementation uses a shared instance of {@link org.apache.sis.io.wkt.WKTFormat}
 * with the addition of thread-safety. This is okay for occasional use,
 * but is sub-optimal if this method is extensively used in a multi-thread environment.
 * Furthermore this method offers no control on the WKT {@linkplain org.apache.sis.io.wkt.Convention conventions}
 * in use and on the handling of {@linkplain org.apache.sis.io.wkt.Warnings warnings}.
 * Applications which need to parse a large amount of WKT strings should consider to use
 * the {@link org.apache.sis.io.wkt.WKTFormat} class instead than this method.
 *
 * @param  text  coordinate system encoded in Well-Known Text format (version 1 or 2).
 * @throws FactoryException if the object creation failed.
 *
 * @see org.apache.sis.io.wkt
 * @see org.apache.sis.referencing.CRS#fromWKT(String)
 * @see <a href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html">WKT 2 specification</a>
 * @see <a href="http://www.geoapi.org/3.0/javadoc/org/opengis/referencing/doc-files/WKT.html">Legacy WKT 1</a>
 */
@Override
public CoordinateReferenceSystem createFromWKT(final String text) throws FactoryException {
    Parser p = parser.getAndSet(null);
    if (p == null)
        try {
            Constructor<? extends Parser> c = parserConstructor;
            if (c == null) {
                c = Class.forName("org.apache.sis.io.wkt.GeodeticObjectParser").asSubclass(Parser.class).getConstructor(Map.class, ObjectFactory.class, MathTransformFactory.class);
                // For allowing use in inner class or lambda expression.
                final Constructor<?> cp = c;
                AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
                    cp.setAccessible(true);
                    return null;
                });
                parserConstructor = c;
            }
            p = c.newInstance(defaultProperties, this, getMathTransformFactory());
        } catch (ReflectiveOperationException e) {
            throw new FactoryException(e);
        }
    final Object object;
    try {
        object = p.createFromWKT(text);
    } catch (FactoryException e) {
        /*
             * In the case of map projection, the parsing may fail because a projection parameter is not known to SIS.
             * If this happen, replace the generic exception thrown be the parser (which is FactoryException) by a
             * more specific one. Note that InvalidGeodeticParameterException is defined only in this sis-referencing
             * module, so we could not throw it from the sis-metadata module that contain the parser.
             */
        Throwable cause = e.getCause();
        while (cause != null) {
            if (cause instanceof ParameterNotFoundException) {
                throw new InvalidGeodeticParameterException(e.getLocalizedMessage(), cause);
            }
            cause = cause.getCause();
        }
        throw e;
    }
    parser.set(p);
    if (object instanceof CoordinateReferenceSystem) {
        return (CoordinateReferenceSystem) object;
    } else {
        throw new FactoryException(Errors.getResources(defaultProperties).getString(Errors.Keys.IllegalClass_2, CoordinateReferenceSystem.class, object.getClass()));
    }
}
Also used : PrivilegedAction(java.security.PrivilegedAction) FactoryException(org.opengis.util.FactoryException) Constructor(java.lang.reflect.Constructor) AbstractIdentifiedObject(org.apache.sis.referencing.AbstractIdentifiedObject) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException) Parser(org.apache.sis.io.wkt.Parser)

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