use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class CustomGroovyTransformation method evaluate.
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
// store script as parameter
if (!CustomPropertyFunctionType.GROOVY.equals(customFunction.getFunctionType())) {
throw new TransformationException("Custom function is not of type groovy");
}
Value scriptValue = customFunction.getFunctionDefinition();
if (scriptValue.isEmpty()) {
throw new NoResultException("Script not defined");
}
ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
params.put(GroovyTransformation.PARAMETER_SCRIPT, new ParameterValue(scriptValue));
setParameters(params);
// instance builder
InstanceBuilder builder = GroovyTransformation.createBuilder(resultProperty);
// create the script binding
Binding binding = createGroovyBinding(variables, getCell(), getTypeCell(), builder, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
Object result;
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script groovyScript = GroovyUtil.getScript(this, binding, service, true);
// evaluate the script
result = GroovyTransformation.evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
} catch (TransformationException | NoResultException e) {
throw e;
} catch (Throwable e) {
throw new TransformationException("Error evaluating the custom function script", e);
}
if (result == null) {
throw new NoResultException();
}
return result;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class CalculateArea method evaluate.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.impl.AbstractSingleTargetPropertyTransformation#evaluate(java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.lang.String,
* eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition,
* java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
// get input geometry
PropertyValue input = variables.get(null).get(0);
Object inputValue = input.getValue();
// depth first traverser that on cancel continues traversal but w/o the
// children of the current object
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(inputValue, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry geom = null;
if (geoms.size() > 1) {
int area = 0;
for (GeometryProperty<?> geoProp : geoms) {
area += geoProp.getGeometry().getArea();
}
return area;
} else {
geom = geoms.get(0).getGeometry();
}
if (geom != null) {
return geom.getArea();
} else {
throw new TransformationException("Geometry for calculate area could not be retrieved.");
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class Centroid method calculateCentroid.
/**
* Calculate the centroid for a given geometry or object holding a geometry.
*
* @param geometryHolder {@link Geometry}, {@link GeometryProperty} or
* {@link Instance} holding a geometry
* @return the centroid of the geometry
* @throws TransformationException if the no geometry could be extracted
* from the input
*/
public static GeometryProperty<?> calculateCentroid(Object geometryHolder) throws TransformationException {
// depth first traverser that on cancel continues traversal but w/o the
// children of the current object
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(geometryHolder, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry result;
CRSDefinition oldCRS = null;
if (geoms.size() > 1) {
// multiple geometries -> create a multi point
// XXX is this the desired behavior?
Point[] centroids = new Point[geoms.size()];
GeometryFactory geomFactory = new GeometryFactory();
for (int i = 0; i < geoms.size(); i++) {
GeometryProperty<?> prop = geoms.get(i);
centroids[i] = prop.getGeometry().getCentroid();
if (oldCRS == null) {
// assume the same CRS for all points
oldCRS = prop.getCRSDefinition();
}
}
result = geomFactory.createMultiPoint(centroids);
} else {
Geometry geom = geoms.get(0).getGeometry();
oldCRS = geoms.get(0).getCRSDefinition();
if (geom != null) {
result = geom.getCentroid();
} else {
throw new TransformationException("Geometry for centroid could not be retrieved.");
}
}
return new DefaultGeometryProperty<Geometry>(oldCRS, result);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class NetworkExpansion method evaluate.
/**
* @see AbstractSingleTargetScriptedPropertyTransformation#evaluate(String,
* TransformationEngine, ListMultimap, String,
* PropertyEntityDefinition, Map, TransformationLog)
*/
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
// get the buffer width parameter
String bufferWidthString = getTransformedParameterChecked(PARAMETER_BUFFER_WIDTH).as(String.class);
double bufferWidth;
try {
bufferWidth = Double.parseDouble(bufferWidthString);
} catch (NumberFormatException e) {
// For backwards compatibility try to run the string as script.
MathScript mathScript = new MathScript();
try {
Object result = mathScript.evaluate(bufferWidthString, variables.get(ENTITY_VARIABLE), getExecutionContext());
bufferWidth = ConversionUtil.getAs(result, Double.class);
} catch (ScriptException e1) {
throw new TransformationException("Failed to evaluate buffer width expression.", e1);
} catch (ConversionException e2) {
throw new TransformationException("Failed to convert buffer width expression result to double.", e2);
}
}
// get input geometry
PropertyValue input = variables.get(null).get(0);
Object inputValue = input.getValue();
if (inputValue instanceof Instance) {
inputValue = ((Instance) inputValue).getValue();
}
GeometryProperty<Geometry> result = calculateBuffer(inputValue, bufferWidth, log);
// try to yield a result compatible to the target
if (result != null) {
TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
// TODO check element type?
Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getGeometry().getClass())) {
return result.getGeometry();
} else {
return result;
}
}
throw new TransformationException("Geometry for network expansion could not be retrieved.");
}
use of eu.esdihumboldt.hale.common.align.transformation.function.TransformationException in project hale by halestudio.
the class SpatialJoinHandler method partitionInstances.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
* java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_SPATIAL_JOIN) || transformationParameters.get(PARAMETER_SPATIAL_JOIN).isEmpty()) {
throw new TransformationException("No join parameter defined");
}
if (services == null) {
throw new IllegalStateException("ServiceProvider must be set before calling partitionInstances");
}
SpatialJoinParameter joinParameter = transformationParameters.get(PARAMETER_SPATIAL_JOIN).get(0).as(SpatialJoinParameter.class);
String validation = joinParameter.validate();
if (validation != null) {
throw new TransformationException("Spatial Join parameter invalid: " + validation);
}
List<TypeEntityDefinition> types = joinParameter.types;
// ChildType -> DirectParentType
int[] directParent = new int[joinParameter.types.size()];
// ChildType -> (ParentType -> Collection<JoinCondition>)
Map<Integer, Multimap<Integer, SpatialJoinCondition>> joinTable = new HashMap<>();
for (SpatialJoinCondition condition : joinParameter.conditions) {
int baseTypeIndex = types.indexOf(AlignmentUtil.getTypeEntity(condition.baseProperty));
int joinTypeIndex = types.indexOf(AlignmentUtil.getTypeEntity(condition.joinProperty));
Multimap<Integer, SpatialJoinCondition> typeTable = joinTable.get(joinTypeIndex);
if (typeTable == null) {
typeTable = ArrayListMultimap.create(2, 2);
joinTable.put(joinTypeIndex, typeTable);
}
typeTable.put(baseTypeIndex, condition);
// update highest type if necessary
if (directParent[joinTypeIndex] < baseTypeIndex) {
directParent[joinTypeIndex] = baseTypeIndex;
}
}
// remember instances of first type to start join afterwards
Collection<InstanceReference> startInstances = new LinkedList<InstanceReference>();
// iterate once over all instances
ResourceIterator<Instance> iterator = instances.iterator();
try {
while (iterator.hasNext()) {
Instance next = iterator.next();
// remember instances of first type
if (next.getDefinition().equals(types.get(0).getDefinition())) {
startInstances.add(instances.getReference(next));
}
}
} finally {
iterator.close();
}
return new SpatialJoinIterator(instances, startInstances, directParent, services, joinTable);
}
Aggregations