3

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]

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

My column is type of geography(Point,4326)
Actually I took PGobject geolocation from ResultSet and then I implemented the code below: String g = geolocation.getValue(); try { Geometry fr = new PGgeometry().geomFromString(g); Point p = fr.getPoint(0); this.geolocation = p; } catch (SQLException e) { e.printStackTrace(); } But I think that this code is not good code...
1

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.

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. It also doesn't hurt to mention why this answer is more appropriate than others.
1

That works for me and I think that is the proper solution.

String g = geolocation.getValue(); 
try { 
    Geometry fr = new PGgeometry().geomFromString(g); 
    Point p = fr.getPoint(0); this.geolocation = p; 
} catch (SQLException e) 
{ 
    e.printStackTrace(); 
}

1 Comment

New there is new PGgeometry(String) constructor, but the solution still works.
0

I haven't ever used postGIS, but I bet there's probably (for most of contexts) easier solution than installing postgis jdbc driver (as Marat proposed). You can always extend the type from PGobject and add it to your connection this way:

Connection conn = Database.getConnectionFromPool(); // you probably have something like this

((org.postgresql.PGConnection)conn).addDataType("PGgeometry", (Class<? extends PGobject>) Class.forName("org.postgis.PGgeometry"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.