LOGGER.info(new StringBuilder("Coverage before: ").append("\n")
.append(gc.getCoordinateReferenceSystem().toWKT()).append(
gc.getEnvelope().toString()).toString());
}
final CoordinateReferenceSystem sourceCRS = gc.getCoordinateReferenceSystem2D();
final GeneralEnvelope sourceEnvelope = (GeneralEnvelope) gc.getEnvelope();
final GridGeometry2D sourcedGG = (GridGeometry2D) gc.getGridGeometry();
final MathTransform sourceG2W = sourcedGG.getGridToCRS(PixelInCell.CELL_CENTER);
// /////////////////////////////////////////////////////////////////////
//
//
// CROP
//
//
// /////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////
//
// Crop the original coverage.
//
// /////////////////////////////////////////////////////////////////////
double xc = sourceEnvelope.getMedian(0);
double yc = sourceEnvelope.getMedian(1);
double xl = sourceEnvelope.getSpan(0);
double yl = sourceEnvelope.getSpan(1);
final GeneralEnvelope cropEnvelope = new GeneralEnvelope(new double[] {
xc - xl / 4.0, yc - yl / 4.0 }, new double[] { xc + xl / 4.0,
yc + yl / 4.0 });
final CoverageProcessor processor = CoverageProcessor.getInstance();
final ParameterValueGroup param = processor
.getOperation("CoverageCrop").getParameters();
param.parameter("Source").setValue(gc);
param.parameter("Envelope").setValue(cropEnvelope);
final GridCoverage2D cropped = (GridCoverage2D) processor.doOperation(param);
// /////////////////////////////////////////////////////////////////////
//
// Check that we got everything correctly after the crop.
//
// /////////////////////////////////////////////////////////////////////
// checking the ranges of the output image.
final GridGeometry2D croppedGG = (GridGeometry2D) cropped.getGridGeometry();
final GridEnvelope croppedGR = croppedGG.getGridRange();
final MathTransform croppedG2W = croppedGG.getGridToCRS(PixelInCell.CELL_CENTER);
final GeneralEnvelope croppedEnvelope = (GeneralEnvelope) cropped.getEnvelope();
assertTrue("min x do not match after crop", 29 == croppedGR.getLow(0));
assertTrue("min y do not match after crop", 30 == croppedGR.getLow(1));
assertTrue("max x do not match after crop", 90 == croppedGR.getHigh(0)+1);
assertTrue("max y do not match after crop", 91 == croppedGR.getHigh(1)+1);
// check that the affine transform are the same thing
assertTrue(
"The Grdi2World tranformations of the original and the cropped covearage do not match",
sourceG2W.equals(croppedG2W));
// check that the envelope is correct
final GeneralEnvelope expectedEnvelope = new GeneralEnvelope(croppedGR,PixelInCell.CELL_CENTER, croppedG2W, cropped.getCoordinateReferenceSystem2D());
assertTrue("Expected envelope is different from the computed one",expectedEnvelope.equals(croppedEnvelope, XAffineTransform.getScale((AffineTransform) croppedG2W) / 2.0, false));
//release things
cropped.dispose(true);
gc.dispose(true);
try{