Search in sources :

Example 66 with ProcessException

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

the class BoundaryProcess method execute.

@Override
protected void execute() throws ProcessException {
    try {
        final Geometry geom = inputParameters.getValue(GEOM);
        final CoordinateReferenceSystem geomCRS = JTS.findCoordinateReferenceSystem(geom);
        final Geometry result = geom.getBoundary();
        JTS.setCRS(result, geomCRS);
        outputParameters.getOrCreate(RESULT_GEOM).setValue(result);
    } catch (NoSuchAuthorityCodeException ex) {
        throw new ProcessException(ex.getMessage(), this, ex);
    } catch (FactoryException ex) {
        throw new ProcessException(ex.getMessage(), this, ex);
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) ProcessException(org.geotoolkit.process.ProcessException) FactoryException(org.opengis.util.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 67 with ProcessException

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

the class SampleClassifier method buildClasses.

// public int getBand() {
// return inputParameters.intValue(SampleClassifierDescriptor.BAND);
// }
// 
// public void setBand(int bandIdx) {
// inputParameters.getOrCreate(SampleClassifierDescriptor.BAND).setValue(bandIdx);
// }
private ClassMap buildClasses() throws ProcessException {
    final List<ParameterValueGroup> classes = inputParameters.groups(SampleClassifierDescriptor.CATEGORIES.getName().getCode());
    if (classes == null || classes.isEmpty()) {
        throw new ProcessException("No class provided", this);
    }
    final byte fillValue = getFillValue();
    final List<Map.Entry<NumberRange<Float>, Byte>> sortedClasses = classes.stream().map(Parameters::castOrWrap).map(p -> {
        final NumberRange<Float> nr = new NumberRange<>(Float.class, p.getMandatoryValue(SampleClassifierDescriptor.MIN), true, p.getMandatoryValue(SampleClassifierDescriptor.MAX), false);
        final Byte classValue = p.getMandatoryValue(SampleClassifierDescriptor.CLASS_VALUE);
        return new AbstractMap.SimpleImmutableEntry<>(nr, classValue);
    }).sorted((o1, o2) -> o1.getKey().getMinValue().compareTo(o2.getKey().getMinValue())).collect(Collectors.toList());
    final List<Float> intervals = new ArrayList<>();
    final List<Byte> classValues = new ArrayList<>();
    float previousMax = Float.NEGATIVE_INFINITY;
    for (final Map.Entry<NumberRange<Float>, Byte> entry : sortedClasses) {
        final float minVal = entry.getKey().getMinValue();
        if (minVal > previousMax) {
            // Disjoint intervals. We fill the hole with a no-data category.
            intervals.add(previousMax);
            classValues.add(fillValue);
        }
        intervals.add(minVal);
        classValues.add(entry.getValue());
        previousMax = entry.getKey().getMaxValue();
    }
    intervals.add(previousMax);
    if (!Float.isInfinite(previousMax)) {
        intervals.add(Float.POSITIVE_INFINITY);
        classValues.add(fillValue);
    }
    final float[] pIntervals = new float[intervals.size()];
    for (int i = 0; i < pIntervals.length; i++) {
        pIntervals[i] = intervals.get(i);
    }
    final byte[] pClasses = new byte[classValues.size()];
    for (int i = 0; i < pClasses.length; i++) {
        pClasses[i] = classValues.get(i);
    }
    return new ClassMap(pIntervals, pClasses, fillValue);
}
Also used : Rectangle(java.awt.Rectangle) Arrays(java.util.Arrays) PixelIterator(org.apache.sis.image.PixelIterator) Parameters(org.apache.sis.parameter.Parameters) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) NumberRange(org.apache.sis.measure.NumberRange) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) Point(java.awt.Point) UnaryOperator(java.util.function.UnaryOperator) Collectors(java.util.stream.Collectors) AbstractProcess(org.geotoolkit.processing.AbstractProcess) ArrayList(java.util.ArrayList) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Map(java.util.Map) Raster(java.awt.image.Raster) WritableRenderedImage(java.awt.image.WritableRenderedImage) ProcessException(org.geotoolkit.process.ProcessException) WritablePixelIterator(org.apache.sis.image.WritablePixelIterator) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ArrayList(java.util.ArrayList) Point(java.awt.Point) NumberRange(org.apache.sis.measure.NumberRange) ProcessException(org.geotoolkit.process.ProcessException) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 68 with ProcessException

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

the class PredictorTest method sampleDrift.

