Search in sources :

Example 6 with DefaultExtent

use of org.apache.sis.metadata.iso.extent.DefaultExtent in project sis by apache.

the class GeodeticObjectParser method parseMetadataAndClose.

/**
 * Parses an <strong>optional</strong> metadata elements and close.
 * This include elements like {@code "SCOPE"}, {@code "ID"} (WKT 2) or {@code "AUTHORITY"} (WKT 1).
 * This WKT 1 element has the following pattern:
 *
 * {@preformat wkt
 *     AUTHORITY["<name>", "<code>"]
 * }
 *
 * <div class="section">Fallback</div>
 * The name is a mandatory property, but some invalid WKT with an empty string exist. In such case,
 * we will use the name of the enclosed datum. Indeed, it is not uncommon to have the same name for
 * a geographic CRS and its geodetic datum.
 *
 * @param  parent    the parent element.
 * @param  name      the name of the parent object being parsed.
 * @param  fallback  the fallback to use if {@code name} is empty.
 * @return a properties map with the parent name and the optional authority code.
 * @throws ParseException if an element can not be parsed.
 */
@SuppressWarnings("ReturnOfCollectionOrArrayField")
private Map<String, Object> parseMetadataAndClose(final Element parent, final String name, final IdentifiedObject fallback) throws ParseException {
    properties.clear();
    properties.put(IdentifiedObject.NAME_KEY, (name.isEmpty() && fallback != null) ? fallback.getName() : name);
    Element element;
    while ((element = parent.pullElement(OPTIONAL, ID_KEYWORDS)) != null) {
        final String codeSpace = element.pullString("codeSpace");
        // Accepts Integer as well as String.
        final String code = element.pullObject("code").toString();
        // Accepts Number as well as String.
        final Object version = element.pullOptional(Object.class);
        final Element citation = element.pullElement(OPTIONAL, WKTKeywords.Citation);
        final String authority;
        if (citation != null) {
            authority = citation.pullString("authority");
            citation.close(ignoredElements);
        } else {
            authority = codeSpace;
        }
        final Element uri = element.pullElement(OPTIONAL, WKTKeywords.URI);
        if (uri != null) {
            // TODO: not yet stored, since often redundant with other informations.
            uri.pullString("URI");
            uri.close(ignoredElements);
        }
        element.close(ignoredElements);
        /*
             * Note: we could be tempted to assign the authority to the name as well, like below:
             *
             *     if (name instanceof String) {
             *         name = new NamedIdentifier(authority, (String) name);
             *     }
             *     properties.put(IdentifiedObject.NAME_KEY, name);
             *
             * However experience shows that it is often wrong in practice, because peoples often
             * declare EPSG codes but still use WKT names much shorter than the EPSG names
             * (for example "WGS84" for the datum instead than "World Geodetic System 1984"),
             * so the name in WKT is often not compliant with the name actually defined by the authority.
             */
        final ImmutableIdentifier id = new ImmutableIdentifier(Citations.fromName(authority), codeSpace, code, (version != null) ? version.toString() : null, null);
        final Object previous = properties.put(IdentifiedObject.IDENTIFIERS_KEY, id);
        if (previous != null) {
            Identifier[] identifiers;
            if (previous instanceof Identifier) {
                identifiers = new Identifier[] { (Identifier) previous, id };
            } else {
                identifiers = (Identifier[]) previous;
                final int n = identifiers.length;
                identifiers = Arrays.copyOf(identifiers, n + 1);
                identifiers[n] = id;
            }
            properties.put(IdentifiedObject.IDENTIFIERS_KEY, identifiers);
        // REMINDER: values associated to IDENTIFIERS_KEY shall be recognized by 'toIdentifier(Object)'.
        }
    }
    /*
         * Other metadata (SCOPE, AREA, etc.).  ISO 19162 said that at most one of each type shall be present,
         * but our parser accepts an arbitrary amount of some kinds of metadata. They can be recognized by the
         * 'while' loop.
         *
         * Most WKT do not contain any of those metadata, so we perform an 'isEmpty()' check as an optimization
         * for those common cases.
         */
    if (!parent.isEmpty()) {
        /*
             * Example: SCOPE["Large scale topographic mapping and cadastre."]
             */
        element = parent.pullElement(OPTIONAL, WKTKeywords.Scope);
        if (element != null) {
            // Other types like Datum use the same key.
            properties.put(ReferenceSystem.SCOPE_KEY, element.pullString("scope"));
            element.close(ignoredElements);
        }
        /*
             * Example: AREA["Netherlands offshore."]
             */
        DefaultExtent extent = null;
        while ((element = parent.pullElement(OPTIONAL, WKTKeywords.Area)) != null) {
            final String area = element.pullString("area");
            element.close(ignoredElements);
            if (extent == null) {
                extent = new DefaultExtent(area, null, null, null);
            } else {
                extent.getGeographicElements().add(new DefaultGeographicDescription(area));
            }
        }
        /*
             * Example: BBOX[51.43, 2.54, 55.77, 6.40]
             */
        while ((element = parent.pullElement(OPTIONAL, WKTKeywords.BBox)) != null) {
            final double southBoundLatitude = element.pullDouble("southBoundLatitude");
            final double westBoundLongitude = element.pullDouble("westBoundLongitude");
            final double northBoundLatitude = element.pullDouble("northBoundLatitude");
            final double eastBoundLongitude = element.pullDouble("eastBoundLongitude");
            element.close(ignoredElements);
            if (extent == null)
                extent = new DefaultExtent();
            extent.getGeographicElements().add(new DefaultGeographicBoundingBox(westBoundLongitude, eastBoundLongitude, southBoundLatitude, northBoundLatitude));
        }
        /*
             * Example: VERTICALEXTENT[-1000, 0, LENGTHUNIT[“metre”, 1]]
             *
             * Units are optional, default to metres (no "contextual units" here).
             */
        while ((element = parent.pullElement(OPTIONAL, WKTKeywords.VerticalExtent)) != null) {
            final double minimum = element.pullDouble("minimum");
            final double maximum = element.pullDouble("maximum");
            Unit<Length> unit = parseScaledUnit(element, WKTKeywords.LengthUnit, Units.METRE);
            element.close(ignoredElements);
            if (unit == null)
                unit = Units.METRE;
            if (extent == null)
                extent = new DefaultExtent();
            verticalElements = new VerticalInfo(verticalElements, extent, minimum, maximum, unit).resolve(verticalCRS);
        }
        /*
             * Example: TIMEEXTENT[2013-01-01, 2013-12-31]
             *
             * TODO: syntax like TIMEEXTENT[“Jurassic”, “Quaternary”] is not yet supported.
             * See https://issues.apache.org/jira/browse/SIS-163
             *
             * This operation requires the the sis-temporal module. If not available,
             * we will report a warning and leave the temporal extent missing.
             */
        while ((element = parent.pullElement(OPTIONAL, WKTKeywords.TimeExtent)) != null) {
            if (element.peekValue() instanceof String) {
                element.pullString("startTime");
                element.pullString("endTime");
                element.close(ignoredElements);
                warning(parent, element, Errors.formatInternational(Errors.Keys.UnsupportedType_1, "TimeExtent[String,String]"), null);
            } else {
                final Date startTime = element.pullDate("startTime");
                final Date endTime = element.pullDate("endTime");
                element.close(ignoredElements);
                try {
                    final DefaultTemporalExtent t = new DefaultTemporalExtent();
                    t.setBounds(startTime, endTime);
                    if (extent == null)
                        extent = new DefaultExtent();
                    extent.getTemporalElements().add(t);
                } catch (UnsupportedOperationException e) {
                    warning(parent, element, null, e);
                }
            }
        }
        if (extent != null) {
            properties.put(ReferenceSystem.DOMAIN_OF_VALIDITY_KEY, extent);
        }
        /*
             * Example: REMARK["Замечание на русском языке"]
             */
        element = parent.pullElement(OPTIONAL, WKTKeywords.Remark);
        if (element != null) {
            properties.put(IdentifiedObject.REMARKS_KEY, element.pullString("remarks"));
            element.close(ignoredElements);
        }
    }
    parent.close(ignoredElements);
    return properties;
}
Also used : Date(java.util.Date) DefaultGeographicDescription(org.apache.sis.metadata.iso.extent.DefaultGeographicDescription) ImmutableIdentifier(org.apache.sis.metadata.iso.ImmutableIdentifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Identifier(org.opengis.metadata.Identifier) DefaultGeographicBoundingBox(org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox) DefaultExtent(org.apache.sis.metadata.iso.extent.DefaultExtent) Length(javax.measure.quantity.Length) DefaultTemporalExtent(org.apache.sis.metadata.iso.extent.DefaultTemporalExtent) IdentifiedObject(org.opengis.referencing.IdentifiedObject) ImmutableIdentifier(org.apache.sis.metadata.iso.ImmutableIdentifier)

Example 7 with DefaultExtent

use of org.apache.sis.metadata.iso.extent.DefaultExtent in project sis by apache.

the class CRSTest method testSuggestCommonTarget.

/**
 * Tests {@link CRS#suggestCommonTarget(GeographicBoundingBox, CoordinateReferenceSystem...)}.
 *
 * @since 0.8
 */
@Test
public void testSuggestCommonTarget() {
    /*
         * Prepare 4 CRS with different datum (so we can more easily differentiate them in the assertions) and
         * different domain of validity. CRS[1] is given a domain large enough for all CRS except the last one.
         */
    final Map<String, Object> properties = new HashMap<>(4);
    final CartesianCS cs = (CartesianCS) StandardDefinitions.createCoordinateSystem(Constants.EPSG_PROJECTED_CS);
    final ProjectedCRS[] crs = new ProjectedCRS[4];
    for (int i = 0; i < crs.length; i++) {
        final CommonCRS baseCRS;
        final double ymin, ymax;
        switch(i) {
            case 0:
                baseCRS = CommonCRS.WGS84;
                ymin = 2;
                ymax = 4;
                break;
            case 1:
                baseCRS = CommonCRS.WGS72;
                ymin = 1;
                ymax = 4;
                break;
            case 2:
                baseCRS = CommonCRS.SPHERE;
                ymin = 2;
                ymax = 3;
                break;
            case 3:
                baseCRS = CommonCRS.NAD27;
                ymin = 3;
                ymax = 5;
                break;
            default:
                throw new AssertionError(i);
        }
        properties.put(DefaultProjectedCRS.NAME_KEY, "CRS #" + i);
        properties.put(DefaultProjectedCRS.DOMAIN_OF_VALIDITY_KEY, new DefaultExtent(null, new DefaultGeographicBoundingBox(-1, +1, ymin, ymax), null, null));
        crs[i] = new DefaultProjectedCRS(properties, baseCRS.geographic(), HardCodedConversions.MERCATOR, cs);
    }
    // Exclude the last CRS only.
    final ProjectedCRS[] overlappingCRS = Arrays.copyOf(crs, 3);
    /*
         * Test between the 3 overlapping CRS without region of interest. We expect the CRS having a domain
         * of validity large enough for all CRS; this is the second CRS created in above 'switch' statement.
         */
    assertSame("Expected CRS with widest domain of validity.", crs[1], CRS.suggestCommonTarget(null, overlappingCRS));
    /*
         * If we specify a smaller region of interest, we should get the CRS having the smallest domain of validity that
         * cover the ROI. Following lines gradually increase the ROI size and verify that we get CRS for larger domain.
         */
    final DefaultGeographicBoundingBox regionOfInterest = new DefaultGeographicBoundingBox(-1, +1, 2.1, 2.9);
    assertSame("Expected best fit for [2.1 … 2.9]°N", crs[2], CRS.suggestCommonTarget(regionOfInterest, overlappingCRS));
    regionOfInterest.setNorthBoundLatitude(3.1);
    assertSame("Expected best fit for [2.1 … 3.1]°N", crs[0], CRS.suggestCommonTarget(regionOfInterest, overlappingCRS));
    regionOfInterest.setSouthBoundLatitude(1.9);
    assertSame("Expected best fit for [1.9 … 3.1]°N", crs[1], CRS.suggestCommonTarget(regionOfInterest, overlappingCRS));
    /*
         * All above tests returned one of the CRS in the given array. Test now a case where none of those CRS
         * have a domain of validity wide enough, so suggestCommonTarget(…) need to search among the base CRS.
         */
    assertSame("Expected a GeodeticCRS since none of the ProjectedCRS have a domain of validity wide enough.", crs[0].getBaseCRS(), CRS.suggestCommonTarget(null, crs));
    /*
         * With the same domain of validity than above, suggestCommonTarget(…) should not need to fallback on the
         * base CRS anymore.
         */
    assertSame("Expected best fit for [1.9 … 3.1]°N", crs[1], CRS.suggestCommonTarget(regionOfInterest, crs));
}
Also used : CartesianCS(org.opengis.referencing.cs.CartesianCS) HashMap(java.util.HashMap) DefaultGeographicBoundingBox(org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) DefaultExtent(org.apache.sis.metadata.iso.extent.DefaultExtent) DefaultProjectedCRS(org.apache.sis.referencing.crs.DefaultProjectedCRS) Test(org.junit.Test)

Example 8 with DefaultExtent

use of org.apache.sis.metadata.iso.extent.DefaultExtent in project sis by apache.

the class BursaWolfParametersTest method createED87_to_WGS84.

/**
 * Returns the parameters for the <cite>ED87 to WGS 84 (1)</cite> transformation (EPSG:1146).
 * Area of validity is the North Sea: 5.05°W to 11.13°E in longitude and 51.04°N to 62.0°N in latitude.
 */
static BursaWolfParameters createED87_to_WGS84() {
    final BursaWolfParameters bursaWolf = new BursaWolfParameters(GeodeticDatumMock.WGS84, new DefaultExtent("Europe - North Sea", new DefaultGeographicBoundingBox(-5.05, 11.13, 51.04, 62.0), null, null));
    bursaWolf.tX = -82.981;
    bursaWolf.tY = -99.719;
    bursaWolf.tZ = -110.709;
    bursaWolf.rX = -0.5076;
    bursaWolf.rY = 0.1503;
    bursaWolf.rZ = 0.3898;
    bursaWolf.dS = -0.3143;
    bursaWolf.verify(PrimeMeridianMock.GREENWICH);
    assertFalse("isIdentity", bursaWolf.isIdentity());
    assertFalse("isTranslation", bursaWolf.isTranslation());
    return bursaWolf;
}
Also used : DefaultGeographicBoundingBox(org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox) DefaultExtent(org.apache.sis.metadata.iso.extent.DefaultExtent)

Aggregations

DefaultExtent (org.apache.sis.metadata.iso.extent.DefaultExtent)8 DefaultGeographicBoundingBox (org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox)6 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 Date (java.util.Date)2 ImmutableIdentifier (org.apache.sis.metadata.iso.ImmutableIdentifier)2 DefaultTemporalExtent (org.apache.sis.metadata.iso.extent.DefaultTemporalExtent)2 DependsOnMethod (org.apache.sis.test.DependsOnMethod)2 Extent (org.opengis.metadata.extent.Extent)2 InternationalString (org.opengis.util.InternationalString)2 IOException (java.io.IOException)1 RoundingMode (java.math.RoundingMode)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 NumberFormat (java.text.NumberFormat)1 ParseException (java.text.ParseException)1 Locale (java.util.Locale)1 Length (javax.measure.quantity.Length)1 TableAppender (org.apache.sis.io.TableAppender)1 Angle (org.apache.sis.measure.Angle)1