Search in sources :

Example 1 with DataStoreContentException

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

the class GeoTiffStore method getMetadata.

/**
 * Returns information about the dataset as a whole. The returned metadata object can contain information
 * such as the spatiotemporal extent of the dataset, contact information about the creator or distributor,
 * data quality, usage constraints and more.
 *
 * @return information about the dataset.
 * @throws DataStoreException if an error occurred while reading the data.
 */
@Override
public synchronized Metadata getMetadata() throws DataStoreException {
    if (metadata == null) {
        final Reader reader = reader();
        final MetadataBuilder builder = reader.metadata;
        try {
            builder.setFormat(Constants.GEOTIFF);
        } catch (MetadataStoreException e) {
            warning(null, e);
        }
        builder.addEncoding(encoding, MetadataBuilder.Scope.METADATA);
        builder.addResourceScope(ScopeCode.valueOf("COVERAGE"), null);
        final Locale locale = getLocale();
        int n = 0;
        try {
            ImageFileDirectory dir;
            while ((dir = reader.getImageFileDirectory(n++)) != null) {
                dir.completeMetadata(builder, locale);
            }
        } catch (IOException e) {
            throw new DataStoreException(errors().getString(Errors.Keys.CanNotRead_1, reader.input.filename), e);
        } catch (FactoryException | ArithmeticException e) {
            throw new DataStoreContentException(getLocale(), Constants.GEOTIFF, reader.input.filename, null).initCause(e);
        }
        /*
             * Add the filename as an identifier only if the input was something convertible to URI (URL, File or Path),
             * otherwise reader.input.filename may not be useful; it may be just the InputStream classname. If the TIFF
             * file did not specified any ImageDescription tag, then we will had the filename as a title instead than an
             * identifier because the title is mandatory in ISO 19115 metadata.
             */
        if (location != null) {
            builder.addTitleOrIdentifier(IOUtilities.filenameWithoutExtension(reader.input.filename), MetadataBuilder.Scope.ALL);
        }
        metadata = builder.build(true);
    }
    return metadata;
}
Also used : Locale(java.util.Locale) MetadataStoreException(org.apache.sis.metadata.sql.MetadataStoreException) DataStoreException(org.apache.sis.storage.DataStoreException) MetadataBuilder(org.apache.sis.internal.storage.MetadataBuilder) DataStoreContentException(org.apache.sis.storage.DataStoreContentException) FactoryException(org.opengis.util.FactoryException) IOException(java.io.IOException)

Example 2 with DataStoreContentException

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

the class Reader method parseWayPoint.

/**
 * Parses a {@code <wpt>}, {@code <rtept>} or {@code <trkpt>} element.
 * The STAX reader {@linkplain XMLStreamReader#getEventType() current event} must be a {@link #START_ELEMENT}.
 * After this method invocation, the reader will be on {@link #END_ELEMENT}.
 *
 * @throws Exception see the list of exceptions documented in {@link #parse(Consumer, boolean)}.
 */