@Test
public void sampleDrift() throws Exception {
    final Parameters input = Parameters.castOrWrap(DESCRIPTOR.getInputDescriptor().createValue());
    input.getOrCreate(PredictorDescriptor.MAX_POINTS).setValue(200);
    addWeight(input, 0.5f, 0.5f, 0.5f);
    addWeight(input, 0.2f, 0.8f, 0.8f);
    input.getOrCreate(PredictorDescriptor.TARGET_RESOLUTION).setValue(1);
    input.getOrCreate(PredictorDescriptor.TARGET_WIDTH).setValue(256);
    input.getOrCreate(PredictorDescriptor.TARGET_HEIGHT).setValue(256);
    input.getOrCreate(PredictorDescriptor.TIMESTEP).setValue(1);
    input.getOrCreate(PredictorDescriptor.START_TIMESTAMP).setValue(0);
    final long expectedEndTime = 7000;
    input.getOrCreate(PredictorDescriptor.END_TIMESTAMP).setValue(expectedEndTime);
    input.getOrCreate(PredictorDescriptor.START_POINT).setValue(new DirectPosition2D(CommonCRS.defaultGeographic(), 0.1, 0.2));
    input.getOrCreate(PredictorDescriptor.WIND_RESOURCE).setValue(new MemoryGridResource(null, MOCK_UV_DATA));
    input.getOrCreate(PredictorDescriptor.CURRENT_RESOURCE).setValue(new MemoryGridResource(null, MOCK_UV_DATA));
    final Predictor predictor = new Predictor(DESCRIPTOR, input);
    predictor.addListener(new ProcessListenerAdapter() {

        @Override
        public void progressing(ProcessEvent event) {
            final Exception error = event.getException();
            if (error != null) {
                Utilities.LOGGER.log(Level.WARNING, event.getTask().toString(), error);
            } else {
                Utilities.LOGGER.log(Level.INFO, event.getTask().toString());
            }
        }
    });
    Utilities.LOGGER.info("Starting drift processing");
    final Parameters output = CompletableFuture.supplyAsync(() -> {
        try {
            return predictor.call();
        } catch (ProcessException ex) {
            throw new RuntimeException(ex);
        }
    }).thenApply(Parameters::castOrWrap).get(30, TimeUnit.SECONDS);
    final long outTime = output.getMandatoryValue(PredictorDescriptor.ACTUAL_END_TIMESTAMP);
    final Path netcdf = output.getMandatoryValue(PredictorDescriptor.OUTPUT_DATA);
    try {
        assertEquals("Expected time of ending", expectedEndTime, outTime);
        assertNotNull("Output file path", netcdf);
        assertTrue("Output file is not readable", Files.isRegularFile(netcdf));
        checkContent(netcdf);
    } finally {
        Files.delete(netcdf);
    }
}
Also used : Path(java.nio.file.Path) ProcessException(org.geotoolkit.process.ProcessException) Parameters(org.apache.sis.parameter.Parameters) ProcessEvent(org.geotoolkit.process.ProcessEvent) MemoryGridResource(org.apache.sis.internal.storage.MemoryGridResource) ProcessListenerAdapter(org.geotoolkit.processing.ProcessListenerAdapter) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) Test(org.junit.Test)

Example 69 with ProcessException

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

the class SpatialJoinProcess method join.

/**
 * This function join target Feature with another Feature form a source FeatureCollection.
 *
 * If boolean <code>method</code> is true, the method used is Intersect, else it's Nearest.
 *
 * If there is no Feature which Intersect the target Geometry, the return Feature
 * will have "joined attributes" set to null.
 *
 * If there is more than one result for Nearest method
 * (many Feature at the same distance), we use the first returned.
 *
 * If there is more than one result for Intersect method , we use the Feature
 * with the biggest intersection area with target Geometry.
 *
 * @param target the target Feature
 * @param newType the concatenated FeatureType
 * @param sourceFC the source FeatureCollection
 * @param method the used method. True -> Intersect, False -> Nearest
 * @return the joined feature
 */
