use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup in project hale by halestudio.
the class InstanceBuilder method getValue.
/**
* Get the value for a target node.
*
* @param node the target node
* @param typeLog the type transformation log
* @return the value or {@link NoObject#NONE} representing no value
*/
private Object getValue(TargetNode node, TransformationLog typeLog) {
if (node.getChildren(true).isEmpty()) {
// simple leaf
if (node.isDefined()) {
// XXX this is done in FunctionExecutor
return node.getResult();
} else {
return NoObject.NONE;
}
}
boolean isProperty = node.getDefinition().asProperty() != null;
boolean isGroup = node.getDefinition().asGroup() != null;
if (isProperty && node.isDefined()) {
// it's a property and we have a value/values
Object nodeValue = node.getResult();
if (!(nodeValue instanceof MultiValue)) {
// pack single value into multivalue
MultiValue nodeMultiValue = new MultiValue();
nodeMultiValue.add(nodeValue);
nodeValue = nodeMultiValue;
}
MultiValue nodeMultiValue = (MultiValue) nodeValue;
if (!nodeMultiValue.isEmpty()) {
// Create n instances
MultiValue resultMultiValue = new MultiValue(nodeMultiValue.size());
for (Object value : nodeMultiValue) {
// the value may have been wrapped in an Instance
if (value instanceof Instance) {
value = ((Instance) value).getValue();
}
MutableInstance instance = new DefaultInstance(node.getDefinition().asProperty().getPropertyType(), null);
instance.setValue(value);
// XXX since this is the same for all instances maybe do
// this on a dummy and only copy properties for each?
// XXX MultiValue w/ target node children => strange results
populateGroup(instance, node, typeLog);
resultMultiValue.add(instance);
}
return resultMultiValue;
}
// if nodeMultiValue is empty fall through to below
// it the instance could still have children even without a value
}
// it's a property or group with no value
MutableGroup group;
if (isGroup) {
group = new DefaultGroup(node.getDefinition().asGroup());
} else if (isProperty) {
group = new DefaultInstance(node.getDefinition().asProperty().getPropertyType(), null);
} else {
throw new IllegalStateException("Illegal child definition");
}
// populate with children
if (populateGroup(group, node, typeLog)) {
return group;
} else {
return NoObject.NONE;
}
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup in project hale by halestudio.
the class GroupPath method createChildGroups.
/**
* Create groups for the children in the path (which are only represented as
* definitions). May only be called if the path is valid. This will also
* update the path to include the groups instead of the definitions.
*
* @return the list of created groups
*
* @see #isValid()
*/
protected List<MutableGroup> createChildGroups() {
MutableGroup parent = parents.get(parents.size() - 1);
final List<MutableGroup> result = new ArrayList<MutableGroup>();
for (DefinitionGroup child : children) {
checkState(child instanceof GroupPropertyDefinition);
// create group
MutableGroup group = new DefaultGroup(child);
// add to parent
QName propertyName = ((GroupPropertyDefinition) child).getName();
parent.addProperty(propertyName, group);
// add to result
result.add(group);
// prepare for next iteration
parent = group;
}
// update children and parents
children.clear();
parents.addAll(result);
return result;
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup in project hale by halestudio.
the class Rename method structuralRename.
/**
* Performs a structural rename on the given source object to the given
* target definition.
*
* @param source the source value (or group/instance)
* @param targetDefinition the target definition
* @param allowIgnoreNamespaces if for the structure comparison, namespaces
* may be ignored
* @param instanceFactory the instance factory
* @param copyGeometries specifies if geometry objects should be copied
* @return the transformed value (or group/instance) or NO_MATCH
*/
public static Object structuralRename(Object source, ChildDefinition<?> targetDefinition, boolean allowIgnoreNamespaces, InstanceFactory instanceFactory, boolean copyGeometries) {
if (!(source instanceof Group)) {
// source simple value
if (targetDefinition.asProperty() != null) {
// target can have value
TypeDefinition propertyType = targetDefinition.asProperty().getPropertyType();
if (copyGeometries || !isGeometry(source)) {
if (propertyType.getChildren().isEmpty()) {
// simple value
return convertValue(source, targetDefinition.asProperty().getPropertyType());
} else {
// instance with value
MutableInstance instance = instanceFactory.createInstance(propertyType);
instance.setDataSet(DataSet.TRANSFORMED);
instance.setValue(convertValue(source, propertyType));
return instance;
}
} else {
return Result.NO_MATCH;
}
}
}
// source is group or instance
if (targetDefinition.asProperty() != null) {
// target can have value
TypeDefinition propertyType = targetDefinition.asProperty().getPropertyType();
if (source instanceof Instance) {
// source has value
if (propertyType.getChildren().isEmpty()) {
// simple value
return convertValue(((Instance) source).getValue(), targetDefinition.asProperty().getPropertyType());
} else {
// instance with value
MutableInstance instance = instanceFactory.createInstance(targetDefinition.asProperty().getPropertyType());
instance.setDataSet(DataSet.TRANSFORMED);
if (copyGeometries || !isGeometry(((Instance) source).getValue())) {
instance.setValue(convertValue(((Instance) source).getValue(), targetDefinition.asProperty().getPropertyType()));
}
renameChildren((Group) source, instance, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries);
return instance;
}
} else {
// source has no value
if (targetDefinition.asProperty().getPropertyType().getChildren().isEmpty())
// no match possible
return Result.NO_MATCH;
else {
// instance with no value set
MutableInstance instance = instanceFactory.createInstance(targetDefinition.asProperty().getPropertyType());
instance.setDataSet(DataSet.TRANSFORMED);
if (renameChildren((Group) source, instance, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries))
return instance;
else
// no child matched and no value
return Result.NO_MATCH;
}
}
} else if (targetDefinition.asGroup() != null) {
// target can not have a value
if (targetDefinition.asGroup().getDeclaredChildren().isEmpty())
// target neither has a value nor
return Result.NO_MATCH;
else // children?
{
// group
MutableGroup group = new DefaultGroup(targetDefinition.asGroup());
if (renameChildren((Group) source, group, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries))
return group;
else
// no child matched and no value
return Result.NO_MATCH;
}
} else {
// neither asProperty nor asGroup -> illegal ChildDefinition
throw new IllegalStateException("Illegal child type.");
}
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup in project hale by halestudio.
the class ReprojectGeometryTest method testReproject.
@SuppressWarnings("javadoc")
@Test
public void testReproject() throws Exception {
TestData tr = new TestData(TestDataConfiguration.REPROJECT);
List<Instance> result = transformData(tr);
assertTrue(result.size() > 0);
Geometry aspectedGeometry = null;
InstanceCollection sourceInstances = tr.getSourceInstances();
Iterator<Instance> sit = sourceInstances.iterator();
if (sit.hasNext()) {
Instance i = sit.next();
DefaultInstance di = (DefaultInstance) (i.getProperty(new QName("eu:esdihumboldt:hale:test", "geometry"))[0]);
DefaultGroup dg = (DefaultGroup) (di.getProperty(new QName("http://www.opengis.net/gml/_Geometry", "choice"))[0]);
DefaultInstance dig = (DefaultInstance) (dg.getProperty(new QName("http://www.opengis.net/gml", "Point"))[0]);
DefaultGeometryProperty<?> value = (DefaultGeometryProperty<?>) dig.getValue();
DefaultGeometryProperty<?> geom = value;
MathTransform transform = CRS.findMathTransform(geom.getCRSDefinition().getCRS(), CRS.decode("EPSG:4326"), false);
aspectedGeometry = JTS.transform(geom.getGeometry(), transform);
}
assertNotNull(aspectedGeometry);
Instance resultInstance = result.get(0);
DefaultGeometryProperty<?> geom = (DefaultGeometryProperty<?>) ((DefaultInstance) resultInstance.getProperty(new QName("eu:esdihumboldt:hale:test", "geometry"))[0]).getValue();
String code = CRS.lookupIdentifier(geom.getCRSDefinition().getCRS(), true);
assertEquals("EPSG:4326", code);
assertEquals(aspectedGeometry.getCoordinate().x, geom.getGeometry().getCoordinate().x, 0);
assertEquals(aspectedGeometry.getCoordinate().y, geom.getGeometry().getCoordinate().y, 0);
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup in project hale by halestudio.
the class StreamGmlWriterTest method fillFeatureTest.
/**
* Create a feature, fill it with values, write it as GML, validate the GML
* and load the GML file again to compare the loaded values with the ones
* that were written
*
* @param elementName the element name of the feature type to use, if
* <code>null</code> a random element will be used
* @param targetSchema the schema to use, the first element will be used for
* the type of the feature
* @param values the values to set on the feature
* @param testName the name of the test
* @param srsName the SRS name
* @param skipValueTest if the check for equality shall be skipped
* @param expectWriteFail if the GML writing is expected to fail
* @param windingOrderParam winding order parameter or <code>null</code>
* @return the validation report or the GML writing report if writing
* expected to fail
* @throws Exception if any error occurs
*/
private IOReport fillFeatureTest(String elementName, URI targetSchema, Map<List<QName>, Object> values, String testName, String srsName, boolean skipValueTest, boolean expectWriteFail, EnumWindingOrderTypes windingOrderParam) throws Exception {
// load the sample schema
XmlSchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(null);
reader.setSource(new DefaultInputSupplier(targetSchema));
IOReport schemaReport = reader.execute(null);
assertTrue(schemaReport.isSuccess());
XmlIndex schema = reader.getSchema();
XmlElement element = null;
if (elementName == null) {
element = schema.getElements().values().iterator().next();
if (element == null) {
// $NON-NLS-1$
fail("No element found in the schema");
}
} else {
for (XmlElement candidate : schema.getElements().values()) {
if (candidate.getName().getLocalPart().equals(elementName)) {
element = candidate;
break;
}
}
if (element == null) {
// $NON-NLS-1$ //$NON-NLS-2$
fail("Element " + elementName + " not found in the schema");
}
}
if (element == null) {
throw new IllegalStateException();
}
// create feature
MutableInstance feature = new DefaultInstance(element.getType(), null);
// set some values
for (Entry<List<QName>, Object> entry : values.entrySet()) {
MutableGroup parent = feature;
List<QName> properties = entry.getKey();
for (int i = 0; i < properties.size() - 1; i++) {
QName propertyName = properties.get(i);
DefinitionGroup def = parent.getDefinition();
Object[] vals = parent.getProperty(propertyName);
if (vals != null && vals.length > 0) {
Object value = vals[0];
if (value instanceof MutableGroup) {
parent = (MutableGroup) value;
} else {
MutableGroup child;
ChildDefinition<?> childDef = def.getChild(propertyName);
if (childDef.asProperty() != null || value != null) {
// create instance
child = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
} else {
// create group
child = new DefaultGroup(childDef.asGroup());
}
if (value != null) {
// wrap value
((MutableInstance) child).setValue(value);
}
parent = child;
}
}
}
parent.addProperty(properties.get(properties.size() - 1), entry.getValue());
}
InstanceCollection instances = new DefaultInstanceCollection(Collections.singleton(feature));
// write to file
InstanceWriter writer = new GmlInstanceWriter();
if (windingOrderParam != null) {
writer.setParameter(GeoInstanceWriter.PARAM_UNIFY_WINDING_ORDER, Value.of(windingOrderParam));
}
writer.setInstances(instances);
DefaultSchemaSpace schemaSpace = new DefaultSchemaSpace();
schemaSpace.addSchema(schema);
writer.setTargetSchema(schemaSpace);
// $NON-NLS-1$
File outFile = File.createTempFile(testName, ".gml");
writer.setTarget(new FileIOSupplier(outFile));
if (windingOrderParam != null && windingOrderParam == EnumWindingOrderTypes.counterClockwise) {
assertTrue(writer.getParameter(GeoInstanceWriter.PARAM_UNIFY_WINDING_ORDER).as(EnumWindingOrderTypes.class) == EnumWindingOrderTypes.counterClockwise);
}
// new LogProgressIndicator());
IOReport report = writer.execute(null);
if (expectWriteFail) {
assertFalse("Writing the GML output should not be successful", report.isSuccess());
return report;
} else {
assertTrue("Writing the GML output not successful", report.isSuccess());
}
List<? extends Locatable> validationSchemas = writer.getValidationSchemas();
System.out.println(outFile.getAbsolutePath());
System.out.println(targetSchema.toString());
// if (!DEL_TEMP_FILES && Desktop.isDesktopSupported()) {
// Desktop.getDesktop().open(outFile);
// }
IOReport valReport = validate(outFile.toURI(), validationSchemas);
// load file
InstanceCollection loaded = loadGML(outFile.toURI(), schema);
ResourceIterator<Instance> it = loaded.iterator();
try {
assertTrue(it.hasNext());
if (!skipValueTest) {
Instance l = it.next();
// test values
for (Entry<List<QName>, Object> entry : values.entrySet()) {
// XXX conversion?
Object expected = entry.getValue();
// String propertyPath = Joiner.on('.').join(Collections2.transform(entry.getKey(), new Function<QName, String>() {
//
// @Override
// public String apply(QName input) {
// return input.toString();
// }
// }));
// Collection<Object> propValues = PropertyResolver.getValues(
// l, propertyPath, true);
// assertEquals(1, propValues.size());
// Object value = propValues.iterator().next();
Collection<GeometryProperty<?>> geoms = GeometryUtil.getAllGeometries(l);
assertEquals(1, geoms.size());
Object value = geoms.iterator().next().getGeometry();
if (expected instanceof Geometry && value instanceof Geometry) {
if (windingOrderParam == null || windingOrderParam == EnumWindingOrderTypes.noChanges) {
matchGeometries((Geometry) expected, (Geometry) value);
}
// Winding Order Test.
if (windingOrderParam != null) {
if (windingOrderParam == EnumWindingOrderTypes.counterClockwise) {
assertTrue(((Geometry) expected).getNumGeometries() == ((Geometry) value).getNumGeometries());
assertTrue(WindingOrder.isCounterClockwise((Geometry) value));
} else if (windingOrderParam == EnumWindingOrderTypes.clockwise) {
assertFalse(WindingOrder.isCounterClockwise((Geometry) value));
} else {
assertTrue(WindingOrder.isCounterClockwise((Geometry) value) == WindingOrder.isCounterClockwise((Geometry) expected));
}
} else {
// TODO check winding order is CCW
if (value instanceof Polygon || value instanceof MultiPolygon)
assertTrue(WindingOrder.isCounterClockwise((Geometry) value));
}
} else {
assertEquals(expected.toString(), value.toString());
}
}
assertFalse(it.hasNext());
}
} finally {
it.close();
}
if (DEL_TEMP_FILES) {
outFile.deleteOnExit();
}
return valReport;
}
Aggregations