Search in sources :

Example 6 with GridCoverageStack

use of org.geotoolkit.coverage.grid.GridCoverageStack in project geotoolkit by Geomatys.

the class TileMatrixSetCoverageReader method rebuildCoverage.

/**
 * @param groups
 * @param wantedEnv
 * @param deferred
 * @param axisIndex
 * @return GridCoverage
 * @throws CoverageStoreException
 */
private GridCoverage rebuildCoverage(TreeMap<Double, Object> groups, Envelope wantedEnv, int axisIndex, int... range) throws DataStoreException {
    final CoordinateReferenceSystem crs = wantedEnv.getCoordinateReferenceSystem();
    final CoordinateSystem cs = crs.getCoordinateSystem();
    int nbDim = cs.getDimension();
    final List<GridCoverageStack.Element> elements = new ArrayList<>();
    final List<Entry<Double, Object>> entries = new ArrayList<>(groups.entrySet());
    for (int k = 0, kn = entries.size(); k < kn; k++) {
        final Entry<Double, Object> entry = entries.get(k);
        final Double z = entry.getKey();
        final Object obj = entry.getValue();
        final GeneralEnvelope sliceEnvelop = new GeneralEnvelope(crs);
        for (int i = 0; i < nbDim; i++) {
            if (i == axisIndex) {
                sliceEnvelop.setRange(i, z, z);
            } else {
                sliceEnvelop.setRange(i, wantedEnv.getMinimum(i), wantedEnv.getMaximum(i));
            }
        }
        final GridCoverage subCoverage;
        if (obj instanceof TileMatrix) {
            subCoverage = readSlice((TileMatrix) obj, sliceEnvelop, range);
        } else if (obj instanceof TreeMap) {
            subCoverage = rebuildCoverage((TreeMap) obj, sliceEnvelop, axisIndex - 1, range);
        } else {
            throw new DataStoreException("Found an object which is not a Coverage or a Map group, should not happen : " + obj);
        }
        // calculate the range
        double min;
        double max;
        if (k == 0) {
            if (kn == 1) {
                // a single element, use a range of 1
                min = z - 0.5;
                max = z + 0.5;
            } else {
                final double nextD = entries.get(k + 1).getKey();
                final double diff = (nextD - z) / 2.0;
                min = z - diff;
                max = z + diff;
            }
        } else if (k == kn - 1) {
            final double previousD = entries.get(k - 1).getKey();
            final double diff = (z - previousD) / 2.0;
            min = z - diff;
            max = z + diff;
        } else {
            final double prevD = entries.get(k - 1).getKey();
            final double nextD = entries.get(k + 1).getKey();
            min = z - (z - prevD) / 2.0;
            max = z + (nextD - z) / 2.0;
        }
        elements.add(new GridCoverageStack.Adapter(subCoverage, NumberRange.create(min, true, max, false), z));
    }
    try {
        return new GridCoverageStack("HyperCube" + nbDim + "D", crs, elements, axisIndex);
    } catch (IOException | TransformException | FactoryException ex) {
        throw new DataStoreException(ex);
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) FactoryException(org.opengis.util.FactoryException) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) ArrayList(java.util.ArrayList) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) GridCoverageStack(org.geotoolkit.coverage.grid.GridCoverageStack) Point(java.awt.Point) Entry(java.util.Map.Entry) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) TileMatrix(org.geotoolkit.storage.multires.TileMatrix)

Example 7 with GridCoverageStack

use of org.geotoolkit.coverage.grid.GridCoverageStack in project geotoolkit by Geomatys.

the class PyramidReaderTest method coverage4DTest.

/**
 * 4D calc test
 * @throws Exception
 */
