Skip to content
This repository

Extra functions added for compatibility with hibernate spatial #13

Closed
wants to merge 3 commits into from

2 participants

Jacques Bouman Justin Deoliveira
Jacques Bouman

Hi,

For our GIS projects we heavily rely on hibernate spatial. This works perfectly, but until recently we had to use a real database for testing. Last week I investigated the possibility of using GeoDB for this and was happily surprised at it's capabilities. I did however miss some spatial functions (for my current project for instance the extent function). Looking at the source of GeoDB it seemed easy to implement so this weekend I had a try. Could you review what I have done?

Greetings,

Jacques Bouman

Justin Deoliveira
Owner

Hi Jacques,

This looks really great, thanks for that. One thing though can we just add the extra functions to the GeoDB class directly, rather than a separate class. Other than that it looks great, will merge in and put out a new release. Thanks again.

Jacques Bouman

Hi Justin,

I just moved the extra functions to the GeoDB class. Just to be curious, why do you prefer to keep all functions in the same class instead of creating a seperate class for each function (as no is done for Extent and Union for instance)?

Greetings,

Jacques

Justin Deoliveira
Owner

Hi Jacques,

Thanks for doing that. As for the organization of the functions I just figured they belonged with the other existing functions. Like for instance why put intersection in one class and union in another? I am open a reorganization of functions into specific classes but not sure exactly what that would be? Until then I prefer to keep things consolidated to a single class.

Also, to match conventions with the other functions in the GeoDB class, and the simple features for sql standard, i wplan to prefix all the function names with "ST_". Any objections?

-Justin

Jacques Bouman

Hi Justin,

Just renamed the functions and moved them to a more logical place in de GeoDB class. Had some problems with the naming of the aggregate union functions. Apperently it is not allowed to have the same name for a function and an aggregate in H2. The aggregate now is named TT_Union, but feel free to give it a more logical name.

Giving each function each own class (and file) would have the benifit of giving more insight in the structure of the code. Navigating through the GeoDB class is now a little bit difficult and will become more difficult as you add more functions. Furthermore the chance of merge conflicts will rise if more people will start working on the code since almost all changes will probably be on the GeoDB class.

As for the naming of the functions. I initially tried to stick to the java conventions of giving methods Camel cased names starting with a lowercased letter.

Jacques

Justin Deoliveira
Owner

Hi Jacques,

Thanks again for this! I agree that it would be good to reorganize the functions as that main class is getting pretty crowded. As you can imagine the class started small but has ballooned since then :) I think a good organization would be to use the postgis function reference as a guide.

http://postgis.refractions.net/documentation/manual-1.3/ch06.html

And group the functions into appropriate classes. But let's leave that for a another issue (feel free to open it) and for now we can just stick then in GeoDB.java.

As for the naming convention of functions, i also think it makes sense to keep java naming conventions, but in the geodb.sql registering the function alias with the ST_prefix. That would fix the aggregate function naming issue. Perhaps we can open a separate issue for that as well.

Again thanks for this, I will get these changes merged in asap.

Justin Deoliveira
Owner

Hi Jacques,

About to merge this pull request, one question though. For aggregate function names that clash with regular function names i would like to come up with a naming convention. I am thinking either a. we drop the "ST_" prefix or we use a prefix/postfix like "Aggregate". So it would make for "Untion" or "ST_UntionAggregate" or "ST_AggregateUnion".

ANy preference?

Jacques Bouman

I just looked up what at ST stands for (spatial type) and since these functions are also related to that i don't think that dropping the ST is a good choice. I would prefer to use your suggestion with the aggregate postfix, but add a underscore between the functionname and aggregate (ST_Union_Aggregate).

Justin Deoliveira jdeolive closed this pull request from a commit
deolivej added extra functions for hibernate spatial capabilities, closes #13,…
… pull request submitted by jacques-finalist
1cd52a6
Justin Deoliveira jdeolive closed this in 1cd52a6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
126  core/src/main/java/geodb/GeoDB.java
@@ -24,6 +24,7 @@
24 24
 import com.vividsolutions.jts.geom.Envelope;
25 25
 import com.vividsolutions.jts.geom.Geometry;
26 26
 import com.vividsolutions.jts.geom.GeometryFactory;
  27
+import com.vividsolutions.jts.geom.IntersectionMatrix;
27 28
 import com.vividsolutions.jts.geom.Point;
28 29
 import com.vividsolutions.jts.io.InputStreamInStream;
29 30
 import com.vividsolutions.jts.io.OutputStreamOutStream;
