Search in sources :

Example 1 with Formatter

use of org.apache.sis.io.wkt.Formatter in project sis by apache.

the class ServicesForMetadata method toFormattableObject.

/**
 * Converts the given object in a {@code FormattableObject} instance. Callers should verify that the given
 * object is not already an instance of {@code FormattableObject} before to invoke this method. This method
 * returns {@code null} if it can not convert the object.
 *
 * @param  object    the object to wrap.
 * @param  internal  {@code true} if the formatting convention is {@code Convention.INTERNAL}.
 * @return the given object converted to a {@code FormattableObject} instance, or {@code null}.
 *
 * @since 0.6
 */
@Override
public FormattableObject toFormattableObject(final MathTransform object, boolean internal) {
    Matrix matrix;
    final ParameterValueGroup parameters;
    if (internal && (matrix = MathTransforms.getMatrix(object)) != null) {
        parameters = Affine.parameters(matrix);
    } else if (object instanceof Parameterized) {
        parameters = ((Parameterized) object).getParameterValues();
    } else {
        matrix = MathTransforms.getMatrix(object);
        if (matrix == null) {
            return null;
        }
        parameters = Affine.parameters(matrix);
    }
    return new FormattableObject() {

        @Override
        protected String formatTo(final Formatter formatter) {
            WKTUtilities.appendParamMT(parameters, formatter);
            return WKTKeywords.Param_MT;
        }
    };
}
Also used : Parameterized(org.apache.sis.parameter.Parameterized) Matrix(org.opengis.referencing.operation.Matrix) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Formatter(org.apache.sis.io.wkt.Formatter) FormattableObject(org.apache.sis.io.wkt.FormattableObject)

Example 2 with Formatter

use of org.apache.sis.io.wkt.Formatter in project sis by apache.

the class GeocentricAffine method asDatumShift.

/**
 * Given a transformation chain, conditionally replaces the affine transform elements by an alternative object
 * showing the Bursa-Wolf parameters. The replacement is applied if and only if the affine transform is a scale,
 * translation or rotation in the geocentric domain.
 *
 * <p>This method is invoked only by {@code ConcatenatedTransform.getPseudoSteps()} for the need of WKT formatting.
 * The purpose of this method is very similar to the purpose of {@code AbstractMathTransform.beforeFormat(List, int,
 * boolean)} except that we need to perform the {@code forDatumShift(…)} work only after {@code beforeFormat(…)}
 * finished its work for all {@code ContextualParameters}, including the {@code EllipsoidToCentricTransform}'s one.</p>
 *
 * @param  transforms  the full chain of concatenated transforms.
 */
public static void asDatumShift(final List<Object> transforms) {
    for (int i = transforms.size() - 2; --i >= 0; ) {
        if (isOperation(GeographicToGeocentric.NAME, transforms.get(i)) && isOperation(GeocentricToGeographic.NAME, transforms.get(i + 2))) {
            final Object step = transforms.get(i + 1);
            if (step instanceof LinearTransform) {
                final BursaWolfParameters parameters = new BursaWolfParameters(null, null);
                try {
                    parameters.setPositionVectorTransformation(((LinearTransform) step).getMatrix(), BURSAWOLF_TOLERANCE);
                } catch (IllegalArgumentException e) {
                    /*
                         * Should not occur, except sometime on inverse transform of relatively complex datum shifts
                         * (more than just translation terms). We can fallback on formatting the full matrix.
                         */
                    log(Loggers.WKT, "asDatumShift", e);
                    continue;
                }
                final boolean isTranslation = parameters.isTranslation();
                final Parameters values = createParameters(isTranslation ? GeocentricTranslation.PARAMETERS : PositionVector7Param.PARAMETERS, parameters, isTranslation);
                transforms.set(i + 1, new FormattableObject() {

                    @Override
                    protected String formatTo(final Formatter formatter) {
                        WKTUtilities.appendParamMT(values, formatter);
                        return WKTKeywords.Param_MT;
                    }
                });
            }
        }
    }
}
Also used : Parameters(org.apache.sis.parameter.Parameters) BursaWolfParameters(org.apache.sis.referencing.datum.BursaWolfParameters) Formatter(org.apache.sis.io.wkt.Formatter) FormattableObject(org.apache.sis.io.wkt.FormattableObject) BursaWolfParameters(org.apache.sis.referencing.datum.BursaWolfParameters) LinearTransform(org.apache.sis.referencing.operation.transform.LinearTransform) FormattableObject(org.apache.sis.io.wkt.FormattableObject)

Example 3 with Formatter

use of org.apache.sis.io.wkt.Formatter in project sis by apache.

the class ImmutableIdentifier method formatTo.