@Test
public void coverage4DTest() throws Exception {
    // create a small pyramid
    final InMemoryStore store = new InMemoryStore();
    final CoordinateReferenceSystem horizontal = CommonCRS.WGS84.normalizedGeographic();
    final CoordinateReferenceSystem vertical = CommonCRS.Vertical.ELLIPSOIDAL.crs();
    final CoordinateReferenceSystem temporal = CommonCRS.Temporal.JAVA.crs();
    final CoordinateReferenceSystem crs = new GeodeticObjectBuilder().addName("4dcrs").createCompoundCRS(horizontal, vertical, temporal);
    final int width = 28;
    final int height = 13;
    final InMemoryTiledGridCoverageResource ref1 = (InMemoryTiledGridCoverageResource) store.add(new DefiningTiledGridCoverageResource(NamesExt.create("test1")));
    ref1.setSampleDimensions(Collections.singletonList(new SampleDimension.Builder().setName(0).build()));
    create4DPyramid(ref1, crs, width, height, new double[][] { { -5, -9, 12 }, { -5, 0, -7 }, { -5, 21, 51 }, { 62, -9, -3 }, { 62, 0, 5 }, { 62, 21, 17 } });
    final GridGeometry gridGeomRef = ref1.getGridGeometry();
    final GridExtent gridEnvRef = gridGeomRef.getExtent();
    final MathTransform gridToCrsRef = gridGeomRef.getGridToCRS(PixelInCell.CELL_CENTER);
    final GridCoverage result = ref1.read(null);
    Assert.assertEquals(crs, result.getCoordinateReferenceSystem());
    final GridGeometry gridGeom = result.getGridGeometry();
    final GridExtent gridEnv = gridGeom.getExtent();
    final MathTransform gridToCrs = gridGeom.getGridToCRS(PixelInCell.CELL_CENTER);
    // -- we must have the same grid grometry definition between the reader and the coverage
    Assert.assertEquals(gridEnvRef, gridEnv);
    final long[] lowerCorner = GridGeometryIterator.getLow(gridEnv);
    final long[] highCorner = GridGeometryIterator.getHigh(gridEnv);
    // check grid envelope
    Assert.assertEquals(0, lowerCorner[0]);
    Assert.assertEquals(0, lowerCorner[1]);
    Assert.assertEquals(0, lowerCorner[2]);
    Assert.assertEquals(0, lowerCorner[3]);
    // 28 * 4 -1
    Assert.assertEquals(111, highCorner[0]);
    // 13 * 3 -1
    Assert.assertEquals(38, highCorner[1]);
    // 2 slices
    Assert.assertEquals(1, highCorner[2]);
    // 3 slices
    Assert.assertEquals(2, highCorner[3]);
    // check transform
    Assert.assertEquals(gridToCrsRef, gridToCrs);
    final double[] buffer = new double[4];
    gridToCrs.transform(new double[] { 0, 0, 0, 0 }, 0, buffer, 0, 1);
    Assert.assertArrayEquals(new double[] { -49.5, 59.5, 28.0, -5 }, buffer, DELTA);
    gridToCrs.transform(new double[] { 0, 0, 0, 1 }, 0, buffer, 0, 1);
    Assert.assertArrayEquals(new double[] { -49.5, 59.5, 28.0, 10 }, buffer, DELTA);
    gridToCrs.transform(new double[] { 0, 0, 0, 2 }, 0, buffer, 0, 1);
    Assert.assertArrayEquals(new double[] { -49.5, 59.5, 28.0, 20.5 }, buffer, DELTA);
    gridToCrs.transform(new double[] { 0, 0, 1, 0 }, 0, buffer, 0, 1);
    Assert.assertArrayEquals(new double[] { -49.5, 59.5, 61.5, -5 }, buffer, DELTA);
    gridToCrs.transform(new double[] { 0, 0, 1, 1 }, 0, buffer, 0, 1);
    Assert.assertArrayEquals(new double[] { -49.5, 59.5, 61.5, 10 }, buffer, DELTA);
    gridToCrs.transform(new double[] { 0, 0, 1, 2 }, 0, buffer, 0, 1);
    Assert.assertArrayEquals(new double[] { -49.5, 59.5, 61.5, 20.5 }, buffer, DELTA);
    // check each block range
    Assert.assertTrue(result instanceof GridCoverageStack);
    final GridCoverageStack stackT = (GridCoverageStack) result;
    final GridCoverageStack.Element[] elementsT = stackT.getElements();
    Assert.assertEquals(-9.0, elementsT[0].getZCenter());
    Assert.assertEquals(0.0, elementsT[1].getZCenter());
    Assert.assertEquals(21.0, elementsT[2].getZCenter());
    // [ (-9-((0+9)/2) ... (-9+((0+ 9)/2) [
    Assert.assertEquals(NumberRange.create(-13.5, true, -4.5, false), elementsT[0].getZRange());
    // [ ( 0-((0+9)/2) ... ( 0+((0+21)/2) [
    Assert.assertEquals(NumberRange.create(-4.5, true, 10.5, false), elementsT[1].getZRange());
    // [ (21-((0+9)/2) ... (21+((0+21)/2) [
    Assert.assertEquals(NumberRange.create(10.5, true, 31.5, false), elementsT[2].getZRange());
    final GridCoverageStack stackT0 = (GridCoverageStack) stackT.coveragesAt(-9).get(0);
    final GridCoverageStack.Element[] elementsT0 = stackT0.getElements();
    Assert.assertEquals(-5.0, elementsT0[0].getZCenter());
    Assert.assertEquals(62.0, elementsT0[1].getZCenter());
    // [ -5-((-5+62)/2) ... -5-((-5+62)/2) [
    Assert.assertEquals(NumberRange.create(-38.5, true, 28.5, false), elementsT0[0].getZRange());
    // [ 62-((-5+62)/2) ... 62-((-5+62)/2) [
    Assert.assertEquals(NumberRange.create(28.5, true, 95.5, false), elementsT0[1].getZRange());
    final GridCoverageStack stackT1 = (GridCoverageStack) stackT.coveragesAt(0).get(0);
    final GridCoverageStack.Element[] elementsT1 = stackT0.getElements();
    Assert.assertEquals(-5.0, elementsT1[0].getZCenter());
    Assert.assertEquals(62.0, elementsT1[1].getZCenter());
    // [ -5-((-5+62)/2) ... -5-((-5+62)/2) [
    Assert.assertEquals(NumberRange.create(-38.5, true, 28.5, false), elementsT1[0].getZRange());
    // [ 62-((-5+62)/2) ... 62-((-5+62)/2) [
    Assert.assertEquals(NumberRange.create(28.5, true, 95.5, false), elementsT1[1].getZRange());
    final GridCoverageStack stackT2 = (GridCoverageStack) stackT.coveragesAt(21).get(0);
    final GridCoverageStack.Element[] elementsT2 = stackT0.getElements();
    Assert.assertEquals(-5.0, elementsT2[0].getZCenter());
    Assert.assertEquals(62.0, elementsT2[1].getZCenter());
    // [ -5-((-5+62)/2) ... -5-((-5+62)/2) [
    Assert.assertEquals(NumberRange.create(-38.5, true, 28.5, false), elementsT2[0].getZRange());
    // [ 62-((-5+62)/2) ... 62-((-5+62)/2) [
    Assert.assertEquals(NumberRange.create(28.5, true, 95.5, false), elementsT2[1].getZRange());
}
Also used : InMemoryTiledGridCoverageResource(org.geotoolkit.storage.memory.InMemoryTiledGridCoverageResource) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) MathTransform(org.opengis.referencing.operation.MathTransform) GeodeticObjectBuilder(org.apache.sis.internal.referencing.GeodeticObjectBuilder) GridCoverageStack(org.geotoolkit.coverage.grid.GridCoverageStack) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) InMemoryStore(org.geotoolkit.storage.memory.InMemoryStore) GeodeticObjectBuilder(org.apache.sis.internal.referencing.GeodeticObjectBuilder) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Test(org.junit.Test)

