Search in sources :

Example 11 with UnavailableFactoryException

use of org.apache.sis.referencing.factory.UnavailableFactoryException 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

UnavailableFactoryException (org.apache.sis.referencing.factory.UnavailableFactoryException)11 FactoryException (org.opengis.util.FactoryException)4 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ParserException (javax.measure.format.ParserException)2 InvalidGeodeticParameterException (org.apache.sis.referencing.factory.InvalidGeodeticParameterException)2 CoordinateOperationContext (org.apache.sis.referencing.operation.CoordinateOperationContext)2 DefaultCoordinateOperationFactory (org.apache.sis.referencing.operation.DefaultCoordinateOperationFactory)2 SimpleInternationalString (org.apache.sis.util.iso.SimpleInternationalString)2 Connection (java.sql.Connection)1 LogRecord (java.util.logging.LogRecord)1 UnformattableObjectException (org.apache.sis.io.wkt.UnformattableObjectException)1 WKTFormat (org.apache.sis.io.wkt.WKTFormat)1 ImmutableIdentifier (org.apache.sis.metadata.iso.ImmutableIdentifier)1 FactoryDataException (org.apache.sis.referencing.factory.FactoryDataException)1 Test (org.junit.Test)1 Identifier (org.opengis.metadata.Identifier)1 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)1 ParameterValue (org.opengis.parameter.ParameterValue)1