.append(inputLocation);
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine(message.toString());
fireEvent(message.toString(), 0);
ImageMosaicReader inReader = null;
try {
inReader = new ImageMosaicReader(inputLocation, new Hints(Hints.OVERVIEW_POLICY, OverviewPolicy.IGNORE));
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
fireException(e);
return;
}
// /////////////////////////////////////////////////////////////////////
//
//
// Preparing all the params
//
//
// /////////////////////////////////////////////////////////////////////
// output files' directory
if (!outputLocation.exists())
outputLocation.mkdir();
// getting envelope and other information about dimension
final GeneralEnvelope envelope = inReader.getOriginalEnvelope();
message = new StringBuilder("Original envelope is ").append(envelope.toString());
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine(message.toString());
fireEvent(message.toString(), 0);
final GridEnvelope range = inReader.getOriginalGridRange();
message = new StringBuilder("Original range is ").append(range
.toString());
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine(message.toString());
fireEvent(message.toString(), 0);
// new number of rows and columns
final double newWidth = (range.getSpan(0) * 1.0) / scaleFactor;
final double newHeight = (range.getSpan(1) * 1.0) / scaleFactor;
if (tileW > newWidth)
tileW = newWidth;
if (tileH > newHeight)
tileH = newHeight;
message = new StringBuilder("New dimension is (W,H)==(").append(newWidth).append(",").append(newHeight).append(")");
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine(message.toString());
fireEvent(message.toString(), 0);
int newCols = (int) (newWidth / tileW);
int newRows = (int) (newHeight / tileH);
final boolean hasRemainingColum = (newWidth % tileW) != 0;
final boolean hasRemainingRow = (newHeight % tileH) != 0;
message = new StringBuilder("New matrix dimension is (cols,rows)==(").append(newCols).append(",").append(newRows).append(")");
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine(message.toString());
fireEvent(message.toString(), 0);
final double minx = envelope.getMinimum(0);
final double miny = envelope.getMinimum(1);
final double maxx = envelope.getMaximum(0);
final double maxy = envelope.getMaximum(1);
double _maxx = 0.0;
double _maxy = 0.0;
double _minx = 0.0;
double _miny = 0.0;
// ///////////////////////////////////////////////////////////////////
//
// MAIN LOOP
//
//
// ///////////////////////////////////////////////////////////////////
newRows += hasRemainingRow ? 1 : 0;
newCols += hasRemainingColum ? 1 : 0;
final double totalNumberOfFile = newRows * newCols;
// getting resolution of each tile
final double tileGeoWidth = envelope.getSpan(0) / newCols;
final double tileGeoHeight = envelope.getSpan(1) / newRows;
final int uppers[] = range.getHigh().getCoordinateValues();
uppers[0] ++;
uppers[1] ++;
final double newRange[] = new double[] { uppers[0] / newCols,uppers[1] / newRows };
final CoverageProcessor processor = CoverageProcessor.getInstance();
for (int i = 0; i < newRows; i++)
for (int j = 0; j < newCols; j++) {
// //
//
// computing the bbox for this tile
//
// //
_maxx = minx + (j + 1) * tileGeoWidth;
_minx = minx + (j) * tileGeoWidth;
_maxy = miny + (i + 1) * tileGeoHeight;
_miny = miny + (i) * tileGeoHeight;
if (_maxx > maxx)
_maxx = maxx;
if (_maxy > maxy)
_maxy = maxy;
// //
//
// creating the output file
//
// //
final File fileOut = new File(outputLocation, new StringBuilder(
"mosaic").append("_").append(
Integer.toString(i * newCols + j)).append(".").append(
"tiff").toString());
if (fileOut.exists())
fileOut.delete();
message = new StringBuilder("Preparing tile (col,row)==(")
.append(j)
.append(",")
.append(i)
.append(") to file ")
.append(fileOut);
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine(message.toString());
fireEvent(message.toString(), (j + i * newCols)/ totalNumberOfFile);
// //
//
// building gridgeometry for the read operation
//
// //
final ParameterValue<GridGeometry2D> gg = ImageMosaicFormat.READ_GRIDGEOMETRY2D.createValue();
final GeneralEnvelope cropEnvelope = new GeneralEnvelope(new double[] { _minx, _miny }, new double[] { _maxx,_maxy });
cropEnvelope.setCoordinateReferenceSystem(inReader.getCrs());
//we need to supply the requeste grid range but we use a fake one since we are using the ignore overviews switch
gg.setValue(new GridGeometry2D(new GridEnvelope2D(new Rectangle(0, 0, 800, 800)), cropEnvelope));
message = new StringBuilder("Reading with grid envelope ").append(cropEnvelope.toString());
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine(message.toString());
fireEvent(message.toString(), (j + i * newCols)
/ totalNumberOfFile);
// //
//
// read the needed part and then crop to be sure that we have what we need
//
// //
GridCoverage2D gc;
try {
gc = (GridCoverage2D) inReader
.read(new GeneralParameterValue[] { gg });
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
fireEvent(e.getLocalizedMessage(), 0);
return;
}
ParameterValueGroup param = processor.getOperation("CoverageCrop").getParameters();
param.parameter("Source").setValue(gc);
param.parameter("Envelope").setValue(cropEnvelope);
param.parameter("ConserveEnvelope").setValue(Boolean.TRUE);
final GridCoverage2D cropped = (GridCoverage2D) processor.doOperation(param);
// //
//
// Adjusting the resolution in order to be the same as for all the others coverage
//
// //
final GridEnvelope2D newGridrange = new GridEnvelope2D(new Rectangle2D.Double(0.0, 0.0, newRange[0],newRange[1]).getBounds());
final GridGeometry2D scaledGridGeometry = new GridGeometry2D(newGridrange, cropEnvelope);
param = processor.getOperation("Resample").getParameters();
param.parameter("Source").setValue(cropped);
param.parameter("CoordinateReferenceSystem").setValue(inReader.getCrs());
param.parameter("GridGeometry").setValue(scaledGridGeometry);
param.parameter("InterpolationType").setValue(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
gc = (GridCoverage2D) processor.doOperation(param);