//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 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 bbox is equal to requestede bbox
BoundingBox computedBBox = spatialRequestHelper.getComputedBBox();
assertFalse(computedBBox.isEmpty());
ReferencedEnvelope finalReferencedEnvelope = new ReferencedEnvelope(
0,100,
0,70,
sourceCRS);
// 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(finalReferencedEnvelope, 1E-4, true));
// computed raster area is equal to requested raster area
Rectangle computedRasterArea = spatialRequestHelper.getComputedRasterArea();
assertFalse(computedRasterArea.isEmpty());
GridToEnvelopeMapper gridToEnvelopeMapper = new GridToEnvelopeMapper(
new GridEnvelope2D(requestedRasterArea),
requestedBBox);
gridToEnvelopeMapper.setPixelAnchor(PixelInCell.CELL_CORNER);
assertEquals(
computedRasterArea,
new Rectangle(0,0,142,256));
// g2w transform
// all this intersecting and so on MUST not impact the requested resolutions
// transform
AffineTransform computedG2W = spatialRequestHelper.getComputedGridToWorld();
assertNotNull(computedG2W);
double[] computedResolution = spatialRequestHelper.getComputedResolution();
// all this intersecting and so on MUST not impact the requested resolutions
gridToEnvelopeMapper = new GridToEnvelopeMapper(
new GridEnvelope2D(computedRasterArea),
finalReferencedEnvelope);
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);