Hibernate Spatial cannot parse WKT from PostGIS

Description

During the analysis of a problem we have, I had a look on the code to convert a Postgres object into a GeoLatte geometry. That would be org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor. The method toGeometry tries to parse the PGobject either as WKT or WKB. But the check is flawed, as it expects every WKT to begin with an SRID and therfore is only valid for EWKT. A geometry without SRID will throw a NumberFormatException as it tries to parse a WKB:

java.lang.NumberFormatException: For input string: "PO"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at org.geolatte.geom.ByteBuffer.from(ByteBuffer.java:78)
at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor.toGeometry(PGGeometryTypeDescriptor.java:116)
at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor$2.doExtract(PGGeometryTypeDescriptor.java:88)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:258)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:254)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:244)
at org.hibernate.loader.hql.QueryLoader.getResultRow(QueryLoader.java:453)

The 'PO' comes from POLYGON((3.332144E7 5979576.800000002,3.332144E7 5979767.2,3.33216928E7 5979767.2,3.33216928E7 5979576.800000002,3.332144E7 5979576.800000002)).
I know that PostGIS 2.0 is not supported by Hibernate Spatial yet, but this problems seems to be more general to me.
The code in question is:

PGGeometryTypeDescriptor#toGeometry

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public static Geometry toGeometry(Object object) { if ( object == null ) { return null; } ByteBuffer buffer = null; if ( object instanceof PGobject ) { 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() ); }

Environment

Hibernate 5.2.2, PostGIS 2.1

Status

Assignee

Karel Maesen

Reporter

Marko Skočibušić

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.2

Priority

Major
Configure