Search in sources :

Example 6 with GridCoverage

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

the class MultiResolutionCoverageLoaderTest method assertLoadEquals.

/**
 * Verifies that loading a coverage at the specified level result in a grid coverage
 * with the given scale factors.
 */
private void assertLoadEquals(final int level, final double sx, final double sy, final double sz) throws DataStoreException {
    final GridCoverage coverage = loader.getOrLoad(level);
    final MathTransform gridToCRS = coverage.getGridGeometry().getGridToCRS(PixelInCell.CELL_CORNER);
    final MathTransform expected = MathTransforms.scale(sx, sy, sz);
    assertEquals(expected, gridToCRS);
    assertSame(coverage, loader.getOrLoad(level));
}
Also used : GridCoverage(org.apache.sis.coverage.grid.GridCoverage) MathTransform(org.opengis.referencing.operation.MathTransform)

Example 7 with GridCoverage

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

the class RasterReaderTest method compareReadResult.

/**
 * Reads the file for the given test enumeration and compares with the expected raster.
 * The given reader and input are used for reading the raster. The input will be closed.
 */
static void compareReadResult(final TestRaster test, final RasterReader reader, final ChannelDataInput input) throws Exception {
    final GridCoverage coverage = reader.readAsCoverage(input);
    input.channel.close();
    assertEquals(TestRaster.SRID, reader.getSRID());
    assertEquals(TestRaster.getGridToCRS(), reader.getGridToCRS());
    compareReadResult(test, coverage);
}
Also used : GridCoverage(org.apache.sis.coverage.grid.GridCoverage)

Example 8 with GridCoverage

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

the class CoverageReadConsistency method readAndCompareRandomRegions.

/**
 * Implementation of methods testing reading in random sub-regions with random sub-samplings.
 *
 * @param  label  a label for the test being run.
 * @throws DataStoreException if an error occurred while using the resource.
 */
