Skip to content

Commit d3b4ae7

Browse files
committed
HHH-10157 - Postgis deserializer accepts EWKT
Backport of fix to 5.0 branch.
1 parent 052aad0 commit d3b4ae7

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

hibernate-spatial/hibernate-spatial.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ mavenPom {
2121

2222
dependencies {
2323
compile(project(':hibernate-core'))
24+
2425
compile([group: 'postgresql', name: 'postgresql', version: '8.4-701.jdbc4'])
25-
compile([group: 'org.geolatte', name: 'geolatte-geom', version: '1.0'])
26+
compile([group: 'org.geolatte', name: 'geolatte-geom', version: '1.0.1'])
2627

2728
compile(libraries.dom4j) {
2829
transitive = false

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public static Geometry from(Object object) {
7575
return null;
7676
}
7777
try {
78+
79+
if (object instanceof com.vividsolutions.jts.geom.Geometry) {
80+
return JTS.from( (com.vividsolutions.jts.geom.Geometry) object );
81+
}
7882
final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 );
7983
if ( object instanceof Blob ) {
8084
return decoder.decode( toByteBuffer( (Blob) object ) );

hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.geolatte.geom.codec.Wkb;
2020
import org.geolatte.geom.codec.WkbDecoder;
2121
import org.geolatte.geom.codec.WkbEncoder;
22+
import org.geolatte.geom.codec.Wkt;
23+
import org.geolatte.geom.codec.WktDecoder;
2224
import org.postgresql.util.PGobject;
2325

2426
import org.hibernate.type.descriptor.ValueBinder;
@@ -90,17 +92,23 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
9092
};
9193
}
9294

93-
private Geometry toGeometry(Object object) {
95+
public static Geometry toGeometry(Object object) {
9496
if ( object == null ) {
9597
return null;
9698
}
9799
ByteBuffer buffer = null;
98100
if ( object instanceof PGobject ) {
99-
buffer = ByteBuffer.from( ( (PGobject) object ).getValue() );
100-
final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 );
101-
return decoder.decode( buffer );
101+
String pgValue = ((PGobject) object ).getValue();
102+
if (pgValue.charAt( 0 ) == 'S') { // /we have a Wkt value
103+
final WktDecoder decoder = Wkt.newDecoder( Wkt.Dialect.POSTGIS_EWKT_1 );
104+
return decoder.decode(pgValue);
105+
}
106+
else {
107+
buffer = ByteBuffer.from( pgValue );
108+
final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 );
109+
return decoder.decode( buffer );
110+
}
102111
}
103112
throw new IllegalStateException( "Received object of type " + object.getClass().getCanonicalName() );
104-
105113
}
106114
}

hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.jboss.logging.Logger;
1919

2020
import org.hibernate.spatial.HSMessageLogger;
21+
import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor;
2122
import org.hibernate.spatial.testing.AbstractExpectationsFactory;
2223
import org.hibernate.spatial.testing.DataSourceUtils;
2324
import org.hibernate.spatial.testing.NativeSQLStatement;
@@ -244,17 +245,8 @@ protected NativeSQLStatement createNativeDisjointStatement(Geometry geom) {
244245
//remove redundancy with toGeometry function in PGGeometryTypeDescriptor
245246
@Override
246247
protected Geometry decode(Object object) {
247-
ByteBuffer buffer = null;
248-
if (object instanceof PGobject ) {
249-
buffer = ByteBuffer.from( ( (PGobject) object ).getValue() );
250-
} else if ( object instanceof byte[] ) {
251-
byte[] bytes = (byte[]) object;
252-
ByteBuffer.from( bytes );
253-
} else {
254-
throw new IllegalStateException( "Received object of type " + object.getClass().getCanonicalName() );
255-
}
256-
WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 );
257-
return JTS.to(decoder.decode( buffer));
248+
org.geolatte.geom.Geometry geometry = PGGeometryTypeDescriptor.toGeometry( object );
249+
return JTS.to( geometry );
258250
}
259251

260252
}

0 commit comments

Comments
 (0)