use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testAggregateOnSeparateBands.
/**
* Test aggregating coverages with a single band to separate output bands.
*/
@Test
public void testAggregateOnSeparateBands() throws DataStoreException, TransformException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final SampleDimension sd = new SampleDimension.Builder().setName("data").build();
final List<SampleDimension> bands = Arrays.asList(sd);
/*
Coverage 1
+---+---+---+
|NaN| 2 | 3 |
+---+---+---+
Coverage 2
+---+---+---+
| 4 |NaN| 6 |
+---+---+---+
Coverage 3
+---+---+---+
| 7 | 8 |NaN|
+---+---+---+
*/
final GridGeometry grid1 = new GridGeometry(new GridExtent(3, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0, 0), crs);
final GridCoverage coverage1 = new BufferedGridCoverage(grid1, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverage coverage2 = new BufferedGridCoverage(grid1, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverage coverage3 = new BufferedGridCoverage(grid1, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverageResource resource1 = new InMemoryGridCoverageResource(coverage1);
final GridCoverageResource resource2 = new InMemoryGridCoverageResource(coverage2);
final GridCoverageResource resource3 = new InMemoryGridCoverageResource(coverage3);
final WritablePixelIterator write1 = WritablePixelIterator.create((WritableRenderedImage) coverage1.render(null));
final WritablePixelIterator write2 = WritablePixelIterator.create((WritableRenderedImage) coverage2.render(null));
final WritablePixelIterator write3 = WritablePixelIterator.create((WritableRenderedImage) coverage3.render(null));
write1.moveTo(0, 0);
write1.setPixel(new double[] { Double.NaN });
write1.moveTo(1, 0);
write1.setPixel(new double[] { 2.0 });
write1.moveTo(2, 0);
write1.setPixel(new double[] { 3.0 });
write2.moveTo(0, 0);
write2.setPixel(new double[] { 4.0 });
write2.moveTo(1, 0);
write2.setPixel(new double[] { Double.NaN });
write2.moveTo(2, 0);
write2.setPixel(new double[] { 6.0 });
write3.moveTo(0, 0);
write3.setPixel(new double[] { 7.0 });
write3.moveTo(1, 0);
write3.setPixel(new double[] { 8.0 });
write3.moveTo(2, 0);
write3.setPixel(new double[] { Double.NaN });
/*
We expect a final coverage with values on a single row
+-------+-------+-------+
|NaN:4:7|2:NaN:8|3:6:NaN|
+-------+-------+-------+
*/
final AggregatedCoverageResource.VirtualBand band0 = new AggregatedCoverageResource.VirtualBand();
band0.setSources(new AggregatedCoverageResource.Source(resource1, 0));
final AggregatedCoverageResource.VirtualBand band1 = new AggregatedCoverageResource.VirtualBand();
band1.setSources(new AggregatedCoverageResource.Source(resource2, 0));
final AggregatedCoverageResource.VirtualBand band2 = new AggregatedCoverageResource.VirtualBand();
band2.setSources(new AggregatedCoverageResource.Source(resource3, 0));
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource(Arrays.asList(band0, band1, band2), AggregatedCoverageResource.Mode.ORDER, crs);
aggregate.setInterpolation(Interpolation.NEAREST);
final GridGeometry gridGeometry = aggregate.getGridGeometry();
assertEquals(grid1, gridGeometry);
final GridCoverage coverage = aggregate.read(grid1);
final RenderedImage image = coverage.render(null);
final PixelIterator reader = PixelIterator.create(image);
reader.moveTo(0, 0);
assertEquals(Double.NaN, reader.getSampleDouble(0), 0.0);
assertEquals(4, reader.getSampleDouble(1), 0.0);
assertEquals(7, reader.getSampleDouble(2), 0.0);
reader.moveTo(1, 0);
assertEquals(2, reader.getSampleDouble(0), 0.0);
assertEquals(Double.NaN, reader.getSampleDouble(1), 0.0);
assertEquals(8, reader.getSampleDouble(2), 0.0);
reader.moveTo(2, 0);
assertEquals(3, reader.getSampleDouble(0), 0.0);
assertEquals(6, reader.getSampleDouble(1), 0.0);
assertEquals(Double.NaN, reader.getSampleDouble(2), 0.0);
}
use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testNaNAdded.
/**
* Test aggregation generates a NaN between coverages
*
* @throws DataStoreException
* @throws TransformException
*/
@Test
public void testNaNAdded() throws DataStoreException, TransformException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final SampleDimension sd = new SampleDimension.Builder().setName("data").build();
final List<SampleDimension> bands = Arrays.asList(sd);
/*
Coverage 1
+---+
| 1 |
+---+
Coverage 3
+---+
| 3 |
+---+
*/
final GridGeometry grid = new GridGeometry(new GridExtent(3, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0, 0), crs);
final GridGeometry grid1 = new GridGeometry(new GridExtent(1, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0, 0), crs);
final GridGeometry grid3 = new GridGeometry(new GridExtent(1, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 2, 0), crs);
final GridCoverage coverage1 = new BufferedGridCoverage(grid1, bands, DataBuffer.TYPE_INT);
final GridCoverage coverage3 = new BufferedGridCoverage(grid3, bands, DataBuffer.TYPE_INT);
final GridCoverageResource resource1 = new InMemoryGridCoverageResource(coverage1);
final GridCoverageResource resource3 = new InMemoryGridCoverageResource(coverage3);
final WritablePixelIterator write1 = WritablePixelIterator.create((WritableRenderedImage) coverage1.render(null));
final WritablePixelIterator write3 = WritablePixelIterator.create((WritableRenderedImage) coverage3.render(null));
write1.moveTo(0, 0);
write1.setSample(0, 1);
write3.moveTo(0, 0);
write3.setSample(0, 3);
/*
We expect a final coverage with values [1,NaN,3] on a single row
+---+---+---+
| 1 |NaN| 3 |
+---+---+---+
*/
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource();
aggregate.setInterpolation(Interpolation.NEAREST);
aggregate.setMode(AggregatedCoverageResource.Mode.ORDER);
aggregate.setDataType(DataBuffer.TYPE_DOUBLE);
aggregate.add(resource1);
aggregate.add(resource3);
final double[] resolution = aggregate.getGridGeometry().getResolution(true);
Assert.assertArrayEquals(new double[] { 1.0, 1.0 }, resolution, 0.0);
final GridCoverage coverage = aggregate.read(grid);
final RenderedImage image = coverage.render(null);
final PixelIterator reader = PixelIterator.create(image);
reader.moveTo(0, 0);
assertEquals(1, reader.getSample(0));
reader.moveTo(1, 0);
assertEquals(Double.NaN, reader.getSampleDouble(0), 0.0);
reader.moveTo(2, 0);
assertEquals(3, reader.getSample(0));
}
use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testDefinedNaNAdded.
/**
* Test aggregation generates a NaN between coverages.
* One coverage has a special no data value.
*
* @throws DataStoreException
* @throws TransformException
*/
@Test
public void testDefinedNaNAdded() throws DataStoreException, TransformException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final SampleDimension sd = new SampleDimension.Builder().setName("data").build();
final SampleDimension sd1 = new SampleDimension.Builder().setName("data").build();
final List<SampleDimension> bands1 = Arrays.asList(sd1);
final SampleDimension sd3 = new SampleDimension.Builder().setName("data").addQualitative(null, -1).addQuantitative("data", 0, 10, 1, 0, Units.UNITY).build();
final List<SampleDimension> bands3 = Arrays.asList(sd3);
/*
Coverage 1
+---+
| 1 |
+---+
Coverage 3
+---+---+
|-1 | 3 |
+---+---+
*/
final GridGeometry grid = new GridGeometry(new GridExtent(3, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0, 0), crs);
final GridGeometry grid1 = new GridGeometry(new GridExtent(1, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0, 0), crs);
final GridGeometry grid3 = new GridGeometry(new GridExtent(2, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 1, 0), crs);
final GridCoverage coverage1 = new BufferedGridCoverage(grid1, bands1, DataBuffer.TYPE_INT);
final GridCoverage coverage3 = new BufferedGridCoverage(grid3, bands3, DataBuffer.TYPE_INT);
final GridCoverageResource resource1 = new InMemoryGridCoverageResource(Names.createLocalName(null, null, "Coverage1"), coverage1);
final GridCoverageResource resource3 = new InMemoryGridCoverageResource(Names.createLocalName(null, null, "Coverage3"), coverage3);
final WritablePixelIterator write1 = WritablePixelIterator.create((WritableRenderedImage) coverage1.render(null));
final WritablePixelIterator write3 = WritablePixelIterator.create((WritableRenderedImage) coverage3.render(null));
write1.moveTo(0, 0);
write1.setSample(0, 1);
write3.moveTo(0, 0);
write3.setSample(0, -1);
write3.moveTo(1, 0);
write3.setSample(0, 3);
/*
We expect a final coverage with values [1,NaN,3] on a single row
+---+---+---+
| 1 |NaN| 3 |
+---+---+---+
*/
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource();
aggregate.setInterpolation(Interpolation.NEAREST);
aggregate.setMode(AggregatedCoverageResource.Mode.ORDER);
aggregate.add(resource1);
aggregate.add(resource3);
aggregate.setSampleDimensions(Collections.singletonList(sd));
final double[] resolution = aggregate.getGridGeometry().getResolution(true);
Assert.assertArrayEquals(new double[] { 1.0, 1.0 }, resolution, 0.0);
final GridGeometry gridGeometry = aggregate.getGridGeometry();
final GridCoverage coverage = aggregate.read(grid);
final RenderedImage image = coverage.render(null);
final PixelIterator reader = PixelIterator.create(image);
reader.moveTo(0, 0);
assertEquals(1, reader.getSample(0));
reader.moveTo(1, 0);
assertEquals(Double.NaN, reader.getSampleDouble(0), 0.0);
reader.moveTo(2, 0);
assertEquals(3, reader.getSample(0));
}
use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testSharedGridGeometry.
/**
* Test the grid geometry is preserved if all coverages have the same.
*/
@Test
public void testSharedGridGeometry() throws DataStoreException, TransformException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final SampleDimension sd = new SampleDimension.Builder().setName("data").build();
final List<SampleDimension> bands = Arrays.asList(sd);
/*
Coverage 1
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
Coverage 2
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
*/
final GridGeometry grid1 = new GridGeometry(new GridExtent(3, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0, 0), crs);
final GridCoverage coverage1 = new BufferedGridCoverage(grid1, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverage coverage2 = new BufferedGridCoverage(grid1, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverageResource resource1 = new InMemoryGridCoverageResource(coverage1);
final GridCoverageResource resource2 = new InMemoryGridCoverageResource(coverage2);
final WritablePixelIterator write1 = WritablePixelIterator.create((WritableRenderedImage) coverage1.render(null));
final WritablePixelIterator write2 = WritablePixelIterator.create((WritableRenderedImage) coverage2.render(null));
write1.moveTo(0, 0);
write1.setPixel(new double[] { 1 });
write1.moveTo(1, 0);
write1.setPixel(new double[] { 2 });
write1.moveTo(2, 0);
write1.setPixel(new double[] { 3 });
write2.moveTo(0, 0);
write2.setPixel(new double[] { 4 });
write2.moveTo(1, 0);
write2.setPixel(new double[] { 5 });
write2.moveTo(2, 0);
write2.setPixel(new double[] { 6 });
/*
We expect a final coverage with values [1:4,2:5,3:6] on a single row
+---+---+---+
|1:4|2:5|3:6|
+---+---+---+
*/
final AggregatedCoverageResource.VirtualBand band0 = new AggregatedCoverageResource.VirtualBand();
band0.setSources(new AggregatedCoverageResource.Source(resource1, 0));
final AggregatedCoverageResource.VirtualBand band1 = new AggregatedCoverageResource.VirtualBand();
band1.setSources(new AggregatedCoverageResource.Source(resource2, 0));
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource(Arrays.asList(band0, band1), AggregatedCoverageResource.Mode.ORDER, null);
aggregate.setInterpolation(Interpolation.NEAREST);
final GridGeometry gridGeometry = aggregate.getGridGeometry();
assertEquals(grid1, gridGeometry);
final GridCoverage coverage = aggregate.read(grid1);
final RenderedImage image = coverage.render(null);
final PixelIterator reader = PixelIterator.create(image);
reader.moveTo(0, 0);
assertEquals(1, reader.getSample(0));
assertEquals(4, reader.getSample(1));
reader.moveTo(1, 0);
assertEquals(2, reader.getSample(0));
assertEquals(5, reader.getSample(1));
reader.moveTo(2, 0);
assertEquals(3, reader.getSample(0));
assertEquals(6, reader.getSample(1));
}
use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method typeInference.
/**
* Ensure that aggregation properly detect source data types and re-use them for output production.
*/
@Test
public void typeInference() throws Exception {
final CoordinateReferenceSystem crs = CommonCRS.defaultGeographic();
final AffineTransform2D gridToCRS = new AffineTransform2D(1, 0, 0, 1, 0, 0);
final IntFunction<GridGeometry> translateGrid = i -> new GridGeometry(new GridExtent(1, 1).translate(i, 0), CELL_CENTER, gridToCRS, crs);
final GridGeometry gridLeft = translateGrid.apply(0);
final GridGeometry gridCenterLeft = translateGrid.apply(1);
final GridGeometry gridCenterRight = translateGrid.apply(2);
final GridGeometry gridRight = translateGrid.apply(3);
final GridGeometry overallGrid = new GridGeometry(new GridExtent(4, 1), CELL_CENTER, gridToCRS, crs);
final SampleDimension sd = new SampleDimension.Builder().setName("data").build();
final GridCoverage bandShort = new BufferedGridCoverage(gridLeft, Collections.singletonList(sd), DataBuffer.TYPE_SHORT);
final GridCoverage bandInt = new BufferedGridCoverage(gridCenterLeft, Collections.singletonList(sd), DataBuffer.TYPE_INT);
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource();
aggregate.setInterpolation(Interpolation.NEAREST);
aggregate.add(new InMemoryGridCoverageResource(bandInt));
aggregate.add(new InMemoryGridCoverageResource(bandShort));
RenderedImage rendering = aggregate.read(overallGrid).render(null);
assertEquals("Infered sample type is wrong", DataBuffer.TYPE_INT, rendering.getSampleModel().getDataType());
final GridCoverage bandFloat = new BufferedGridCoverage(gridCenterRight, Collections.singletonList(sd), DataBuffer.TYPE_FLOAT);
aggregate.add(new InMemoryGridCoverageResource(bandFloat));
rendering = aggregate.read(overallGrid).render(null);
assertEquals("Infered sample type is wrong", DataBuffer.TYPE_FLOAT, rendering.getSampleModel().getDataType());
// Test non-homogeneous aggregation
final SampleDimension userDefinedSd = new SampleDimension.Builder().setName("data").setBackground("NaN", Double.NaN).addQuantitative("percentage", 0, 1, 100d, 0d, Units.PERCENT).build();
aggregate.add(new InMemoryGridCoverageResource(new BufferedGridCoverage(gridRight, Collections.singletonList(userDefinedSd), DataBuffer.TYPE_DOUBLE)));
rendering = aggregate.read(overallGrid).render(null);
assertEquals("Infered sample type is wrong", DataBuffer.TYPE_DOUBLE, rendering.getSampleModel().getDataType());
}
Aggregations