use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class WPS2Process method createRequest.
/**
* Make a WPS Execute request from {@link ParameterValueGroup values}.
*/
private ExecuteRequest createRequest() throws ProcessException {
try {
final ParameterValueGroup inputs = getInput();
final List<GeneralParameterDescriptor> inputParamDesc = inputs.getDescriptor().descriptors();
final List<GeneralParameterDescriptor> outputParamDesc = descriptor.getOutputDescriptor().descriptors();
final List<DataInput> wpsIN = new ArrayList<>();
final List<OutputDefinition> wpsOUT = new ArrayList<>();
final String processId = descriptor.getIdentifier().getCode();
for (final GeneralParameterValue inputValue : inputs.values()) {
GeneralParameterDescriptor inputGeneDesc = inputValue.getDescriptor();
if (inputGeneDesc instanceof ParameterDescriptor) {
final ParameterDescriptor inputDesc = (ParameterDescriptor) inputGeneDesc;
final DataAdaptor adaptor = (DataAdaptor) ((ExtendedParameterDescriptor) inputDesc).getUserObject().get(DataAdaptor.USE_ADAPTOR);
final Object value = ((ParameterValue) inputValue).getValue();
if (value == null)
continue;
final DataInput dataInput;
if (adaptor instanceof LiteralAdaptor) {
dataInput = ((LiteralAdaptor) adaptor).toWPS2Input(value, rawLiteralData);
} else {
dataInput = adaptor.toWPS2Input(value);
}
dataInput.setId(inputDesc.getName().getCode());
wpsIN.add(dataInput);
}
}
/*
* OUTPUTS
*/
for (final GeneralParameterDescriptor outputGeneDesc : outputParamDesc) {
if (outputGeneDesc instanceof ParameterDescriptor) {
final ParameterDescriptor outputDesc = (ParameterDescriptor) outputGeneDesc;
final DataAdaptor adaptor = (DataAdaptor) ((ExtendedParameterDescriptor) outputDesc).getUserObject().get(DataAdaptor.USE_ADAPTOR);
final String outputIdentifier = outputDesc.getName().getCode();
String mime = null;
String encoding = null;
String schema = null;
if (adaptor instanceof ComplexAdaptor) {
final ComplexAdaptor cadaptor = (ComplexAdaptor) adaptor;
mime = cadaptor.getMimeType();
encoding = cadaptor.getEncoding();
schema = cadaptor.getSchema();
}
final OutputDefinition out = new OutputDefinition(outputIdentifier, asReference);
out.setEncoding(encoding);
out.setMimeType(mime);
out.setSchema(schema);
wpsOUT.add(out);
} else if (outputGeneDesc instanceof ParameterDescriptorGroup) {
final ParameterDescriptorGroup outputDesc = (ParameterDescriptorGroup) outputGeneDesc;
final OutputDefinition out = new OutputDefinition(outputDesc.getName().getCode(), asReference);
wpsOUT.add(out);
}
}
final ExecuteRequest request = registry.getClient().createExecute();
request.setClientSecurity(security);
final Execute execute = request.getContent();
execute.setIdentifier(processId);
final Execute.Mode mode = executionMode == null ? Execute.Mode.auto : executionMode;
execute.setMode(mode);
execute.setResponse(rawOutput ? Execute.Response.raw : Execute.Response.document);
execute.getInput().addAll(wpsIN);
execute.getOutput().addAll(wpsOUT);
WPSProcessingRegistry.LOGGER.log(Level.FINER, "Execute request created for {0} in {1} mode.", new Object[] { processId, mode });
return request;
} catch (UnconvertibleObjectException ex) {
throw new ProcessException("Error during conversion step.", null, ex);
}
}
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);
// PARAMETERS CHECK ////////////////////////////////////////////////////
final GridCoverage[] inputCoverage = inputParameters.getValue(IN_COVERAGES);
if (inputCoverage.length == 0) {
throw new ProcessException("No coverage to combine", this, null);
} else if (inputCoverage.length == 1) {
// nothing to do
outputParameters.getOrCreate(OUT_COVERAGE).setValue(inputCoverage[0]);
return;
}
// TODO: better logic
GridGeometry outputGeom = null;
// CALL IMAGE BAND COMBINE /////////////////////////////////////////////
final RenderedImage[] images = new RenderedImage[inputCoverage.length];
final List<SampleDimension> sds = new ArrayList<>();
for (int i = 0; i < inputCoverage.length; i++) {
final GridCoverage gridCoverage2D = inputCoverage[i];
final List<SampleDimension> covSds = gridCoverage2D.getSampleDimensions();
if (covSds.isEmpty())
throw new ProcessException("Cannot extract sample dimension from input coverage " + i, this);
sds.addAll(covSds);
final GridGeometry gg = gridCoverage2D.getGridGeometry();
if (gg.getDimension() <= 2) {
images[i] = gridCoverage2D.render(null);
if (outputGeom == null)
outputGeom = gridCoverage2D.getGridGeometry();
} else {
final GridGeometryIterator sliceIt = new GridGeometryIterator(gg);
if (!sliceIt.hasNext())
throw new ProcessException("Input coverage [at index " + i + "] is empty", this);
final GridGeometry nextGeom = sliceIt.next();
if (outputGeom == null)
outputGeom = nextGeom;
images[i] = gridCoverage2D.render(nextGeom.getExtent());
}
}
final ProcessDescriptor imageCombineDesc = org.geotoolkit.processing.image.bandcombine.BandCombineDescriptor.INSTANCE;
final Parameters params = Parameters.castOrWrap(imageCombineDesc.getInputDescriptor().createValue());
params.parameter("images").setValue(images);
final Process process = imageCombineDesc.createProcess(params);
RenderedImage resultImage = (RenderedImage) process.call().parameter("result").getValue();
// REBUILD COVERAGE ////////////////////////////////////////////////////
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setValues(resultImage);
gcb.setDomain(outputGeom);
gcb.setRanges(sds);
final GridCoverage resultCoverage = gcb.build();
outputParameters.getOrCreate(OUT_COVERAGE).setValue(resultCoverage);
}
use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class CoverageToVectorProcess method execute.
@Override
protected void execute() throws ProcessException {
ArgumentChecks.ensureNonNull("inputParameters", inputParameters);
final GridCoverage coverage = inputParameters.getValue(CoverageToVectorDescriptor.COVERAGE);
final NumberRange[] ranges = inputParameters.getValue(CoverageToVectorDescriptor.RANGES);
Integer band = inputParameters.getValue(CoverageToVectorDescriptor.BAND);
if (band == null) {
band = 0;
}
Geometry[] result = null;
try {
result = toPolygon(coverage, ranges, 0);
} catch (IOException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
} catch (TransformException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
// avoid memory use
buffers = null;
polygons.clear();
outputParameters.getOrCreate(CoverageToVectorDescriptor.GEOMETRIES).setValue(result);
}
use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class MathCalcProcess method execute.
@Override
protected void execute() throws ProcessException {
final GridCoverage[] inCoverages = inputParameters.getValue(MathCalcDescriptor.IN_COVERAGES);
final String inFormula = inputParameters.getValue(MathCalcDescriptor.IN_FORMULA);
final String[] inMapping = inputParameters.getValue(MathCalcDescriptor.IN_MAPPING);
final WritableGridCoverageResource outRef = inputParameters.getValue(MathCalcDescriptor.IN_RESULT_COVERAGE);
final GridGeometry gg;
try {
gg = outRef.getGridGeometry();
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
// create expression
final FilterFactory2 ff = FilterUtilities.FF;
final Expression exp;
try {
exp = CQL.parseExpression(inFormula, ff);
} catch (CQLException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
// prepare dynamic pick object
final MathCalcCoverageEvaluator evaluator;
try {
evaluator = new MathCalcCoverageEvaluator(inCoverages, inMapping, exp, gg.getCoordinateReferenceSystem());
} catch (FactoryException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
final FillCoverage filler = new FillCoverage();
try {
if (outRef instanceof TiledResource) {
filler.fill((TiledResource) outRef, evaluator);
} else {
filler.fill(outRef, evaluator, null);
}
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
} catch (TransformException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
} catch (FactoryException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
}
use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.
the class ShadedRelief method execute.
@Override
protected void execute() throws ProcessException {
GridCoverage coverage = inputParameters.getValue(ShadedReliefDescriptor.COVERAGE);
GridCoverage elevation = inputParameters.getValue(ShadedReliefDescriptor.ELEVATION);
MathTransform1D eleConv = inputParameters.getValue(ShadedReliefDescriptor.ELECONV);
// prepare coverage for the expected work
coverage = coverage.forConvertedValues(false);
elevation = elevation.forConvertedValues(true);
// light informations
final Vector3f lightDirection = new Vector3f(1, 1, 1);
final Vector3f fragToEye = new Vector3f(0, 0, 1);
lightDirection.normalize();
final RenderedImage baseImage = coverage.render(null);
final ColorModel cm = baseImage.getColorModel();
final Raster baseRaster = getData(baseImage);
final Raster eleImage = getData(elevation.render(null));
final int width = baseImage.getWidth();
final int height = baseImage.getHeight();
final BufferedImage resImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// list all coordinates
// we need 1 extract point for the last row and col triangles
final float[] coords = new float[(width + 1) * (height + 1) * 2];
int k = -1;
for (int y = 0; y < height + 1; y++) {
for (int x = 0; x < width + 1; x++) {
coords[++k] = x;
coords[++k] = y;
}
}
// we convert everything to meters
final MathTransform gridToData = coverage.getGridGeometry().getGridToCRS(PixelInCell.CELL_CORNER);
final MathTransform dataToMercator;
try {
dataToMercator = CRS.findOperation(coverage.getCoordinateReferenceSystem(), MERCATOR, null).getMathTransform();
final MathTransform gridToMercator = MathTransforms.concatenate(gridToData, dataToMercator);
gridToMercator.transform(coords, 0, coords, 0, coords.length / 2);
// loop on each pixel, create 2 triangles and calculate shaded color
final int lineLength = (width + 1) * 2;
final float[] fa = new float[3];
final float[] fb = new float[3];
final float[] fc = new float[3];
final float[] fd = new float[3];
final Vector3f v1 = new Vector3f();
final Vector3f v2 = new Vector3f();
final Vector3f v3 = new Vector3f();
final Vector3f n1 = new Vector3f();
final Vector3f n2 = new Vector3f();
final Vector3f n = new Vector3f();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// get 4 corner coordinates
int offset1 = lineLength * y + x * 2;
int offset2 = lineLength * (y + 1) + x * 2;
int ex = (x == width - 1) ? x : x + 1;
int ey = (y == height - 1) ? y : y + 1;
fa[0] = coords[offset1 + 0];
fa[1] = coords[offset1 + 1];
fa[2] = (float) eleConv.transform(eleImage.getSampleFloat(x, y, 0));
fb[0] = coords[offset1 + 2];
fb[1] = coords[offset1 + 3];
fb[2] = (float) eleConv.transform(eleImage.getSampleFloat(ex, y, 0));
fc[0] = coords[offset2 + 0];
fc[1] = coords[offset2 + 1];
fc[2] = (float) eleConv.transform(eleImage.getSampleFloat(x, ey, 0));
fd[0] = coords[offset2 + 2];
fd[1] = coords[offset2 + 3];
fd[2] = (float) eleConv.transform(eleImage.getSampleFloat(ex, ey, 0));
boolean flipx = (fa[0] > fb[0]);
boolean flipy = (fa[1] < fc[1]);
boolean invert = (flipx || flipy) && !(flipx && flipy);
// calculate average normal of the triangles
v1.x = fa[0];
v1.y = fa[1];
v1.z = fa[2];
v2.x = fb[0];
v2.y = fb[1];
v2.z = fb[2];
v3.x = fc[0];
v3.y = fc[1];
v3.z = fc[2];
n1.set(Geometries.calculateNormal(v1, v3, v2));
v1.x = fb[0];
v1.y = fb[1];
v1.z = fb[2];
v2.x = fc[0];
v2.y = fc[1];
v2.z = fc[2];
v3.x = fd[0];
v3.y = fd[1];
v3.z = fd[2];
n2.set(Geometries.calculateNormal(v1, v2, v3));
n.set(n1);
n.add(n2);
n.normalize();
if (invert) {
n.scale(-1f);
}
int argb = cm.getRGB(baseRaster.getDataElements(x, y, null));
float cr = (float) ((argb >> 16) & 0xFF) / 255f;
float cg = (float) ((argb >> 8) & 0xFF) / 255f;
float cb = (float) ((argb >> 0) & 0xFF) / 255f;
float ca = (float) ((argb >> 24) & 0xFF) / 255f;
float ratio = 1f;
// the elevation model has a hole in the grid
if (!Float.isNaN(n.x) && !Float.isNaN(n.y) && !Float.isNaN(n.z)) {
// calculate shaded color
ratio = (float) Math.max(lightDirection.dot(n), 0.0f);
// next line is to indensify average colors, lights darken flat areas so we compensate a little
ratio = ratio + (float) (Math.sin(ratio * Math.PI) * 0.20);
}
argb = toARGB(cr * ratio, cg * ratio, cb * ratio, ca);
// float r = XMath.clamp( (fa[2] * 0.0001f), 0f, 1f);
// argb = toARGB(r, r, r, 1f);
resImage.setRGB(x, y, argb);
}
}
} catch (FactoryException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
} catch (TransformException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
final GridCoverageBuilder gcb = new GridCoverageBuilder();
gcb.setDomain(coverage.getGridGeometry());
gcb.setValues(resImage);
final GridCoverage result = gcb.build();
outputParameters.getOrCreate(ShadedReliefDescriptor.OUTCOVERAGE).setValue(result);
}
Aggregations