Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException: org.postgresql.util.PGobject cannot be cast to org.postgis.Point

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]
like image 968
pik4 Avatar asked Jun 11 '26 11:06

pik4


2 Answers

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.

like image 67
Marat Avatar answered Jun 13 '26 00:06

Marat


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.

like image 31
Željko Trogrlić Avatar answered Jun 13 '26 00:06

Željko Trogrlić