Search in sources :

Example 1 with DefinitionVerifier

use of org.apache.sis.internal.referencing.DefinitionVerifier in project sis by apache.

the class Store method unmarshal.

/**
 * Unmarshal the object, if not already done. Note that {@link #object} may still be null
 * if an exception has been thrown at this invocation time or in previous invocation.
 *
 * @throws DataStoreException if an error occurred during the unmarshalling process.
 */
private void unmarshal() throws DataStoreException {
    final StreamSource s = source;
    final Closeable in = input(s);
    // Cleared first in case of error.
    source = null;
    if (in != null)
        try {
            try {
                object = XML.unmarshal(s, properties());
            } finally {
                in.close();
            }
        } catch (JAXBException | IOException e) {
            throw new DataStoreException(Errors.format(Errors.Keys.CanNotRead_1, getDisplayName()), e);
        }
    if (object instanceof CoordinateReferenceSystem)
        try {
            final DefinitionVerifier v = DefinitionVerifier.withAuthority((CoordinateReferenceSystem) object, null, false);
            if (v != null) {
                log(v.warning(false));
            }
        } catch (FactoryException e) {
            listeners.warning(null, e);
        }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) FactoryException(org.opengis.util.FactoryException) StreamSource(javax.xml.transform.stream.StreamSource) Closeable(java.io.Closeable) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) DefinitionVerifier(org.apache.sis.internal.referencing.DefinitionVerifier)

Example 2 with DefinitionVerifier

use of org.apache.sis.internal.referencing.DefinitionVerifier in project sis by apache.

the class CRS method fromAuthority.

/**
 * Replaces the given coordinate reference system by an authoritative description, if one can be found.
 * This method can be invoked after constructing a CRS in a context where the EPSG (or other authority)
 * code is suspected more reliable than the rest of the description. A common case is a <cite>Well Known
 * Text</cite> (WKT) string declaring wrong projection method or parameter values for the EPSG code that
 * it pretends to describe. For example:
 *
 * <blockquote>
 *   {@code PROJCS["WGS 84 / Pseudo-Mercator",}<br>
 *   {@code   }(…base CRS omitted for brevity…)<br>
 *   {@code   PROJECTION["Mercator (variant A)"],} — <em><b>wrong:</b> shall be "Popular Visualisation Pseudo Mercator"</em><br>
 *   {@code   }(…parameters and axes omitted for brevity…)<br>
 *   {@code   AUTHORITY["EPSG", "3857"]]}
 * </blockquote>
 *
 * In such cases, Apache SIS behavior in {@link #fromWKT(String)}, {@link #fromXML(String)} and other methods is
 * conform to the <a href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html">ISO 19162 specification</a>:
 *
 * <blockquote><cite>"Should any attributes or values given in the cited identifier be in conflict with attributes
 * or values given explicitly in the WKT description, the WKT values shall prevail."</cite></blockquote>
 *
 * In situations where the opposite behavior is desired (i.e. to make the authority identifier prevails),
 * this method can be invoked. This method performs the following actions:
 *
 * <ul>
 *   <li>If the given CRS has an {@linkplain AbstractIdentifiedObject#getIdentifiers() identifier} and if the authority factory can
 *     {@linkplain org.apache.sis.referencing.factory.GeodeticAuthorityFactory#createCoordinateReferenceSystem(String) create a CRS}
 *     for that identifier, then:
 *     <ul>
 *       <li>If the CRS defined by the authority is {@linkplain Utilities#equalsIgnoreMetadata equal, ignoring metadata},
 *         to the given CRS, then this method returns silently the <em>authoritative</em> CRS.</li>
 *       <li>Otherwise if the CRS defined by the authority is equal, ignoring axis order and units, to the given CRS,
 *         then this method returns a <em>new</em> CRS derived from the authoritative one but with same
 *         {@linkplain org.apache.sis.referencing.cs.AxesConvention axes convention} than the given CRS.
 *         A warning is emitted.</li>
 *       <li>Otherwise this method discards the given CRS and returns the <em>authoritative</em> CRS.
 *         A warning is emitted with a message indicating where a difference has been found.</li>
 *     </ul>
 *   </li>
 *   <li>Otherwise if the given CRS does not have identifier, then this method
 *       {@linkplain org.apache.sis.referencing.factory.IdentifiedObjectFinder searches for an equivalent CRS}
 *       defined by the authority factory. If such CRS is found, then:
 *     <ul>
 *       <li>If the CRS defined by the authority is {@linkplain Utilities#equalsIgnoreMetadata equal, ignoring metadata},
 *         to the given CRS, then this method returns silently the <em>authoritative</em> CRS.</li>
 *       <li>Otherwise if the CRS defined by the authority is equal, ignoring axis order and units, to the given CRS,
 *         then this method returns silently a <em>new</em> CRS derived from the authoritative one but with same
 *         {@linkplain org.apache.sis.referencing.cs.AxesConvention axes convention} than the given CRS.</li>
 *     </ul>
 *   </li>
 *   <li>Otherwise this method silently returns the given CRS as-is.</li>
 * </ul>
 *
 * <b>Note:</b> the warnings emitted by this method are redundant with the warnings emitted by
 * {@link #fromWKT(String)} and {@link #fromXML(String)}, so the {@code warnings} argument should be {@code null}
 * when {@code fromAuthority(…)} is invoked for the CRS parsed by one of above-mentioned methods.
 * A non-null {@code warnings} argument is more useful for CRS parsed by {@link org.apache.sis.io.wkt.WKTFormat}
 * or {@link org.apache.sis.xml.XML#unmarshal(String)} for instance.
 *
 * @param  crs       the CRS to replace by an authoritative CRS, or {@code null}.
 * @param  factory   the factory where to search for authoritative definitions, or {@code null} for the default.
 * @param  listener  where to send warnings, or {@code null} for ignoring warnings.
 * @return the suggested CRS to use (may be the {@code crs} argument itself), or {@code null} if the given CRS was null.
 * @throws FactoryException if an error occurred while querying the authority factory.
 *
 * @since 0.8
 */
