use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class BandCombineProcess method execute.
@Override
protected void execute() throws ProcessException {
ArgumentChecks.ensureNonNull("inputParameter", inputParameters);
inputImages = inputParameters.getValue(BandCombineDescriptor.IN_IMAGES);
if (inputImages.length == 0)
throw new ProcessException("No image to combine", this, null);
if (inputImages.length == 1) {
// nothing to do
outputParameters.getOrCreate(BandCombineDescriptor.OUT_IMAGE).setValue(inputImages[0]);
return;
}
// check and extract informations, all images should have the same size and sample type.
nbBands = new int[inputImages.length];
nbBandIndex = new int[inputImages.length];
// -- attribut use only during same tile size
readItes = new PixelIterator[inputImages.length];
// -- minimum image coordinates only use during assert
minXYs = new int[inputImages.length][2];
tilesSize = new Dimension[inputImages.length];
for (int i = 0; i < inputImages.length; i++) {
final RenderedImage image = inputImages[i];
final SampleModel sm = image.getSampleModel();
if (sampleType == -1) {
// first image
sampleType = sm.getDataType();
width = image.getWidth();
height = image.getHeight();
} else {
// check same model
if (sampleType != sm.getDataType())
throw new ProcessException("Images do not have the same sample type", this, null);
if (width != image.getWidth() || height != image.getHeight())
throw new ProcessException("Images do not have the same size", this, null);
}
minXYs[i][0] = image.getMinX();
minXYs[i][1] = image.getMinY();
tilesSize[i] = new Dimension(image.getTileWidth(), image.getTileHeight());
readItes[i] = new PixelIterator.Builder().create(image);
nbBands[i] = sm.getNumBands();
nbBandIndex[i] = nbtotalbands;
nbtotalbands += sm.getNumBands();
}
// TODO : use a subclass of computed image.
final BufferedImage img = BufferedImages.createImage(width, height, nbtotalbands, sampleType);
final org.apache.sis.image.PixelIterator[] ins = new org.apache.sis.image.PixelIterator[inputImages.length];
for (int i = 0; i < inputImages.length; i++) {
ins[i] = org.apache.sis.image.PixelIterator.create(inputImages[i]);
}
final org.apache.sis.image.WritablePixelIterator out = WritablePixelIterator.create(img);
final double[] sample = new double[nbtotalbands];
int y, x, i, b;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
out.moveTo(x, y);
for (i = 0; i < inputImages.length; i++) {
ins[i].moveTo(x, y);
if (nbBands[i] == 1) {
sample[nbBandIndex[i]] = ins[i].getSampleDouble(0);
} else {
for (b = 0; b < nbBands[i]; b++) {
sample[nbBandIndex[i] + b] = ins[i].getSampleDouble(b);
}
}
}
out.setPixel(sample);
}
}
outputParameters.getOrCreate(BandCombineDescriptor.OUT_IMAGE).setValue(img);
}
use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class BandSelectProcess method execute.
@Override
protected void execute() throws ProcessException {
ArgumentChecks.ensureNonNull("inputParameter", inputParameters);
final RenderedImage inputImage = inputParameters.getValue(IN_IMAGE);
final int[] bands = inputParameters.getValue(IN_BANDS);
final SampleModel inputSampleModel = inputImage.getSampleModel();
final int inputNbBand = inputSampleModel.getNumBands();
final int inputType = inputSampleModel.getDataType();
// check band indexes
for (int targetIndex = 0; targetIndex < bands.length; targetIndex++) {
if (bands[targetIndex] >= inputNbBand) {
// wrong index, no band for this index
throw new ProcessException("Invalid band index " + bands[targetIndex] + " , image only have " + inputNbBand + " bands", this, null);
}
}
// create the output image
final int width = inputImage.getWidth();
final int height = inputImage.getHeight();
final int nbBand = bands.length;
final Point upperLeft = new Point(inputImage.getMinX(), inputImage.getMinY());
final WritableRaster raster;
try {
raster = ReformatProcess.createRaster(inputType, width, height, nbBand, upperLeft);
} catch (IllegalArgumentException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
// -- study Color Model
final int dataType = inputImage.getSampleModel().getDataType();
final SampleType st = SampleType.valueOf(inputImage.getSampleModel().getDataType());
// -- if we choose only one band grayScale else RGB
final PhotometricInterpretation pI = (bands.length == 1) ? PhotometricInterpretation.GRAYSCALE : PhotometricInterpretation.RGB;
ColorModel outCm = null;
try {
outCm = ImageUtils.createColorModel(st, bands.length, pI, false, false, null);
} catch (Exception ex) {
// various exceptions may happen here, RGB color model compatibility is obscur
// fallback on grayscale
outCm = ColorModelFactory.createGrayScale(dataType, nbBand, 0, 0, 1);
}
final BufferedImage resultImage = new BufferedImage(outCm, raster, false, new Hashtable<>());
// copy datas
final PixelIterator readIte = new PixelIterator.Builder().create(inputImage);
final WritablePixelIterator writeIte = new PixelIterator.Builder().createWritable(raster);
final double[] pixel = new double[inputNbBand];
int trgBandIdx = 0;
while (readIte.next() && writeIte.next()) {
trgBandIdx = 0;
// read source pixels
readIte.getPixel(pixel);
// write target pixels
for (int b = 0; b < bands.length; b++) {
int tidx = bands[b];
if (tidx != -1)
writeIte.setSample(b, pixel[tidx]);
}
}
outputParameters.getOrCreate(OUT_IMAGE).setValue(resultImage);
}
use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class Statistics method execute.
@Override
protected void execute() throws ProcessException {
final RenderedImage inImage = inputParameters.getValue(IMAGE);
final boolean excludeNoData = inputParameters.getValue(EXCLUDE_NO_DATA);
fireProgressing("Pre-analysing", 0f, false);
final RenderedImage image;
final ImageStatistics sc;
if (inImage != null) {
image = inImage;
final SampleModel sm = image.getSampleModel();
final SampleType sampleType = SampleType.valueOf(sm.getDataType());
final int nbBands = sm.getNumBands();
// create empty statistic object
sc = new ImageStatistics(nbBands, sampleType);
} else {
final GridCoverage inCoverage = inputParameters.getValue(COVERAGE);
GridCoverage candidate = null;
if (inCoverage != null) {
candidate = inCoverage;
} else {
final GridCoverageResource ref = inputParameters.getValue(REF);
if (ref != null) {
candidate = getCoverage(ref);
}
}
// we want the statistics on the real data values
candidate = candidate.forConvertedValues(true);
if (candidate == null) {
throw new ProcessException("Null Coverage.", this, null);
}
// TODO extract view as process input parameter.
image = candidate.render(null);
final SampleModel sm = image.getSampleModel();
final SampleType sampleType = SampleType.valueOf(sm.getDataType());
final int nbBands = sm.getNumBands();
sc = new ImageStatistics(nbBands, sampleType);
final SampleDimension[] sampleDimensions = candidate.getSampleDimensions().toArray(new SampleDimension[0]);
// add no data values and name on bands
for (int i = 0; i < sampleDimensions.length; i++) {
sc.getBand(i).setNoData(SampleDimensionUtils.getNoDataValues(sampleDimensions[i]));
sc.getBand(i).setName(sampleDimensions[i].getName().toString());
}
}
final DoublePredicate[] validityTests = initValueValidityTests(sc, excludeNoData);
outputParameters.getOrCreate(OUTCOVERAGE).setValue(sc);
fireProgressing("Pre-analysing finished", 10f, true);
fireProgressing("Start range/histogram computing", 10f, true);
final ImageStatistics.Band[] bands = sc.getBands();
final org.apache.sis.math.Statistics[] stats = new org.apache.sis.math.Statistics[bands.length];
for (int i = 0; i < bands.length; i++) stats[i] = new org.apache.sis.math.Statistics("stats");
int nbBands = bands.length;
// optimization for GridMosaicRenderedImage impl
NumericHistogram[] histo = new NumericHistogram[nbBands];
final int startX = image.getMinTileX();
final int startY = image.getMinTileY();
final int endX = startX + image.getNumXTiles();
final int endY = startY + image.getNumYTiles();
long totalTiles = image.getNumXTiles() * image.getNumYTiles();
// analyse each tiles
// todo make this parallel
Raster tile;
PixelIterator pix;
int step = 1;
for (long y = startY; y < endY; y++) {
for (long x = startX; x < endX; x++) {
tile = image.getTile(Math.toIntExact(x - startX), Math.toIntExact(y - startY));
pix = new PixelIterator.Builder().create(tile);
analyseRange(pix, stats, bands, excludeNoData);
pix.rewind();
mergeHistograms(histo, analyseHistogram(pix, bands, stats, validityTests));
updateBands(bands, histo);
fireProgressing("Histogram progressing", (step / totalTiles) * 0.9f, true);
step++;
}
}
// copy statistics in band container
for (int i = 0; i < bands.length; i++) {
bands[i].setMin(stats[i].minimum());
bands[i].setMax(stats[i].maximum());
bands[i].setMean(stats[i].mean());
bands[i].setStd(stats[i].standardDeviation(true));
}
}
use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class GroovyConditionProcess method execute.
@Override
protected void execute() throws ProcessException {
final Map variables = inputParameters.getValue(GroovyDescriptor.VARIABLES);
final String expression = inputParameters.getValue(GroovyDescriptor.SCRIPT);
final String behavior = inputParameters.getValue(GroovyDescriptor.BEHAVIOR);
final Binding binding = new Binding();
final GroovyShell shell = new GroovyShell(binding);
final Set<String> keys = variables.keySet();
for (String key : keys) {
shell.setVariable(key, variables.get(key));
}
Object result = shell.evaluate(expression);
if ("EXCEPTION".equals(behavior)) {
if (result != null && result instanceof Boolean && !((Boolean) result)) {
throw new ProcessException("Groovy expression failed." + expression, this, null);
}
}
outputParameters.getOrCreate(GroovyDescriptor.RESULT).setValue(result);
}
use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class PackFile method execute.
/**
* {@inheritDoc }
*/
@Override
protected void execute() throws ProcessException {
fireProcessStarted("Start pack");
final File[] source = inputParameters.getValue(PackFileDescriptor.SOURCE_IN);
final File target = inputParameters.getValue(PackFileDescriptor.TARGET_IN);
// Prepare compression
try {
final FileOutputStream fileOS = new FileOutputStream(target);
final CheckedOutputStream checksumOS = new CheckedOutputStream(fileOS, new Adler32());
final BufferedOutputStream buffOS = new BufferedOutputStream(checksumOS);
final ZipOutputStream zipOS = new ZipOutputStream(buffOS);
zipOS.setMethod(ZipOutputStream.DEFLATED);
zipOS.setLevel(Deflater.BEST_COMPRESSION);
// Compress all files
compressAllDirectoryFiles(zipOS, new byte[BUFFER], source, null);
// End compression
zipOS.close();
buffOS.close();
checksumOS.close();
fileOS.close();
} catch (IOException ex) {
throw new ProcessException("IO exception while packing files", this, ex);
}
outputParameters.getOrCreate(PackFileDescriptor.RESULT_OUT).setValue(target);
fireProcessCompleted("Pack done.");
}
Aggregations