Search in sources :

Example 1 with DismissProcessException

use of org.geotoolkit.process.DismissProcessException in project geotoolkit by Geomatys.

the class Categorize method execute.

@Override
protected void execute() throws ProcessException {
    final GridCoverageResource source = getSource();
    final WritableGridCoverageResource destination = getDestination();
    try {
        final GridGeometry inputGG = source.getGridGeometry();
        final GridGeometry readGeom;
        Envelope env = getEnvelope();
        if (env == null) {
            env = inputGG.getEnvelope();
            readGeom = inputGG;
        } else {
            MathTransform gridToCRS = inputGG.getGridToCRS(PixelInCell.CELL_CORNER);
            GeographicBoundingBox bbox = null;
            try {
                bbox = ReferencingUtilities.findGeographicBBox(source).orElse(null);
            } catch (DataStoreException e) {
                /* This error is not directly related to data. It could be
                     * caused by malformed metadata. In which case, we just
                     * ignore it.
                     */
                LOGGER.log(Level.FINE, "Cannot deduce geographic extent from metadata.", e);
            }
            if (env.getCoordinateReferenceSystem() != null) {
                final CoordinateOperation op = CRS.findOperation(inputGG.getCoordinateReferenceSystem(), env.getCoordinateReferenceSystem(), bbox);
                gridToCRS = MathTransforms.concatenate(gridToCRS, op.getMathTransform());
                // Crop area of interest on source coverage area
                final GeneralEnvelope sourceEnv;
                try {
                    sourceEnv = Envelopes.transform(op, inputGG.getEnvelope());
                } catch (TransformException ex) {
                    throw new ProcessException("Cannot check input envelope validity against source coverage.", this, ex);
                }
                sourceEnv.intersect(env);
                env = sourceEnv;
            } else {
                final GeneralEnvelope tmpEnv = new GeneralEnvelope(env);
                tmpEnv.setCoordinateReferenceSystem(inputGG.getCoordinateReferenceSystem());
                // Crop area of interest on source coverage area
                tmpEnv.intersect(inputGG.getEnvelope());
                env = tmpEnv;
            }
            readGeom = new GridGeometry(PixelInCell.CELL_CORNER, gridToCRS, env, GridRoundingMode.ENCLOSING);
        }
        final GridGeometryIterator it = new GridGeometryIterator(readGeom);
        while (it.hasNext()) {
            final GridGeometry sliceGeom = it.next();
            final GeneralEnvelope expectedSliceEnvelope = GeneralEnvelope.castOrCopy(sliceGeom.getEnvelope());
            GridCoverage sourceCvg = source.read(sliceGeom);
            if (sourceCvg instanceof GridCoverageStack) {
                // Try to unravel expected slice
                final Optional<GridCoverage> slice = extractSlice((GridCoverageStack) sourceCvg, sliceGeom.getEnvelope());
                if (slice.isPresent()) {
                    sourceCvg = slice.get();
                }
            }
            // If the reader has not returned a coverage fitting queried
            // geometry, we have to resample input ourselves.
            GridCoverage source2D = sourceCvg;
            source2D = source2D.forConvertedValues(true);
            final boolean compliantCrs = Utilities.equalsApproximately(expectedSliceEnvelope.getCoordinateReferenceSystem(), source2D.getCoordinateReferenceSystem());
            final boolean compliantEnvelope = expectedSliceEnvelope.contains(source2D.getGridGeometry().getEnvelope(), true);
            if (!(compliantCrs && compliantEnvelope)) {
                source2D = resample(source2D, sliceGeom);
            }
            final RenderedImage slice = categorize(source2D.render(null));
            final GridCoverageBuilder builder = new GridCoverageBuilder();
            builder.setDomain(source2D.getGridGeometry());
            builder.setValues(slice);
            final GridCoverage resultCoverage = builder.build();
            destination.write(resultCoverage);
        }
    } catch (TransformException ex) {
        throw new ProcessException("Cannot adapt input geometry", this, ex);
    } catch (FactoryException ex) {
        throw new ProcessException("Failure on EPSG database use", this, ex);
    } catch (DataStoreException ex) {
        throw new ProcessException("Cannot access either input or output data source", this, ex);
    } catch (CancellationException ex) {
        throw new DismissProcessException("Process cancelled", this, ex);
    }
}
Also used : GridGeometryIterator(org.geotoolkit.coverage.grid.GridGeometryIterator) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) DataStoreException(org.apache.sis.storage.DataStoreException) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.util.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) CoordinateOperation(org.opengis.referencing.operation.CoordinateOperation) GeographicBoundingBox(org.opengis.metadata.extent.GeographicBoundingBox) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) GridCoverageStack(org.geotoolkit.coverage.grid.GridCoverageStack) ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) CancellationException(java.util.concurrent.CancellationException) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) WritableGridCoverageResource(org.apache.sis.storage.WritableGridCoverageResource) WritableGridCoverageResource(org.apache.sis.storage.WritableGridCoverageResource) DismissProcessException(org.geotoolkit.process.DismissProcessException) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) RenderedImage(java.awt.image.RenderedImage)

