Search in sources :

Example 16 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class CalculateLength 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 length = 0;
        for (GeometryProperty<?> geoProp : geoms) {
            length += geoProp.getGeometry().getLength();
        }
        return length;
    } else {
        geom = geoms.get(0).getGeometry();
    }
    if (geom != null) {
        return geom.getLength();
    } else {
        throw new TransformationException("Geometry for calculate length 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 17 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class Centroid 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 {
    // get input geometry
    PropertyValue input = variables.get(null).get(0);
    Object inputValue = input.getValue();
    GeometryProperty<?> result = calculateCentroid(inputValue);
    // try to yield a result compatible to the target
    TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
    // TODO check element type?
    Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
    if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getClass())) {
        return result.getGeometry();
    }
    return result;
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) Geometry(com.vividsolutions.jts.geom.Geometry) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 18 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class OrdinatesToPoint method evaluate.

/**
 * @see eu.esdihumboldt.hale.common.align.transformation.function.impl.AbstractPropertyTransformation#evaluate(java.lang.String,
 *      eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
 *      com.google.common.collect.ListMultimap,
 *      com.google.common.collect.ListMultimap, 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 {
    // get x, y and z properties
    PropertyValue x = variables.get("x").get(0);
    PropertyValue y = variables.get("y").get(0);
    PropertyValue z = null;
    if (!variables.get("z").isEmpty())
        z = variables.get("z").get(0);
    // get crs definition if srs is specified
    CRSDefinition crsDef = null;
    String srs = getOptionalParameter(PARAMETER_REFERENCE_SYSTEM, null).as(String.class);
    if (srs != null)
        crsDef = new CodeDefinition(srs, null);
    // convert values to double and create a point
    double xValue = x.getValueAs(Double.class);
    double yValue = y.getValueAs(Double.class);
    Point resultPoint;
    GeometryFactory geomFactory = new GeometryFactory();
    if (z == null)
        resultPoint = geomFactory.createPoint(new Coordinate(xValue, yValue));
    else
        resultPoint = geomFactory.createPoint(new Coordinate(xValue, yValue, z.getValueAs(Double.class)));
    // pack result into geometry property and return it
    GeometryProperty<Point> result = new DefaultGeometryProperty<Point>(crsDef, resultPoint);
    return result;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) Coordinate(com.vividsolutions.jts.geom.Coordinate) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Point(com.vividsolutions.jts.geom.Point)

Example 19 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class ReprojectGeometry 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 {
    // Get input geometry
    PropertyValue input = variables.get("source").get(0);
    Object inputValue = input.getValue();
    InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
    GeometryFinder geoFind = new GeometryFinder(null);
    traverser.traverse(inputValue, geoFind);
    List<GeometryProperty<?>> geoms = geoFind.getGeometries();
    Geometry sourceGeometry = geoms.get(0).getGeometry();
    CRSDefinition crsDef = geoms.get(0).getCRSDefinition();
    if (crsDef == null) {
        throw new TransformationException("Geometry does not have an associated Coordinate Reference System");
    }
    CoordinateReferenceSystem sourceCRS = crsDef.getCRS();
    Geometry resultGeometry = sourceGeometry;
    CoordinateReferenceSystem targetCRS = sourceCRS;
    // Get input parameter
    String srs = getParameterChecked(PARAMETER_REFERENCE_SYSTEM).as(String.class);
    if (srs != null) {
        try {
            targetCRS = parseReferenceSystemParamter(srs);
        } catch (Exception e) {
            throw new TransformationException("Error determining destination Cordinate Reference System.", e);
        }
        // Retrieve transformation from cell context, or create a new
        // instance
        Map<Object, Object> cellContext = getExecutionContext().getCellContext();
        MathTransform transform = getOrCreateMathTransform(sourceCRS, targetCRS, cellContext);
        // Apply transformation
        try {
            resultGeometry = JTS.transform(sourceGeometry, transform);
        } catch (MismatchedDimensionException | TransformException e) {
            throw new TransformationException("Problem on execute transformation from: " + sourceCRS + " to " + targetCRS, e);
        }
    }
    return new DefaultGeometryProperty<Geometry>(new CodeDefinition(CRS.toSRS(targetCRS), targetCRS), resultGeometry);
}
Also used : InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) 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) MathTransform(org.opengis.referencing.operation.MathTransform) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) TransformException(org.opengis.referencing.operation.TransformException) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) FactoryException(org.opengis.referencing.FactoryException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) TransformException(org.opengis.referencing.operation.TransformException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) Geometry(com.vividsolutions.jts.geom.Geometry) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 20 with PropertyValue

use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.

the class GroovyTransformation method createGroovyBinding.

/**
 * Create a Groovy binding from the list of variables.
 *
 * @param vars the variable values
 * @param varDefs definition of the assigned variables, in case some
 *            variable values are not set, may be <code>null</code>
 * @param cell the cell the binding is created for
 * @param typeCell the type cell the binding is created for, may be
 *            <code>null</code>
 * @param builder the instance builder for creating target instances, or
 *            <code>null</code> if not applicable
 * @param useInstanceVariables if instances should be used as variables for
 *            the binding instead of extracting the instance values
 * @param log the transformation log
 * @param context the execution context
 * @param targetInstanceType the type of the target instance
 * @return the binding for use with {@link GroovyShell}
 */
public static Binding createGroovyBinding(List<PropertyValue> vars, List<? extends Entity> varDefs, Cell cell, Cell typeCell, InstanceBuilder builder, boolean useInstanceVariables, TransformationLog log, ExecutionContext context, TypeDefinition targetInstanceType) {
    Binding binding = GroovyUtil.createBinding(builder, cell, typeCell, log, context, targetInstanceType);
    // collect definitions to check if all were provided
    Set<EntityDefinition> notDefined = new HashSet<>();
    if (varDefs != null) {
        for (Entity entity : varDefs) {
            notDefined.add(entity.getDefinition());
        }
    }
    // keep only defs where no value is provided
    if (!notDefined.isEmpty()) {
        for (PropertyValue var : vars) {
            notDefined.remove(var.getProperty());
        }
    }
    // add null value for missing variables
    if (!notDefined.isEmpty()) {
        vars = new ArrayList<>(vars);
        for (EntityDefinition entityDef : notDefined) {
            if (entityDef instanceof PropertyEntityDefinition) {
                vars.add(new PropertyValueImpl(null, (PropertyEntityDefinition) entityDef));
            }
        }
    }
    for (PropertyValue var : vars) {
        // add the variable to the environment
        addToBinding(binding, var.getProperty(), getUseValue(var.getValue(), useInstanceVariables));
    }
    return binding;
}
Also used : Binding(groovy.lang.Binding) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Entity(eu.esdihumboldt.hale.common.align.model.Entity) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) HashSet(java.util.HashSet) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)

Aggregations

PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)24 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)9 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)7 Geometry (com.vividsolutions.jts.geom.Geometry)6 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)5 ConversionException (org.springframework.core.convert.ConversionException)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 PropertyValueImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)4 Binding (groovy.lang.Binding)4 ArrayList (java.util.ArrayList)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 Value (eu.esdihumboldt.hale.common.core.io.Value)3 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)3 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)3 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)3 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)3 Environment (com.iabcinc.jmep.Environment)2 XExpression (com.iabcinc.jmep.XExpression)2 Constant (com.iabcinc.jmep.hooks.Constant)2