Search in sources :

Example 21 with GridExtent

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

the class AbstractGridResourceTest method testRangeArgumentForInterleavedModel.

/**
 * Tests {@link AbstractGridResource.RangeArgument} for data organized in an interleaved sample model.
 * This is the state when the {@code insert} methods are invoked.
 */
@Test
public void testRangeArgumentForInterleavedModel() {
    final AbstractGridResource.RangeArgument r = resource.validateRangeArgument(7, new int[] { 4, 6, 2 });
    assertEquals(3, r.insertBandDimension(new GridExtent(360, 180), 2).getDimension());
    assertArrayEquals(new int[] { 3, 1, 2 }, r.insertSubsampling(new int[] { 3, 1 }, 2));
    assertEquals("numBands", 3, r.getNumBands());
    assertEquals("first", 4, r.getFirstSpecified());
    // Expect sorted source indices: {2, 4, 6}.
    assertEquals("source", 2, r.getSourceIndex(0));
    assertEquals("source", 4, r.getSourceIndex(1));
    assertEquals("source", 6, r.getSourceIndex(2));
    // Expect original positions of sorted indices: {2, 0, 1}.
    assertEquals("target", 2, r.getTargetIndex(0));
    assertEquals("target", 0, r.getTargetIndex(1));
    assertEquals("target", 1, r.getTargetIndex(2));
    // Expect source indices divided by 2 minus 1.
    assertEquals("subsampled", 0, r.getSubsampledIndex(0));
    assertEquals("subsampled", 1, r.getSubsampledIndex(1));
    assertEquals("subsampled", 2, r.getSubsampledIndex(2));
    assertEquals("pixelStride", 3, r.getPixelStride());
}
Also used : GridExtent(org.apache.sis.coverage.grid.GridExtent) Test(org.junit.Test)

Example 22 with GridExtent

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

the class CoverageReadConsistency method iterator.

/**
 * Creates a pixel iterator for a sub-region in a slice of the specified coverage.
 * All coordinates given to this method are in the coordinate space of subsampled coverage subset.
 * This method returns {@code null} if the arguments are valid but the image can not be created
 * because of a restriction in {@code PixelInterleavedSampleModel} constructor.
 *
 * @param  coverage     the coverage from which to get the iterator, or {@code null} if unavailable.
 * @param  sliceMin     lower bounds of the <var>n</var>-dimensional region of the coverage for which to get an iterator.
 * @param  sliceMax     upper bounds of the <var>n</var>-dimensional region of the coverage for which to get an iterator.
 * @param  subsampling  subsampling factors to apply on the image.
 * @param  subOffsets   offsets to add after multiplication by subsampling factors.
 * @return pixel iterator over requested area, or {@code null} if unavailable.
 */
private static PixelIterator iterator(final GridCoverage coverage, long[] sliceMin, long[] sliceMax, final int[] subsampling, final int[] subOffsets, final boolean allowSubsampling) {
    if (coverage == null) {
        return null;
    }
    /*
         * Same extent than `areaOfInterest` but in two dimensions and with (0,0) origin.
         * We use that for clipping iteration to the area that we requested even if the
         * coverage gave us a larger area.
         */
    final Rectangle sliceAOI = new Rectangle(StrictMath.toIntExact(sliceMax[0] - sliceMin[0] + 1), StrictMath.toIntExact(sliceMax[1] - sliceMin[1] + 1));
    /*
         * If the given coordinates were in a subsampled space while the coverage is at full resolution,
         * convert the coordinates to full resolution.
         */
    if (allowSubsampling) {
        sliceMin = sliceMin.clone();
        sliceMax = sliceMax.clone();
        for (int i = 0; i < sliceMin.length; i++) {
            sliceMin[i] = sliceMin[i] * subsampling[i] + subOffsets[i];
            sliceMax[i] = sliceMax[i] * subsampling[i] + subOffsets[i];
        }
    }
    RenderedImage image = coverage.render(new GridExtent(null, sliceMin, sliceMax, true));
    /*
         * The subsampling offsets were included in the extent given to above `render` method call, so in principle
         * they should not be given again to `SubsampledImage` constructor.  However the `render` method is free to
         * return an image with a larger extent, which may result in different offsets. The result can be "too much"
         * offset. We want to compensate by subtracting the surplus. But because we can not have negative offsets,
         * we shift the whole `sliceAOI` (which is equivalent to subtracting `subX|Y` in full resolution coordinates)
         * and set the offset to the complement.
         */
    if (allowSubsampling) {
        final int subX = subsampling[0];
        final int subY = subsampling[1];
        if (subX > image.getTileWidth() || subY > image.getTileHeight()) {
            // `SubsampledImage` does not support this case.
            return null;
        }
        int offX = StrictMath.floorMod(image.getMinX(), subX);
        int offY = StrictMath.floorMod(image.getMinY(), subY);
        if (offX != 0) {
            sliceAOI.x--;
            offX = subX - offX;
        }
        if (offY != 0) {
            sliceAOI.y--;
            offY = subY - offY;
        }
        image = SubsampledImage.create(image, subX, subY, offX, offY);
        if (image == null) {
            return null;
        }
    }
    return new PixelIterator.Builder().setRegionOfInterest(sliceAOI).create(image);
}
Also used : GridExtent(org.apache.sis.coverage.grid.GridExtent) Rectangle(java.awt.Rectangle) RenderedImage(java.awt.image.RenderedImage) Point(java.awt.Point)

Aggregations

GridExtent (org.apache.sis.coverage.grid.GridExtent)22 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)10 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)6 Point (java.awt.Point)4 RenderedImage (java.awt.image.RenderedImage)3 IOException (java.io.IOException)3 SampleDimension (org.apache.sis.coverage.SampleDimension)3 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)3 TransformException (org.opengis.referencing.operation.TransformException)3 Rectangle (java.awt.Rectangle)2 BufferedImage (java.awt.image.BufferedImage)2 GridCoverage2D (org.apache.sis.coverage.grid.GridCoverage2D)2 GridDerivation (org.apache.sis.coverage.grid.GridDerivation)2 RenderException (org.apache.sis.portrayal.RenderException)2 DimensionNameType (org.opengis.metadata.spatial.DimensionNameType)2 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 AffineTransform (java.awt.geom.AffineTransform)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 Rectangle2D (java.awt.geom.Rectangle2D)1