use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection in project hale by halestudio.
the class XLSInstanceReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
boolean skipFirst = getParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE).as(Boolean.class);
// first sheet as default
sheetNum = getParameter(InstanceTableIOConstants.SHEET_INDEX).as(int.class, 0);
instances = new DefaultInstanceCollection(new ArrayList<Instance>());
try {
// analyze the excel sheet to get all information
analyser = new AnalyseXLSSchemaTable(getSource().getLocation(), sheetNum);
} catch (Exception e) {
reporter.error(new IOMessageImpl("Reading the excel sheet has failed", e));
return reporter;
}
// get type definition of the schema
type = getSourceSchema().getType(QName.valueOf(getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class)));
// get property definition
propAr = type.getChildren().toArray(new PropertyDefinition[type.getChildren().size()]);
Collection<List<String>> rows = analyser.getRows();
// skip if first row is a header
if (!skipFirst) {
// otherwise first line is also an instance
createInstanceCollection(analyser.getHeader(), reporter);
line++;
}
// iterate over all rows to create the instances
Iterator<List<String>> allRows = rows.iterator();
while (allRows.hasNext()) {
List<String> row = allRows.next();
createInstanceCollection(row, reporter);
line++;
}
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection in project hale by halestudio.
the class SimplePartitionerTest method createCollection.
static InstanceCollection createCollection(int num) {
Collection<Instance> instances = new ArrayList<>();
for (int i = 0; i < num; i++) {
instances.add(new DefaultInstance(null, null));
}
InstanceCollection res = new DefaultInstanceCollection(instances);
return res;
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection in project hale by halestudio.
the class InstanceBuilderReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Run instance builder", ProgressIndicator.UNKNOWN);
try {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setScriptBaseClass(DelegatingScript.class.getName());
// Configure the GroovyShell and pass the compiler configuration.
GroovyShell shell = new GroovyShell(getClass().getClassLoader(), new Binding(), compilerConfiguration);
DelegatingScript script;
try (InputStream in = getSource().getInput();
InputStreamReader reader = new InputStreamReader(in, getCharset())) {
script = (DelegatingScript) shell.parse(reader);
}
InstanceBuilder builder = new InstanceBuilder();
// apply schema
builder.setTypes(getSourceSchema());
script.setDelegate(builder);
Object res = script.run();
if (res == null) {
throw new IllegalStateException("Null returned by script");
} else if (res instanceof InstanceCollection) {
instances = (InstanceCollection) res;
} else if (res instanceof Instance) {
instances = new DefaultInstanceCollection(Collections.singleton((Instance) res));
} else {
throw new IllegalStateException("Unrecognised return type: " + res.getClass().getName());
}
reporter.setSuccess(true);
} catch (Exception e) {
reporter.setSuccess(false);
reporter.error("Error running instance builder", e);
} finally {
progress.end();
}
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection in project hale by halestudio.
the class AbstractDBTest method readAndCountInstances.
/**
* Read the instances from the db, check if it is same as instances written
* to the db.
*
* @param originalInstances instance created and written to db
* @param schema schema read
* @param gType the geometry type definition.
* @return The count of instances which are equal to the original instances
* @throws Exception if reading the instances fails.
*/
protected int readAndCountInstances(InstanceCollection originalInstances, Schema schema, TypeDefinition gType) throws Exception {
InstanceCollection instancesRead = readInstances(schema).select(new TypeFilter(gType));
List<Instance> originals = new DefaultInstanceCollection(originalInstances).toList();
ResourceIterator<Instance> ri = instancesRead.iterator();
int count = 0;
try {
while (ri.hasNext()) {
Instance instance = ri.next();
String error = InstanceUtil.checkInstance(instance, originals);
assertNull(error, error);
count++;
}
} finally {
ri.close();
}
return count;
}
use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection 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