public static CoordinateReferenceSystem fromAuthority(CoordinateReferenceSystem crs, final CRSAuthorityFactory factory, final WarningListener<?> listener) throws FactoryException {
    if (crs != null) {
        final DefinitionVerifier verification = DefinitionVerifier.withAuthority(crs, factory, true);
        if (verification != null) {
            crs = verification.authoritative;
            if (listener != null) {
                final LogRecord record = verification.warning(false);
                if (record != null) {
                    record.setLoggerName(Modules.REFERENCING);
                    record.setSourceClassName(CRS.class.getName());
                    record.setSourceMethodName("fromAuthority");
                    listener.warningOccured(null, record);
                }
            }
        }
    }
    return crs;
}
Also used : LogRecord(java.util.logging.LogRecord) CompoundCRS(org.opengis.referencing.crs.CompoundCRS) EngineeringCRS(org.opengis.referencing.crs.EngineeringCRS) SingleCRS(org.opengis.referencing.crs.SingleCRS) GeographicCRS(org.opengis.referencing.crs.GeographicCRS) DefaultCompoundCRS(org.apache.sis.referencing.crs.DefaultCompoundCRS) DefaultEngineeringCRS(org.apache.sis.referencing.crs.DefaultEngineeringCRS) DefaultVerticalCRS(org.apache.sis.referencing.crs.DefaultVerticalCRS) DefaultGeographicCRS(org.apache.sis.referencing.crs.DefaultGeographicCRS) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) TemporalCRS(org.opengis.referencing.crs.TemporalCRS) VerticalCRS(org.opengis.referencing.crs.VerticalCRS) GeneralDerivedCRS(org.opengis.referencing.crs.GeneralDerivedCRS) GeodeticCRS(org.opengis.referencing.crs.GeodeticCRS) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) DefinitionVerifier(org.apache.sis.internal.referencing.DefinitionVerifier)

Aggregations

DefinitionVerifier (org.apache.sis.internal.referencing.DefinitionVerifier)2 Closeable (java.io.Closeable)1 LogRecord (java.util.logging.LogRecord)1 StreamSource (javax.xml.transform.stream.StreamSource)1 DefaultCompoundCRS (org.apache.sis.referencing.crs.DefaultCompoundCRS)1 DefaultEngineeringCRS (org.apache.sis.referencing.crs.DefaultEngineeringCRS)1 DefaultGeographicCRS (org.apache.sis.referencing.crs.DefaultGeographicCRS)1 DefaultProjectedCRS (org.apache.sis.referencing.crs.DefaultProjectedCRS)1 DefaultVerticalCRS (org.apache.sis.referencing.crs.DefaultVerticalCRS)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 CompoundCRS (org.opengis.referencing.crs.CompoundCRS)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 EngineeringCRS (org.opengis.referencing.crs.EngineeringCRS)1 GeneralDerivedCRS (org.opengis.referencing.crs.GeneralDerivedCRS)1 GeodeticCRS (org.opengis.referencing.crs.GeodeticCRS)1 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)1 ProjectedCRS (org.opengis.referencing.crs.ProjectedCRS)1 SingleCRS (org.opengis.referencing.crs.SingleCRS)1 TemporalCRS (org.opengis.referencing.crs.TemporalCRS)1 VerticalCRS (org.opengis.referencing.crs.VerticalCRS)1