Question

I need to store PostgreSQL's geometry(MultiPolygon) type variable into a java variable this is because im trying to add the geometry column to a non-spatial table for querying a map using mapserver

i tried to store it in a String which caused an error.

Please suggest me a data type in Java so that i can store the value in the geometry column of a spatial database.

here is my code snippet :

    String query="select geom from apmandal where mandalname='"+mname+"'";
    ResultSet rs=st.executeQuery(query);
    rs.next();
    Geometry geomval=rs.getXXX();

please suggest a getXXX() method so that i can retrieve the geometry column value

Thanks in advance

Edit from comment:

String query = "select geom from apmandal order by area desc;"; 
ResultSet rs = st0.executeQuery(query); 
rs.next(); 
String geomval = rs.getString(1).toString(); 
String newquery = "insert into test(mname, geom) values('ZAHEERABAD','"+geomval+"');"; 
int tel = st1.executeUpdate(newquery); 
System.out.println("inserted "+tel+" record");
Was it helpful?

Solution

You could simply cast to text representation:

String query="select geom::text from apmandal where mandalname='"+mname+"'";

Then you can treat it just like any other text.
I quote the PostGIS manual here:

At least as of PostgreSQL 8.3 - Everything can be CAST to text (presumably because of the magical unknown type), so no defined CASTS for that need to be present for you to CAST an object to text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top