Search in sources :

Example 1 with DisjointExtentException

use of org.apache.sis.coverage.grid.DisjointExtentException in project sis by apache.

the class AbstractGridResource method canNotRead.

/**
 * Creates an exception for a failure to load data. If the failure may be caused by an envelope
 * outside the resource domain, that envelope will be inferred from the {@code request} argument.
 *
 * @param  filename  some identification (typically a file name) of the data that can not be read.
 * @param  request   the requested domain, or {@code null} if unspecified.
 * @param  cause     the cause of the failure, or {@code null} if none.
 * @return the exception to throw.
 */
protected final DataStoreException canNotRead(final String filename, final GridGeometry request, Throwable cause) {
    final int DOMAIN = 1, REFERENCING = 2, CONTENT = 3;
    // One of above constants, with 0 for "none of above".
    int type = 0;
    Envelope bounds = null;
    if (cause instanceof DisjointExtentException) {
        type = DOMAIN;
        if (request != null && request.isDefined(GridGeometry.ENVELOPE)) {
            bounds = request.getEnvelope();
        }
    } else if (cause instanceof RuntimeException) {
        Throwable c = cause.getCause();
        if (isReferencing(c)) {
            type = REFERENCING;
            cause = c;
        } else if (cause instanceof ArithmeticException || cause instanceof RasterFormatException) {
            type = CONTENT;
        }
    } else if (isReferencing(cause)) {
        type = REFERENCING;
    }
    final String message = createExceptionMessage(filename, bounds);
    switch(type) {
        case DOMAIN:
            return new NoSuchDataException(message, cause);
        case REFERENCING:
            return new DataStoreReferencingException(message, cause);
        case CONTENT:
            return new DataStoreContentException(message, cause);
        default:
            return new DataStoreException(message, cause);
    }
}
Also used : DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) DataStoreException(org.apache.sis.storage.DataStoreException) DataStoreContentException(org.apache.sis.storage.DataStoreContentException) DataStoreReferencingException(org.apache.sis.storage.DataStoreReferencingException) Envelope(org.opengis.geometry.Envelope) NoSuchDataException(org.apache.sis.storage.NoSuchDataException) RasterFormatException(java.awt.image.RasterFormatException)

Example 2 with DisjointExtentException

use of org.apache.sis.coverage.grid.DisjointExtentException in project sis by apache.

the class CoverageSubset method clip.

/**
 * Clips the given domain to the area of interest specified by the query. If any grid geometry is null,
 * the other one is returned. The {@code domain} argument should be the domain to read as specified to
 * {@link #read(GridGeometry, int...)}, or the full {@code CoverageSubset} domain if no value were given
 * to the {@code read(…)} method.
 *
 * @param  domain    the domain requested in a read operation, or {@code null}.
 * @param  rounding  whether to clip to nearest box or an enclosing box.
 * @param  clipping  whether to clip the resulting extent to the specified {@code domain} extent.
 * @return intersection of the given grid geometry with the query domain.
 * @throws DataStoreException if the intersection can not be computed.
 */
private GridGeometry clip(final GridGeometry domain, final GridRoundingMode rounding, final GridClippingMode clipping) throws DataStoreException {
    final GridGeometry areaOfInterest = query.getSelection();
    if (domain == null)
        return areaOfInterest;
    if (areaOfInterest == null)
        return domain;
    try {
        final GridDerivation derivation = domain.derive().rounding(rounding).clipping(clipping);
        final int expansion = query.getSourceDomainExpansion();
        if (expansion != 0) {
            final int[] margins = new int[domain.getDimension()];
            Arrays.fill(margins, expansion);
            derivation.margin(margins);
        }
        return derivation.subgrid(areaOfInterest).build();
    } catch (IllegalArgumentException | IllegalStateException e) {
        final String msg = Resources.forLocale(getLocale()).getString(Resources.Keys.CanNotIntersectDataWithQuery_1, getSourceName());
        final Throwable cause = e.getCause();
        if (cause instanceof FactoryException || cause instanceof TransformException) {
            throw new DataStoreReferencingException(msg, cause);
        } else if (e instanceof DisjointExtentException) {
            throw new NoSuchDataException(msg, e);
        } else {
            throw new DataStoreException(msg, e);
        }
    }
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) FactoryException(org.opengis.util.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) GridDerivation(org.apache.sis.coverage.grid.GridDerivation)

Example 3 with DisjointExtentException

use of org.apache.sis.coverage.grid.DisjointExtentException in project sis by apache.

the class TiledGridCoverage method render.

/**
 * Returns a two-dimensional slice of grid data as a rendered image.
 *
 * @param  sliceExtent  a subspace of this grid coverage extent, or {@code null} for the whole image.
 * @return the grid slice as a rendered image. Image location is relative to {@code sliceExtent}.
 */
