3

Do you know how can convert the type of PGobject to Point in Java? Actually PGobject is an object like this: PGobject geolocation:

type: geography
value: 0101000020E6100000C006335CD3043840504BBDB89EC14140

How can convert this number into Point?

pik4
  • 1,283
  • 3
  • 21
  • 56

2 Answers2

3

Do you mean a jts Point?

If yes so use WkbReader, because your geometry is in well-know-binary format:

import com.vividsolutions.jts.io.WKBReader;


WKBReader wkbReader = new WKBReader();
byte[] geom = wkbReader.hexToBytes('0101000020E6100000C006335CD3043840504BBDB89EC14140');
System.out.println(wkbReader.read(geom));     
// prints POINT (24.01885010000001 35.5126563)

The result is of type com.vividsolutions.jts.geom.Point

Tom-db
  • 6,528
  • 3
  • 30
  • 44
2

My solution is below and it works fine, but I think that your solution is better

String g = geolocation.getValue();
try {
    Geometry fr = new PGgeometry().geomFromString(g);
    Point p = fr.getPoint(0);
    this.geolocation = p;
} catch (SQLException e) {
    e.printStackTrace();
}
pik4
  • 1,283
  • 3
  • 21
  • 56
  • 1
    My solution is actually wrong, because it returns a Point of type com.vividsolutions.jts.geom.Point, but you need a Point of type PGgeometry (from the Postgis driver). So your solution is the correct one – Tom-db Jun 20 '16 at 11:19