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