Search in sources :

Example 1 with TransformationException

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;
}
Also used : Binding(groovy.lang.Binding) ParameterBinding(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.ParameterBinding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 2 with TransformationException

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.");
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)

Example 3 with TransformationException

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);
}
Also used : DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) Geometry(com.vividsolutions.jts.geom.Geometry) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)

Example 4 with TransformationException

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.");
}
Also used : ConversionException(org.springframework.core.convert.ConversionException) Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Geometry(com.vividsolutions.jts.geom.Geometry) ScriptException(javax.script.ScriptException) MathScript(eu.esdihumboldt.hale.common.scripting.scripts.mathematical.MathScript)

Example 5 with TransformationException

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);
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) HashMap(java.util.HashMap) SpatialJoinCondition(eu.esdihumboldt.cst.functions.geometric.join.SpatialJoinParameter.SpatialJoinCondition) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) LinkedList(java.util.LinkedList) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) ListMultimap(com.google.common.collect.ListMultimap) Multimap(com.google.common.collect.Multimap) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference)

Aggregations

TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)30 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)11 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)9 Geometry (com.vividsolutions.jts.geom.Geometry)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)8 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)7 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)7 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)7 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)5 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)4 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)4 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)4 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)4 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)4 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)4 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)4 Binding (groovy.lang.Binding)4 Script (groovy.lang.Script)4