diff --git a/hibernate-spatial/hibernate-spatial.gradle b/hibernate-spatial/hibernate-spatial.gradle index c20609ee1a..64187b3788 100644 --- a/hibernate-spatial/hibernate-spatial.gradle +++ b/hibernate-spatial/hibernate-spatial.gradle @@ -21,8 +21,9 @@ mavenPom { dependencies { compile(project(':hibernate-core')) + compile([group: 'postgresql', name: 'postgresql', version: '8.4-701.jdbc4']) - compile([group: 'org.geolatte', name: 'geolatte-geom', version: '1.0']) + compile([group: 'org.geolatte', name: 'geolatte-geom', version: '1.0.1']) compile(libraries.dom4j) { transitive = false diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java index 23e7586a7a..6fcd9b6467 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java @@ -75,6 +75,10 @@ public class GeoDbWkb { return null; } try { + + if (object instanceof com.vividsolutions.jts.geom.Geometry) { + return JTS.from( (com.vividsolutions.jts.geom.Geometry) object ); + } final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); if ( object instanceof Blob ) { return decoder.decode( toByteBuffer( (Blob) object ) ); diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java index e42f188811..4bd3d967a3 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java @@ -19,6 +19,8 @@ import org.geolatte.geom.Geometry; import org.geolatte.geom.codec.Wkb; import org.geolatte.geom.codec.WkbDecoder; import org.geolatte.geom.codec.WkbEncoder; +import org.geolatte.geom.codec.Wkt; +import org.geolatte.geom.codec.WktDecoder; import org.postgresql.util.PGobject; import org.hibernate.type.descriptor.ValueBinder; @@ -90,17 +92,23 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor { }; } - private Geometry toGeometry(Object object) { + public static Geometry toGeometry(Object object) { if ( object == null ) { return null; } ByteBuffer buffer = null; if ( object instanceof PGobject ) { - buffer = ByteBuffer.from( ( (PGobject) object ).getValue() ); - final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); - return decoder.decode( buffer ); + String pgValue = ((PGobject) object ).getValue(); + if (pgValue.charAt( 0 ) == 'S') { // /we have a Wkt value + final WktDecoder decoder = Wkt.newDecoder( Wkt.Dialect.POSTGIS_EWKT_1 ); + return decoder.decode(pgValue); + } + else { + buffer = ByteBuffer.from( pgValue ); + final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); + return decoder.decode( buffer ); + } } throw new IllegalStateException( "Received object of type " + object.getClass().getCanonicalName() ); - } } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java index 9ee12891fe..e50b2b96bf 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java @@ -18,6 +18,7 @@ import org.postgresql.util.PGobject; import org.jboss.logging.Logger; import org.hibernate.spatial.HSMessageLogger; +import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor; import org.hibernate.spatial.testing.AbstractExpectationsFactory; import org.hibernate.spatial.testing.DataSourceUtils; import org.hibernate.spatial.testing.NativeSQLStatement; @@ -244,17 +245,8 @@ public class PostgisExpectationsFactory extends AbstractExpectationsFactory { //remove redundancy with toGeometry function in PGGeometryTypeDescriptor @Override protected Geometry decode(Object object) { - ByteBuffer buffer = null; - if (object instanceof PGobject ) { - buffer = ByteBuffer.from( ( (PGobject) object ).getValue() ); - } else if ( object instanceof byte[] ) { - byte[] bytes = (byte[]) object; - ByteBuffer.from( bytes ); - } else { - throw new IllegalStateException( "Received object of type " + object.getClass().getCanonicalName() ); - } - WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); - return JTS.to(decoder.decode( buffer)); + org.geolatte.geom.Geometry geometry = PGGeometryTypeDescriptor.toGeometry( object ); + return JTS.to( geometry ); } }