Uploaded image for project: 'Hibernate ORM'
  1. HHH-11197

Hibernate Spatial cannot parse WKT from PostGIS


    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.2.2
    • Fix Version/s: 5.1.3, 5.2.5, 5.0.12
    • Component/s: hibernate-spatial
    • Labels:
    • Environment:
      Hibernate 5.2.2, PostGIS 2.1
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
    • Sprint:


      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:

      	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() );




            • Votes:
              0 Vote for this issue
              1 Start watching this issue


              • Created: