I take columns from result set and retrieve geolocation as PGObject. In the next step I want to convert it to org.postgis.Point but I get the following exception.
Do you know how can get latitude and longitude from PGObject? Actually I want to convert PGObject type to org.postgis.Point.
I tried this, but does not work. In the result set:
(PGobject) rs.getObject("geolocation"),
In the constructor of object.
this.geolocation = (Point) geolocation;
Exception:
java.lang.ClassCastException: org.postgresql.util.PGobject cannot be cast to org.postgis.Point
at com.kaloudia.api.domain.custom.CompanyResultView.<init>(CompanyResultView.java:87) ~[classes/:na]
at com.kaloudia.api.manager.SearchManager.lambda$searchCompanies$0(SearchManager.java:166) ~[classes/:na]
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
If you want to use org.postgis.Point yout need to install postgis plugin to your postgres and postgis jdbc driver as well as postgres jdbc driver.
After that, if your column type is postgis point, you will be able to do PGgeometry geolocation = (PGgeometry) rs.getObject("geolocation");. Note that postgres geometry types are not the same with postGIS geometry types.
And finally you should do an extra action: org.postgis.Geometry geom = geolocation.getGeometry(); The geom should be of org.postgis.Point type.
This is the shortest version without any unnecessary object creation or method calls:
PGobject pGobject = (PGobject) rs.getObject("geolocation");
Point postgisPoint = (Point) PGgeometry.geomFromString(pGobject.getValue());
To make it work, you will need both PotgreSQL and PostGIS jars; if you use Maven or similar, note that PostGIS moved from org to net domain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With