@@ -881,6 +882,129 @@ public static boolean ST_Within( byte[] wkb1, byte[] wkb2) {
881 882
     ST_Union - Returns a geometry that represents the point set union of the Geometries.
882 883
     */
883 884
     
  885
+	public static Integer ST_Dimension(byte[] wkb) {
  886
+		Geometry geometry = gFromWKB(wkb);
  887
+		if (geometry != null) {
  888
+			return geometry.getDimension();
  889
+		}
  890
+		return null;
  891
+	}
  892
+
  893
+	public static byte[] ST_Boundary(byte[] wkb) {
  894
+		Geometry geometry = gFromWKB(wkb);
  895
+		if (geometry != null) {
  896
+			Geometry boundary = geometry.getBoundary();
  897
+			if (boundary != null) {
  898
+				return gToWKB(boundary);
  899
+			}
  900
+		}
  901
+		return null;
  902
+	}
  903
+
  904
+	public static boolean ST_Relate(byte[] wkb1, byte[] wkb2,
  905
+			String intersectionPattern) {
  906
+		Geometry geometry1 = gFromWKB(wkb1);
  907
+		Geometry geometry2 = gFromWKB(wkb1);
  908
+		if (geometry1 != null && geometry2 != null) {
  909
+			return geometry1.relate(geometry2, intersectionPattern);
  910
+		}
  911
+		return false;
  912
+	}
  913
+
  914
+	public static String ST_Relate(byte[] wkb1, byte[] wkb2) {
  915
+		Geometry geometry1 = gFromWKB(wkb1);
  916
+		Geometry geometry2 = gFromWKB(wkb1);
  917
+		if (geometry1 != null && geometry2 != null) {
  918
+			IntersectionMatrix result = geometry1.relate(geometry2);
  919
+			return result.toString();
  920
+		}
  921
+		return null;
  922
+	}
  923
+
  924
+	public static byte[] ST_ConvexHull(byte[] wkb) {
  925
+		Geometry geometry = gFromWKB(wkb);
  926
+		if (geometry != null) {
  927
+			Geometry boundary = geometry.convexHull();
  928
+			if (boundary != null) {
  929
+				return gToWKB(boundary);
  930
+			}
  931
+		}
  932
+		return null;
  933
+	}
  934
+
  935
+	public static byte[] ST_Difference(byte[] wkb1, byte[] wkb2) {
  936
+		if (wkb1 == null) {
  937
+			return null;
  938
+		}
  939
+		if (wkb2 == null) {
  940
+			return wkb1;
  941
+		}
  942
+		Geometry geometry1 = gFromWKB(wkb1);
  943
+		Geometry geometry2 = gFromWKB(wkb2);
  944
+		if (geometry1 == null) {
  945
+			return null;
  946
+		}
  947
+		if (geometry2 == null) {
  948
+			return wkb1;
  949
+		}
  950
+	
  951
+		return gToWKB(geometry1.difference(geometry2));
  952
+	}
  953
+
  954
+	public static byte[] ST_Intersection(byte[] wkb1, byte[] wkb2) {
  955
+		if (wkb1 == null || wkb2 == null) {
  956
+			return null;
  957
+		}
  958
+		Geometry geometry1 = gFromWKB(wkb1);
  959
+		Geometry geometry2 = gFromWKB(wkb2);
  960
+		if (geometry1 == null || geometry2 == null) {
  961
+			return null;
  962
+		}
  963
+	
  964
+		return gToWKB(geometry1.intersection(geometry2));
  965
+	}
  966
+
  967
+	public static byte[] ST_SymDifference(byte[] wkb1, byte[] wkb2) {
  968
+		if (wkb1 == null) {
  969
+			return wkb2;
  970
+		}
  971
+		if (wkb2 == null) {
  972
+			return wkb1;
  973
+		}
  974
+		Geometry geometry1 = gFromWKB(wkb1);
  975
+		Geometry geometry2 = gFromWKB(wkb2);
  976
+		if (geometry1 == null) {
  977
+			return gToWKB(geometry2);
  978
+		}
  979
+		if (geometry2 == null) {
  980
+			return gToWKB(geometry1);
  981
+		}
  982
+	
  983
+		return gToWKB(geometry1.symDifference(geometry2));
  984
+	}
  985
+
  986
+	public static byte[] ST_Union(byte[] wkb1, byte[] wkb2) {
  987
+		if (wkb1 == null) {
  988
+			return wkb2;
  989
+		}
  990
+		if (wkb2 == null) {
  991
+			return wkb1;
  992
+		}
  993
+		Geometry geometry1 = gFromWKB(wkb1);
  994
+		Geometry geometry2 = gFromWKB(wkb2);
  995
+		if (geometry1 == null) {
  996
+			return gToWKB(geometry2);
  997
+		}
  998
+		if (geometry2 == null) {
  999
+			return gToWKB(geometry1);
  1000
+		}
  1001
+	
  1002
+		return gToWKB(geometry1.union(geometry2));
  1003
+	}    
  1004
+    
  1005
+    
  1006
+    
  1007
+    
884 1008
     //
885 1009
     // Miscellaneous Functions
886 1010
     //
@@ -1425,4 +1549,6 @@ static String geotypeConstraint(String schema, String table, String column) {
1425 1549
         }
1426 1550
         return "ENFORCE_GEOTYPE_" + name;
1427 1551
     }
  1552
+
  1553
+
1428 1554
 }
34  core/src/main/java/geodb/aggregate/Extent.java
... ...
@@ -0,0 +1,34 @@
  1
+package geodb.aggregate;
  2
+
  3
+import java.sql.Connection;
  4
+import java.sql.SQLException;
  5
+
  6
+import com.vividsolutions.jts.geom.Geometry;
  7
+
  8
