Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.postgresql.util.PGobject to org.postgis.Point;

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?

like image 627
pik4 Avatar asked Mar 24 '26 00:03

pik4


2 Answers

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

like image 196
Tom-db Avatar answered Mar 25 '26 14:03

Tom-db


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();
}
like image 33
pik4 Avatar answered Mar 25 '26 13:03

pik4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!