@Override
public RenderedImage render(GridExtent sliceExtent) {
    final GridExtent available = getGridGeometry().getExtent();
    final int dimension = available.getDimension();
    if (sliceExtent == null) {
        sliceExtent = available;
    } else {
        final int sd = sliceExtent.getDimension();
        if (sd != dimension) {
            throw new MismatchedDimensionException(Errors.format(Errors.Keys.MismatchedDimension_3, "sliceExtent", dimension, sd));
        }
    }
    final int[] selectedDimensions = sliceExtent.getSubspaceDimensions(BIDIMENSIONAL);
    if (selectedDimensions[1] != 1) {
        // TODO
        throw new UnsupportedOperationException("Non-horizontal slices not yet implemented.");
    }
    final RenderedImage image;
    try {
        // Indices of first tile to read, inclusive.
        final int[] tileLower = new int[dimension];
        // Indices of last tile to read, exclusive.
        final int[] tileUpper = new int[dimension];
        // Pixel offset compared to Area Of Interest.
        final int[] offsetAOI = new int[dimension];
        // Subsampled image size.
        final int[] imageSize = new int[dimension];
        for (int i = 0; i < dimension; i++) {
            // Lowest valid coordinate in subsampled image.
            final long min = available.getLow(i);
            // Highest valid coordinate, inclusive.
            final long max = available.getHigh(i);
            // Requested coordinate in subsampled image.
            final long aoiMin = sliceExtent.getLow(i);
            final long aoiMax = sliceExtent.getHigh(i);
            final long tileUp = incrementExact(toTileMatrixCoordinate(Math.min(aoiMax, max), i));
            final long tileLo = toTileMatrixCoordinate(Math.max(aoiMin, min), i);
            if (tileUp <= tileLo) {
                final String message = Errors.getResources(getLocale()).getString(Errors.Keys.IllegalRange_2, aoiMin, aoiMax);
                if (aoiMin > aoiMax) {
                    throw new IllegalArgumentException(message);
                } else {
                    throw new DisjointExtentException(message);
                }
            }
            // Lower and upper coordinates in subsampled image, rounded to integer number of tiles and clipped to available data.
            final long lower = /* inclusive */
            Math.max(toSubsampledPixel(/* inclusive */
            multiplyExact(tileLo, tileSize[i]), i), min);
            final long upper = incrementExact(Math.min(toSubsampledPixel(decrementExact(multiplyExact(tileUp, tileSize[i])), i), max));
            imageSize[i] = toIntExact(subtractExact(upper, lower));
            offsetAOI[i] = toIntExact(subtractExact(lower, aoiMin));
            tileLower[i] = toIntExact(subtractExact(tileLo, tmcOfFirstTile[i]));
            tileUpper[i] = toIntExact(subtractExact(tileUp, tmcOfFirstTile[i]));
        }
        /*
             * Prepare an iterator over all tiles to read, together with the following properties:
             *    - Two-dimensional conversion from pixel coordinates to "real world" coordinates.
             */
        final AOI iterator = new AOI(tileLower, tileUpper, offsetAOI, dimension);
        final Map<String, Object> properties = DeferredProperty.forGridGeometry(getGridGeometry(), selectedDimensions);
        if (deferredTileReading) {
            image = new TiledDeferredImage(imageSize, tileLower, properties, iterator);
        } else {
            /*
                 * If the loading strategy is not `RasterLoadingStrategy.AT_GET_TILE_TIME`, get all tiles
                 * in the area of interest now. I/O operations, if needed, happen in `readTiles(…)` call.
                 */
            final Raster[] result = readTiles(iterator);
            image = new TiledImage(properties, colors, imageSize[X_DIMENSION], imageSize[Y_DIMENSION], tileLower[X_DIMENSION], tileLower[Y_DIMENSION], result);
        }
    } catch (Exception e) {
        // Too many exception types for listing them all.
        throw new CannotEvaluateException(Resources.forLocale(getLocale()).getString(Resources.Keys.CanNotRenderImage_1, getDisplayName()), e);
    }
    return image;
}
Also used : DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) GridExtent(org.apache.sis.coverage.grid.GridExtent) Raster(java.awt.image.Raster) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) Point(java.awt.Point) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) CannotEvaluateException(org.apache.sis.coverage.CannotEvaluateException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) RenderedImage(java.awt.image.RenderedImage) TiledImage(org.apache.sis.internal.coverage.j2d.TiledImage) CannotEvaluateException(org.apache.sis.coverage.CannotEvaluateException)

Aggregations

DisjointExtentException (org.apache.sis.coverage.grid.DisjointExtentException)3 DataStoreException (org.apache.sis.storage.DataStoreException)2 Point (java.awt.Point)1 Raster (java.awt.image.Raster)1 RasterFormatException (java.awt.image.RasterFormatException)1 RenderedImage (java.awt.image.RenderedImage)1 IOException (java.io.IOException)1 CannotEvaluateException (org.apache.sis.coverage.CannotEvaluateException)1 GridDerivation (org.apache.sis.coverage.grid.GridDerivation)1 GridExtent (org.apache.sis.coverage.grid.GridExtent)1 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)1 TiledImage (org.apache.sis.internal.coverage.j2d.TiledImage)1 DataStoreContentException (org.apache.sis.storage.DataStoreContentException)1 DataStoreReferencingException (org.apache.sis.storage.DataStoreReferencingException)1 NoSuchDataException (org.apache.sis.storage.NoSuchDataException)1 Envelope (org.opengis.geometry.Envelope)1 MismatchedDimensionException (org.opengis.geometry.MismatchedDimensionException)1 TransformException (org.opengis.referencing.operation.TransformException)1 FactoryException (org.opengis.util.FactoryException)1