+public class Extent extends GeoAggregateFunction {
  9
+
  10
+	private Geometry result;
  11
+
  12
+	@Override
  13
+	protected void add(Geometry geometry) {
  14
+		if (result == null) {
  15
+			result = geometry;
  16
+		} else {
  17
+			if (geometry != null) {
  18
+				result=result.union(geometry.getEnvelope());
  19
+			}
  20
+		}
  21
+	}
  22
+
  23
+	@Override
  24
+	protected Geometry getGeometryResult() {
  25
+		if (result!=null){
  26
+			return result.getEnvelope();
  27
+		}
  28
+		return null;
  29
+	}
  30
+
  31
+	public void init(Connection arg0) throws SQLException {
  32
+		result=null;
  33
+	}
  34
+}
58  core/src/main/java/geodb/aggregate/GeoAggregateFunction.java
... ...
@@ -0,0 +1,58 @@
  1
+package geodb.aggregate;
  2
+
  3
+import geodb.GeoDB;
  4
+
  5
+import java.io.ByteArrayInputStream;
  6
+import java.io.IOException;
  7
+import java.sql.SQLException;
  8
+import java.sql.Types;
  9
+
  10
+import org.h2.api.AggregateFunction;
  11
+
  12
+import com.vividsolutions.jts.geom.Geometry;
  13
+import com.vividsolutions.jts.io.InputStreamInStream;
  14
+import com.vividsolutions.jts.io.ParseException;
  15
+import com.vividsolutions.jts.io.WKBReader;
  16
+
  17
+public abstract class GeoAggregateFunction implements AggregateFunction{
  18
+
  19
+
  20
+	private Geometry createGeometry(ByteArrayInputStream stream) {
  21
+		InputStreamInStream inputStreamInStream = new InputStreamInStream(stream);
  22
+		Geometry geometry=null;
  23
+		try {
  24
+			geometry = new WKBReader().read(inputStreamInStream );
  25
+		} catch (IOException e) {
  26
+			throw new IllegalArgumentException("given geometry can not be found",e);
  27
+		} catch (ParseException e) {
  28
+			throw new IllegalArgumentException("given geometry is not valid",e);
  29
+		}
  30
+		return geometry;
  31
+		
  32
+	}
  33
+
  34
+	protected abstract void add(Geometry geometry);
  35
+	protected abstract Geometry getGeometryResult();
  36
+	
  37
+	public final void add(Object arg0) throws SQLException {
  38
+		if (arg0 != null) {
  39
+			Geometry geometry = createGeometry((ByteArrayInputStream) arg0);
  40
+			if (geometry!=null){
  41
+				add(geometry);
  42
+			}
  43
+		}
  44
+	}
  45
+
  46
+	public final Object getResult() throws SQLException {
  47
+		Geometry geometryResult = getGeometryResult();
  48
+		if (geometryResult!=null){
  49
+			return GeoDB.gToWKB(geometryResult);
  50
+		}
  51
+		return null;
  52
+	}
  53
+
  54
+	public final int getType(int[] arg0) throws SQLException {
  55
+		return Types.BLOB;
  56
+	}
  57
+
  58
+}
31  core/src/main/java/geodb/aggregate/Union.java
... ...
@@ -0,0 +1,31 @@
  1
+package geodb.aggregate;
  2
+
  3
+import java.sql.Connection;
  4
+import java.sql.SQLException;
  5
+
  6
+import com.vividsolutions.jts.geom.Geometry;
  7
+
  8
+public class Union extends GeoAggregateFunction {
  9
+
  10
+	private Geometry result;
  11
+
  12
+	@Override
  13
+	protected void add(Geometry geometry) {
  14
+		if (result == null) {
  15
+			result = geometry;
  16
+		} else {
  17
+			if (geometry != null) {
  18
+				result=result.union(geometry);
  19
+			}
  20
+		}
  21
+	}
  22
+
  23
+	@Override
  24
+	protected Geometry getGeometryResult() {
  25
+		return result;
  26
+	}
  27
+
  28
+	public void init(Connection arg0) throws SQLException {
  29
+		result=null;
  30
+	}
  31
+}
10  core/src/main/resources/geodb/geodb.sql
@@ -11,13 +11,17 @@ CREATE ALIAS ST_AsEWKT FOR "geodb.GeoDB.ST_AsEWKT"
11 11
 CREATE ALIAS ST_AsHexEWKB FOR "geodb.GeoDB.ST_AsHexEWKB"
12 12
 CREATE ALIAS ST_AsText FOR "geodb.GeoDB.ST_AsText"
13 13
 CREATE ALIAS ST_BBOX FOR "geodb.GeoDB.ST_BBox"
  14
+CREATE ALIAS ST_Boundary FOR "geodb.GeoDB.ST_Boundary"
14 15
 CREATE ALIAS ST_Buffer FOR "geodb.GeoDB.ST_Buffer" 
15 16
 CREATE ALIAS ST_Centroid FOR "geodb.GeoDB.ST_Centroid"
16 17
 CREATE ALIAS ST_Crosses FOR "geodb.GeoDB.ST_Crosses"
17 18
 CREATE ALIAS ST_Contains FOR "geodb.GeoDB.ST_Contains"
  19
+CREATE ALIAS ST_ConvexHull FOR "geodb.GeoDB.ST_ConvexHull"
18 20
 CREATE ALIAS ST_DWithin FOR "geodb.GeoDB.ST_DWithin"
