/*
* the pixel space region that will be extracted. Since the origin
* is always 0,0, we can continue to see this in world view.
*/
sourceRegion = new Rectangle(xmin, ymin, (xmax - xmin), ymax - ymin);
requestedRegionEnvelope = new Envelope2D(crs, requestedWest, requestedSouth, requestedEast - requestedWest,
requestedNorth - requestedSouth);
/*
* the real world deltas
*/
xDeltaW = requestedWest - fileWest;
yDeltaS = requestedSouth - fileSouth;
xDeltaE = requestedEast - fileEast;
yDeltaN = requestedNorth - fileNorth;
// yDelta = requestedNorth - north;
/*
* the real world envelope covering the read map part.
*/
tmpDxW = xDeltaW > 0.0 ? 0.0 : xDeltaW;
tmpDyS = yDeltaS > 0.0 ? 0.0 : yDeltaS;
tmpDyN = yDeltaN < 0.0 ? 0.0 : yDeltaN;
tmpDxE = xDeltaE < 0.0 ? 0.0 : xDeltaE;
// set the region to the requestedRegion, this value is passed to
// the coverage to
// transform the JAI space into the real space.
/*
* define the subsampling values. This done starting from the
* original's image resolution.
*/
if (!useSubSamplingAsRequestedColsRows) {
/*
* in this case we respect the original subsampling contract.
*/
JGrassRegion tmpRegion = new JGrassRegion(region);
tmpRegion.setWEResolution((fileEast - fileWest) / (double) fileCols);
tmpRegion.setNSResolution((fileNorth - fileSouth) / (double) fileRows);
subSamplingX = (int) Math.floor((double) tmpRegion.getCols() / (double) requestedCols);
subSamplingY = (int) Math.floor((double) tmpRegion.getRows() / (double) requestedRows);
if (subSamplingX == 0)
subSamplingX = 1;
if (subSamplingY == 0)
subSamplingY = 1;
if (subSamplingX != subSamplingY) {
if (subSamplingX < subSamplingY) {
subSamplingY = subSamplingX;
} else {
subSamplingX = subSamplingY;
}
}
} else {
/*
* in this case the subsampling values are interpreted as
* columns and row numbers to be used to calculate the
* resolution from the given boundaries.
*/
double sourceCols = (requestedEast - requestedWest) / (1 + (xPaddingSx / (xmax - xmin))) / requestedXres;
double sourceRows = (requestedNorth - requestedSouth) / (1 + (yPaddingTop / (ymax - ymin))) / requestedYres;
/*
* the padding has to be removed since inside the reader
* the padding is ignored and non present in the sourceRegion that
* is passed.
*/
sourceCols = sourceCols + xPaddingSx;
sourceRows = sourceRows + yPaddingTop;
subSamplingX = (int) Math.round(sourceCols);
subSamplingY = (int) Math.round(sourceRows);
if (subSamplingX < 1) {
subSamplingX = 1;
}
if (subSamplingY < 1) {
subSamplingY = 1;
}
}
} else {
/*
* if no region has been requested, the source and requested region
* are the same, i.e. the whole raster is read and passed.
*/
requestedRows = fileRows;
requestedCols = fileCols;
requestedWest = fileWest;
requestedEast = fileEast;
requestedSouth = fileSouth;
requestedNorth = fileNorth;
double scaleX = fileCols / (fileEast - fileWest);
double scaleY = fileRows / (fileNorth - fileSouth);
double EPS = 1E-6;
int xmin = (int) Math.floor((requestedWest - fileWest) * scaleX + EPS);
int xmax = (int) Math.ceil((requestedEast - fileWest) * scaleX - EPS);
int ymin = (int) Math.floor((fileNorth - requestedNorth) * scaleY + EPS);
int ymax = (int) Math.ceil((fileNorth - requestedSouth) * scaleY - EPS);
sourceRegion = new Rectangle(xmin, ymin, (xmax - xmin), ymax - ymin);
requestedRegionEnvelope = new Envelope2D(crs, requestedWest, requestedSouth, requestedEast - requestedWest,
requestedNorth - requestedSouth);
/*
* define the subsampling values. This done starting from the
* original's image resolution.