Search in sources :

Example 1 with Parser

use of org.apache.sis.io.wkt.Parser 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)

Example 2 with Parser

use of org.apache.sis.io.wkt.Parser 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

Constructor (java.lang.reflect.Constructor)2 PrivilegedAction (java.security.PrivilegedAction)2 Parser (org.apache.sis.io.wkt.Parser)2 ParameterNotFoundException (org.opengis.parameter.ParameterNotFoundException)2 FactoryException (org.opengis.util.FactoryException)2 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)1 InvalidGeodeticParameterException (org.apache.sis.referencing.factory.InvalidGeodeticParameterException)1 MathTransform (org.opengis.referencing.operation.MathTransform)1