private void readAndCompareRandomRegions(final String label) throws DataStoreException {
    randomConfigureResource();
    final GridGeometry gg = resource.getGridGeometry();
    final int dimension = gg.getDimension();
    final long[] low = new long[dimension];
    final long[] high = new long[dimension];
    final int[] subsampling = new int[dimension];
    final int[] subOffsets = new int[dimension];
    final int numBands = resource.getSampleDimensions().size();
    /*
         * We will collect statistics on execution time only if the
         * test is executed in a more verbose mode than the default.
         */
    final Statistics durations = (VERBOSE || !failOnMismatch) ? new Statistics(label) : null;
    int failuresCount = 0;
    for (int it = 0; it < numIterations; it++) {
        final GridGeometry domain = randomDomain(gg, low, high, subsampling);
        final int[] selectedBands = randomRange(numBands);
        /*
             * Read a coverage containing the requested sub-domain. Note that the reader is free to read
             * more data than requested. The extent actually read is `actualReadExtent`. It shall contain
             * fully the requested `domain`.
             */
        final long startTime = System.nanoTime();
        final GridCoverage subset = resource.read(domain, selectedBands);
        final GridExtent actualReadExtent = subset.getGridGeometry().getExtent();
        if (failOnMismatch) {
            assertEquals("Unexpected number of dimensions.", dimension, actualReadExtent.getDimension());
            for (int d = 0; d < dimension; d++) {
                if (subsampling[d] == 1) {
                    assertTrue("Actual extent is too small.", actualReadExtent.getSize(d) > high[d] - low[d]);
                    assertTrue("Actual extent is too small.", actualReadExtent.getLow(d) <= low[d]);
                    assertTrue("Actual extent is too small.", actualReadExtent.getHigh(d) >= high[d]);
                }
            }
        }
        /*
             * If subsampling was enabled, the factors selected by the reader may be different than
             * the subsampling factors that we specified. The following block updates those values.
             */
        if (allowSubsampling && full != null) {
            final GridDerivation change = full.getGridGeometry().derive().subgrid(subset.getGridGeometry());
            System.arraycopy(change.getSubsampling(), 0, subsampling, 0, dimension);
            System.arraycopy(change.getSubsamplingOffsets(), 0, subOffsets, 0, dimension);
        }
        /*
             * Iterate over all dimensions greater than 2. In the common case where we are reading a
             * two-dimensional image, the following loop will be executed only once. If reading a 3D
             * or 4D image, the loop is executed for all possible two-dimensional slices in the cube.
             */
        final int sd = actualReadExtent.getDimension();
        final long[] sliceMin = new long[sd];
        final long[] sliceMax = new long[sd];
        for (int i = 0; i < sd; i++) {
            sliceMin[i] = actualReadExtent.getLow(i);
            sliceMax[i] = actualReadExtent.getHigh(i);
        }
        nextSlice: for (; ; ) {
            System.arraycopy(sliceMin, BIDIMENSIONAL, sliceMax, BIDIMENSIONAL, dimension - BIDIMENSIONAL);
            final PixelIterator itr = iterator(full, sliceMin, sliceMax, subsampling, subOffsets, allowSubsampling);
            final PixelIterator itc = iterator(subset, sliceMin, sliceMax, subsampling, subOffsets, false);
            if (itr != null) {
                assertEquals(itr.getDomain().getSize(), itc.getDomain().getSize());
                final double[] expected = new double[selectedBands.length];
                double[] reference = null, actual = null;
                while (itr.next()) {
                    assertTrue(itc.next());
                    reference = itr.getPixel(reference);
                    actual = itc.getPixel(actual);
                    for (int i = 0; i < selectedBands.length; i++) {
                        expected[i] = reference[selectedBands[i]];
                    }
                    if (!Arrays.equals(expected, actual)) {
                        failuresCount++;
                        if (!failOnMismatch)
                            break;
                        final Point pr = itr.getPosition();
                        final Point pc = itc.getPosition();
                        final StringBuilder message = new StringBuilder(100).append("Mismatch at position (").append(pr.x).append(", ").append(pr.y).append(") in full image and (").append(pc.x).append(", ").append(pc.y).append(") in tested sub-image");
                        findMatchPosition(itr, pr, selectedBands, actual, message);
                        assertArrayEquals(message.toString(), expected, actual, STRICT);
                    /*
                             * POSSIBLE CAUSES FOR TEST FAILURE (known issues):
                             *
                             *   - If the `GridGeometry` has no `gridToCRS` transform, then `GridDerivation` manages
                             *     to save the scales (subsampling factors) anyway but the translations (subsampling
                             *     offsets) are lost. It causes an image shift if the offsets were not zero. Because
                             *     `gridToCRS` should never be null with spatial data, we do not complexify the code
                             *     for what may be a non-issue.
                             */
                    }
                }
                assertFalse(itc.next());
            } else {
                // Unable to create a reference image. Just check that no exception is thrown.
                double[] actual = null;
                while (itc.next()) {
                    actual = itc.getPixel(actual);
                }
            }
            /*
                 * Move to the next two-dimensional slice and read again.
                 * We stop the loop after we have read all 2D slices.
                 */
            for (int d = dimension; --d >= BIDIMENSIONAL; ) {
                if (sliceMin[d]++ <= actualReadExtent.getHigh(d))
                    continue nextSlice;
                sliceMin[d] = actualReadExtent.getLow(d);
            }
            break;
        }
        if (durations != null) {
            durations.accept((System.nanoTime() - startTime) / (double) StandardDateFormat.NANOS_PER_MILLISECOND);
        }
    }
    /*
         * Show statistics only if the test are executed with the `VERBOSE` flag set,
         * or if this `CoverageReadConsistency` is used for benchmark.
         */
    if (durations != null) {
        if (statistics == null) {
            statistics = new ArrayList<>();
        }
        statistics.add(durations);
        final int totalCount = durations.count();
        out.println("Number of failures: " + failuresCount + " / " + totalCount + " (" + (failuresCount / (totalCount / 100f)) + "%)");
    }
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) PixelIterator(org.apache.sis.image.PixelIterator) Point(java.awt.Point) Statistics(org.apache.sis.math.Statistics) Point(java.awt.Point) GridDerivation(org.apache.sis.coverage.grid.GridDerivation) GridCoverage(org.apache.sis.coverage.grid.GridCoverage)

Example 9 with GridCoverage

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

the class CoverageQueryTest method verifyRead.

/**
 * Verifies that the read operation adds the expected margins.
 */