private AbstractFeature parseWayPoint(final int index) throws Exception {
    assert reader.isStartElement();
    /*
         * Way points might be located in different elements: <wpt>, <rtept> and <trkpt>.
         * We have to keep the current tag name in order to know when we reach the end.
         * We are lenient about namespace since we do not allow nested way points.
         */
    final String tagName = reader.getLocalName();
    final String lat = reader.getAttributeValue(null, Attributes.LATITUDE);
    final String lon = reader.getAttributeValue(null, Attributes.LONGITUDE);
    if (lat == null || lon == null) {
        throw new DataStoreContentException(errors().getString(Errors.Keys.MandatoryAttribute_2, (lat == null) ? Attributes.LATITUDE : Attributes.LONGITUDE, tagName));
    }
    final Types types = ((Store) owner).types;
    final AbstractFeature feature = types.wayPoint.newInstance();
    feature.setPropertyValue("sis:identifier", index);
    feature.setPropertyValue("sis:geometry", types.geometries.createPoint(parseDouble(lon), parseDouble(lat)));
    List<Link> links = null;
    while (true) {
        /*
             * We do not need to check 'reader.hasNext()' in above loop
             * since this check is done by the END_DOCUMENT case below.
             */
        switch(next()) {
            case START_ELEMENT:
                {
                    final Object value;
                    final String name = reader.getLocalName();
                    switch(isGPX() ? name : "") {
                        // Fallthrough to getElementText()
                        case Tags.NAME:
                        // ︙
                        case Tags.COMMENT:
                        // ︙
                        case Tags.DESCRIPTION:
                        // ︙
                        case Tags.SOURCE:
                        // ︙
                        case Tags.SYMBOL:
                        case Tags.TYPE:
                            value = getElementText();
                            break;
                        case Tags.TIME:
                            value = getElementAsTemporal();
                            break;
                        // Fallthrough to getElementAsDouble()
                        case Tags.MAGNETIC_VAR:
                        // ︙
                        case Tags.GEOID_HEIGHT:
                        // ︙
                        case Tags.AGE_OF_GPS_DATA:
                        // ︙
                        case Tags.HDOP:
                        // ︙
                        case Tags.PDOP:
                        // ︙
                        case Tags.VDOP:
                        case Tags.ELEVATION:
                            value = getElementAsDouble();
                            break;
                        // Fallthrough to getElementAsInteger()
                        case Tags.SATELITTES:
                        case Tags.DGPS_ID:
                            value = getElementAsInteger();
                            break;
                        case Tags.FIX:
                            value = Fix.fromGPX(getElementText());
                            break;
                        case Tags.LINK:
                            links = Metadata.addIfNonNull(links, unmarshal(Link.class));
                            continue;
                        case Tags.URL:
                            links = Metadata.addIfNonNull(links, Link.valueOf(getElementAsURI()));
                            continue;
                        default:
                            {
                                if (name.equals(tagName)) {
                                    throw new DataStoreContentException(nestedElement(name));
                                }
                                continue;
                            }
                    }
                    feature.setPropertyValue(name, value);
                    break;
                }
            case END_ELEMENT:
                {
                    if (tagName.equals(reader.getLocalName()) && isGPX()) {
                        if (links != null)
                            feature.setPropertyValue(Tags.LINK, links);
                        return feature;
                    }
                    break;
                }
            case END_DOCUMENT:
                {
                    throw new EOFException(endOfFile());
                }
        }
    }
}
Also used : DataStoreContentException(org.apache.sis.storage.DataStoreContentException) EOFException(java.io.EOFException) AbstractFeature(org.apache.sis.feature.AbstractFeature)

Example 3 with DataStoreContentException

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

the class Reader method parseTrack.

/**
 * Parses a {@code <trk>} element. The STAX reader {@linkplain XMLStreamReader#getEventType() current event}
 * must be a {@link #START_ELEMENT} and the name of that start element must be {@link Tags#TRACKS}.
 *
 * @throws Exception see the list of exceptions documented in {@link #parse(Consumer, boolean)}.
 */
private AbstractFeature parseTrack(final int index) throws Exception {
    assert reader.isStartElement() && Tags.TRACKS.equals(reader.getLocalName());
    final AbstractFeature feature = ((Store) owner).types.track.newInstance();
    feature.setPropertyValue("sis:identifier", index);
    List<AbstractFeature> segments = null;
    List<Link> links = null;
    while (true) {
        /*
             * We do not need to check 'reader.hasNext()' in above loop
             * since this check is done by the END_DOCUMENT case below.
             */
        switch(next()) {
            case START_ELEMENT:
                {
                    final Object value;
                    final String name = reader.getLocalName();
                    switch(isGPX() ? name : "") {
                        default:
                            continue;
                        // Fallthrough to getElementText()
                        case Tags.NAME:
                        // ︙
                        case Tags.COMMENT:
                        // ︙
                        case Tags.DESCRIPTION:
                        // ︙
                        case Tags.SOURCE:
                        case Tags.TYPE:
                            value = getElementText();
                            break;
                        case Tags.NUMBER:
                            value = getElementAsInteger();
                            break;
                        case Tags.LINK:
                            links = Metadata.addIfNonNull(links, unmarshal(Link.class));
                            continue;
                        case Tags.URL:
                            links = Metadata.addIfNonNull(links, Link.valueOf(getElementAsURI()));
                            continue;
                        case Tags.TRACKS:
                            throw new DataStoreContentException(nestedElement(name));
                        case Tags.TRACK_SEGMENTS:
                            {
                                if (segments == null)
                                    segments = new ArrayList<>(8);
                                segments.add(parseTrackSegment(segments.size() + 1));
                                continue;
                            }
                    }
                    feature.setPropertyValue(name, value);
                    break;
                }
            case END_ELEMENT:
                {
                    if (Tags.TRACKS.equals(reader.getLocalName()) && isGPX()) {
                        if (segments != null)
                            feature.setPropertyValue(Tags.TRACK_SEGMENTS, segments);
                        if (links != null)
                            feature.setPropertyValue(Tags.LINK, links);
                        return feature;
                    }
                    break;
                }
            case END_DOCUMENT:
                {
                    throw new EOFException(endOfFile());
                }
        }
    }
}
Also used : DataStoreContentException(org.apache.sis.storage.DataStoreContentException) ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) AbstractFeature(org.apache.sis.feature.AbstractFeature)

