Search in sources :

Example 16 with ParameterNotFoundException

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

the class DefaultMathTransformFactory method createFromWKT.

/**
 * Creates a math transform object from a
 * <a href="http://www.geoapi.org/3.0/javadoc/org/opengis/referencing/doc-files/WKT.html"><cite>Well
 * Known Text</cite> (WKT)</a>.
 * If the given text contains non-fatal anomalies (unknown or unsupported WKT elements,
 * inconsistent unit definitions, <i>etc.</i>), warnings may be reported in a
 * {@linkplain java.util.logging.Logger logger} named {@code "org.apache.sis.io.wkt"}.
 *
 * @param  text  math transform encoded in Well-Known Text format.
 * @return the math transform (never {@code null}).
 * @throws FactoryException if the Well-Known Text can not be parsed,
 *         or if the math transform creation failed from some other reason.
 */
@Override
public MathTransform createFromWKT(final String text) throws FactoryException {
    lastMethod.remove();
    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.MathTransformParser").asSubclass(Parser.class).getConstructor(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(this);
        } catch (ReflectiveOperationException e) {
            throw new FactoryException(e);
        }
    /*
         * No need to check the type of the parsed object, because MathTransformParser
         * should return only instance of MathTransform.
         */
    final Object object;
    try {
        object = p.createFromWKT(text);
    } catch (FactoryException e) {
        /*
             * The parsing may fail because a operation 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);
    return (MathTransform) object;
}
Also used : InvalidGeodeticParameterException(org.apache.sis.referencing.factory.InvalidGeodeticParameterException) MathTransform(org.opengis.referencing.operation.MathTransform) PrivilegedAction(java.security.PrivilegedAction) FactoryException(org.opengis.util.FactoryException) Constructor(java.lang.reflect.Constructor) 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