Skip to content

Commit

Permalink
Introduced extra functions for Hibernate Spatial compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jacques committed Jan 14, 2012
1 parent d772e5d commit 7ff139e
Show file tree
Hide file tree
Showing 9 changed files with 710 additions and 0 deletions.
131 changes: 131 additions & 0 deletions core/src/main/java/geodb/ExtraSpatialFunctions.java
@@ -0,0 +1,131 @@
package geodb;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.IntersectionMatrix;

public class ExtraSpatialFunctions {

private ExtraSpatialFunctions() {
}

public static Integer dimension(byte[] wkb) {
Geometry geometry = GeoDB.gFromWKB(wkb);
if (geometry != null) {
return geometry.getDimension();
}
return null;
}

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

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

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

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

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

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

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

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

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

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

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

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

}
34 changes: 34 additions & 0 deletions core/src/main/java/geodb/aggregate/Extent.java
@@ -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
@@ -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
@@ -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
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.ExtraSpatialFunctions.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.ExtraSpatialFunctions.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.ExtraSpatialFunctions.difference"
CREATE ALIAS ST_Dimension FOR "geodb.ExtraSpatialFunctions.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.ExtraSpatialFunctions.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.ExtraSpatialFunctions.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.ExtraSpatialFunctions.symdifference"
CREATE ALIAS ST_Touches FOR "geodb.GeoDB.ST_Touches"
CREATE ALIAS ST_Union FOR "geodb.ExtraSpatialFunctions.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 EXTENT FOR "geodb.aggregate.Extent"
CREATE AGGREGATE ST_UNION FOR "geodb.aggregate.Union"

0 comments on commit 7ff139e

Please sign in to comment.