19 21
 CREATE ALIAS ST_Disjoint FOR "geodb.GeoDB.ST_Disjoint"
20 22
 CREATE ALIAS ST_Distance FOR "geodb.GeoDB.ST_Distance"
  23
+CREATE ALIAS ST_Difference FOR "geodb.GeoDB.ST_Difference"
  24
+CREATE ALIAS ST_Dimension FOR "geodb.GeoDB.ST_Dimension"
21 25
 CREATE ALIAS ST_Envelope FOR "geodb.GeoDB.ST_Envelope"
22 26
 CREATE ALIAS ST_Equals FOR "geodb.GeoDB.ST_Equals"
23 27
 CREATE ALIAS ST_GeoHash FOR "geodb.GeoDB.ST_GeoHash"
@@ -25,6 +29,7 @@ CREATE ALIAS ST_GeomFromEWKB FOR "geodb.GeoDB.ST_GeomFromEWKB"
25 29
 CREATE ALIAS ST_GeomFromEWKT FOR "geodb.GeoDB.ST_GeomFromEWKT"
26 30
 CREATE ALIAS ST_GeomFromText FOR "geodb.GeoDB.ST_GeomFromText"
27 31
 CREATE ALIAS ST_GeomFromWKB FOR "geodb.GeoDB.ST_GeomFromWKB"
  32
+CREATE ALIAS ST_Intersection FOR "geodb.GeoDB.ST_Intersection"
28 33
 CREATE ALIAS ST_Intersects FOR "geodb.GeoDB.ST_Intersects"
29 34
 CREATE ALIAS ST_IsEmpty FOR "geodb.GeoDB.ST_IsEmpty"
30 35
 CREATE ALIAS ST_IsSimple FOR "geodb.GeoDB.ST_IsSimple"
@@ -32,10 +37,13 @@ CREATE ALIAS ST_IsValid FOR "geodb.GeoDB.ST_IsValid"
32 37
 CREATE ALIAS ST_MakePoint FOR "geodb.GeoDB.ST_MakePoint"
33 38
 CREATE ALIAS ST_MakeBox2D FOR "geodb.GeoDB.ST_MakeBox2D"
34 39
 CREATE ALIAS ST_Overlaps FOR "geodb.GeoDB.ST_Overlaps"
  40
+CREATE ALIAS ST_Relate FOR "geodb.GeoDB.ST_Relate"
35 41
 CREATE ALIAS ST_SRID FOR "geodb.GeoDB.ST_SRID"
36 42
 CREATE ALIAS ST_SetSRID FOR "geodb.GeoDB.ST_SetSRID"
37 43
 CREATE ALIAS ST_Simplify FOR "geodb.GeoDB.ST_Simplify"
  44
+CREATE ALIAS ST_SymDifference FOR "geodb.GeoDB.ST_SymDifference"
38 45
 CREATE ALIAS ST_Touches FOR "geodb.GeoDB.ST_Touches"
  46
+CREATE ALIAS ST_Union FOR "geodb.GeoDB.ST_Union"
39 47
 CREATE ALIAS ST_Within FOR "geodb.GeoDB.ST_Within"
40 48
 CREATE ALIAS Version FOR "geodb.GeoDB.Version"
41 49
 CREATE DOMAIN POINT AS BLOB
@@ -46,3 +54,5 @@ CREATE DOMAIN MULTILINESTRING AS BLOB
46 54
 CREATE DOMAIN MULTIPOLYGON AS BLOB
47 55
 CREATE DOMAIN GEOMETRYCOLLECTION AS BLOB
48 56
 CREATE DOMAIN GEOMETRY AS BLOB
  57
+CREATE AGGREGATE EXTENT FOR "geodb.aggregate.Extent"
  58
+CREATE AGGREGATE TT_Union FOR "geodb.aggregate.Union"
158  core/src/test/java/geodb/GeoDBExtraFunctionsTest.java
... ...
@@ -0,0 +1,158 @@
  1
+package geodb;
  2
+
  3
+import static org.hamcrest.CoreMatchers.is;
  4
+import static org.junit.Assert.assertThat;
  5
+import static org.junit.Assert.assertTrue;
  6
+
  7
+import java.io.IOException;
  8
+import java.io.InputStream;
  9
+import java.sql.ResultSet;
  10
+import java.sql.SQLException;
  11
+import java.sql.Statement;
  12
+
  13
+import org.junit.Before;
  14
+import org.junit.Test;
  15
+
  16
+import com.vividsolutions.jts.geom.Geometry;
  17
+import com.vividsolutions.jts.io.InputStreamInStream;
  18
+import com.vividsolutions.jts.io.ParseException;
  19
+import com.vividsolutions.jts.io.WKBReader;
  20
+
  21
