Search in sources :

Example 71 with InstanceCollection

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();
    }
}
Also used : ShapeInstanceReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeInstanceReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) ShapeSchemaReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeSchemaReader) Test(org.junit.Test)

Example 72 with InstanceCollection

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;
}
Also used : Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InputStream(java.io.InputStream) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) GroovyShell(groovy.lang.GroovyShell) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DelegatingScript(groovy.util.DelegatingScript) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 73 with InstanceCollection

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);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) Test(org.junit.Test)

Example 74 with InstanceCollection

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);
}
Also used : HashMap(java.util.HashMap) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 75 with InstanceCollection

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;
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)

Aggregations

InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)151 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)131 Test (org.junit.Test)116 AbstractHandlerTest (eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)97 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)17 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)17 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)11 ArrayList (java.util.ArrayList)10 Geometry (com.vividsolutions.jts.geom.Geometry)9 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)9 IOException (java.io.IOException)9 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)8 Polygon (com.vividsolutions.jts.geom.Polygon)8 QName (javax.xml.namespace.QName)8 Coordinate (com.vividsolutions.jts.geom.Coordinate)7 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)7 DefaultInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)7 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)6 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)6 TypeFilter (eu.esdihumboldt.hale.common.instance.model.TypeFilter)6