Search in sources :

Example 1 with DataStoreReferencingException

use of org.apache.sis.storage.DataStoreReferencingException in project sis by apache.

the class Store method getMetadata.

/**
 * Returns the metadata associated to the CSV file, or {@code null} if none.
 *
 * @return the metadata associated to the CSV file, or {@code null} if none.
 * @throws DataStoreException if an error occurred during the parsing process.
 */
@Override
public synchronized Metadata getMetadata() throws DataStoreException {
    if (metadata == null) {
        final MetadataBuilder builder = new MetadataBuilder();
        try {
            builder.setFormat(timeEncoding != null && hasTrajectories ? StoreProvider.MOVING : StoreProvider.NAME);
        } catch (MetadataStoreException e) {
            listeners.warning(null, e);
        }
        builder.addEncoding(encoding, MetadataBuilder.Scope.ALL);
        builder.addResourceScope(ScopeCode.DATASET, null);
        try {
            builder.addExtent(envelope);
        } catch (TransformException e) {
            throw new DataStoreReferencingException(getLocale(), StoreProvider.NAME, getDisplayName(), source).initCause(e);
        } catch (UnsupportedOperationException e) {
            /*
                 * Failed to set the temporal components if the sis-temporal module was
                 * not on the classpath, but the other dimensions still have been set.
                 */
            listeners.warning(null, e);
        }
        builder.addFeatureType(featureType, null);
        addTitleOrIdentifier(builder);
        metadata = builder.build(true);
    }
    return metadata;
}
Also used : MetadataStoreException(org.apache.sis.metadata.sql.MetadataStoreException) MetadataBuilder(org.apache.sis.internal.storage.MetadataBuilder) TransformException(org.opengis.referencing.operation.TransformException) DataStoreReferencingException(org.apache.sis.storage.DataStoreReferencingException)

Example 2 with DataStoreReferencingException

use of org.apache.sis.storage.DataStoreReferencingException in project sis by apache.

the class Store method parseEnvelope.

/**
 * Parses the envelope described by the header line starting with {@code @stboundedby}.
 * The envelope returned by this method will be stored in the {@link #envelope} field.
 *
 * <p>Example:</p>
 * {@preformat text
 *   &#64;stboundedby, urn:ogc:def:crs:CRS:1.3:84, 2D, 50.23 9.23, 50.31 9.27, 2012-01-17T12:33:41Z, 2012-01-17T12:37:00Z, sec
 * }
 *
 * This method sets {@link #timeEncoding} and {@link #spatialDimensionCount} as a side-effect.
 *
 * @param  elements  the line elements. The first elements should be {@code "@stboundedby"}.
 * @return the envelope, or {@code null} if the given list does not contain enough elements.
 */
