You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by am...@apache.org on 2020/01/21 15:35:00 UTC
[sis] 04/04: wip(SQLStore): Start test cases for spatial functions
over in memory H2GIS
This is an automated email from the ASF dual-hosted git repository.
amanin pushed a commit to branch fix/fast-envelope
in repository https://gitbox.apache.org/repos/asf/sis.git
commit b3ac6a59bc58d63293e3a42c2f8fc626edc387ea
Author: Alexis Manin <am...@apache.org>
AuthorDate: Tue Jan 21 16:34:30 2020 +0100
wip(SQLStore): Start test cases for spatial functions over in memory H2GIS
---
storage/sis-sqlstore/pom.xml | 6 ++
.../apache/sis/internal/sql/feature/EWKBTest.java | 13 +--
.../org/apache/sis/storage/sql/H2SpatialTest.java | 98 ++++++++++++++++++++++
.../org/apache/sis/storage/sql/h2_base.sql | 9 ++
4 files changed, 120 insertions(+), 6 deletions(-)
diff --git a/storage/sis-sqlstore/pom.xml b/storage/sis-sqlstore/pom.xml
index 86d7cc4..65d2e13 100644
--- a/storage/sis-sqlstore/pom.xml
+++ b/storage/sis-sqlstore/pom.xml
@@ -171,6 +171,12 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.orbisgis</groupId>
+ <artifactId>h2gis</artifactId>
+ <version>1.5.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/EWKBTest.java b/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/EWKBTest.java
index 9653e34..398fb83 100644
--- a/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/EWKBTest.java
+++ b/storage/sis-sqlstore/src/test/java/org/apache/sis/internal/sql/feature/EWKBTest.java
@@ -9,11 +9,10 @@ import org.apache.sis.referencing.CommonCRS;
import org.apache.sis.setup.GeometryLibrary;
import org.apache.sis.test.TestCase;
-import org.junit.Assert;
-import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
+import static org.junit.jupiter.api.Assertions.assertEquals;
public class EWKBTest extends TestCase {
@@ -24,15 +23,17 @@ public class EWKBTest extends TestCase {
public void decodeHexadecimal(String wkt, String hexEWKB) throws Exception {
final GeographicCRS expectedCrs = CommonCRS.defaultGeographic();
final EWKBReader reader = new EWKBReader(GF).forCrs(expectedCrs);
- Assert.assertEquals("WKT and hexadecimal EWKB representation don't match",
- GF.parseWKT(wkt).implementation(), reader.readHexa(hexEWKB));
+ assertEquals(
+ GF.parseWKT(wkt).implementation(), reader.readHexa(hexEWKB),
+ "WKT and hexadecimal EWKB representation don't match"
+ );
}
/**
* The purpose of this test is not to check complex geometries, which is validated by above one. We just want to
* ensure that decoding directly a byte stream behaves in the same way than through hexadecimal.
*/
- @Test
+ @org.junit.Test
public void testBinary() {
final ByteBuffer point = ByteBuffer.allocate(163);
// Skip first byte: XDR mode
@@ -46,6 +47,6 @@ public class EWKBTest extends TestCase {
point.position(0);
final Object read = new EWKBReader(GeometryLibrary.JTS).read(point);
- Assert.assertEquals(GF.createPoint(42.2, 43.3), read);
+ assertEquals(GF.createPoint(42.2, 43.3), read);
}
}
diff --git a/storage/sis-sqlstore/src/test/java/org/apache/sis/storage/sql/H2SpatialTest.java b/storage/sis-sqlstore/src/test/java/org/apache/sis/storage/sql/H2SpatialTest.java
new file mode 100644
index 0000000..9d67a3a
--- /dev/null
+++ b/storage/sis-sqlstore/src/test/java/org/apache/sis/storage/sql/H2SpatialTest.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.storage.sql;
+
+import java.sql.Connection;
+import java.util.logging.Level;
+
+import org.opengis.feature.FeatureType;
+import org.opengis.feature.PropertyType;
+import org.opengis.geometry.Envelope;
+
+import org.apache.sis.feature.Features;
+import org.apache.sis.geometry.AbstractEnvelope;
+import org.apache.sis.geometry.Envelope2D;
+import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.FeatureSet;
+import org.apache.sis.storage.StorageConnector;
+import org.apache.sis.test.sql.TestDatabase;
+import org.apache.sis.util.logging.Logging;
+
+import org.h2gis.functions.factory.H2GISFunctions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class H2SpatialTest {
+
+ private static TestDatabase db;
+ private static SQLStore store;
+
+ @BeforeAll
+ public static void createDatabase() throws Exception {
+ db = TestDatabase.createOnH2("spatialFunctions", false);
+ try (Connection c = db.source.getConnection()) {
+ H2GISFunctions.load(c);
+ }
+ db.executeSQL(H2SpatialTest.class, "file:"+"h2_base.sql");
+
+ store = new SQLStore(
+ new SQLStoreProvider(), new StorageConnector(db.source),
+ SQLStoreProvider.createTableName(null, null, "roads")
+ );
+ }
+
+ @org.junit.Test public void preventVintageError() {}
+
+ @Test
+ public void getEnvelope() throws DataStoreException {
+ final FeatureSet roads = (FeatureSet) store.findResource("roads");
+ final Envelope envelope = roads.getEnvelope()
+ .orElseThrow(() -> new AssertionError("No envelope available for spatial dataset"));
+ final Envelope2D expected = new Envelope2D(CommonCRS.WGS84.geographic(), 15, 5, 16, 20);
+
+ assertTrue(
+ AbstractEnvelope.castOrCopy(envelope).equals(expected, 1e-4, false),
+ () -> String.format("Bad envelope.%nExpected: %s%nBut was: %s", expected.toString(), envelope.toString())
+ );
+ }
+
+ //@Test
+ public void readGeometries() throws DataStoreException {
+ final FeatureSet roads = (FeatureSet) store.findResource("ROADS");
+ final FeatureType type = roads.getType();
+ final PropertyType geom = Features.getDefaultGeometry(type);
+ // TODO: check CRS and if it's a linestring, then try to read rows.
+ }
+
+ //@Test
+ public void bboxFilter() throws DataStoreException {
+ // TODO: try to execute a simple query with an envelope.
+ }
+
+ @AfterAll
+ public static void destroyDb() {
+ if (db == null) return;
+ try (AutoCloseable closeDb = db::close ; AutoCloseable closeStore = () -> { if (store != null) store.close(); }) {
+ } catch (Exception e) {
+ Logging.getLogger("org.apache.sis.storage.sql").log(Level.WARNING, "Cannot properly free test database", e);
+ }
+ }
+}
diff --git a/storage/sis-sqlstore/src/test/resources/org/apache/sis/storage/sql/h2_base.sql b/storage/sis-sqlstore/src/test/resources/org/apache/sis/storage/sql/h2_base.sql
new file mode 100644
index 0000000..0090da9
--- /dev/null
+++ b/storage/sis-sqlstore/src/test/resources/org/apache/sis/storage/sql/h2_base.sql
@@ -0,0 +1,9 @@
+-- A sample database to test spatial operations over database.
+-- This is designed to work over orbisgis:H2GIS database.
+-- Inspired by official example at https://github.com/orbisgis/orbisgis-samples/blob/master/demoh2gis/src/main/java/org/orbisgis/demoh2gis/Main.java
+
+CREATE TABLE ROADS (geometry LINESTRING, speed_limit INT) CHECK ST_SRID(geometry)=4326;
+
+INSERT INTO ROADS VALUES (ST_GeomFromText('LINESTRING(15 5, 20 6, 25 7)', 4326), 80)
+INSERT INTO ROADS VALUES (ST_GeomFromText('LINESTRING(20 6, 21 15, 21 25)', 4326), 50)
+INSERT INTO ROADS VALUES (ST_GeomFromText('LINESTRING(34 12, 36 13, 35.2 16)', 4326), 70)