Example 4 with DataStoreContentException

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

the class Reader method initialize.

/**
 * Reads the metadata. This method should be invoked exactly once after construction.
 * This work is performed outside the constructor for allowing {@link #close()} method
 * invocation no matter if this {@code initialize(boolean)} method fails.
 *
 * @param  readMetadata  if {@code false}, skip the reading of metadata elements.
 * @return the GPX file version, or {@code null} if no version information was found.
 * @throws DataStoreException if the root element is not the expected one.
 * @throws XMLStreamException if an error occurred while reading the XML file.
 * @throws JAXBException if an error occurred while parsing GPX 1.1 metadata.
 * @throws ClassCastException if an object unmarshalled by JAXB was not of the expected type.
 * @throws URISyntaxException if an error occurred while parsing URI in GPX 1.0 metadata.
 * @throws DateTimeParseException if a text can not be parsed as a date.
 * @throws EOFException if the file seems to be truncated.
 */
public Version initialize(final boolean readMetadata) throws DataStoreException, XMLStreamException, JAXBException, URISyntaxException, EOFException {
    /*
         * Skip comments, characters, entity declarations, etc. until we find the root element.
         * If that root is anything other than <gpx>, we consider that this is not a GPX file.
         */
    moveToRootElement(Reader::isGPX, Tags.GPX);
    /*
         * If a version attribute is found on the <gpx> element, use that value for detecting the GPX version.
         * If a version is specified, we require major.minor version 1.0 or 1.1 but accept any bug-fix versions
         * (e.g. 1.1.x). If no version attribute was found, try to infer the version from the namespace URL.
         */
    namespace = reader.getNamespaceURI();
    String ver = reader.getAttributeValue(null, Attributes.VERSION);
    Version version = null;
    if (ver != null) {
        version = new Version(ver);
        if (version.compareTo(StoreProvider.V1_0, 2) < 0 || version.compareTo(StoreProvider.V1_1, 2) > 0) {
            throw new DataStoreContentException(errors().getString(Errors.Keys.UnsupportedFormatVersion_2, owner.getFormatName(), version));
        }
    } else if (namespace != null) {
        switch(namespace) {
            case Tags.NAMESPACE_V10:
                version = StoreProvider.V1_0;
                break;
            case Tags.NAMESPACE_V11:
                version = StoreProvider.V1_1;
                break;
        }
    }
    /*
         * Read metadata immediately, from current position until the beginning of way points, tracks or routes.
         * The metadata can appear in two forms:
         *
         *   - In GPX 1.0, they are declared directly in the <gpx> body.
         *     Those elements are parsed in the switch statement below.
         *
         *   - In GPX 1.1, they are declared in a <metadata> sub-element and their structure is a little bit
         *     more elaborated than what it was in the previous version. We will use JAXB for parsing them.
         */
    parse: while (reader.hasNext()) {
        switch(next()) {
            case START_ELEMENT:
                {
                    /*
                     * GPX 1.0 and 1.1 metadata should not be mixed. However the following code will work even
                     * if GPX 1.0 metadata like <name> or <author> appear after the GPX 1.1 <metadata> element.
                     * If both kind of metadata are specified, the latest value overwrites the values before it.
                     */
                    if (isGPX()) {
                        final String name = reader.getLocalName();
                        if (readMetadata) {
                            switch(name) {
                                // GPX 1.1 metadata
                                case Tags.METADATA:
                                    metadata = unmarshal(Metadata.class);
                                    break;
                                // GPX 1.0 metadata
                                case Tags.NAME:
                                    metadata().name = getElementText();
                                    break;
                                case Tags.DESCRIPTION:
                                    metadata().description = getElementText();
                                    break;
                                case Tags.AUTHOR:
                                    author().name = getElementText();
                                    break;
                                case Tags.EMAIL:
                                    author().email = getElementText();
                                    break;
                                case Tags.URL:
                                    link().uri = getElementAsURI();
                                    break;
                                case Tags.URL_NAME:
                                    link().text = getElementText();
                                    break;
                                case Tags.TIME:
                                    metadata().time = getElementAsDate();
                                    break;
                                case Tags.KEYWORDS:
                                    metadata().keywords = getElementAsList();
                                    break;
                                case Tags.BOUNDS:
                                    metadata().bounds = unmarshal(Bounds.class);
                                    break;
                                // stop metadata parsing.
                                case Tags.WAY_POINT:
                                case Tags.TRACKS:
                                case Tags.ROUTES:
                                    break parse;
                                case Tags.GPX:
                                    throw new DataStoreContentException(nestedElement(Tags.GPX));
                            }
                        } else {
                            /*
                             * If the caller asked to skip metadata, just look for the end of metadata elements.
                             */
                            switch(name) {
                                case Tags.METADATA:
                                    skipUntilEnd(reader.getName());
                                    break;
                                case Tags.WAY_POINT:
                                case Tags.TRACKS:
                                case Tags.ROUTES:
                                    break parse;
                                case Tags.GPX:
                                    throw new DataStoreContentException(nestedElement(Tags.GPX));
                            }
                        }
                    }
                    break;
                }
            case END_ELEMENT:
                {
                    /*
                     * Reminder: calling next() after getElementText(), getElementAsFoo() and unmarshal(…) methods
                     * moves the reader after the END_ELEMENT event. Consequently there is only the enclosing <gpx>
                     * tag to check here.
                     */
                    if (isEndGPX()) {
                        break parse;
                    }
                    break;
                }
        }
    }
    if (readMetadata) {
        metadata().store = (Store) owner;
    }
    return version;
}
Also used : DataStoreContentException(org.apache.sis.storage.DataStoreContentException) Version(org.apache.sis.util.Version) StaxStreamReader(org.apache.sis.internal.storage.xml.stream.StaxStreamReader) XMLStreamReader(javax.xml.stream.XMLStreamReader)