Example 2 with DismissProcessException

use of org.geotoolkit.process.DismissProcessException in project geotoolkit by Geomatys.

the class WPS2Process method dismissProcess.

/**
 * Request to stop the process.<br>
 * This request has no effect if the process has not start or is already finished or canceled.
 */
@Override
public void dismissProcess() {
    if (isDimissed() || jobId == null)
        return;
    super.dismissProcess();
    // send a stop request
    final DismissRequest request = registry.getClient().createDismiss(jobId);
    request.setDebug(debug);
    request.setClientSecurity(security);
    try {
        checkResult(request.getResponse());
    } catch (JAXBException ex) {
        registry.getClient().getLogger().log(Level.WARNING, ex.getMessage(), ex);
    } catch (IOException ex) {
        registry.getClient().getLogger().log(Level.WARNING, ex.getMessage(), ex);
    } catch (InterruptedException ex) {
        registry.getClient().getLogger().log(Level.WARNING, ex.getMessage(), ex);
    } catch (DismissProcessException ex) {
    // do nothing, normal behaviour
    } catch (ProcessException ex) {
        registry.getClient().getLogger().log(Level.WARNING, ex.getMessage(), ex);
    }
}
Also used : ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) DismissRequest(org.geotoolkit.wps.client.DismissRequest) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) DismissProcessException(org.geotoolkit.process.DismissProcessException)

Example 3 with DismissProcessException

use of org.geotoolkit.process.DismissProcessException in project geotoolkit by Geomatys.

the class WPS2Process method execute.

@Override
protected void execute() throws ProcessException {
    final Result result;
    try {
        if (jobId != null) {
            try {
                // It's an already running process we've got here. All we have to do
                // is checking its status, to ensure/wait its completion.
                result = checkResult(getStatus());
            } catch (JAXBException | IOException ex) {
                throw new ProcessException("Cannot get process status", this, ex);
            }
        } else {
            final ExecuteRequest exec = createRequest();
            exec.setDebug(debug);
            exec.setClientSecurity(security);
            result = sendExecuteRequest(exec);
        }
    } catch (InterruptedException e) {
        throw new DismissProcessException("Process interrupted while executing", this, e);
    }
    if (!isDimissed()) {
        fillOutputs(result);
    }
}
Also used : ExecuteRequest(org.geotoolkit.wps.client.ExecuteRequest) ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) DismissProcessException(org.geotoolkit.process.DismissProcessException) Result(org.geotoolkit.wps.xml.v200.Result)

Example 4 with DismissProcessException

use of org.geotoolkit.process.DismissProcessException in project geotoolkit by Geomatys.

the class WPS2Process method checkResult.

/**
 * A Function to ensure response object is success or failure. Otherwise, we request continually status until
 * we reach a result.
 *
 * @param response The execute response given by service.
 */
