Search in sources :

Example 6 with DataStoreContentException

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

the class Reader method parseRoute.

/**
 * Parses a {@code <rte>} 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#ROUTES}.
 *
 * @throws Exception see the list of exceptions documented in {@link #parse(Consumer, boolean)}.
 */
private AbstractFeature parseRoute(final int index) throws Exception {
    assert reader.isStartElement() && Tags.ROUTES.equals(reader.getLocalName());
    final AbstractFeature feature = ((Store) owner).types.route.newInstance();
    feature.setPropertyValue("sis:identifier", index);
    List<AbstractFeature> wayPoints = 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.ROUTES:
                            throw new DataStoreContentException(nestedElement(name));
                        case Tags.ROUTE_POINTS:
                            {
                                if (wayPoints == null)
                                    wayPoints = new ArrayList<>(8);
                                wayPoints.add(parseWayPoint(wayPoints.size() + 1));
                                continue;
                            }
                    }
                    feature.setPropertyValue(name, value);
                    break;
                }
            case END_ELEMENT:
                {
                    if (Tags.ROUTES.equals(reader.getLocalName()) && isGPX()) {
                        if (wayPoints != null)
                            feature.setPropertyValue(Tags.ROUTE_POINTS, wayPoints);
                        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 7 with DataStoreContentException

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

the class Reader method parseTrackSegment.

/**
 * Parses a {@code <trkseg>} 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#TRACK_SEGMENTS}.
 *
 * @throws Exception see the list of exceptions documented in {@link #parse(Consumer, boolean)}.
 */
private AbstractFeature parseTrackSegment(final int index) throws Exception {
    assert reader.isStartElement() && Tags.TRACK_SEGMENTS.equals(reader.getLocalName());
    final AbstractFeature feature = ((Store) owner).types.trackSegment.newInstance();
    feature.setPropertyValue("sis:identifier", index);
    List<AbstractFeature> wayPoints = 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(reader.next()) {
            case START_ELEMENT:
                {
                    final String name = reader.getLocalName();
                    switch(isGPX() ? name : "") {
                        default:
                            continue;
                        case Tags.TRACK_POINTS:
                            {
                                if (wayPoints == null)
                                    wayPoints = new ArrayList<>(8);
                                wayPoints.add(parseWayPoint(wayPoints.size() + 1));
                                continue;
                            }
                        case Tags.TRACK_SEGMENTS:
                            throw new DataStoreContentException(nestedElement(name));
                    }
                }
            case END_ELEMENT:
                {
                    if (Tags.TRACK_SEGMENTS.equals(reader.getLocalName()) && isGPX()) {
                        if (wayPoints != null)
                            feature.setPropertyValue(Tags.TRACK_POINTS, wayPoints);
                        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 8 with DataStoreContentException

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

the class ImageFileDirectory method validateMandatoryTags.

/**
 * Verifies that the mandatory tags are present and consistent with each others.
 * If a mandatory tag is absent, then there is a choice:
 *
 * <ul>
 *   <li>If the tag can be inferred from other tag values, performs that computation and logs a warning.</li>
 *   <li>Otherwise throws an exception.</li>
 * </ul>
 *
 * This method opportunistically computes default value of optional fields
 * when those values can be computed from other (usually mandatory) fields.
 *
 * @throws DataStoreContentException if a mandatory tag is missing and can not be inferred.
 */
final void validateMandatoryTags() throws DataStoreContentException {
    if (imageWidth < 0)
        throw missingTag(Tags.ImageWidth);
    if (imageHeight < 0)
        throw missingTag(Tags.ImageLength);
    final short offsetsTag, byteCountsTag;
    switch(tileTagFamily) {
        case STRIP:
            {
                if (tileWidth < 0)
                    tileWidth = Math.toIntExact(imageWidth);
                if (tileHeight < 0)
                    tileHeight = Math.toIntExact(imageHeight);
                offsetsTag = Tags.StripOffsets;
                byteCountsTag = Tags.StripByteCounts;
                break;
            }
        case TILE:
            {
                offsetsTag = Tags.TileOffsets;
                byteCountsTag = Tags.TileByteCounts;
                break;
            }
        default:
            {
                throw new DataStoreContentException(reader.resources().getString(Resources.Keys.InconsistentTileStrip_1, filename()));
            }
    }
    if (tileOffsets == null) {
        throw missingTag(offsetsTag);
    }
    if (samplesPerPixel == 0) {
        samplesPerPixel = 1;
        missingTag(Tags.SamplesPerPixel, 1, false);
    }
    if (bitsPerSample == 0) {
        bitsPerSample = 1;
        missingTag(Tags.BitsPerSample, 1, false);
    }
    if (colorMap != null) {
        ensureSameLength(Tags.ColorMap, Tags.BitsPerSample, colorMap.size(), 3 * (1 << bitsPerSample));
    }
    if (sampleFormat != FLOAT) {
        long minValue, maxValue;
        if (sampleFormat == UNSIGNED) {
            minValue = 0L;
            // All bits set to 1.
            maxValue = -1L;
        } else {
            minValue = Long.MIN_VALUE;
            maxValue = Long.MAX_VALUE;
        }
        final int shift = Long.SIZE - bitsPerSample;
        if (shift >= 0 && shift < Long.SIZE) {
            minValue >>>= shift;
            maxValue >>>= shift;
            if (minValue < maxValue) {
                // Exclude the unsigned long case since we can not represent it.
                minValues = extremum(minValues, Vector.createSequence(minValue, 0, samplesPerPixel), false);
                maxValues = extremum(maxValues, Vector.createSequence(maxValue, 0, samplesPerPixel), true);
            }
        }
    }
    /*
         * All of tile width, height and length information should be provided. But if only one of them is missing,
         * we can compute it provided that the file does not use any compression method. If there is a compression,
         * then we set a bit for preventing the 'switch' block to perform a calculation but we let the code performs
         * the other checks in order to get an exception to be thrown with a good message.
         */
    int missing = !isPlanar && compression.equals(Compression.NONE) ? 0 : 0b1000;
    if (tileWidth < 0)
        missing |= 0b0001;
    if (tileHeight < 0)
        missing |= 0b0010;
    if (tileByteCounts == null)
        missing |= 0b0100;
    switch(missing) {
        case 0:
        case 0b1000:
            {
                // Every thing is ok.
                break;
            }
        case 0b0001:
            {
                // Compute missing tile width.
                tileWidth = computeTileSize(tileHeight);
                missingTag(Tags.TileWidth, tileWidth, true);
                break;
            }
        case 0b0010:
            {
                // Compute missing tile height.
                tileHeight = computeTileSize(tileWidth);
                missingTag(Tags.TileLength, tileHeight, true);
                break;
            }
        case 0b0100:
            {
                // Compute missing tile byte count.
                final long tileByteCount = pixelToByteCount(Math.multiplyExact(tileWidth, tileHeight));
                final long[] tileByteCountArray = new long[tileOffsets.size()];
                Arrays.fill(tileByteCountArray, tileByteCount);
                tileByteCounts = Vector.create(tileByteCountArray, true);
                missingTag(byteCountsTag, tileByteCount, true);
                break;
            }
        default:
            {
                final short tag;
                switch(Integer.lowestOneBit(missing)) {
                    case 0b0001:
                        tag = Tags.TileWidth;
                        break;
                    case 0b0010:
                        tag = Tags.TileLength;
                        break;
                    default:
                        tag = byteCountsTag;
                        break;
                }
                throw missingTag(tag);
            }
    }
    /*
         * Report an error if the tile offset and tile byte count vectors do not have the same length.
         * Then ensure that the number of tiles is equal to the expected number. The formula below is the
         * one documented in the TIFF specification and reproduced in tileWidth & tileHeight fields javadoc.
         */
    ensureSameLength(offsetsTag, byteCountsTag, tileOffsets.size(), tileByteCounts.size());
    long expectedCount = Math.multiplyExact(Math.addExact(imageWidth, tileWidth - 1) / tileWidth, Math.addExact(imageHeight, tileHeight - 1) / tileHeight);
    if (isPlanar) {
        expectedCount = Math.multiplyExact(expectedCount, samplesPerPixel);
    }
    final int actualCount = Math.min(tileOffsets.size(), tileByteCounts.size());
    if (actualCount != expectedCount) {
        throw new DataStoreContentException(reader.resources().getString(Resources.Keys.UnexpectedTileCount_3, filename(), expectedCount, actualCount));
    }
    /*
         * If a "grid to CRS" conversion has been specified with only the scale factor, we need to compute
         * the translation terms now.
         */
    if (gridToCRS != null && !completeMatrixSpecified) {
        if (!GridGeometry.setTranslationTerms(gridToCRS, modelTiePoints)) {
            throw missingTag(Tags.ModelTiePoints);
        }
    }
}
Also used : DataStoreContentException(org.apache.sis.storage.DataStoreContentException)

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