Vitali Melamud
Vitali Melamud

Reputation: 1397

Reading Postgres geometry into byte[] with hibernate?

I'm trying to read a PostGIS geometry column and save it as a Java byte[] with hibernate.

As far as I know I should be storing it in Java the way it's represented in psql.

I manage to write it and even see it within psql but when I read it with the wkbreader - I get the following error:

com.vividsolutions.jts.io.ParseException: Unknown WKB type 48

Upvotes: 1

Views: 2325

Answers (3)

OLS
OLS

Reputation: 345

Run this command as Postgres

ALTER DATABASE table SET bytea_output TO 'escape';

then restart Postgres.

Upvotes: 0

dbaston
dbaston

Reputation: 1002

I have seen this error resolved by upgrading the JDBC driver version. I'm able to able to read PostGIS geometry into JTS with the following:

Query:

SELECT ST_AsBinary(geom) FROM mytable;

Code:

// myWKBReader is a JTS WKBReader
// myResultSet is a JDBC ResultSet
Geometry geom = myWKBReader.read(myResultSet.getBytes("st_asbinary"));

POM:

  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.2-1003-jdbc4</version>
  </dependency>

Upvotes: 1

Craig Ringer
Craig Ringer

Reputation: 325141

PostGIS exports a ST_AsBinary function or (if you want to preserve SRID) ST_AsEWKB.

You should store the results of this function, rather than trying to cast the display representation, and then load with ST_GeomFromWKB or ST_GeomFromEWKB (depending on whether you used the AsBinary or AsEWKB form on output).

Upvotes: 2

Related Questions