Example 8 with GridCoverageStack

use of org.geotoolkit.coverage.grid.GridCoverageStack in project geotoolkit by Geomatys.

the class GridCoverageStackTest method createCube3D.

private static GridCoverageStack createCube3D(int width, int height, CoordinateReferenceSystem crs) throws IOException, TransformException, FactoryException {
    final GridCoverage slice0 = createSlice3D(width, height, 10, crs);
    final GridCoverage slice1 = createSlice3D(width, height, 20, crs);
    final GridCoverage slice2 = createSlice3D(width, height, 50, crs);
    return new GridCoverageStack(null, Arrays.asList(slice0, slice1, slice2), 2);
}
Also used : GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageStack(org.geotoolkit.coverage.grid.GridCoverageStack)

Example 9 with GridCoverageStack

use of org.geotoolkit.coverage.grid.GridCoverageStack in project geotoolkit by Geomatys.

the class GridCoverageStackTest method createSubStack3D.

private static GridCoverageStack createSubStack3D(int width, int height, double t, CoordinateReferenceSystem crs) throws IOException, TransformException, FactoryException {
    final GridCoverage slice0 = createSlice4D(width, height, 10, t, crs);
    final GridCoverage slice1 = createSlice4D(width, height, 20, t, crs);
    final GridCoverage slice2 = createSlice4D(width, height, 50, t, crs);
    return new GridCoverageStack(null, Arrays.asList(slice0, slice1, slice2), 2);
}
Also used : GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageStack(org.geotoolkit.coverage.grid.GridCoverageStack)

