Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced extra functions for Hibernate Spatial compatibility
- Loading branch information
jacques
committed
Jan 14, 2012
1 parent
d772e5d
commit 7ff139e
Showing
9 changed files
with
710 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
core/src/main/java/geodb/aggregate/GeoAggregateFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.