public void testGridToWorldEnvelope() throws Exception {
final double TOL = 1.0E-6;
GridGeometry2D gg = getRandomCoverage().getGridGeometry();
Envelope2D worldBounds = gg.getEnvelope2D();
GridEnvelope2D gridBounds = gg.getGridRange2D();
assertTrue(worldBounds.boundsEquals(gg.gridToWorld(gridBounds), 0, 1, TOL));
// test sub-area conversion
GridEnvelope2D subGrid = new GridEnvelope2D(
gridBounds.x + 1,
gridBounds.y + 1,
gridBounds.width - 2,
gridBounds.height - 2);
Envelope2D subEnv = gg.gridToWorld(subGrid);
double cellWidthX = worldBounds.getWidth() / gridBounds.getWidth();
double cellWidthY = worldBounds.getHeight() / gridBounds.getHeight();
Envelope2D expectedEnv = new Envelope2D(gg.getCoordinateReferenceSystem2D(),
worldBounds.getMinX() + cellWidthX,
worldBounds.getMinY() + cellWidthY,
worldBounds.getWidth() - 2 * cellWidthX,
worldBounds.getHeight() - 2 * cellWidthY);
assertTrue( expectedEnv.boundsEquals(subEnv, 0, 1, TOL) );
}