+public class GeoDBExtraFunctionsTest extends GeoDBTestSupport {
  22
+
  23
+    @Before
  24
+    public void setup() throws Exception {
  25
+        super.setUp();
  26
+        Statement st = cx.createStatement();
  27
+        st.execute("DROP TABLE IF EXISTS _GEODB");
  28
+        st.execute("DROP TABLE IF EXISTS spatial");
  29
+        st.execute("DROP TABLE IF EXISTS spatial_hatbox");
  30
+        st.execute("DROP TABLE IF EXISTS noindex");
  31
+        st.close();
  32
+        GeoDB.InitGeoDB(cx);
  33
+    }
  34
+ 
  35
+    
  36
+    @Test
  37
+    public void test_dimension() throws SQLException, IOException, ParseException {
  38
+    	insert_three_points();
  39
+    	Statement st = cx.createStatement();
  40
+        ResultSet rs = st.executeQuery("select st_dimension(geom) from spatial");
  41
+        rs.next();
  42
+        int srid = rs.getInt(1);
  43
+        st.close();
  44
+        assertThat(srid, is(0));
  45
+    }
  46
+
  47
+    @Test
  48
+    public void test_boundary() throws SQLException, IOException, ParseException {
  49
+    	insert_three_points();
  50
+    	Statement st = cx.createStatement();
  51
+        ResultSet rs = st.executeQuery("select st_boundary(geom) from spatial");
  52
+        rs.next();
  53
+        InputStream binaryStream = rs.getBinaryStream(1);
  54
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  55
+        st.close();
  56
+        assertThat(geometry.getArea(), is(0.0));
  57
+    }
  58
+
  59
+    @Test
  60
+    public void test_relate() throws SQLException, IOException, ParseException {
  61
+    	Statement st = cx.createStatement();
  62
+        ResultSet rs = st.executeQuery("SELECT ST_Relate(ST_GeomFromText('POINT(1 2)',4326), ST_Buffer(ST_GeomFromText('POINT(1 2)',4326),2))");
  63
+        rs.next();
  64
+        String result = rs.getString(1);
  65
+        st.close();
  66
+      //I don't really understand this function so not sure if the result is correct. At least the result of both tests seems consistent.
  67
+        assertThat(result, is("0FFFFFFF2"));
  68
+    }
  69
+
  70
+    @Test
  71
+    public void test_relate_with_matrix() throws SQLException, IOException, ParseException {
  72
+    	Statement st = cx.createStatement();
  73
+        ResultSet rs = st.executeQuery("SELECT ST_Relate(ST_GeomFromText('POINT(1 2)',4326), ST_Buffer(ST_GeomFromText('POINT(1 2)',4326),2), '0FFFFFFF2')");
  74
+        rs.next();
  75
+        boolean result = rs.getBoolean(1);
  76
+        st.close();
  77
+    	//I don't really understand this function so not sure if the result is correct. At least the result of both tests seems consistent.
  78
+        assertTrue(result);
  79
+    }
  80
+
  81
+    @Test
  82
+    public void test_convexHull() throws SQLException, IOException, ParseException {
  83
+    	insert_three_points();
  84
+    	Statement st = cx.createStatement();
  85
+        ResultSet rs = st.executeQuery("select st_convexhull(geom) from spatial");
  86
+        rs.next();
  87
+        InputStream binaryStream = rs.getBinaryStream(1);
  88
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  89
+        st.close();
  90
+        assertThat(geometry.getArea(), is(0.0));
  91
+    }
  92
+
  93
+    
  94
+    @Test
  95
+    public void test_difference() throws SQLException, IOException, ParseException {
  96
+    	Statement st = cx.createStatement();
  97
+        ResultSet rs = st.executeQuery("select ST_Difference(" +
  98
+        													 "ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))', 4326)," +
  99
+        													 "ST_GeomFromText('POLYGON((5 5, 5 10, 10 10, 10 5, 5 5))', 4326))");
  100
+        rs.next();
  101
+        InputStream binaryStream = rs.getBinaryStream(1);
  102
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  103
+        st.close();
  104
+        assertThat(geometry.getArea(), is(75.0));
  105
+    }
  106
+    
  107
+    @Test
  108
+    public void test_intersection() throws SQLException, IOException, ParseException {
  109
+    	Statement st = cx.createStatement();
  110
+        ResultSet rs = st.executeQuery("select ST_Intersection(" +
  111
+				 "ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))', 4326)," +
  112
+				 "ST_GeomFromText('POLYGON((5 5, 5 10, 10 10, 10 5, 5 5))', 4326))");
  113
+        rs.next();
  114
+        InputStream binaryStream = rs.getBinaryStream(1);
  115
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  116
+        st.close();
  117
+        assertThat(geometry.getArea(), is(25.0));
  118
+    }
  119
+
  120
+    @Test
  121
+    public void test_symdifference() throws SQLException, IOException, ParseException {
  122
+    	Statement st = cx.createStatement();
  123
+        ResultSet rs = st.executeQuery("select ST_SymDifference(" +
  124
+				 "ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))', 4326)," +
  125
+				 "ST_GeomFromText('POLYGON((5 5, 5 15, 10 15, 10 5, 5 5))', 4326))");
  126
+        rs.next();
  127
+        InputStream binaryStream = rs.getBinaryStream(1);
  128
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  129
+        st.close();
  130
+        assertThat(geometry.getArea(), is(100.0));
  131
+    }
  132
+
  133
+
  134
+    @Test
  135
+    public void test_union() throws SQLException, IOException, ParseException {
  136
+    	Statement st = cx.createStatement();
  137
+        ResultSet rs = st.executeQuery("select ST_Union(" +
  138
+				 "ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))', 4326)," +
  139
+				 "ST_GeomFromText('POLYGON((5 5, 5 15, 10 15, 10 5, 5 5))', 4326))");
  140
+        rs.next();
  141
+        InputStream binaryStream = rs.getBinaryStream(1);
  142
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  143
+        st.close();
  144
+        assertThat(geometry.getArea(), is(125.0));
  145
+    }
  146
+
  147
+    private void insert_three_points() throws SQLException{
  148
+    	Statement st = cx.createStatement();
  149
+        st.execute("CREATE TABLE spatial (id INT AUTO_INCREMENT PRIMARY KEY, geom BLOB)");
  150
+        st.execute("INSERT INTO spatial (geom) VALUES (ST_GeomFromText('POINT(0 0)', 4326))");
  151
+        st.execute("INSERT INTO spatial (geom) VALUES (ST_GeomFromText('POINT(1 1)', 4326))");
  152
+        st.execute("INSERT INTO spatial (geom) VALUES (ST_GeomFromText('POINT(2 2)', 4326))");
  153
+        st.close();
  154
+    }
  155
+    
  156
+    
  157
+
  158
+}
2  core/src/test/java/geodb/GeoDBTest.java
@@ -3,10 +3,8 @@
3 3
 import static org.junit.Assert.assertEquals;