Example 10 with GridCoverageStack

use of org.geotoolkit.coverage.grid.GridCoverageStack in project geotoolkit by Geomatys.

the class GridCoverageStackTest method test4D.

/**
 * Verify 4D grid coverage stack creation and correct grid geometry.
 */
@Test
public void test4D() throws FactoryException, IOException, TransformException {
    final CoordinateReferenceSystem horizontal = CommonCRS.WGS84.normalizedGeographic();
    final CoordinateReferenceSystem vertical = CommonCRS.Vertical.ELLIPSOIDAL.crs();
    final CoordinateReferenceSystem temporal = CommonCRS.Temporal.JAVA.crs();
    final CoordinateReferenceSystem crs3d = new GeodeticObjectBuilder().addName("wgs84+ele").createCompoundCRS(horizontal, vertical);
    final CoordinateReferenceSystem crs4d = new GeodeticObjectBuilder().addName("wgs84+ele+time").createCompoundCRS(crs3d, temporal);
    final GridCoverageStack stack = createCube4D(100, 100, crs4d);
    assertTrue(Utilities.equalsIgnoreMetadata(crs4d, stack.getCoordinateReferenceSystem()));
    final GridGeometry gridGeom = stack.getGridGeometry();
    assertNotNull(gridGeom);
    // check grid envelope
    final GridExtent gridEnv = gridGeom.getExtent();
    assertNotNull(gridEnv);
    assertEquals(4, gridEnv.getDimension());
    assertEquals(0, gridEnv.getLow(0));
    assertEquals(0, gridEnv.getLow(1));
    assertEquals(0, gridEnv.getLow(2));
    assertEquals(0, gridEnv.getLow(3));
    assertEquals(99, gridEnv.getHigh(0));
    assertEquals(99, gridEnv.getHigh(1));
    assertEquals(2, gridEnv.getHigh(2));
    assertEquals(3, gridEnv.getHigh(3));
    // check grid to crs
    // -- in convention gridToCrs in PixelInCell.Center
    final MathTransform gridToCRS = PixelTranslation.translate(gridGeom.getGridToCRS(PixelInCell.CELL_CENTER), PixelInCell.CELL_CENTER, PixelInCell.CELL_CORNER);
    assertEquals(4, gridToCRS.getSourceDimensions());
    assertEquals(4, gridToCRS.getTargetDimensions());
    final double[] lower = new double[] { 0, 0, 0, 0 };
    final double[] upper = new double[] { 99, 99, 2, 3 };
    gridToCRS.transform(lower, 0, lower, 0, 1);
    gridToCRS.transform(upper, 0, upper, 0, 1);
    assertEquals(0.0, lower[0], DELTA);
    assertEquals(0.0, lower[1], DELTA);
    assertEquals(10, lower[2], DELTA);
    assertEquals(3, lower[3], DELTA);
    assertEquals(99, upper[0], DELTA);
    assertEquals(99, upper[1], DELTA);
    assertEquals(50, upper[2], DELTA);
    assertEquals(12, upper[3], DELTA);
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) MathTransform(org.opengis.referencing.operation.MathTransform) GeodeticObjectBuilder(org.apache.sis.internal.referencing.GeodeticObjectBuilder) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GridCoverageStack(org.geotoolkit.coverage.grid.GridCoverageStack) Test(org.junit.Test)

Aggregations

GridCoverageStack (org.geotoolkit.coverage.grid.GridCoverageStack)10 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)7 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)5 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)5 MathTransform (org.opengis.referencing.operation.MathTransform)5 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)4 GridExtent (org.apache.sis.coverage.grid.GridExtent)3 GeodeticObjectBuilder (org.apache.sis.internal.referencing.GeodeticObjectBuilder)3 Test (org.junit.Test)3 DataStoreException (org.apache.sis.storage.DataStoreException)2 Envelope (org.opengis.geometry.Envelope)2 TransformException (org.opengis.referencing.operation.TransformException)2 FactoryException (org.opengis.util.FactoryException)2 Point (java.awt.Point)1 RenderedImage (java.awt.image.RenderedImage)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 CancellationException (java.util.concurrent.CancellationException)1