Skip to content

Commit

Permalink
added extra functions for hibernate spatial capabilities, closes #13,…
Browse files Browse the repository at this point in the history
… pull request submitted by jacques-finalist
  • Loading branch information
jdeolive committed Jan 18, 2012
1 parent d772e5d commit 1cd52a6
Show file tree
Hide file tree
Showing 10 changed files with 703 additions and 2 deletions.
124 changes: 124 additions & 0 deletions core/src/main/java/geodb/GeoDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.IntersectionMatrix;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.InputStreamInStream;
import com.vividsolutions.jts.io.OutputStreamOutStream;
Expand Down Expand Up @@ -880,6 +881,127 @@ public static byte[] ST_Simplify( byte[] wkb, double tol ) {
ST_SymDifference - Returns a geometry that represents the portions of A and B that do not intersect. It is called a symmetric difference because ST_SymDifference(A,B) = ST_SymDifference(B,A).
ST_Union - Returns a geometry that represents the point set union of the Geometries.
*/
public static Integer ST_Dimension(byte[] wkb) {
Geometry geometry = gFromWKB(wkb);
if (geometry != null) {
return geometry.getDimension();
}
return null;
}

public static byte[] ST_Boundary(byte[] wkb) {
Geometry geometry = gFromWKB(wkb);
if (geometry != null) {
Geometry boundary = geometry.getBoundary();
if (boundary != null) {
return gToWKB(boundary);
}
}
return null;
}

public static boolean ST_Relate(byte[] wkb1, byte[] wkb2, String intersectionPattern) {
Geometry geometry1 = gFromWKB(wkb1);
Geometry geometry2 = gFromWKB(wkb1);
if (geometry1 != null && geometry2 != null) {
return geometry1.relate(geometry2, intersectionPattern);
}
return false;
}

public static String ST_Relate(byte[] wkb1, byte[] wkb2) {
Geometry geometry1 = gFromWKB(wkb1);
Geometry geometry2 = gFromWKB(wkb1);
if (geometry1 != null && geometry2 != null) {
IntersectionMatrix result = geometry1.relate(geometry2);
return result.toString();
}
return null;
}

public static byte[] ST_ConvexHull(byte[] wkb) {
Geometry geometry = gFromWKB(wkb);
if (geometry != null) {
Geometry boundary = geometry.convexHull();
if (boundary != null) {
return gToWKB(boundary);
}
}
return null;
}

public static byte[] ST_Difference(byte[] wkb1, byte[] wkb2) {
if (wkb1 == null) {
return null;
}
if (wkb2 == null) {
return wkb1;
}
Geometry geometry1 = gFromWKB(wkb1);
Geometry geometry2 = gFromWKB(wkb2);
if (geometry1 == null) {
return null;
}
if (geometry2 == null) {
return wkb1;
}

return gToWKB(geometry1.difference(geometry2));
}

public static byte[] ST_Intersection(byte[] wkb1, byte[] wkb2) {
if (wkb1 == null || wkb2 == null) {
return null;
}
Geometry geometry1 = gFromWKB(wkb1);
Geometry geometry2 = gFromWKB(wkb2);
if (geometry1 == null || geometry2 == null) {
return null;
}

return gToWKB(geometry1.intersection(geometry2));
}

public static byte[] ST_SymDifference(byte[] wkb1, byte[] wkb2) {
if (wkb1 == null) {
return wkb2;
}
if (wkb2 == null) {
return wkb1;
}
Geometry geometry1 = gFromWKB(wkb1);
Geometry geometry2 = gFromWKB(wkb2);
if (geometry1 == null) {
return gToWKB(geometry2);
}
if (geometry2 == null) {
return gToWKB(geometry1);
}

return gToWKB(geometry1.symDifference(geometry2));
}

public static byte[] ST_Union(byte[] wkb1, byte[] wkb2) {
if (wkb1 == null) {
return wkb2;
}
if (wkb2 == null) {
return wkb1;
}
Geometry geometry1 = gFromWKB(wkb1);
Geometry geometry2 = gFromWKB(wkb2);
if (geometry1 == null) {
return gToWKB(geometry2);
}
if (geometry2 == null) {
return gToWKB(geometry1);
}

return gToWKB(geometry1.union(geometry2));
}




//
// Miscellaneous Functions
Expand Down Expand Up @@ -1425,4 +1547,6 @@ static String geotypeConstraint(String schema, String table, String column) {
}
return "ENFORCE_GEOTYPE_" + name;
}


}
34 changes: 34 additions & 0 deletions core/src/main/java/geodb/aggregate/Extent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package geodb.aggregate;

