hints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
// get a reader
final URL url = file.toURI().toURL();
final Object source = url;
final BaseGDALGridCoverage2DReader reader = new EsriHdrReader(source,
hints);
// Testing the getSource method
Assert.assertEquals(reader.getSource(), source);
// /////////////////////////////////////////////////////////////////////
//
// read once
//
// /////////////////////////////////////////////////////////////////////
GridCoverage2D gc = (GridCoverage2D) reader.read(null);
forceDataLoading(gc);
// /////////////////////////////////////////////////////////////////////
//
// read again with subsampling and crop
//
// /////////////////////////////////////////////////////////////////////
final double cropFactor = 2.0;
final int oldW = gc.getRenderedImage().getWidth();
final int oldH = gc.getRenderedImage().getHeight();
final Rectangle range =((GridEnvelope2D)reader.getOriginalGridRange());
final GeneralEnvelope oldEnvelope = reader.getOriginalEnvelope();
final GeneralEnvelope cropEnvelope = new GeneralEnvelope(new double[] {
oldEnvelope.getLowerCorner().getOrdinate(0)
+ (oldEnvelope.getSpan(0) / cropFactor),
oldEnvelope.getLowerCorner().getOrdinate(1)
+ (oldEnvelope.getSpan(1) / cropFactor) },
new double[] { oldEnvelope.getUpperCorner().getOrdinate(0),
oldEnvelope.getUpperCorner().getOrdinate(1) });
cropEnvelope.setCoordinateReferenceSystem(reader.getCrs());
final ParameterValue gg = (ParameterValue) ((AbstractGridFormat) reader
.getFormat()).READ_GRIDGEOMETRY2D.createValue();
gg.setValue(new GridGeometry2D(new GridEnvelope2D(new Rectangle(0, 0,
(int) (range.width / 4.0 / cropFactor),
(int) (range.height / 4.0 / cropFactor))), cropEnvelope));
gc = (GridCoverage2D) reader.read(new GeneralParameterValue[] { gg });
Assert.assertNotNull(gc);
// NOTE: in some cases might be too restrictive
Assert.assertTrue(cropEnvelope.equals(gc.getEnvelope(), XAffineTransform
.getScale(((AffineTransform) ((GridGeometry2D) gc
.getGridGeometry()).getGridToCRS2D())) / 2, true));
forceDataLoading(gc);
// /////////////////////////////////////////////////////////////////////
//
// Attempt to read an envelope which doesn't intersect the dataset one
//
// /////////////////////////////////////////////////////////////////////
final double translate0 = oldEnvelope.getSpan(0) + 100;
final double translate1 = oldEnvelope.getSpan(1) + 100;
final GeneralEnvelope wrongEnvelope = new GeneralEnvelope(new double[] {
oldEnvelope.getLowerCorner().getOrdinate(0) + translate0,
oldEnvelope.getLowerCorner().getOrdinate(1) + translate1 },
new double[] {
oldEnvelope.getUpperCorner().getOrdinate(0)
+ translate0,
oldEnvelope.getUpperCorner().getOrdinate(1)
+ translate1 });
wrongEnvelope.setCoordinateReferenceSystem(reader.getCrs());
final ParameterValue gg2 = (ParameterValue) ((AbstractGridFormat) reader
.getFormat()).READ_GRIDGEOMETRY2D.createValue();
gg2.setValue(new GridGeometry2D(new GridEnvelope2D(new Rectangle(0,
0, (int) (range.width), (int) (range.height))), wrongEnvelope));
gc = (GridCoverage2D) reader.read(new GeneralParameterValue[] { gg2 });
Assert.assertNull("Wrong envelope requested", gc);
}