use of org.apache.sis.coverage.grid.BufferedGridCoverage in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testResourceMetaInError.
/**
* Test fail safe when a resource fails on metadatas and read operation.
*
* @throws DataStoreException
* @throws TransformException
*/
@Test
public void testResourceMetaInError() 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 |NaN|NaN|
+---+---+---+
Coverage 2
+---+---+---+
| 2 | 2 |NaN| <-- will fail at metadata and reading time
+---+---+---+
Coverage 3
+---+---+---+
| 3 | 3 | 3 |
+---+---+---+
*/
final GridGeometry grid1 = new GridGeometry(new GridExtent(3, 1), PixelInCell.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) {
@Override
public GridGeometry getGridGeometry() throws DataStoreException {
throw new RuntimeException("Metadata failing");
}
@Override
public GridCoverage read(GridGeometry domain, int... range) throws DataStoreException {
throw new RuntimeException("Read failing");
}
};
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.setSample(0, 1);
write1.moveTo(1, 0);
write1.setSample(0, Double.NaN);
write1.moveTo(2, 0);
write1.setSample(0, Double.NaN);
write2.moveTo(0, 0);
write2.setSample(0, 2);
write2.moveTo(1, 0);
write2.setSample(0, 2);
write2.moveTo(2, 0);
write2.setSample(0, Double.NaN);
write3.moveTo(0, 0);
write3.setSample(0, 3);
write3.moveTo(1, 0);
write3.setSample(0, 3);
write3.moveTo(2, 0);
write3.setSample(0, 3);
/*
We expect a final coverage with values [1,3,3] on a single row
since 2 has failed reading
+---+---+---+
| 1 | 3 | 3 |
+---+---+---+
*/
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource();
aggregate.setInterpolation(Interpolation.NEAREST);
aggregate.setMode(AggregatedCoverageResource.Mode.ORDER);
aggregate.add(resource1);
aggregate.add(resource2);
aggregate.add(resource3);
final double[] resolution = aggregate.getGridGeometry().getResolution(true);
Assert.assertArrayEquals(new double[] { 1.0, 1.0 }, resolution, 0.0);
final GridGeometry gridGeometry = aggregate.getGridGeometry();
Assert.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);
Assert.assertEquals(1, reader.getSample(0));
reader.moveTo(1, 0);
Assert.assertEquals(3, reader.getSample(0));
reader.moveTo(2, 0);
Assert.assertEquals(3, reader.getSample(0));
}
use of org.apache.sis.coverage.grid.BufferedGridCoverage in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testSampleTransform.
/**
* Test sample transform is applied and NaN value is evaluated after transform.
*/
@Test
public void testSampleTransform() 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 |NaN|NaN|
+---+---+---+
Coverage 2
+---+---+---+
| 2 | -5 |NaN|
+---+---+---+
Coverage 3
+---+---+---+
| 3 | 3 | 0 |
+---+---+---+
*/
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.setSample(0, 1);
write1.moveTo(1, 0);
write1.setSample(0, Double.NaN);
write1.moveTo(2, 0);
write1.setSample(0, Double.NaN);
write2.moveTo(0, 0);
write2.setSample(0, 2);
write2.moveTo(1, 0);
write2.setSample(0, -5);
write2.moveTo(2, 0);
write2.setSample(0, Double.NaN);
write3.moveTo(0, 0);
write3.setSample(0, 3);
write3.moveTo(1, 0);
write3.setSample(0, 3);
write3.moveTo(2, 0);
write3.setSample(0, 0);
/*
We expect a final coverage with values [1,2,3] on a single row
+---+---+---+
| 12 |-3|NaN|
+---+---+---+
*/
final MathTransform1D trs1 = new AbstractMathTransform1D() {
@Override
public double transform(double value) throws TransformException {
return value * 12.0;
}
@Override
public double derivative(double value) throws TransformException {
throw new TransformException("Not supported.");
}
};
final MathTransform1D trs2 = new AbstractMathTransform1D() {
@Override
public double transform(double value) throws TransformException {
return value < 0 ? Double.NaN : value;
}
@Override
public double derivative(double value) throws TransformException {
throw new TransformException("Not supported.");
}
};
final MathTransform1D trs3 = new AbstractMathTransform1D() {
@Override
public double transform(double value) throws TransformException {
return value > 0 ? -value : Double.NaN;
}
@Override
public double derivative(double value) throws TransformException {
throw new TransformException("Not supported.");
}
};
final AggregatedCoverageResource.VirtualBand band = new AggregatedCoverageResource.VirtualBand();
final AggregatedCoverageResource.Source source1 = new AggregatedCoverageResource.Source(resource1, 0, trs1);
final AggregatedCoverageResource.Source source2 = new AggregatedCoverageResource.Source(resource2, 0, trs2);
final AggregatedCoverageResource.Source source3 = new AggregatedCoverageResource.Source(resource3, 0, trs3);
band.setSources(source1, source2, source3);
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource(Collections.singletonList(band), AggregatedCoverageResource.Mode.ORDER, null);
aggregate.setInterpolation(Interpolation.NEAREST);
final double[] resolution = aggregate.getGridGeometry().getResolution(true);
Assert.assertArrayEquals(new double[] { 1.0, 1.0 }, resolution, 0.0);
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(12.0, reader.getSampleDouble(0), 0.0);
reader.moveTo(1, 0);
assertEquals(-3.0, reader.getSampleDouble(0), 0.0);
reader.moveTo(2, 0);
assertEquals(Double.NaN, reader.getSampleDouble(0), 0.0);
}
use of org.apache.sis.coverage.grid.BufferedGridCoverage in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testResourceReadInError.
/**
* Test fail safe when a resource fails a read operation.
*
* @throws DataStoreException
* @throws TransformException
*/
@Test
public void testResourceReadInError() 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 |NaN|NaN|
+---+---+---+
Coverage 2
+---+---+---+
| 2 | 2 |NaN| <-- will fail at reading time
+---+---+---+
Coverage 3
+---+---+---+
| 3 | 3 | 3 |
+---+---+---+
*/
final GridGeometry grid1 = new GridGeometry(new GridExtent(3, 1), PixelInCell.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) {
@Override
public GridCoverage read(GridGeometry domain, int... range) throws DataStoreException {
throw new RuntimeException("Read failing");
}
};
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.setSample(0, 1);
write1.moveTo(1, 0);
write1.setSample(0, Double.NaN);
write1.moveTo(2, 0);
write1.setSample(0, Double.NaN);
write2.moveTo(0, 0);
write2.setSample(0, 2);
write2.moveTo(1, 0);
write2.setSample(0, 2);
write2.moveTo(2, 0);
write2.setSample(0, Double.NaN);
write3.moveTo(0, 0);
write3.setSample(0, 3);
write3.moveTo(1, 0);
write3.setSample(0, 3);
write3.moveTo(2, 0);
write3.setSample(0, 3);
/*
We expect a final coverage with values [1,3,3] on a single row
since 2 has failed reading
+---+---+---+
| 1 | 3 | 3 |
+---+---+---+
*/
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource();
aggregate.setInterpolation(Interpolation.NEAREST);
aggregate.setMode(AggregatedCoverageResource.Mode.ORDER);
aggregate.add(resource1);
aggregate.add(resource2);
aggregate.add(resource3);
final double[] resolution = aggregate.getGridGeometry().getResolution(true);
Assert.assertArrayEquals(new double[] { 1.0, 1.0 }, resolution, 0.0);
final GridGeometry gridGeometry = aggregate.getGridGeometry();
Assert.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);
Assert.assertEquals(1, reader.getSample(0));
reader.moveTo(1, 0);
Assert.assertEquals(3, reader.getSample(0));
reader.moveTo(2, 0);
Assert.assertEquals(3, reader.getSample(0));
}
use of org.apache.sis.coverage.grid.BufferedGridCoverage in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testModeScale.
/**
* Test aggregation user order is preserved.
*
* @throws DataStoreException
* @throws TransformException
*/
@Test
public void testModeScale() throws DataStoreException, TransformException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final SampleDimension sd = new SampleDimension.Builder().setName("data").setBackground(null, Double.NaN).build();
final List<SampleDimension> bands = Arrays.asList(sd);
/*
Coverage 1
+---+---+---+
|NaN| 1 | 1 |
+---+---+---+
Coverage 2
+---+---+---+---+---+---+
|NaN|NaN| 2 | 2 |NaN|NaN|
+---+---+---+---+---+---+
|NaN|NaN| 2 | 2 |NaN|NaN|
+---+---+---+---+---+---+
Coverage 3
+---+---+---+---+---+---+---+---+---+---+---+---+
|NaN|NaN|NaN|NaN|NaN|NaN| 3 | 3 | 3 | 3 |NaN|NaN|
+---+---+---+---+---+---+---+---+---+---+---+---+
|NaN|NaN|NaN|NaN|NaN|NaN| 3 | 3 | 3 | 3 |NaN|NaN|
+---+---+---+---+---+---+---+---+---+---+---+---+
|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|
+---+---+---+---+---+---+---+---+---+---+---+---+
|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|
+---+---+---+---+---+---+---+---+---+---+---+---+
*/
final GridGeometry grid1 = new GridGeometry(new GridExtent(3, 1), CELL_CENTER, new AffineTransform2D(2.0, 0, 0, 2.0, 1.00, 1.00), crs);
final GridGeometry grid2 = new GridGeometry(new GridExtent(6, 2), CELL_CENTER, new AffineTransform2D(1.0, 0, 0, 1.0, 0.50, 0.50), crs);
final GridGeometry grid3 = new GridGeometry(new GridExtent(12, 4), CELL_CENTER, new AffineTransform2D(0.5, 0, 0, 0.5, 0.25, 0.25), crs);
final GridCoverage coverage1 = new BufferedGridCoverage(grid1, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverage coverage2 = new BufferedGridCoverage(grid2, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverage coverage3 = new BufferedGridCoverage(grid3, bands, DataBuffer.TYPE_DOUBLE);
final GridCoverageResource resource1 = new InMemoryGridCoverageResource(coverage1);
final GridCoverageResource resource2 = new InMemoryGridCoverageResource(coverage2);
final GridCoverageResource resource3 = new InMemoryGridCoverageResource(coverage3);
final WritableRenderedImage img1 = (WritableRenderedImage) coverage1.render(null);
final WritableRenderedImage img2 = (WritableRenderedImage) coverage2.render(null);
final WritableRenderedImage img3 = (WritableRenderedImage) coverage3.render(null);
final WritablePixelIterator write1 = WritablePixelIterator.create(img1);
final WritablePixelIterator write2 = WritablePixelIterator.create(img2);
final WritablePixelIterator write3 = WritablePixelIterator.create(img3);
BufferedImages.setAll(img1, new double[] { Double.NaN });
BufferedImages.setAll(img2, new double[] { Double.NaN });
BufferedImages.setAll(img3, new double[] { Double.NaN });
write1.moveTo(1, 0);
write1.setSample(0, 1);
write1.moveTo(2, 0);
write1.setSample(0, 1);
write2.moveTo(2, 0);
write2.setSample(0, 2);
write2.moveTo(3, 0);
write2.setSample(0, 2);
write2.moveTo(2, 1);
write2.setSample(0, 2);
write2.moveTo(3, 1);
write2.setSample(0, 2);
write3.moveTo(6, 0);
write3.setSample(0, 3);
write3.moveTo(7, 0);
write3.setSample(0, 3);
write3.moveTo(8, 0);
write3.setSample(0, 3);
write3.moveTo(9, 0);
write3.setSample(0, 3);
write3.moveTo(6, 1);
write3.setSample(0, 3);
write3.moveTo(7, 1);
write3.setSample(0, 3);
write3.moveTo(8, 1);
write3.setSample(0, 3);
write3.moveTo(9, 1);
write3.setSample(0, 3);
/*
We expect a final coverage to be :
+---+---+---+---+---+---+
|NaN|NaN| 2 | 2 | 3 | 1 |
+---+---+---+---+---+---+
|NaN|NaN| 2 | 2 | 1 | 1 |
+---+---+---+---+---+---+
*/
final AggregatedCoverageResource aggregate = new AggregatedCoverageResource();
aggregate.setInterpolation(Interpolation.NEAREST);
aggregate.setMode(AggregatedCoverageResource.Mode.SCALE);
aggregate.add(resource1);
aggregate.add(resource2);
aggregate.add(resource3);
// we must obtain the lowest resolution
final double[] resolution = aggregate.getGridGeometry().getResolution(true);
Assert.assertArrayEquals(new double[] { 0.5, 0.5 }, resolution, 0.0);
final GridGeometry gridGeometry = aggregate.getGridGeometry();
Assert.assertTrue(!gridGeometry.isDefined(GridGeometry.EXTENT));
Assert.assertTrue(gridGeometry.isDefined(GridGeometry.ENVELOPE));
final GridCoverage coverage = aggregate.read(grid2);
final RenderedImage image = coverage.render(null);
final PixelIterator reader = PixelIterator.create(image);
reader.moveTo(0, 0);
Assert.assertTrue(Double.isNaN(reader.getSampleDouble(0)));
reader.moveTo(1, 0);
Assert.assertTrue(Double.isNaN(reader.getSampleDouble(0)));
reader.moveTo(2, 0);
assertEquals(2, reader.getSample(0));
reader.moveTo(3, 0);
assertEquals(2, reader.getSample(0));
reader.moveTo(4, 0);
assertEquals(3, reader.getSample(0));
reader.moveTo(5, 0);
assertEquals(1, reader.getSample(0));
reader.moveTo(0, 1);
Assert.assertTrue(Double.isNaN(reader.getSampleDouble(0)));
reader.moveTo(1, 1);
Assert.assertTrue(Double.isNaN(reader.getSampleDouble(0)));
reader.moveTo(2, 1);
assertEquals(2, reader.getSample(0));
reader.moveTo(3, 1);
assertEquals(2, reader.getSample(0));
reader.moveTo(4, 1);
assertEquals(1, reader.getSample(0));
reader.moveTo(5, 1);
assertEquals(1, reader.getSample(0));
}
use of org.apache.sis.coverage.grid.BufferedGridCoverage in project geotoolkit by Geomatys.
the class AggregatedCoverageResourceTest method testNoDataAdded.
/**
* Test aggregation add a NaN value to fill spaces.
*
* @throws DataStoreException
* @throws TransformException
*/
@Test
public void testNoDataAdded() throws DataStoreException, TransformException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final SampleDimension sd = new SampleDimension.Builder().setName("data").setBackground("no-data", Short.MIN_VALUE).build();
final List<SampleDimension> bands = Arrays.asList(sd);
/*
Coverage 1
+---+
| 1 |
+---+
Coverage 2
+---+
| 2 |
+---+
*/
final GridGeometry grid1 = new GridGeometry(new GridExtent(1, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0, 0), crs);
final GridGeometry grid2 = new GridGeometry(new GridExtent(1, 1), CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 2, 0), crs);
final GridGeometry grid = 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_SHORT);
final GridCoverage coverage2 = new BufferedGridCoverage(grid2, bands, DataBuffer.TYPE_SHORT);
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.setSample(0, 1);
write2.moveTo(0, 0);
write2.setSample(0, 2);
/*
We expect a final coverage with values :
+---+------+---+
| 1 |-32768| 2 |
+---+------+---+
*/
final GridCoverageResource aggregate = AggregatedCoverageResource.create(null, AggregatedCoverageResource.Mode.ORDER, resource1, resource2);
((AggregatedCoverageResource) aggregate).setInterpolation(Interpolation.NEAREST);
final GridGeometry gridGeometry = aggregate.getGridGeometry();
Assert.assertTrue(!gridGeometry.isDefined(GridGeometry.EXTENT));
Assert.assertTrue(gridGeometry.isDefined(GridGeometry.ENVELOPE));
final GridCoverage coverage = aggregate.read(grid).forConvertedValues(true);
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(Short.MIN_VALUE, reader.getSample(0));
reader.moveTo(2, 0);
assertEquals(2, reader.getSample(0));
}
Aggregations