use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class FilterTest method simpleSchemaTestCQL.
@Test
public void simpleSchemaTestCQL() throws Exception {
ShapeSchemaReader schemaReader = new ShapeSchemaReader();
schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/GN_Point/GN_Point.shp").toURI()));
schemaReader.validate();
IOReport report = schemaReader.execute(null);
assertTrue(report.isSuccess());
Schema schema = schemaReader.getSchema();
ShapeInstanceReader instanceReader = new ShapeInstanceReader();
instanceReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/GN_Point/GN_Point.shp").toURI()));
instanceReader.setSourceSchema(schema);
instanceReader.validate();
report = instanceReader.execute(null);
assertTrue(report.isSuccess());
InstanceCollection instances = instanceReader.getInstances();
assertFalse(instances.isEmpty());
ResourceIterator<Instance> ri = instances.iterator();
try {
boolean foundIt = false;
boolean stayFalse = false;
boolean stayFalseToo = false;
Filter cqlfilter = new FilterGeoCqlImpl("NEV = 'Piritulus'");
Filter foulfilter = new FilterGeoCqlImpl("HERP = 'DERP'");
Filter foulfilter1 = new FilterGeoCqlImpl("NEV = 'HURR'");
while (ri.hasNext()) {
Instance inst = ri.next();
assertNotNull(inst);
if (cqlfilter.match(inst)) {
foundIt = true;
}
if (foulfilter.match(inst)) {
stayFalse = true;
}
if (foulfilter1.match(inst)) {
stayFalseToo = true;
}
}
assertTrue(foundIt);
assertFalse(stayFalse);
assertFalse(stayFalseToo);
} finally {
ri.close();
}
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection 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.InstanceCollection in project hale by halestudio.
the class MsSqlDataReaderTest method testInstanceReader.
/**
* Test instance reader
*
* @throws Exception if error occurred in reading instances
*/
@SuppressWarnings("unchecked")
@Test
public void testInstanceReader() throws Exception {
// ****** read Schema ******//
Schema schema = readSchema();
assertEquals(3, schema.getMappingRelevantTypes().size());
// ****** read Instances ******//
InstanceCollection instances = readInstances(schema);
assertTrue(instances.hasSize());
assertEquals(SOURCE_TOTAL_INSTANCES_COUNT, instances.size());
InstanceCollection filteredInstances = null;
// Check SpatialTable "jdbc:sqlserver:dbo","SpatialTable"
filteredInstances = instances.select(new TypeFilter(schema.getType(new QName("jdbc:sqlserver:dbo", "SpatialTable"))));
int geometryPropertyCount = 0;
int idPropertyCount = 0;
ResourceIterator<Instance> it = filteredInstances.iterator();
while (it.hasNext()) {
Instance in = it.next();
for (String propertyName : SOURCE_GEOMETRY_TYPE_PROPERTY_NAMES) {
Object value = in.getProperty(QName.valueOf(propertyName))[0];
if (value == null)
continue;
if (value instanceof GeometryProperty) {
assertTrue(((GeometryProperty<Geometry>) value).getGeometry().toText().equalsIgnoreCase(String.valueOf(PROPERTY_GEO_VALUES[geometryPropertyCount])));
geometryPropertyCount++;
} else {
assertTrue(((int) value) == ((int) PROPERTY_ID_VALUES[idPropertyCount]));
idPropertyCount++;
}
}
}
assertEquals(SOURCE_GEOMETRY_INSTANCE_COUNT, geometryPropertyCount);
assertEquals(SOURCE_GEOMETRY_INSTANCE_COUNT, idPropertyCount);
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class SpatiaLiteTestSuite method instanceReaderTest.
/**
* Test - reads a sample SpatiaLite schema and data.
*
* @throws Exception if an error occurs
*/
public void instanceReaderTest() throws Exception {
if (!isSpatiaLiteExtensionAvailable()) {
log.info("Skipping test because SpatiaLite extension is not available");
return;
}
Map<String, Object> propertyMap = new HashMap<String, Object>();
for (int i = 0; i < SOUURCE_TYPE_PROPERTY_NAMES.length; i++) {
String key = SOUURCE_TYPE_PROPERTY_NAMES[i];
Object value = SOUURCE_TYPE_PROPERTY_VALUES[i];
propertyMap.put(key, value);
}
// ****** read Schema ******//
Schema schema = readSchema(getSourceTempFilePath());
assertNotNull(schema);
assertEquals(1, schema.getMappingRelevantTypes().size());
// Test properties
TypeDefinition schemaType = schema.getMappingRelevantTypes().iterator().next();
// Check every property for their existence
checkType(schemaType, SOUURCE_TYPE_LOCAL_NAME, propertyMap.keySet());
// ****** read Instances ******//
InstanceCollection instances = readInstances(schema, getSourceTempFilePath());
assertTrue(instances.hasSize());
assertEquals(SOURCE_INSTANCES_COUNT, instances.size());
checkInstances(instances, propertyMap);
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection 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;
}
Aggregations