static Feature join(final Feature target, final FeatureType newType, final FeatureSet sourceFC, final boolean method) throws DataStoreException {
    Feature resultFeature = newType.newInstance();
    FeatureExt.setId(resultFeature, FeatureExt.getId(target));
    // copy target Feature
    for (final PropertyType targetProperty : target.getType().getProperties(true)) {
        if (targetProperty instanceof AttributeType && !AttributeConvention.contains(targetProperty.getName())) {
            final String name = targetProperty.getName().toString();
            resultFeature.setPropertyValue(name, target.getPropertyValue(name));
        }
    }
    ProcessDescriptor desc;
    org.geotoolkit.process.Process proc;
    Parameters in;
    ArrayList<Feature> featureOutArray;
    // for each target feature geometry
    for (final PropertyType property : target.getType().getProperties(true)) {
        if (AttributeConvention.isGeometryAttribute(property)) {
            final Geometry targetGeometry = (Geometry) target.getPropertyValue(property.getName().toString());
            final CoordinateReferenceSystem geomCRS = FeatureExt.getCRS(property);
            // add CRS to the used data geometry
            JTS.setCRS(targetGeometry, geomCRS);
            // use intersect method
            if (method) {
                desc = IntersectDescriptor.INSTANCE;
                in = Parameters.castOrWrap(desc.getInputDescriptor().createValue());
                in.getOrCreate(IntersectDescriptor.FEATURESET_IN).setValue(sourceFC);
                in.getOrCreate(IntersectDescriptor.GEOMETRY_IN).setValue(targetGeometry);
                proc = desc.createProcess(in);
            } else {
                // use nearest method
                desc = NearestDescriptor.INSTANCE;
                in = Parameters.castOrWrap(desc.getInputDescriptor().createValue());
                in.getOrCreate(NearestDescriptor.FEATURESET_IN).setValue(sourceFC);
                in.getOrCreate(NearestDescriptor.GEOMETRY_IN).setValue(targetGeometry);
                proc = desc.createProcess(in);
            }
            // run it
            final FeatureSet featureOut;
            try {
                featureOut = (FeatureSet) proc.call().parameter("feature_out").getValue();
            } catch (ProcessException ex) {
                Logger.getLogger("org.geotoolkit.processing.vector.spatialjoin").log(Level.WARNING, null, ex);
                return null;
            }
            try (final Stream<Feature> outputFeatures = featureOut.features(false)) {
                featureOutArray = new ArrayList<>(outputFeatures.collect(Collectors.toList()));
            }
            if (method) {
                if (featureOutArray.isEmpty()) {
                    // no intersection
                    return resultFeature;
                } else {
                    if (featureOutArray.size() > 1) {
                        // more than one intersection
                        final Feature biggestFeature = biggestIntersection(featureOut, targetGeometry);
                        resultFeature = copyAttributes(target, biggestFeature, newType);
                    } else {
                        // only one intersection
                        resultFeature = copyAttributes(target, featureOutArray.get(0), newType);
                    }
                }
            } else {
                // nearest method
                if (featureOutArray.isEmpty()) {
                    return resultFeature;
                } else {
                    resultFeature = copyAttributes(target, featureOutArray.get(0), newType);
                }
            }
        }
    }
    return resultFeature;
}
Also used : Parameters(org.apache.sis.parameter.Parameters) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) Geometry(org.locationtech.jts.geom.Geometry) ProcessException(org.geotoolkit.process.ProcessException) AttributeType(org.opengis.feature.AttributeType) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) FeatureSet(org.apache.sis.storage.FeatureSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 70 with ProcessException

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

the class SpatialJoinProcess method execute.

/**
 *  {@inheritDoc }
 */
@Override
protected void execute() throws ProcessException {
    final FeatureSet sourceFeatureList = inputParameters.getValue(VectorDescriptor.FEATURESET_IN);
    final FeatureSet targetFeatureList = inputParameters.getValue(SpatialJoinDescriptor.FEATURE_TARGET);
    final boolean method = inputParameters.getValue(SpatialJoinDescriptor.INTERSECT);
    final FeatureSet resultFeatureList;
    try {
        resultFeatureList = new SpatialJoinFeatureCollection(sourceFeatureList, targetFeatureList, method);
    } catch (DataStoreException ex) {
        throw new ProcessException(ex.getMessage(), this);
    }
    outputParameters.getOrCreate(VectorDescriptor.FEATURESET_OUT).setValue(resultFeatureList);
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) ProcessException(org.geotoolkit.process.ProcessException) FeatureSet(org.apache.sis.storage.FeatureSet)

Aggregations

ProcessException (org.geotoolkit.process.ProcessException)70 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)27 Geometry (org.locationtech.jts.geom.Geometry)26 FactoryException (org.opengis.util.FactoryException)24 TransformException (org.opengis.referencing.operation.TransformException)21 RenderedImage (java.awt.image.RenderedImage)15 DataStoreException (org.apache.sis.storage.DataStoreException)13 IOException (java.io.IOException)12 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)12 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)11 ProcessDescriptor (org.geotoolkit.process.ProcessDescriptor)11 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)11 BufferedImage (java.awt.image.BufferedImage)10 ArrayList (java.util.ArrayList)10 PixelIterator (org.apache.sis.image.PixelIterator)9 List (java.util.List)8 Parameters (org.apache.sis.parameter.Parameters)8 DismissProcessException (org.geotoolkit.process.DismissProcessException)8 WritablePixelIterator (org.apache.sis.image.WritablePixelIterator)7 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)7