4 4
 import static org.junit.Assert.assertFalse;
5 5
 import static org.junit.Assert.assertTrue;
6  
-import static org.junit.Assert.fail;
7 6
 
8 7
 import java.sql.ResultSet;
9  
-import java.sql.SQLException;
10 8
 import java.sql.Statement;
11 9
 
12 10
 import org.junit.Before;
64  core/src/test/java/geodb/aggregate/AggregateTest.java
... ...
@@ -0,0 +1,64 @@
  1
+package geodb.aggregate;
  2
+
  3
+import static org.hamcrest.CoreMatchers.is;
  4
+import static org.junit.Assert.assertThat;
  5
+import geodb.GeoDB;
  6
+import geodb.GeoDBTestSupport;
  7
+
  8
+import java.io.InputStream;
  9
+import java.sql.ResultSet;
  10
+import java.sql.Statement;
  11
+
  12
+import org.junit.Before;
  13
+import org.junit.Test;
  14
+
  15
+import com.vividsolutions.jts.geom.Geometry;
  16
+import com.vividsolutions.jts.io.InputStreamInStream;
  17
+import com.vividsolutions.jts.io.WKBReader;
  18
+
  19
+public class AggregateTest extends GeoDBTestSupport {
  20
+
  21
+    @Before
  22
+    public void setup() throws Exception {
  23
+        super.setUp();
  24
+        
  25
+        Statement st = cx.createStatement();
  26
+        st.execute("DROP TABLE IF EXISTS _GEODB");
  27
+        st.execute("DROP TABLE IF EXISTS spatial");
  28
+        st.execute("DROP TABLE IF EXISTS spatial_hatbox");
  29
+        st.execute("DROP TABLE IF EXISTS noindex");
  30
+        st.close();
  31
+        GeoDB.InitGeoDB(cx);
  32
+      
  33
+        st = cx.createStatement();
  34
+        st.execute("CREATE TABLE spatial (id INT AUTO_INCREMENT PRIMARY KEY, geom BLOB)");
  35
+        st.execute("INSERT INTO spatial (geom) VALUES (ST_GeomFromText('POINT(0 0)', 4326))");
  36
+        st.execute("INSERT INTO spatial (geom) VALUES (ST_GeomFromText('POINT(1 1)', 4326))");
  37
+        st.execute("INSERT INTO spatial (geom) VALUES (ST_GeomFromText('POINT(2 2)', 4326))");
  38
+        st.close();
  39
+    }
  40
+ 
  41
+    @Test
  42
+    public void test_extent() throws Exception {
  43
+        Statement st = cx.createStatement();
  44
+        ResultSet rs = st.executeQuery("select extent(geom) from spatial");
  45
+        rs.next();
  46
+        InputStream binaryStream = rs.getBinaryStream(1);
  47
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  48
+        st.close();
  49
+        assertThat(geometry.getArea(), is(4.0));
  50
+    }
  51
+
  52
+    @Test
  53
+    public void test_union() throws Exception {
  54
+        Statement st = cx.createStatement();
  55
+        ResultSet rs = st.executeQuery("select tt_union(geom) from spatial");
  56
+        rs.next();
  57
+        InputStream binaryStream = rs.getBinaryStream(1);
  58
+        Geometry geometry = new WKBReader().read(new InputStreamInStream(binaryStream));
  59
+        st.close();
  60
+        assertThat(geometry.getArea(), is(0.0));
  61
+    }
  62
+
  63
+
  64
+}
112  core/src/test/java/geodb/aggregate/ExtentTest.java
... ...
@@ -0,0 +1,112 @@
  1
+package geodb.aggregate;
  2
+
  3
+import static org.hamcrest.CoreMatchers.equalTo;
  4
+import static org.hamcrest.CoreMatchers.is;
  5
+import static org.hamcrest.CoreMatchers.not;
  6
+import static org.hamcrest.CoreMatchers.nullValue;
  7