import java.sql.Connection;
import java.sql.SQLException;

import com.vividsolutions.jts.geom.Geometry;

public class Extent extends GeoAggregateFunction {

private Geometry result;

@Override
protected void add(Geometry geometry) {
if (result == null) {
result = geometry;
} else {
if (geometry != null) {
result = result.union(geometry.getEnvelope());
}
}
}

@Override
protected Geometry getGeometryResult() {
if (result != null) {
return result.getEnvelope();
}
return null;
}

public void init(Connection arg0) throws SQLException {
result = null;
}
}
58 changes: 58 additions & 0 deletions core/src/main/java/geodb/aggregate/GeoAggregateFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package geodb.aggregate;

import geodb.GeoDB;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;

import org.h2.api.AggregateFunction;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.InputStreamInStream;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKBReader;

public abstract class GeoAggregateFunction implements AggregateFunction {

private Geometry createGeometry(ByteArrayInputStream stream) {
InputStreamInStream inputStreamInStream = new InputStreamInStream(stream);
Geometry geometry = null;
try {
geometry = new WKBReader().read(inputStreamInStream);
} catch (IOException e) {
throw new IllegalArgumentException("given geometry can not be found", e);
} catch (ParseException e) {
throw new IllegalArgumentException("given geometry is not valid", e);
}
return geometry;

}

protected abstract void add(Geometry geometry);

protected abstract Geometry getGeometryResult();

public final void add(Object arg0) throws SQLException {
if (arg0 != null) {
Geometry geometry = createGeometry((ByteArrayInputStream) arg0);
if (geometry != null) {
add(geometry);
}
}
}

public final Object getResult() throws SQLException {
Geometry geometryResult = getGeometryResult();
if (geometryResult != null) {
return GeoDB.gToWKB(geometryResult);
}
return null;
}

public final int getType(int[] arg0) throws SQLException {
return Types.BLOB;
}

}
31 changes: 31 additions & 0 deletions core/src/main/java/geodb/aggregate/Union.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package geodb.aggregate;

import java.sql.Connection;
import java.sql.SQLException;

import com.vividsolutions.jts.geom.Geometry;