/**
 * Formats this identifier as a <cite>Well Known Text</cite> {@code Id[…]} element.
 * See class javadoc for more information on the WKT format.
 *
 * @param  formatter  the formatter where to format the inner content of this WKT element.
 * @return {@code "Id"} (WKT 2) or {@code "Authority"} (WKT 1).
 *
 * @see <a href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html#33">WKT 2 specification §7.3.4</a>
 */
@Override
protected String formatTo(final Formatter formatter) {
    String keyword = null;
    /*
         * The code, codeSpace, authority and version local variables in this method usually have the exact same
         * value than the fields of the same name in this class.  But we get those values by invoking the public
         * methods in order to give to users a chance to override those properties.  The intent is also to use a
         * consistent approach for all 'formatTo' implementations, since some other classes have no choice other
         * than using the public methods.
         */
    final String code = getCode();
    if (code != null) {
        final String codeSpace = getCodeSpace();
        final Citation authority = getAuthority();
        final String cs = (codeSpace != null) ? codeSpace : org.apache.sis.internal.util.Citations.getIdentifier(authority, true);
        if (cs != null) {
            final Convention convention = formatter.getConvention();
            if (convention.majorVersion() == 1) {
                keyword = WKTKeywords.Authority;
                formatter.append(cs, ElementKind.IDENTIFIER);
                formatter.append(code, ElementKind.IDENTIFIER);
            } else {
                keyword = WKTKeywords.Id;
                formatter.append(cs, ElementKind.IDENTIFIER);
                appendCode(formatter, code);
                final String version = getVersion();
                if (version != null) {
                    appendCode(formatter, version);
                }
                /*
                     * In order to simplify the WKT, format the citation only if it is different than the code space.
                     * We will also omit the citation if this identifier is for a parameter value, because parameter
                     * values are handled in a special way by the international standard:
                     *
                     *   - ISO 19162 explicitely said that we shall format the identifier for the root element only,
                     *     and omit the identifier for all inner elements EXCEPT parameter values and operation method.
                     *   - Exclusion of identifier for inner elements is performed by the Formatter class, so it does
                     *     not need to be checked here.
                     *   - Parameter values are numerous, while operation methods typically appear only once in a WKT
                     *     document. So we will simplify the parameter values only (not the operation methods) except
                     *     if the parameter value is the root element (in which case we will format full identifier).
                     */
                final FormattableObject enclosing = formatter.getEnclosingElement(1);
                final boolean isRoot = formatter.getEnclosingElement(2) == null;
                if (isRoot || !(enclosing instanceof ParameterValue<?>)) {
                    final String citation = org.apache.sis.internal.util.Citations.getIdentifier(authority, false);
                    if (citation != null && !citation.equals(cs)) {
                        formatter.append(new Cite(citation));
                    }
                }
                /*
                     * Do not format the optional URI element for internal convention,
                     * because this property is currently computed rather than stored.
                     * Other conventions format only for the ID[…] of root element.
                     */
                if (isRoot && enclosing != null && convention != Convention.INTERNAL) {
                    final String urn = NameMeaning.toURN(enclosing.getClass(), cs, version, code);
                    if (urn != null) {
                        formatter.append(new FormattableObject() {

                            @Override
                            protected String formatTo(final Formatter formatter) {
                                formatter.append(urn, null);
                                return WKTKeywords.URI;
                            }
                        });
                    }
                }
            }
        }
    }
    return keyword;
}
Also used : Convention(org.apache.sis.io.wkt.Convention) Formatter(org.apache.sis.io.wkt.Formatter) InternationalString(org.opengis.util.InternationalString) Citation(org.opengis.metadata.citation.Citation) FormattableObject(org.apache.sis.io.wkt.FormattableObject)

Example 4 with Formatter

use of org.apache.sis.io.wkt.Formatter in project sis by apache.

the class DefaultPassThroughOperation method formatTo.

/**
 * Formats this coordinate operation in a pseudo-Well Known Text (WKT) format.
 * Current format is specific to Apache SIS and may change in any future version
 * if a standard format for pass through operations is defined.
 *
 * @param  formatter  the formatter to use.
 * @return currently {@code "PassThroughOperation"} (may change in any future version).
 *
 * @since 0.7
 */
@Override
protected String formatTo(final Formatter formatter) {
    super.formatTo(formatter);
    formatter.append(new FormattableObject() {

        @Override
        protected String formatTo(final Formatter formatter) {
            for (final int i : getModifiedCoordinates()) {
                formatter.append(i);
            }
            return "ModifiedCoordinates";
        }
    });
    formatter.newLine();
    formatter.append(castOrCopy(getOperation()));
    formatter.setInvalidWKT(this, null);
    return "PassThroughOperation";
}
Also used : Formatter(org.apache.sis.io.wkt.Formatter) FormattableObject(org.apache.sis.io.wkt.FormattableObject)