+import static org.junit.Assert.assertFalse;
  8
+import static org.junit.Assert.assertThat;
  9
+import static org.junit.Assert.assertTrue;
  10
+
  11
+import geodb.GeoDB;
  12
+
  13
+import java.sql.SQLException;
  14
+
  15
+import org.junit.Before;
  16
+import org.junit.Test;
  17
+
  18
+import com.vividsolutions.jts.geom.Coordinate;
  19
+import com.vividsolutions.jts.geom.Geometry;
  20
+import com.vividsolutions.jts.geom.GeometryFactory;
  21
+import com.vividsolutions.jts.geom.LinearRing;
  22
+import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
  23
+
  24
+public class ExtentTest {
  25
+
  26
+	private Extent extent;
  27
+	
  28
+	private GeometryFactory factory;
  29
+	
  30
+	
  31
+	@Before
  32
+	public void init_extent() throws SQLException{
  33
+		extent=new Extent();
  34
+		extent.init(null);
  35
+	}
  36
+
  37
+	@Before
  38
+	public void init_geometry_factory(){
  39
+		factory=new GeometryFactory();
  40
+	}
  41
+	
  42
+	@Test
  43
+	public void extent_empty_collection() throws SQLException {
  44
+		Object result = extent.getResult();
  45
+		assertThat(result, is(nullValue()));
  46
+	}
  47
+
  48
+	@Test
  49
+	public void extent_one_point() throws SQLException{
  50
+		extent.add(createPoint(3, 5));
  51
+		Object result = extent.getResult();
  52
+		assertThat(result, is(not(nullValue())));
  53
+		Geometry envelope = GeoDB.gFromWKB((byte[]) result);
  54
+		assertThat(envelope.getArea(), is(0.0));
  55
+		assertThat(envelope, is(equalTo(createPoint(3, 5))));
  56
+	}
  57
+
  58
+
  59
+	@Test
  60
+	public void extent_two_points() throws SQLException{
  61
+		extent.add(createPoint(3, 5));
  62
+		extent.add(createPoint(5, 3));
  63
+		Object result = extent.getResult();
  64
+		assertThat(result, is(not(nullValue())));
  65
+		Geometry envelope = GeoDB.gFromWKB((byte[]) result);
  66
+		
  67
+		assertThat(envelope.getArea(), is(4.0));
  68
+		assertTrue(envelope.contains(createPoint(4, 4)));
  69
+		assertTrue(envelope.contains(createPoint(4.99999, 4.9999)));
  70
+		assertFalse(envelope.contains(createPoint(5.00001, 5.00001)));
  71
+	}
  72
+
  73
+	@Test
  74
+	public void extent_triangle_polygon() throws SQLException{
  75
+		extent.add(createPolygon(new Coordinate[]{new Coordinate(0, 1),new Coordinate(2, 2),new Coordinate(2, 0),new Coordinate(0, 1)}));
  76
+		Object result = extent.getResult();
  77
+		assertThat(result, is(not(nullValue())));
  78
+		Geometry envelope = GeoDB.gFromWKB((byte[]) result);
  79
+		
  80
+		assertThat(envelope.getArea(), is(4.0));
  81
+		assertTrue(envelope.contains(createPoint(1, 1)));
  82
+		assertTrue(envelope.contains(createPoint(1.99999, 1.9999)));
  83
+		assertFalse(envelope.contains(createPoint(2.00001, 2.00001)));
  84
+		assertTrue(envelope.contains(createPoint(0.00001, 0.00001)));
  85
+	}
  86
+	
  87
+	@Test
  88
+	public void extent_two_triangle_polygon() throws SQLException{
  89
+		extent.add(createPolygon(new Coordinate[]{new Coordinate(0, 1),new Coordinate(2, 2),new Coordinate(2, 0),new Coordinate(0, 1)}));
  90
+		extent.add(createPolygon(new Coordinate[]{new Coordinate(1, 1),new Coordinate(3, 2),new Coordinate(3, 0),new Coordinate(1, 1)}));
  91
+		Object result = extent.getResult();
  92
+		assertThat(result, is(not(nullValue())));
  93
+		Geometry envelope = GeoDB.gFromWKB((byte[]) result);
  94
+		
  95
+		assertThat(envelope.getArea(), is(6.0));
  96
+		assertTrue(envelope.contains(createPoint(1, 1)));
  97
+		assertTrue(envelope.contains(createPoint(2.99999, 1.9999)));
  98
+		assertFalse(envelope.contains(createPoint(3.00001, 2.00001)));
  99
+		assertTrue(envelope.contains(createPoint(0.00001, 0.00001)));
  100
+	}
  101
+
  102
+
  103
+	
  104
+	private Geometry createPoint(double x,double y){
  105
+		return factory.createPoint(new Coordinate(x, y));
  106
+	}
  107
+	
  108
+	private Geometry createPolygon(Coordinate[] coordinates){
  109
+		return factory.createPolygon(new LinearRing(new CoordinateArraySequence(coordinates), factory),null);
  110
+	}
  111
+
  112
+}
112  core/src/test/java/geodb/aggregate/UnionTest.java
... ...
@@ -0,0 +1,112 @@
  1
+package geodb.aggregate;
  2
+
  3
