use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class RasterSymbolizerTest method coverage_whose_grid_origin_is_lower_left_should_be_flipped.
/**
* Source coverage will be a matrix <em>with origin lower-left</em>:
* <table>
* <tr><td>4</td><td>3</td></tr>
* <tr><td>1</td><td>2</td></tr>
* </table>
* @throws PortrayalException
*/
@Test
public void coverage_whose_grid_origin_is_lower_left_should_be_flipped() throws PortrayalException {
final BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setSample(0, 0, 0, 1);
image.getRaster().setSample(1, 0, 0, 2);
image.getRaster().setSample(1, 1, 0, 3);
image.getRaster().setSample(0, 1, 0, 4);
final GridGeometry geom = new GridGeometry(new GridExtent(2, 2), PixelInCell.CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 10, 10), CommonCRS.defaultGeographic());
final GridCoverage baseData = new GridCoverage2D(geom, null, image);
MapLayer layer = MapBuilder.createLayer(new InMemoryGridCoverageResource(baseData));
final MapLayers ctx = MapBuilder.createContext();
ctx.getComponents().add(layer);
BufferedImage rendering = DefaultPortrayalService.portray(new CanvasDef(new Dimension(2, 2), geom.getEnvelope()), new SceneDef(ctx, new Hints(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR, RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)));
// As display is oriented upper-left, output should be flipped on y axis. Also, the renderer will stretch values
// along 256 colors, so we have to adapt comparison.
final int[] pixels = rendering.getRaster().getPixels(0, 0, 2, 2, (int[]) null);
final int[] expected = { 255, 255, 255, 255, 165, 165, 165, 255, 0, 0, 0, 255, 88, 88, 88, 255 };
assertArrayEquals(expected, pixels);
final ColorMap colorMap = SF.colorMap(SF.interpolateFunction(null, Arrays.asList(SF.interpolationPoint(1, FF.literal(Color.RED)), SF.interpolationPoint(2, FF.literal(Color.GREEN)), SF.interpolationPoint(3, FF.literal(Color.BLUE)), SF.interpolationPoint(4, FF.literal(Color.WHITE))), null, null, FF.literal(Color.BLACK)));
final RasterSymbolizer symbol = SF.rasterSymbolizer(null, null, null, null, colorMap, null, null, null);
ctx.getComponents().set(0, MapBuilder.createCoverageLayer(baseData, SF.style(symbol), "test"));
rendering = DefaultPortrayalService.portray(new CanvasDef(new Dimension(2, 2), geom.getEnvelope()), new SceneDef(ctx, new Hints(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR, RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)));
assertEquals(Color.WHITE.getRGB(), rendering.getRGB(0, 0));
assertEquals(Color.BLUE.getRGB(), rendering.getRGB(1, 0));
assertEquals(Color.RED.getRGB(), rendering.getRGB(0, 1));
assertEquals(Color.GREEN.getRGB(), rendering.getRGB(1, 1));
}
use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class RasterSymbolizerTest method renderInterpolationCoverage.
/**
* Render a coverage with nearest and lanczos interpolation.
*/
@Test
public void renderInterpolationCoverage() throws FactoryException, PortrayalException, IOException {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
final BufferedImage image = new BufferedImage(36, 18, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 18, 9);
g.setColor(Color.GREEN);
g.fillRect(18, 0, 18, 9);
g.setColor(Color.BLUE);
g.fillRect(0, 9, 18, 9);
g.setColor(Color.YELLOW);
g.fillRect(18, 9, 18, 9);
g.dispose();
final GridExtent extent = new GridExtent(36, 18);
final AffineTransform2D gridToCrs = new AffineTransform2D(10, 0, 0, -10, -180, 90);
final GridGeometry grid = new GridGeometry(extent, PixelInCell.CELL_CORNER, gridToCrs, crs);
final SampleDimension red = new SampleDimension.Builder().setName("1").build();
final SampleDimension green = new SampleDimension.Builder().setName("2").build();
final SampleDimension blue = new SampleDimension.Builder().setName("3").build();
final GridCoverage2D coverage = new GridCoverage2D(grid, Arrays.asList(red, green, blue), image);
final GridExtent queryextent = new GridExtent(360, 180);
final GeneralEnvelope queryenv = new GeneralEnvelope(crs);
queryenv.setRange(0, -180, 180);
queryenv.setRange(1, -90, 90);
final GridGeometry querygrid = new GridGeometry(queryextent, queryenv, GridOrientation.HOMOTHETY);
final MapLayers context = MapBuilder.createContext();
context.getComponents().add(MapBuilder.createCoverageLayer(new InMemoryGridCoverageResource(coverage)));
final BufferedImage nearest;
final BufferedImage bicubic;
final BufferedImage lanczos;
{
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.NEIGHBOR);
final CanvasDef cdef = new CanvasDef(querygrid);
final SceneDef sdef = new SceneDef(context, hints);
nearest = DefaultPortrayalService.portray(cdef, sdef);
}
{
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.BICUBIC2);
final CanvasDef cdef = new CanvasDef(querygrid);
final SceneDef sdef = new SceneDef(context, hints);
bicubic = DefaultPortrayalService.portray(cdef, sdef);
}
{
final Hints hints = new Hints();
hints.put(GO2Hints.KEY_INTERPOLATION, InterpolationCase.LANCZOS);
final CanvasDef cdef = new CanvasDef(querygrid);
final SceneDef sdef = new SceneDef(context, hints);
lanczos = DefaultPortrayalService.portray(cdef, sdef);
}
int nearestRgb = nearest.getRGB(179, 0);
int bicubicRgb = bicubic.getRGB(179, 0);
int naczosRgb = lanczos.getRGB(179, 0);
assertTrue(nearestRgb != bicubicRgb);
assertTrue(bicubicRgb != naczosRgb);
}
use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.
the class RasterSymbolizerTest method renderRGBIndexedCoverage.
/**
* Render a coverage with :
* - 3 sample dimensions R,G,B
* - 1 byte indexed color model
* - 1 raster, not tiled
*/
@Test
public void renderRGBIndexedCoverage() throws FactoryException, PortrayalException {
final BufferedImage image = new BufferedImage(18, 9, BufferedImage.TYPE_BYTE_INDEXED);
image.setRGB(0, 0, Color.RED.getRGB());
image.setRGB(17, 0, Color.GREEN.getRGB());
image.setRGB(0, 8, Color.BLUE.getRGB());
final GridExtent extent = new GridExtent(18, 9);
final AffineTransform2D gridToCrs = new AffineTransform2D(20, 0, 0, 20, -170, -80);
final GridGeometry grid = new GridGeometry(extent, PixelInCell.CELL_CENTER, gridToCrs, CommonCRS.WGS84.normalizedGeographic());
/*
* We volontarely name samples 1 to avoid use of names as a hint.
*/
final SampleDimension rgb = new SampleDimension.Builder().setName("1").build();
final GridCoverage2D coverage = new GridCoverage2D(grid, Arrays.asList(rgb), image);
final MapLayers context = MapBuilder.createContext();
context.getComponents().add(MapBuilder.createCoverageLayer(new InMemoryGridCoverageResource(coverage)));
final CanvasDef cdef = new CanvasDef(grid);
final SceneDef sdef = new SceneDef(context);
BufferedImage result = DefaultPortrayalService.portray(cdef, sdef);
assertEquals(Color.RED.getRGB(), result.getRGB(0, 0));
assertEquals(Color.GREEN.getRGB(), result.getRGB(17, 0));
assertEquals(Color.BLUE.getRGB(), result.getRGB(0, 8));
// Now, test with a resample (flip axes):
final GridExtent latLonExtent = new GridExtent(9, 18);
AffineTransform2D latLonG2C = new AffineTransform2D(20, 0, 0, 20, -80, -170);
GridGeometry latLonGrid = new GridGeometry(latLonExtent, PixelInCell.CELL_CENTER, latLonG2C, CommonCRS.WGS84.geographic());
result = DefaultPortrayalService.portray(new CanvasDef(latLonGrid), sdef);
assertEquals(Color.RED.getRGB(), result.getRGB(0, 0));
assertEquals(Color.GREEN.getRGB(), result.getRGB(0, 17));
assertEquals(Color.BLUE.getRGB(), result.getRGB(8, 0));
latLonG2C = new AffineTransform2D(-20, 0, 0, 20, 80, -170);
latLonGrid = new GridGeometry(latLonExtent, PixelInCell.CELL_CENTER, latLonG2C, CommonCRS.WGS84.geographic());
result = DefaultPortrayalService.portray(new CanvasDef(latLonGrid), sdef);
assertEquals(Color.RED.getRGB(), result.getRGB(8, 0));
assertEquals(Color.GREEN.getRGB(), result.getRGB(8, 17));
assertEquals(Color.BLUE.getRGB(), result.getRGB(0, 0));
}
use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource 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.geotoolkit.storage.memory.InMemoryGridCoverageResource 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);
}
Aggregations