public class Union extends GeoAggregateFunction {

private Geometry result;

@Override
protected void add(Geometry geometry) {
if (result == null) {
result = geometry;
} else {
if (geometry != null) {
result = result.union(geometry);
}
}
}

@Override
protected Geometry getGeometryResult() {
return result;
}

public void init(Connection arg0) throws SQLException {
result = null;
}
}
10 changes: 10 additions & 0 deletions core/src/main/resources/geodb/geodb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,39 @@ CREATE ALIAS ST_AsEWKT FOR "geodb.GeoDB.ST_AsEWKT"
CREATE ALIAS ST_AsHexEWKB FOR "geodb.GeoDB.ST_AsHexEWKB"
CREATE ALIAS ST_AsText FOR "geodb.GeoDB.ST_AsText"
CREATE ALIAS ST_BBOX FOR "geodb.GeoDB.ST_BBox"
CREATE ALIAS ST_Boundary FOR "geodb.GeoDB.ST_Boundary"
CREATE ALIAS ST_Buffer FOR "geodb.GeoDB.ST_Buffer"
CREATE ALIAS ST_Centroid FOR "geodb.GeoDB.ST_Centroid"
CREATE ALIAS ST_Crosses FOR "geodb.GeoDB.ST_Crosses"
CREATE ALIAS ST_Contains FOR "geodb.GeoDB.ST_Contains"
CREATE ALIAS ST_ConvexHull FOR "geodb.GeoDB.ST_ConvexHull"
CREATE ALIAS ST_DWithin FOR "geodb.GeoDB.ST_DWithin"
CREATE ALIAS ST_Disjoint FOR "geodb.GeoDB.ST_Disjoint"
CREATE ALIAS ST_Distance FOR "geodb.GeoDB.ST_Distance"
CREATE ALIAS ST_Difference FOR "geodb.GeoDB.ST_Difference"
CREATE ALIAS ST_Dimension FOR "geodb.GeoDB.ST_Dimension"
CREATE ALIAS ST_Envelope FOR "geodb.GeoDB.ST_Envelope"
CREATE ALIAS ST_Equals FOR "geodb.GeoDB.ST_Equals"
CREATE ALIAS ST_GeoHash FOR "geodb.GeoDB.ST_GeoHash"
CREATE ALIAS ST_GeomFromEWKB FOR "geodb.GeoDB.ST_GeomFromEWKB"
CREATE ALIAS ST_GeomFromEWKT FOR "geodb.GeoDB.ST_GeomFromEWKT"
CREATE ALIAS ST_GeomFromText FOR "geodb.GeoDB.ST_GeomFromText"
CREATE ALIAS ST_GeomFromWKB FOR "geodb.GeoDB.ST_GeomFromWKB"
CREATE ALIAS ST_Intersection FOR "geodb.GeoDB.ST_Intersection"
CREATE ALIAS ST_Intersects FOR "geodb.GeoDB.ST_Intersects"
CREATE ALIAS ST_IsEmpty FOR "geodb.GeoDB.ST_IsEmpty"
CREATE ALIAS ST_IsSimple FOR "geodb.GeoDB.ST_IsSimple"
CREATE ALIAS ST_IsValid FOR "geodb.GeoDB.ST_IsValid"
CREATE ALIAS ST_MakePoint FOR "geodb.GeoDB.ST_MakePoint"
CREATE ALIAS ST_MakeBox2D FOR "geodb.GeoDB.ST_MakeBox2D"
CREATE ALIAS ST_Overlaps FOR "geodb.GeoDB.ST_Overlaps"
CREATE ALIAS ST_Relate FOR "geodb.GeoDB.ST_Relate"
CREATE ALIAS ST_SRID FOR "geodb.GeoDB.ST_SRID"
CREATE ALIAS ST_SetSRID FOR "geodb.GeoDB.ST_SetSRID"
CREATE ALIAS ST_Simplify FOR "geodb.GeoDB.ST_Simplify"
CREATE ALIAS ST_SymDifference FOR "geodb.GeoDB.ST_SymDifference"
CREATE ALIAS ST_Touches FOR "geodb.GeoDB.ST_Touches"
CREATE ALIAS ST_Union FOR "geodb.GeoDB.ST_Union"
CREATE ALIAS ST_Within FOR "geodb.GeoDB.ST_Within"
CREATE ALIAS Version FOR "geodb.GeoDB.Version"
CREATE DOMAIN POINT AS BLOB
Expand All @@ -46,3 +54,5 @@ CREATE DOMAIN MULTILINESTRING AS BLOB
CREATE DOMAIN MULTIPOLYGON AS BLOB
CREATE DOMAIN GEOMETRYCOLLECTION AS BLOB
CREATE DOMAIN GEOMETRY AS BLOB
CREATE AGGREGATE ST_Extent FOR "geodb.aggregate.Extent"
CREATE AGGREGATE ST_Union_Aggregate FOR "geodb.aggregate.Union"
Loading

0 comments on commit 1cd52a6

Please sign in to comment.