+import static org.hamcrest.CoreMatchers.equalTo;
  4
+import static org.hamcrest.CoreMatchers.is;
  5
+import static org.hamcrest.CoreMatchers.not;
  6
+import static org.hamcrest.CoreMatchers.nullValue;
  7
+import static org.junit.Assert.assertFalse;
  8
+import static org.junit.Assert.assertThat;
  9
+import static org.junit.Assert.assertTrue;
  10
+
  11
+import geodb.GeoDB;
  12
+
  13
+import java.sql.SQLException;
  14
+
  15
+import org.junit.Before;
  16
+import org.junit.Test;
  17
+
  18
+import com.vividsolutions.jts.geom.Coordinate;
  19
+import com.vividsolutions.jts.geom.Geometry;
  20
+import com.vividsolutions.jts.geom.GeometryFactory;
  21
+import com.vividsolutions.jts.geom.LinearRing;
  22
+import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
  23
+
  24
+public class UnionTest {
  25
+
  26
+	private Union union;
  27
+	
  28
+	private GeometryFactory factory;
  29
+	
  30
+	
  31
+	@Before
  32
+	public void init_extent() throws SQLException{
  33
+		union=new Union();
  34
+		union.init(null);
  35
+	}
  36
+
  37
+	@Before
  38
+	public void init_geometry_factory(){
  39
+		factory=new GeometryFactory();
  40
+	}
  41
+	
  42
+	@Test
  43
+	public void extent_empty_collection() throws SQLException {
  44
+		Object result = union.getResult();
  45
+		assertThat(result, is(nullValue()));
  46
+	}
  47
+
  48
+	@Test
  49
+	public void extent_one_point() throws SQLException{
  50
+		union.add(createPoint(3, 5));
  51
+		Object result = union.getResult();
  52
+		assertThat(result, is(not(nullValue())));
  53
+		Geometry envelope = GeoDB.gFromWKB((byte[]) result);
  54
+		assertThat(envelope.getArea(), is(0.0));
  55
+		assertThat(envelope, is(equalTo(createPoint(3, 5))));
  56
+	}
  57
+
  58
+
  59
+	@Test
  60
+	public void extent_two_points() throws SQLException{
  61
+		union.add(createPoint(3, 5));
  62
+		union.add(createPoint(5, 3));
  63
+		Object result = union.getResult();
  64
+		assertThat(result, is(not(nullValue())));
  65
+		Geometry geomResult = GeoDB.gFromWKB((byte[]) result);
  66
+		
  67
+		assertThat(geomResult.getArea(), is(0.0));
  68
+		assertTrue(geomResult.contains(createPoint(3, 5)));
  69
+		assertTrue(geomResult.contains(createPoint(5, 3)));
  70
+		assertFalse(geomResult.contains(createPoint(4, 4)));
  71
+	}
  72
+
  73
+	@Test
  74
+	public void extent_triangle_polygon() throws SQLException{
  75
+		union.add(createPolygon(new Coordinate[]{new Coordinate(0, 1),new Coordinate(2, 2),new Coordinate(2, 0),new Coordinate(0, 1)}));
  76
+		Object result = union.getResult();
  77
+		assertThat(result, is(not(nullValue())));
  78
+		Geometry envelope = GeoDB.gFromWKB((byte[]) result);
  79
+		
  80
+		assertThat(envelope.getArea(), is(2.0));
  81
+		assertTrue(envelope.contains(createPoint(1, 1)));
  82
+		assertTrue(envelope.contains(createPoint(1.99999, 1.9999)));
  83
+		assertFalse(envelope.contains(createPoint(2.00001, 2.00001)));
  84
+		assertFalse(envelope.contains(createPoint(0, 0)));
  85
+	}
  86
+	
  87
+	@Test
  88
+	public void extent_two_triangle_polygon() throws SQLException{
  89
+		union.add(createPolygon(new Coordinate[]{new Coordinate(0, 1),new Coordinate(2, 2),new Coordinate(2, 0),new Coordinate(0, 1)}));
  90
+		union.add(createPolygon(new Coordinate[]{new Coordinate(1, 1),new Coordinate(3, 2),new Coordinate(3, 0),new Coordinate(1, 1)}));
  91
+		Object result = union.getResult();
  92
+		assertThat(result, is(not(nullValue())));
  93
+		Geometry envelope = GeoDB.gFromWKB((byte[]) result);
  94
+		
  95
+		assertThat(envelope.getArea(), is(3.5));
  96
+		assertTrue(envelope.contains(createPoint(1, 1)));
  97
+		assertTrue(envelope.contains(createPoint(2.99999, 1.9999)));
  98
+		assertFalse(envelope.contains(createPoint(3.00001, 2.00001)));
  99
+		assertFalse(envelope.contains(createPoint(0, 0)));
  100
+	}
  101
+
  102
+
  103
+	
  104
+	private Geometry createPoint(double x,double y){
  105
+		return factory.createPoint(new Coordinate(x, y));
  106
+	}
  107
+	
  108
+	private Geometry createPolygon(Coordinate[] coordinates){
  109
+		return factory.createPolygon(new LinearRing(new CoordinateArraySequence(coordinates), factory),null);
  110
+	}
  111
+
  112
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.