@SuppressWarnings("fallthrough")
private GeneralEnvelope parseEnvelope(final List<String> elements) throws DataStoreException, FactoryException {
    CoordinateReferenceSystem crs = null;
    int spatialDimensionCount = 2;
    boolean isDimExplicit = false;
    double[] lowerCorner = ArraysExt.EMPTY_DOUBLE;
    double[] upperCorner = ArraysExt.EMPTY_DOUBLE;
    Instant startTime = null;
    Instant endTime = null;
    Unit<Time> timeUnit = Units.SECOND;
    boolean isTimeAbsolute = false;
    int ordinal = -1;
    for (final String element : elements) {
        ordinal++;
        if (!element.isEmpty()) {
            switch(ordinal) {
                // The "@stboundedby" header.
                case 0:
                    continue;
                case 1:
                    crs = CRS.forCode(element);
                    continue;
                case 2:
                    if (element.length() == 2 && Character.toUpperCase(element.charAt(1)) == 'D') {
                        isDimExplicit = true;
                        spatialDimensionCount = element.charAt(0) - '0';
                        if (spatialDimensionCount < 1 || spatialDimensionCount > 3) {
                            throw new DataStoreReferencingException(errors().getString(Errors.Keys.IllegalCoordinateSystem_1, element));
                        }
                        continue;
                    }
                    /*
                             * According the Moving Feature specification, the [dim] element is optional.
                             * If we did not recognized the dimension, assume that we have the next element
                             * (i.e. the lower corner). Fall-through so we can process it.
                             */
                    // Fall through
                    ordinal++;
                case 3:
                    lowerCorner = CharSequences.parseDoubles(element, ORDINATE_SEPARATOR);
                    continue;
                case 4:
                    upperCorner = CharSequences.parseDoubles(element, ORDINATE_SEPARATOR);
                    continue;
                case 5:
                    startTime = Instant.parse(element);
                    continue;
                case 6:
                    endTime = Instant.parse(element);
                    continue;
                case 7:
                    switch(element.toLowerCase(Locale.US)) {
                        case "sec":
                        case "second":
                            /* Already SECOND. */
                            continue;
                        case "minute":
                            timeUnit = Units.MINUTE;
                            continue;
                        case "hour":
                            timeUnit = Units.HOUR;
                            continue;
                        case "day":
                            timeUnit = Units.DAY;
                            continue;
                        case "absolute":
                            isTimeAbsolute = true;
                            continue;
                        default:
                            throw new DataStoreReferencingException(errors().getString(Errors.Keys.UnknownUnit_1, element));
                    }
            }
            // If we reach this point, there is some remaining unknown elements. Ignore them.
            break;
        }
    }
    /*
         * Complete the CRS by adding a vertical component if needed, then a temporal component.
         * Only after the CRS has been completed we can create the envelope.
         *
         * Vertical component:
         *   Ideally, should be part of the CRS created from the authority code. But if the authority
         *   code is only for a two-dimensional CRS, we default to an arbitrary height component.
         *
         * Temporal component:
         *   Assumed never part of the authority code. We need to build the temporal component ourselves
         *   in order to set the origin to the start time.
         */
    final GeneralEnvelope envelope;
    if (crs != null) {
        int count = 0;
        final CoordinateReferenceSystem[] components = new CoordinateReferenceSystem[3];
        components[count++] = crs;
        /*
             * If the coordinates are three-dimensional but the CRS is 2D, add a vertical axis.
             * The vertical axis shall be the third one, however we do not enforce that rule
             * since Apache SIS should work correctly even if the vertical axis is elsewhere.
             */
        int dimension = crs.getCoordinateSystem().getDimension();
        if (isDimExplicit) {
            if (spatialDimensionCount > dimension) {
                components[count++] = CommonCRS.Vertical.MEAN_SEA_LEVEL.crs();
                dimension++;
            }
            if (dimension != spatialDimensionCount) {
                throw new DataStoreReferencingException(errors().getString(Errors.Keys.MismatchedDimension_3, "@stboundedby(CRS)", spatialDimensionCount, dimension));
            }
        }
        if (dimension > Short.MAX_VALUE) {
            throw new DataStoreReferencingException(errors().getString(Errors.Keys.ExcessiveNumberOfDimensions_1, dimension));
        }
        spatialDimensionCount = dimension;
        /*
             * Add a temporal axis if we have a start time (no need for end time).
             * This block presumes that the CRS does not already have a time axis.
             * If a time axis was already present, an exception will be thrown at
             * builder.createCompoundCRS(…) invocation time.
             */
        final GeodeticObjectBuilder builder = new GeodeticObjectBuilder();
        String name = crs.getName().getCode();
        if (startTime != null) {
            final TemporalCRS temporal;
            if (isTimeAbsolute) {
                temporal = TimeEncoding.DEFAULT.crs();
                timeEncoding = TimeEncoding.ABSOLUTE;
            } else {
                temporal = builder.createTemporalCRS(Date.from(startTime), timeUnit);
                timeEncoding = new TimeEncoding(temporal.getDatum(), timeUnit);
            }
            components[count++] = temporal;
            name = name + " + " + temporal.getName().getCode();
        }
        crs = builder.addName(name).createCompoundCRS(ArraysExt.resize(components, count));
        envelope = new GeneralEnvelope(crs);
    } else {
        /*
             * While illegal in principle, Apache SIS accepts missing CRS.
             * In such case, use only the number of dimensions.
             */
        int dim = spatialDimensionCount;
        // Same criterion than in above block.
        if (startTime != null)
            dim++;
        envelope = new GeneralEnvelope(dim);
    }
    /*
         * At this point we got the three- or four-dimensional spatio-temporal CRS.
         * We can now set the envelope coordinate values, including temporal values.
         */
    int dim;
    if ((dim = lowerCorner.length) != spatialDimensionCount || (dim = upperCorner.length) != spatialDimensionCount) {
        throw new DataStoreReferencingException(errors().getString(Errors.Keys.MismatchedDimension_3, "@stboundedby(BBOX)", spatialDimensionCount, dim));
    }
    for (int i = 0; i < spatialDimensionCount; i++) {
        envelope.setRange(i, lowerCorner[i], upperCorner[i]);
    }
    if (startTime != null) {
        envelope.setRange(spatialDimensionCount, timeEncoding.toCRS(startTime.toEpochMilli()), (endTime == null) ? Double.NaN : timeEncoding.toCRS(endTime.toEpochMilli()));
    }
    this.spatialDimensionCount = (short) spatialDimensionCount;
    return envelope;
}
Also used : Instant(java.time.Instant) Time(javax.measure.quantity.Time) TemporalCRS(org.opengis.referencing.crs.TemporalCRS) DataStoreReferencingException(org.apache.sis.storage.DataStoreReferencingException) GeodeticObjectBuilder(org.apache.sis.internal.referencing.GeodeticObjectBuilder) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Aggregations

DataStoreReferencingException (org.apache.sis.storage.DataStoreReferencingException)2 Instant (java.time.Instant)1 Time (javax.measure.quantity.Time)1 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)1 GeodeticObjectBuilder (org.apache.sis.internal.referencing.GeodeticObjectBuilder)1 MetadataBuilder (org.apache.sis.internal.storage.MetadataBuilder)1 MetadataStoreException (org.apache.sis.metadata.sql.MetadataStoreException)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 TemporalCRS (org.opengis.referencing.crs.TemporalCRS)1 TransformException (org.opengis.referencing.operation.TransformException)1