Search in sources :

Example 1 with SQLStore

use of org.apache.sis.storage.sql.SQLStore in project sis by apache.

the class PostgresTest method testSpatialFeatures.

/**
 * Tests reading and writing features and rasters.
 *
 * @throws Exception if an error occurred while testing the database.
 */
@Test
public void testSpatialFeatures() throws Exception {
    try (TestDatabase database = TestDatabase.createOnPostgreSQL(SQLStoreTest.SCHEMA, true)) {
        database.executeSQL(PostgresTest.class, "file:SpatialFeatures.sql");
        final StorageConnector connector = new StorageConnector(database.source);
        connector.setOption(OptionKey.GEOMETRY_LIBRARY, GeometryLibrary.JTS);
        final ResourceDefinition table = ResourceDefinition.table(null, SQLStoreTest.SCHEMA, "SpatialData");
        try (SQLStore store = new SQLStore(new SQLStoreProvider(), connector, table)) {
            /*
                 * Invoke the private `model()` method. We have to use reflection because the class
                 * is not in the same package and we do not want to expose the method in public API.
                 */
            final Method modelAccessor = SQLStore.class.getDeclaredMethod("model");
            modelAccessor.setAccessible(true);
            final Postgres<?> pg = (Postgres<?>) modelAccessor.invoke(store);
            try (Connection connection = database.source.getConnection();
                ExtendedInfo info = new ExtendedInfo(pg, connection)) {
                testInfoStatements(info);
                testGeometryGetter(info, connection);
                testRasterReader(TestRaster.USHORT, info, connection);
            }
            /*
                 * Tests through public API.
                 */
            final FeatureSet resource = store.findResource("SpatialData");
            try (Stream<AbstractFeature> features = resource.features(false)) {
                features.forEach(PostgresTest::validate);
            }
            final Envelope envelope = resource.getEnvelope().get();
            assertEquals(envelope.getMinimum(0), -72, 1);
            assertEquals(envelope.getMaximum(1), 43, 1);
        }
    }
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) SQLStore(org.apache.sis.storage.sql.SQLStore) SQLStoreProvider(org.apache.sis.storage.sql.SQLStoreProvider) TestDatabase(org.apache.sis.test.sql.TestDatabase) ResourceDefinition(org.apache.sis.storage.sql.ResourceDefinition) Connection(java.sql.Connection) AbstractFeature(org.apache.sis.feature.AbstractFeature) Method(java.lang.reflect.Method) Envelope(org.opengis.geometry.Envelope) FeatureSet(org.apache.sis.storage.FeatureSet) Test(org.junit.Test) GeometryGetterTest(org.apache.sis.internal.sql.feature.GeometryGetterTest) SQLStoreTest(org.apache.sis.storage.sql.SQLStoreTest)

Aggregations

Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 AbstractFeature (org.apache.sis.feature.AbstractFeature)1 GeometryGetterTest (org.apache.sis.internal.sql.feature.GeometryGetterTest)1 FeatureSet (org.apache.sis.storage.FeatureSet)1 StorageConnector (org.apache.sis.storage.StorageConnector)1 ResourceDefinition (org.apache.sis.storage.sql.ResourceDefinition)1 SQLStore (org.apache.sis.storage.sql.SQLStore)1 SQLStoreProvider (org.apache.sis.storage.sql.SQLStoreProvider)1 SQLStoreTest (org.apache.sis.storage.sql.SQLStoreTest)1 TestDatabase (org.apache.sis.test.sql.TestDatabase)1 Test (org.junit.Test)1 Envelope (org.opengis.geometry.Envelope)1