Search in sources :

Example 11 with InvalidGeodeticParameterException

use of org.apache.sis.referencing.factory.InvalidGeodeticParameterException in project sis by apache.

the class Proj4 method createCRS.

/**
 * Creates a new CRS from the given {@literal Proj.4} definition string.
 * Some examples of definition strings are:
 * <ul>
 *   <li>{@code "+init=epsg:3395 +over"} (see warning below)</li>
 *   <li>{@code "+proj=latlong +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"}</li>
 *   <li>{@code "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +ellps=WGS84 +towgs84=0,0,0"}</li>
 * </ul>
 *
 * <b>Warning:</b> despite the {@code "epsg"} word, coordinate reference systems created by {@code "+init=epsg:"}
 * syntax are not necessarily compliant with EPSG definitions. In particular, the axis order is often different.
 * Units of measurement may also differ.
 *
 * @param  definition  the Proj.4 definition string.
 * @param  dimension   the number of dimension of the CRS to create (2 or 3).
 * @return a CRS created from the given definition string and number of dimensions.
 * @throws NullPointerException if the definition string is {@code null}.
 * @throws IllegalArgumentException if the definition string is empty or the dimension argument is out of range.
 * @throws UnavailableFactoryException if the Proj.4 native library is not available.
 * @throws FactoryException if the CRS creation failed for another reason.
 *
 * @see Proj4Factory#createCoordinateReferenceSystem(String)
 */
public static CoordinateReferenceSystem createCRS(String definition, final int dimension) throws FactoryException {
    ArgumentChecks.ensureNonEmpty("definition", definition);
    ArgumentChecks.ensureBetween("dimension", 2, 3, dimension);
    definition = definition.trim();
    try {
        return Proj4Factory.INSTANCE.createCRS(definition, dimension >= 3);
    } catch (IllegalArgumentException | ParserException e) {
        throw new InvalidGeodeticParameterException(canNotParse(definition), e);
    } catch (UnsatisfiedLinkError | NoClassDefFoundError e) {
        throw new UnavailableFactoryException(unavailable(e), e);
    }
}
Also used : ParserException(javax.measure.format.ParserException) InvalidGeodeticParameterException(org.apache.sis.referencing.factory.InvalidGeodeticParameterException) UnavailableFactoryException(org.apache.sis.referencing.factory.UnavailableFactoryException)

Example 12 with InvalidGeodeticParameterException

use of org.apache.sis.referencing.factory.InvalidGeodeticParameterException in project sis by apache.

the class Proj4Factory method createCoordinateReferenceSystem.

/**
 * Creates a new CRS from the given {@literal Proj.4} definition.
 * The {@code "Proj4:"} prefix (ignoring case), if present, is ignored.
 *
 * <div class="section">Apache SIS extension</div>
 * Proj.4 unconditionally requires 3 letters for the {@code "+axis="} parameter — for example {@code "neu"} for
 * <cite>North</cite>, <cite>East</cite> and <cite>Up</cite> respectively — regardless the number of dimensions
 * in the CRS to create. Apache SIS makes the vertical direction optional:
 *
 * <ul>
 *   <li>If the vertical direction is present (as in {@code "neu"}), a three-dimensional CRS is created.</li>
 *   <li>If the vertical direction is absent (as in {@code "ne"}), a two-dimensional CRS is created.</li>
 * </ul>
 *
 * <div class="note"><b>Examples:</b>
 * <ul>
 *   <li>{@code "+init=epsg:4326"} (<strong>not</strong> equivalent to the standard EPSG::4326 definition)</li>
 *   <li>{@code "+proj=latlong +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"} (default to two-dimensional CRS)</li>
 *   <li>{@code "+proj=latlon +a=6378137.0 +b=6356752.314245179 +pm=0.0 +axis=ne"} (explicitely two-dimensional)</li>
 *   <li>{@code "+proj=latlon +a=6378137.0 +b=6356752.314245179 +pm=0.0 +axis=neu"} (three-dimensional)</li>
 * </ul>
 * </div>
 *
 * @param  code  the Proj.4 definition of the CRS object to create.
 * @return a CRS created from the given definition.
 * @throws FactoryException if the CRS object can not be created for the given definition.
 *
 * @see Proj4#createCRS(String, int)
 */
@Override
public CoordinateReferenceSystem createCoordinateReferenceSystem(String code) throws FactoryException {
    code = trimNamespace(code);
    boolean hasHeight = false;
    /*
         * Count the number of axes declared in the "+axis" parameter.
         * If there is only two axes, add a 'u' (up) direction even in the two-dimensional case.
         * We make this addition because Proj.4 seems to require the 3-letters code in all case.
         */
    int offset = code.indexOf(AXIS_ORDER_PARAM);
    if (offset >= 0) {
        offset += AXIS_ORDER_PARAM.length();
        final CharSequence orientation = CharSequences.token(code, offset);
        for (int i = orientation.length(); --i >= 0; ) {
            final char c = orientation.charAt(i);
            hasHeight = (c == 'u' || c == 'd');
            if (hasHeight)
                break;
        }
        if (!hasHeight && orientation.length() < 3) {
            offset = code.indexOf(orientation.toString(), offset);
            if (offset >= 0) {
                // Should never be -1, but we are paranoiac.
                offset += orientation.length();
                code = new StringBuilder(code).insert(offset, 'u').toString();
            }
        }
    }
    try {
        return createCRS(code, hasHeight);
    } catch (IllegalArgumentException | ParserException e) {
        throw new InvalidGeodeticParameterException(Proj4.canNotParse(code), e);
    } catch (UnsatisfiedLinkError | NoClassDefFoundError e) {
        throw new UnavailableFactoryException(Proj4.unavailable(e), e);
    }
}
Also used : ParserException(javax.measure.format.ParserException) UnavailableFactoryException(org.apache.sis.referencing.factory.UnavailableFactoryException) InvalidGeodeticParameterException(org.apache.sis.referencing.factory.InvalidGeodeticParameterException)

Aggregations

InvalidGeodeticParameterException (org.apache.sis.referencing.factory.InvalidGeodeticParameterException)12 MathTransform (org.opengis.referencing.operation.MathTransform)5 FactoryException (org.opengis.util.FactoryException)3 ParserException (javax.measure.format.ParserException)2 UnavailableFactoryException (org.apache.sis.referencing.factory.UnavailableFactoryException)2 Test (org.junit.Test)2 OperationMethod (org.opengis.referencing.operation.OperationMethod)2 NoSuchIdentifierException (org.opengis.util.NoSuchIdentifierException)2 Constructor (java.lang.reflect.Constructor)1 PrivilegedAction (java.security.PrivilegedAction)1 HashMap (java.util.HashMap)1 DeferredCoordinateOperation (org.apache.sis.internal.referencing.DeferredCoordinateOperation)1 Parser (org.apache.sis.io.wkt.Parser)1 Line (org.apache.sis.math.Line)1 Plane (org.apache.sis.math.Plane)1 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)1 DefaultProjectedCRS (org.apache.sis.referencing.crs.DefaultProjectedCRS)1 DefaultConversion (org.apache.sis.referencing.operation.DefaultConversion)1 DefaultOperationMethod (org.apache.sis.referencing.operation.DefaultOperationMethod)1 MatrixSIS (org.apache.sis.referencing.operation.matrix.MatrixSIS)1