Example 5 with DataStoreContentException

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

the class VariableWrapper method read.

/**
 * Reads a sub-sampled sub-area of the variable.
 *
 * @param  areaLower    index of the first value to read along each dimension.
 * @param  areaUpper    index after the last value to read along each dimension.
 * @param  subsampling  sub-sampling along each dimension. 1 means no sub-sampling.
 * @return the data as an array of a Java primitive type.
 */
@Override
public Vector read(final int[] areaLower, final int[] areaUpper, final int[] subsampling) throws IOException, DataStoreException {
    final int[] size = new int[areaUpper.length];
    for (int i = 0; i < size.length; i++) {
        size[i] = areaUpper[i] - areaLower[i];
    }
    final Array array;
    try {
        array = variable.read(new Section(areaLower, size, subsampling));
    } catch (InvalidRangeException e) {
        throw new DataStoreContentException(e);
    }
    return Vector.create(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
}
Also used : Array(ucar.ma2.Array) DataStoreContentException(org.apache.sis.storage.DataStoreContentException) InvalidRangeException(ucar.ma2.InvalidRangeException) Section(ucar.ma2.Section)

Aggregations

DataStoreContentException (org.apache.sis.storage.DataStoreContentException)8 EOFException (java.io.EOFException)4 AbstractFeature (org.apache.sis.feature.AbstractFeature)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)1 Locale (java.util.Locale)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 MetadataBuilder (org.apache.sis.internal.storage.MetadataBuilder)1 StaxStreamReader (org.apache.sis.internal.storage.xml.stream.StaxStreamReader)1 MetadataStoreException (org.apache.sis.metadata.sql.MetadataStoreException)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 Version (org.apache.sis.util.Version)1 FactoryException (org.opengis.util.FactoryException)1 Array (ucar.ma2.Array)1 InvalidRangeException (ucar.ma2.InvalidRangeException)1 Section (ucar.ma2.Section)1