private void verifyRead(final GridCoverageResource subset, final int expansion) throws DataStoreException {
    /*
         * Test reading the whole image. The grid geometry of returned coverage
         * must be the same than the grid geometry of the GridCoverageResource,
         * which has been verified by the caller to contain the expansion.
         */
    assertEquals(subset.getGridGeometry(), subset.read(null).getGridGeometry());
    /*
         * Request for a smaller area and verify that the request has the expected size,
         * including expansion.
         */
    GridGeometry request = createSubGrid(-4);
    final GridCoverage coverage = subset.read(request);
    if (expansion != 0) {
        request = createSubGrid(-4 + expansion);
    }
    assertEquals(request, coverage.getGridGeometry());
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridCoverage(org.apache.sis.coverage.grid.GridCoverage)

Example 10 with GridCoverage

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

the class CoverageCanvas method cacheRenderingData.

/**
 * Invoked after a paint event for caching rendering data.
 * If the resampled image changed, all previously cached images are discarded.
 */
private void cacheRenderingData(final Worker worker) {
    if (TRACE && data.hasChanged(worker.data)) {
        trace("cacheRenderingData(…): new visual coverage:%n%s", worker.data);
    }
    data = worker.data;
    derivedImages.put(data.selectedDerivative, worker.recoloredImage);
    resampledImage = worker.resampledImage;
    if (TRACE) {
        trace("cacheRenderingData(…): objective bounds after rendering at %tT:%n%s", System.currentTimeMillis(), this);
    }
    /*
         * Notify the "Image properties" tab that the image changed. The `propertyExplorer` field is non-null
         * only if the "Properties" section in `CoverageControls` has been shown at least once.
         */
    if (propertyExplorer != null) {
        propertyExplorer.setImage(resampledImage, worker.getVisibleImageBounds());
        if (TRACE) {
            trace("cacheRenderingData(…): Update image property view with visible area %s.", propertyExplorer.getVisibleImageBounds(resampledImage));
        }
    }
    /*
         * Adjust the accuracy of coordinates shown in the status bar.
         * The number of fraction digits depend on the zoom factor.
         */
    if (statusBar != null) {
        final Object value = resampledImage.getProperty(PlanarImage.POSITIONAL_ACCURACY_KEY);
        Quantity<Length> accuracy = null;
        if (value instanceof Quantity<?>[]) {
            for (final Quantity<?> q : (Quantity<?>[]) value) {
                if (Units.isLinear(q.getUnit())) {
                    accuracy = q.asType(Length.class);
                    accuracy = GUIUtilities.shorter(accuracy, accuracy.getUnit().getConverterTo(Units.METRE).convert(accuracy.getValue().doubleValue()));
                    break;
                }
            }
        }
        statusBar.setLowestAccuracy(accuracy);
    }
    /*
         * If error(s) occurred during calls to `RenderedImage.getTile(tx, ty)`, reports those errors.
         * The `errorReport` field is reset to `null` in preparation for the next rendering operation.
         */
    final LogRecord report = errorReport;
    if (report != null) {
        errorReport = null;
        errorOccurred(report.getThrown());
    }
    /*
         * If the coverage changed, notify user. Note that a null coverage means "no change".
         */
    if (!isCoverageAdjusting) {
        final GridCoverage coverage = worker.coverage;
        if (coverage != null)
            try {
                isCoverageAdjusting = true;
                setCoverage(coverage);
            } finally {
                isCoverageAdjusting = false;
            }
    }
}
Also used : GridCoverage(org.apache.sis.coverage.grid.GridCoverage) Length(javax.measure.quantity.Length) LogRecord(java.util.logging.LogRecord)

Aggregations

GridCoverage (org.apache.sis.coverage.grid.GridCoverage)15 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)5 GridExtent (org.apache.sis.coverage.grid.GridExtent)3 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 Point (java.awt.Point)1 NumberFormat (java.text.NumberFormat)1 LogRecord (java.util.logging.LogRecord)1 Length (javax.measure.quantity.Length)1 GridDerivation (org.apache.sis.coverage.grid.GridDerivation)1 CoverageCanvas (org.apache.sis.gui.coverage.CoverageCanvas)1 PixelIterator (org.apache.sis.image.PixelIterator)1 Compression (org.apache.sis.internal.geotiff.Compression)1 TableAppender (org.apache.sis.io.TableAppender)1 Statistics (org.apache.sis.math.Statistics)1 DataStoreContentException (org.apache.sis.storage.DataStoreContentException)1 Test (org.junit.Test)1 Geometry (org.locationtech.jts.geom.Geometry)1 Point (org.locationtech.jts.geom.Point)1 FactoryException (org.opengis.util.FactoryException)1