//raster area
Rectangle sourceRasterArea = new Rectangle(0,0,1000,1000);
coverageProperties.setRasterArea(sourceRasterArea);
// creating the g2d
final GridToEnvelopeMapper geMapper = new GridToEnvelopeMapper(
new GridEnvelope2D(sourceRasterArea),
sourceBBox);
AffineTransform sourceGridToWorldTransform = geMapper.createAffineTransform();
coverageProperties.setGridToWorld2D((MathTransform2D) sourceGridToWorldTransform);
coverageProperties.setFullResolution(new double[]{
XAffineTransform.getScaleX0(sourceGridToWorldTransform),
XAffineTransform.getScaleY0(sourceGridToWorldTransform)
});
coverageProperties.setGeographicBBox(sourceBBox);
coverageProperties.setGeographicCRS2D(DefaultGeographicCRS.WGS84);
SpatialRequestHelper spatialRequestHelper = new SpatialRequestHelper(coverageProperties);
//
// now the target request
//
spatialRequestHelper.setAccurateResolution(false);
GridEnvelope2D requestedRasterArea = new GridEnvelope2D(0,0,256,256);
CoordinateReferenceSystem requestCRS = CRS.decode("EPSG:3857");
ReferencedEnvelope requestedBBox_ = new ReferencedEnvelope(
0,180,
0,70,
sourceCRS);
GeneralEnvelope requestedBBox = CRS.transform(requestedBBox_, requestCRS);
spatialRequestHelper.setRequestedGridGeometry(new GridGeometry2D(
requestedRasterArea,
requestedBBox));
///// TEST
spatialRequestHelper.compute();
assertFalse(spatialRequestHelper.isEmpty());
assertTrue(spatialRequestHelper.isNeedsReprojection()); // no reprojection
// computed raster area is equal to requested raster area
Rectangle computedRasterArea = spatialRequestHelper.getComputedRasterArea();
assertFalse(computedRasterArea.isEmpty());
assertTrue(computedRasterArea.equals(requestedRasterArea));
// computed bbox is equal to requestede bbox
BoundingBox computedBBox = spatialRequestHelper.getComputedBBox();
assertFalse(computedBBox.isEmpty());
// the source bbox and the computed one are the same
// there might be minor differences due to multiple back and forth roundings, but we need
// to make sure they are negligible
assertTrue(new GeneralEnvelope(computedBBox).equals(requestedBBox_, 1E-4, true));
// transform
AffineTransform computedG2W = spatialRequestHelper.getComputedGridToWorld();
assertNotNull(computedG2W);
double[] computedResolution = spatialRequestHelper.getComputedResolution();
double scaleX=(1000.0/2)/256; // we scaled down the original image but we also use a portion of it
double scaleY=(1000.0/2)/256; // we scaled down the original image but we also use a portion of it
assertNotNull(computedResolution);
assertEquals(scaleX*XAffineTransform.getScaleX0(sourceGridToWorldTransform),computedResolution[0],1E-6);
assertEquals(scaleY*XAffineTransform.getScaleY0(sourceGridToWorldTransform),computedResolution[1],1E-6);
// all this intersecting and so on MUST not impact the requested resolutions
GridToEnvelopeMapper gridToEnvelopeMapper = new GridToEnvelopeMapper(
new GridEnvelope2D(requestedRasterArea),
requestedBBox_);
gridToEnvelopeMapper.setPixelAnchor(PixelInCell.CELL_CORNER);
double[] expectedResolution=new double[]{
XAffineTransform.getScaleX0(gridToEnvelopeMapper.createAffineTransform()),
XAffineTransform.getScaleY0(gridToEnvelopeMapper.createAffineTransform())};
assertNotNull(computedResolution);
assertEquals(expectedResolution[0],computedResolution[0],1E-6);
assertEquals(expectedResolution[1],computedResolution[1],1E-6);
// accurate resolution