Example 5 with Formatter

use of org.apache.sis.io.wkt.Formatter in project sis by apache.

the class DefaultDerivedCRS method formatTo.

/**
 * Formats the inner part of the <cite>Well Known Text</cite> (WKT) representation of this CRS.
 *
 * @return {@code "Fitted_CS"} (WKT 1) or a type-dependent keyword (WKT 2).
 *
 * @see <a href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html#93">WKT 2 specification §15</a>
 */
@Override
protected String formatTo(final Formatter formatter) {
    // Gives to users a chance to override.
    final Conversion conversion = getConversionFromBase();
    if (conversion == null) {
        /*
             * Should never happen except temporarily at construction time, or if the user invoked the copy
             * constructor with an invalid Conversion, or if the user overrides the getConversionFromBase()
             * method. Delegates to the super-class method for avoiding a NullPointerException. That method
             * returns 'null', which will cause the WKT to be declared invalid.
             */
        return super.formatTo(formatter);
    }
    WKTUtilities.appendName(this, formatter, null);
    final Convention convention = formatter.getConvention();
    final boolean isWKT1 = (convention.majorVersion() == 1);
    /*
         * Both WKT 1 and WKT 2 format the base CRS. But WKT 1 formats the MathTransform before the base CRS,
         * while WKT 2 formats the conversion method and parameter values after the base CRS.
         */
    if (isWKT1) {
        MathTransform inverse = conversion.getMathTransform();
        try {
            inverse = inverse.inverse();
        } catch (NoninvertibleTransformException exception) {
            formatter.setInvalidWKT(this, exception);
            inverse = null;
        }
        formatter.newLine();
        formatter.append(inverse);
    }
    formatter.newLine();
    formatter.append(WKTUtilities.toFormattable(getBaseCRS()));
    if (isWKT1) {
        return WKTKeywords.Fitted_CS;
    } else {
        formatter.newLine();
        formatter.append(new // Format inside a "DefiningConversion" element.
        FormattableObject() {

            @Override
            protected String formatTo(final Formatter formatter) {
                WKTUtilities.appendName(conversion, formatter, null);
                formatter.newLine();
                formatter.append(DefaultOperationMethod.castOrCopy(conversion.getMethod()));
                formatter.newLine();
                for (final GeneralParameterValue param : conversion.getParameterValues().values()) {
                    WKTUtilities.append(param, formatter);
                }
                return WKTKeywords.DerivingConversion;
            }
        });
        if (convention == Convention.INTERNAL || !isBaseCRS(formatter)) {
            final CoordinateSystem cs = getCoordinateSystem();
            formatCS(formatter, cs, ReferencingUtilities.getUnit(cs), isWKT1);
        }
        return keyword(formatter);
    }
}
Also used : NoninvertibleTransformException(org.opengis.referencing.operation.NoninvertibleTransformException) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) Convention(org.apache.sis.io.wkt.Convention) AxesConvention(org.apache.sis.referencing.cs.AxesConvention) MathTransform(org.opengis.referencing.operation.MathTransform) Formatter(org.apache.sis.io.wkt.Formatter) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) CS_CoordinateSystem(org.apache.sis.internal.jaxb.referencing.CS_CoordinateSystem) Conversion(org.opengis.referencing.operation.Conversion) DefaultConversion(org.apache.sis.referencing.operation.DefaultConversion)

Aggregations

Formatter (org.apache.sis.io.wkt.Formatter)8 FormattableObject (org.apache.sis.io.wkt.FormattableObject)6 InternationalString (org.opengis.util.InternationalString)3 Convention (org.apache.sis.io.wkt.Convention)2 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)2 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)2 CS_CoordinateSystem (org.apache.sis.internal.jaxb.referencing.CS_CoordinateSystem)1 Parameterized (org.apache.sis.parameter.Parameterized)1 Parameters (org.apache.sis.parameter.Parameters)1 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)1 AxesConvention (org.apache.sis.referencing.cs.AxesConvention)1 BursaWolfParameters (org.apache.sis.referencing.datum.BursaWolfParameters)1 DefaultConversion (org.apache.sis.referencing.operation.DefaultConversion)1 LinearTransform (org.apache.sis.referencing.operation.transform.LinearTransform)1 Citation (org.opengis.metadata.citation.Citation)1 IdentifiedObject (org.opengis.referencing.IdentifiedObject)1 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)1 ConcatenatedOperation (org.opengis.referencing.operation.ConcatenatedOperation)1 Conversion (org.opengis.referencing.operation.Conversion)1 MathTransform (org.opengis.referencing.operation.MathTransform)1