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);
}
}
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);
}
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);
}
}
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;
}
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);
}
Aggregations