InvalidGridGeometryException, TransformException {
final Coordinate envelopeCenterOrig = bbox.centre();
CoordinateReferenceSystem sourceCRS = bbox.getCoordinateReferenceSystem();
final DecimalFormat formatter = new DecimalFormat("0.####");
IGeoResource geoResource = layer.getGeoResource();
GridCoverage2D coverage = null;
GridGeometry2D gridGeometry = null;
Coordinate evaluateCoord = null;
// try to go for the reader first
boolean isOnGrid = false;
boolean hasProblem = false;
Point2D p = null;
if (geoResource.canResolve(AbstractGridCoverage2DReader.class)) {
AbstractGridCoverage2DReader reader = geoResource.resolve(AbstractGridCoverage2DReader.class, monitor);
GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope();
CoordinateReferenceSystem targetCrs = reader.getCrs();
if (targetCrs != null) {
evaluateCoord = transform(sourceCRS, targetCrs, envelopeCenterOrig);
} else {
evaluateCoord = envelopeCenterOrig;
targetCrs = sourceCRS;
}
p = new Point2D.Double(evaluateCoord.x, evaluateCoord.y);
if (originalEnvelope.contains(new DirectPosition2D(p))) {
double delta = 0.0000001;
GeneralParameterValue[] parameterValues = createGridGeometryGeneralParameter(1, 1, evaluateCoord.y + delta,
evaluateCoord.y - delta, evaluateCoord.x + delta, evaluateCoord.x - delta, targetCrs);
coverage = reader.read(parameterValues);
/*
* the following is done since the reader might read a singlwe pixel
* region and the gridcoordinate would be 0, 0 in that case. Later
* we want to supply the gridcoordinate of the position in the whole
* coverage.
*/
gridGeometry = new GridGeometry2D(reader.getOriginalGridRange(), reader.getOriginalEnvelope());
isOnGrid = true;
}
}
// else try with coverage
else if (geoResource.canResolve(GridCoverage.class)) {
coverage = (GridCoverage2D) geoResource.resolve(GridCoverage.class, monitor);
CoordinateReferenceSystem targetCrs = coverage.getCoordinateReferenceSystem();
gridGeometry = coverage.getGridGeometry();
evaluateCoord = transform(sourceCRS, targetCrs, envelopeCenterOrig);
p = new Point2D.Double(evaluateCoord.x, evaluateCoord.y);
Envelope2D envelope2d = coverage.getEnvelope2D();