private Result checkResult(Object response) throws IOException, JAXBException, InterruptedException, ProcessException {
    if (response instanceof ExceptionResponse) {
        final ExceptionResponse report = (ExceptionResponse) response;
        throw new ProcessException("Exception when executing the process.", this, report.toException());
    } else if (response instanceof StatusInfo) {
        final StatusInfo statusInfo = (StatusInfo) response;
        Status status = statusInfo.getStatus();
        jobId = statusInfo.getJobID();
        if (Status.SUCCEEDED.equals(status)) {
            fireProgressing("WPS remote process has been successfully executed", 100f, false);
            return null;
        } else if (Status.FAILED.equals(status)) {
            throw new ProcessException("Process failed", this);
        } else if (Status.DISMISS.equals(status)) {
            throw new DismissProcessException("WPS remote process has been canceled", this);
        } else if (Status.ACCEPTED.equals(status)) {
            // Initial status
            fireProgressing("Process accepted: " + jobId, 0, false);
        } else {
            // Running
            final Integer progress = statusInfo.getPercentCompleted();
            // Not in the standard
            String message = statusInfo.getMessage();
            if (message == null || (message = message.trim()).isEmpty()) {
                message = status.name();
            }
            fireProgressing(message, progress == null ? Float.NaN : progress, false);
        }
        // loop until we have an answer
        Object tmpResponse;
        // TODO : make timelapse configurable
        int timeLapse = 3000;
        // we tolerate a few unmarshalling or IO errors, the servers behave differentely
        // and may not offer the result file right from the start
        int failCount = 0;
        while (true) {
            stopIfDismissed();
            synchronized (this) {
                wait(timeLapse);
            }
            try {
                tmpResponse = getStatus();
                failCount = 0;
            } catch (UnmarshalException | IOException ex) {
                if (failCount < 5 && !isDimissed()) {
                    failCount++;
                    continue;
                } else if (isDimissed()) {
                    throw new DismissProcessException("WPS remote process has been canceled", this);
                } else {
                    // happenning so we consider the process failed
                    throw ex;
                }
            }
            if (tmpResponse instanceof StatusInfo) {
                final StatusInfo statInfo = (StatusInfo) tmpResponse;
                status = statInfo.getStatus();
                if (Status.SUCCEEDED.equals(status)) {
                    fireProgressing("WPS remote process has been successfully executed", 100f, false);
                    return null;
                } else if (Status.FAILED.equals(status)) {
                    throw new ProcessException("Process failed", this);
                } else if (Status.DISMISS.equals(status)) {
                    throw new DismissProcessException("WPS remote process has been canceled", this);
                }
                // Not in the standard
                String message = statusInfo.getMessage();
                if (message == null || (message = message.trim()).isEmpty()) {
                    message = status.name();
                }
                final Integer percentCompleted = statInfo.getPercentCompleted();
                if (!Objects.equals(message, lastMessage) || !Objects.equals(percentCompleted, lastProgress)) {
                    lastMessage = message;
                    lastProgress = percentCompleted;
                    fireProgressing(lastMessage, lastProgress, false);
                }
            } else if (tmpResponse instanceof ExceptionResponse) {
                final ExceptionResponse report = (ExceptionResponse) tmpResponse;
                throw new ProcessException("Exception when executing the process.", this, report.toException());
            }
        }
    } else if (response instanceof Result) {
        final Result result = checkLegacyResult((Result) response);
        if (result.getJobID() != null) {
            jobId = result.getJobID();
        }
        return result;
    } else {
        throw new ProcessException("Unexpected response " + response, this);
    }
}
Also used : Status(org.geotoolkit.wps.xml.v200.Status) LegacyStatus(org.geotoolkit.wps.xml.v100.LegacyStatus) ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) ExceptionResponse(org.geotoolkit.ows.xml.ExceptionResponse) StatusInfo(org.geotoolkit.wps.xml.v200.StatusInfo) DismissProcessException(org.geotoolkit.process.DismissProcessException) Result(org.geotoolkit.wps.xml.v200.Result)

Example 5 with DismissProcessException

use of org.geotoolkit.process.DismissProcessException in project geotoolkit by Geomatys.

the class WPS2Process method getStatus.

/**
 * Get current task status.<br>
 *
 * This method should not be called if process is not running.
 *
 * @return StatusInfo
 * @throws org.geotoolkit.process.ProcessException
 * @throws javax.xml.bind.UnmarshalException
 * @throws java.io.IOException
 */
private StatusInfo getStatus() throws ProcessException, JAXBException, IOException {
    if (jobId == null)
        throw new ProcessException("Process is not started.", this);
    if (isDimissed()) {
        throw new DismissProcessException("Process already dismissed", this);
    }
    final GetStatusRequest req = registry.getClient().createGetStatus(jobId);
    req.setDebug(debug);
    req.setClientSecurity(security);
    final Object response = req.getResponse();
    if (response instanceof ExceptionResponse) {
        final ExceptionResponse report = (ExceptionResponse) response;
        throw new ProcessException("Exception when executing the process.", this, report.toException());
    } else if (response instanceof StatusInfo) {
        return (StatusInfo) response;
    } else {
        throw new ProcessException("Unexpected response " + response.getClass().getName(), this);
    }
}
Also used : ProcessException(org.geotoolkit.process.ProcessException) DismissProcessException(org.geotoolkit.process.DismissProcessException) ExceptionResponse(org.geotoolkit.ows.xml.ExceptionResponse) GetStatusRequest(org.geotoolkit.wps.client.GetStatusRequest) StatusInfo(org.geotoolkit.wps.xml.v200.StatusInfo) DismissProcessException(org.geotoolkit.process.DismissProcessException)

Aggregations

DismissProcessException (org.geotoolkit.process.DismissProcessException)5 ProcessException (org.geotoolkit.process.ProcessException)5 IOException (java.io.IOException)2 JAXBException (javax.xml.bind.JAXBException)2 ExceptionResponse (org.geotoolkit.ows.xml.ExceptionResponse)2 Result (org.geotoolkit.wps.xml.v200.Result)2 StatusInfo (org.geotoolkit.wps.xml.v200.StatusInfo)2 RenderedImage (java.awt.image.RenderedImage)1 CancellationException (java.util.concurrent.CancellationException)1 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)1 GridCoverageBuilder (org.apache.sis.coverage.grid.GridCoverageBuilder)1 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)1 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)1 WritableGridCoverageResource (org.apache.sis.storage.WritableGridCoverageResource)1 GridCoverageStack (org.geotoolkit.coverage.grid.GridCoverageStack)1 GridGeometryIterator (org.geotoolkit.coverage.grid.GridGeometryIterator)1 DismissRequest (org.geotoolkit.wps.client.DismissRequest)1 ExecuteRequest (org.geotoolkit.wps.client.ExecuteRequest)1