Discussion:
Buffering with JDBC
s***@web.de
2007-05-11 14:45:24 UTC
Permalink
import java.sql.*; import java.util.*; import java.lang.*; import org.postgis.*;

public class JavaGIS { public static void main(String[] args)

{

java.sql.Connection conn;

try

{

Class.forName("org.postgresql.Driver");

String url = "jdbc:postgresql://localhost:5432/database"; conn = DriverManager.getConnection(url, "postgres", "");

((org.postgresql.jdbc2.Jdbc2Connection)conn).addDataType("Geometry","org.postgis.PGgeometry");
((org.postgresql.jdbc2.Jdbc2Connection)conn).addDataType("box3d","org.postgis.PGbox3d");

Statement s = conn.createStatement();

ResultSet r = s.executeQuery("SELECT Buffer(GeometryFromText('POINT(10000 20000)',-1),20)");

while( r.next() )

{

PGgeometry geom= (PGgeometry)r.getObject(1);

if( geom.getType() = Geometry.POLYGON )

{

Polygon pl = (Polygon)geom.getGeometry();

for( int r = 0; r < pl.numRings(); r++ )

{

LinearRing rng = pl.getRing(r);

System.out.println("Ring: " + r);

for( int p = 0; p < rng.numPoints(); p++ )

{

Point pt = rng.getPoint(p);

System.out.println("Point: " + p);

System.out.println(pt.toString());

}

}

}

}

s.close();

conn.close();

}

catch( Exception e )

{

e.printStackTrace();

}

}

}

Dear PostGis users,

I want to use buffer function from POSTGIS but I simply cannot convert the result of query in a PGgeometry object. The line:

PGgeometry geom= (PGgeometry)r.getObject(1);

results always in:

java.lang.ClassCastException: org.postgresql.util.PGobject

I got result as a strine when i insert in qery function Astext(), but when I want to work with geometry I am helpless. Has anyone an idea how this problem could be solved?

Thanking in advance



Erweitern Sie FreeMail zu einem noch leistungsstärkeren E-Mail-Postfach!
Mehr Infos unter *http://produkte.web.de/club/?mc=021131* [http://produkte.web.de/club/?mc=021131]
Leticia
2007-05-11 15:49:05 UTC
Permalink
Hi, I don’t know if it is the best way, but I implement this class:





import java.sql.*;

import org.postgis.PGgeometry;

import com.vividsolutions.jts.geom.*;

import com.vividsolutions.jts.io.*;





public class GeometryReader

{

static int SRID= 0;

static GeometryFactory fact= new GeometryFactory(new
PrecisionModel(1E10), SRID);





static public Geometry getGeometry(Object obj) throws SQLException,
ParseException

{

return getGeometry(((PGgeometry)obj).toString());

}



static public Geometry getGeometry(String geometryStr) throws
SQLException, ParseException

{



WKTReader r= new WKTReader( fact );

Geometry geom;



// ADD SRID

if (geometryStr.indexOf(';') != -1)

{

String []temp= PGgeometry.splitSRID(geometryStr);

int srid= Integer.parseInt(temp[0].substring(5));

geom= (Geometry) r.read(temp[1]);

geom.setSRID(SRID);

}

else

geom= (Geometry) r.read(geometryStr);



return geom;

}

}



…

And in every place where I need to recover a geometry (Polygon, Linestring,
etc) I invoke GeometryReader.getGeometry( );.

i.e:



public class Rta

{

public static void main(String[] args) throws ClassNotFoundException,
SQLException, ParseException

{

Class.forName("org.postgresql.Driver");



Connection conn= DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/proof", "postgres", "postgres" );



String queryDML= "SELECT geometryColumn FROM region LIMIT 10";

Statement stmt= conn.createStatement();



ResultSet rs= stmt.executeQuery(queryDML);

while ( rs.next() )

{

Geometry geometryRead=
GeometryReader.getGeometry(rs.getObject(1));

System.out.println(geometryRead.getArea());



}



….

}



}







Then I obtain a Geometry reference.





Leticia





_____

From: postgis-users-***@postgis.refractions.net
[mailto:postgis-users-***@postgis.refractions.net] On Behalf Of
***@web.de
Sent: Friday, May 11, 2007 11:45 AM
To: postgis-***@postgis.refractions.net
Subject: [postgis-users] Buffering with JDBC





import java.sql.*; import java.util.*; import java.lang.*; import
org.postgis.*;

public class JavaGIS { public static void main(String[] args)

{

java.sql.Connection conn;

try

{

Class.forName("org.postgresql.Driver");

String url = "jdbc:postgresql://localhost:5432/database"; conn =
DriverManager.getConnection(url, "postgres", "");


((org.postgresql.jdbc2.Jdbc2Connection)conn).addDataType("Geometry","org.pos
tgis.PGgeometry");
((org.postgresql.jdbc2.Jdbc2Connection)conn).addDataTy
pe("box3d","org.postgis.PGbox3d");

Statement s = conn.createStatement();

ResultSet r = s.executeQuery("SELECT
Buffer(GeometryFromText('POINT(10000 20000)',-1),20)");

while( r.next() )

{

PGgeometry geom= (PGgeometry)r.getObject(1);

if( geom.getType() = Geometry.POLYGON )

{

Polygon pl = (Polygon)geom.getGeometry();

for( int r = 0; r < pl.numRings(); r++ )

{

LinearRing rng = pl.ge tRing(r);

System.out.println("Ring: " + r);

for( int p = 0; p < rng.numPoints(); p++ )

{

Point pt = rng.getPoint(p);

System.out.println("Point: " + p);

System.out.println(pt.toString());

}

}

}

}

s.close();

conn.close();

}

catch( Exception e )

{

e.printStackTrace();

}

}

}

Dear PostGis users,

I want to use buf fer function from POSTGIS but I simply cannot convert the
result of query in a PGgeometry object. The line:

PGgeometry geom= (PGgeometry)r.getObject(1);

results always in:

java.lang.ClassCastException: org.postgresql.util.PGobject

I got result as a strine when i insert in qery function Astext(), but when I
want to work with geometry I am helpless. Has anyone an idea how this
problem could be solved?

Thanking in advance




<Loading Image...>


Erweitern Sie FreeMail zu einem noch leistungsstärkeren E-Mail-Postfach!
Mehr Infos unter <http://produkte.web.de/club/?mc=021131>
http://produkte.web.de/club/?mc=021131& nbsp;
backo lodovik
2007-05-11 17:22:34 UTC
Permalink
Thank you for your answer!!

I would like to ask you where can I find this vivdsolutions libraries?

Also, I would like to ask if someone knows what is the reason for exception, I coppied the code from the PostGIS maual but it does not work although it should have!!





Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
*http://smartsurfer.web.de/?mc=100071&distributionid=000000000066* [http://smartsurfer.web.de/?mc=100071&distributionid=000000000066]
Leticia
2007-05-11 17:27:59 UTC
Permalink
Now I am using this version http://www.vividsolutions.com/jump/





Leticia

_____

From: postgis-users-***@postgis.refractions.net
[mailto:postgis-users-***@postgis.refractions.net] On Behalf Of backo
lodovik
Sent: Friday, May 11, 2007 2:23 PM
To: postgis-***@postgis.refractions.net
Subject: [postgis-users] Buffering with JDBC



Thank you for your answer!!

I would like to ask you where can I find this vivdsolutions libraries?

Also, I would like to ask if someone knows what is the reason for exception,
I coppied the code from the PostGIS maual but it does not work although it
should have!!






<https://img.web.de/p.gif>


Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
<http://smartsurfer.web.de/?mc=100071&distributionid=000000000066>
http://smartsurfer.web.de/?mc=100071&distributionid=000000000066
Obe, Regina
2007-05-11 20:07:27 UTC
Permalink
It might have to do with the version you are using is my guess.

Does your cast work without buffering? E.g. if you did
ResultSet r = s.executeQuery("SELECT GeometryFromText('POINT(10000
20000)',-1)");


I think there was a change between 8.1 and 8.2 so for 8.1 you might have
to do
addDataType("geometry", org.postgis.PGgeometry.class )
As noted in this thread
http://postgis.refractions.net/pipermail/postgis-users/2006-January/0109
48.html

Hope that hleps,
Regina

________________________________

From: postgis-users-***@postgis.refractions.net
[mailto:postgis-users-***@postgis.refractions.net] On Behalf Of
backo lodovik
Sent: Friday, May 11, 2007 1:23 PM
To: postgis-***@postgis.refractions.net
Subject: [postgis-users] Buffering with JDBC



Thank you for your answer!!

I would like to ask you where can I find this vivdsolutions libraries?

Also, I would like to ask if someone knows what is the reason for
exception, I coppied the code from the PostGIS maual but it does not
work although it should have!!





<https://img.web.de/p.gif>
Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
http://smartsurfer.web.de/?mc=100071&distributionid=000000000066
<http://smartsurfer.web.de/?mc=100071&distributionid=000000000066>


-----------------------------------------
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.
Obe, Regina
2007-05-11 20:12:01 UTC
Permalink
Slight correction I think

addDataType("geometry", org.postgis.PGgeometry.class )


Is the newer way. But someone more familiar with this topic correct me.

Thanks,
Regina
________________________________

From: Obe, Regina
Sent: Friday, May 11, 2007 4:07 PM
To: 'PostGIS Users Discussion'
Subject: RE: [postgis-users] Buffering with JDBC


It might have to do with the version you are using is my guess.

Does your cast work without buffering? E.g. if you did
ResultSet r = s.executeQuery("SELECT GeometryFromText('POINT(10000
20000)',-1)");


I think there was a change between 8.1 and 8.2 so for 8.1 you might have
to do
addDataType("geometry", org.postgis.PGgeometry.class )
As noted in this thread
http://postgis.refractions.net/pipermail/postgis-users/2006-January/0109
48.html

Hope that hleps,
Regina

________________________________

From: postgis-users-***@postgis.refractions.net
[mailto:postgis-users-***@postgis.refractions.net] On Behalf Of
backo lodovik
Sent: Friday, May 11, 2007 1:23 PM
To: postgis-***@postgis.refractions.net
Subject: [postgis-users] Buffering with JDBC



Thank you for your answer!!

I would like to ask you where can I find this vivdsolutions libraries?

Also, I would like to ask if someone knows what is the reason for
exception, I coppied the code from the PostGIS maual but it does not
work although it should have!!





<https://img.web.de/p.gif>
Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
http://smartsurfer.web.de/?mc=100071&distributionid=000000000066
<http://smartsurfer.web.de/?mc=100071&distributionid=000000000066>
-----------------------------------------
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.
s***@web.de
2007-05-14 10:14:26 UTC
Permalink
Thank you for your answers. I have POSTGRESQL version 8.1. All proposed changes did not work. As a result I get an exception caused by unknown type (this should be geometry) regardless if I use buffer or any other function. I would be very gateful if someone could give me more ideas how to solve this problem.



Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
*http://smartsurfer.web.de/?mc=100071&distributionid=000000000066* [http://smartsurfer.web.de/?mc=100071&distributionid=000000000066]
Obe, Regina
2007-05-14 14:32:22 UTC
Permalink
Shotka,

Just noticed you are doing this
Class.forName("org.postgresql.Driver");

String url = "jdbc:postgresql://localhost:5432/database"; conn =
DriverManager.getConnection(url, "postgres", "");


((org.postgresql.jdbc2.Jdbc2Connection)conn).addDataType("Geometry","org
..postgis.PGgeometry");
((org.postgresql.jdbc2.Jdbc2Connection)conn).addDataTy
pe("box3d","org.postgis.PGbox3d");

Statement s = conn.createStatement();



and I'm used to seeing people do this



Class.forName("org.postgresql.Driver");

String url = "jdbc:postgresql://localhost:5432/database"; conn =
DriverManager.getConnection(url, "postgres", "");

((org.postgresql.PGConnection)
conn).addDataType("geometry",org.postgis.PGgeometry.class);
((org.postgresql.PGConnection) conn).addDataTy
pe("box3d",org.postgis.PGbox3d.class);

Statement s = conn.createStatement();



or this

((org.postgresql.Connection)
conn).addDataType("geometry",org.postgis.PGgeometry.class);
((org.postgresql.Connection) conn).addDataTy
pe("box3d",org.postgis.PGbox3d.class);

or this (with or without the PGConnection)

((org.postgresql.Connection)
conn).addDataType("geometry","org.postgis.PGgeometry");
((org.postgresql.Connection) conn).addDataTy
pe("box3d","org.postgis.PGbox3d");

I'm really not sure the difference between all of them except I assume
it depends on the postgresql driver version and maybe your approach is
newer or older. Have you tried the other ways?

Also you have the geometry case for addType in upper case, where as it
is normally written in lower case. I'm not sure what sorts of problems
that may or may not cause.



Also if you do something like

out.println(r.getObject(1).getClass().getName());



What do you get?

Hope that helps,

Regina


________________________________

From: postgis-users-***@postgis.refractions.net
[mailto:postgis-users-***@postgis.refractions.net] On Behalf Of
***@web.de
Sent: Monday, May 14, 2007 6:14 AM
To: postgis-***@postgis.refractions.net
Subject: [postgis-users] Re: Buffering with JDBC


Thank you for your answers. I have POSTGRESQL version 8.1. All proposed
changes did not work. As a result I get an exception caused by unknown
type (this should be geometry) regardless if I use buffer or any other
function. I would be very gateful if someone could give me more ideas
how to solve this problem.


<https://img.web.de/p.gif>
Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
http://smartsurfer.web.de/?mc=100071&d istributionid=000000000066
<http://smartsurfer.web.de/?mc=100071&distributionid=000000000066>


-----------------------------------------
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.
Obe, Regina
2007-05-14 18:42:34 UTC
Permalink
Shotka,

I'm not sure if my comment about the case of your "Geometry" got buried in my email. But now thinking about it, I think its important to make sure you got that part right.

Make sure that you have
addDataType("geometry",org.postgis.PGgeometry.class);

and not
addDataType("Geometry",org.postgis.PGgeometry.class);

I'm not absolutely sure, but I think the case of the type string has to match the type that is returned by PGObject otherwise it will not know to map it to the PostGIS class and will just keep it unknown.

Also can you try the below out.println and let us know what that returns.

out.println(((PGobject) r.getObject(1)).getType());

Hope that helps,
Regina




-----------------------------------------
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.
Obe, Regina
2007-05-15 12:48:11 UTC
Permalink
Yes I forgot that that library is not usually imported since its used
transparently by the jdbc driver. Try instead

out.println(((org.postgresql.utils.PGobject) r.getObject(1)).getType());






------------------------------------
Regina,

I tried all options and I always get

for

out.println(((PGobject) r.getObject(1)).getType());

an error : PGobject cannot find symbol.

I must also say that I use NetBeans IDE 4.1. and jdbc2. Does this play
any role?

Thanks




-----------------------------------------
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.
s***@web.de
2007-05-14 16:13:11 UTC
Permalink
Thank you for your help. I have tried all options but it simply will not work.

out.println(r.getObject(1).getClass().getName());

gives: org.postgresql.util.PGobject

and

geom=(org.postgis.PGgeometry)r1.getObject(1);

throws

java.lang.ClassCastException: org.postgresql.util.PGobject



Erweitern Sie FreeMail zu einem noch leistungsstärkeren E-Mail-Postfach!
Mehr Infos unter *http://produkte.web.de/club/?mc=021131* [http://produkte.web.de/club/?mc=021131]
backo lodovik
2007-05-15 12:01:31 UTC
Permalink
Regina,

I tried all options and I always get

for

out.println(((PGobject) r.getObject(1)).getType());

an error : PGobject cannot find symbol.

I must also say that I use NetBeans IDE 4.1. and jdbc2. Does this play any role?

Thanks





Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
*http://smartsurfer.web.de/?mc=100071&distributionid=000000000066* [http://smartsurfer.web.de/?mc=100071&distributionid=000000000066]
s***@web.de
2007-05-15 18:21:56 UTC
Permalink
Regina



For

out.println(((org.postgresql.utils.PGobject) r.getObject(1)).getType());


I get empty string



Erweitern Sie FreeMail zu einem noch leistungsstärkeren E-Mail-Postfach!
Mehr Infos unter *http://produkte.web.de/club/?mc=021131* [http://produkte.web.de/club/?mc=021131]
Continue reading on narkive:
Loading...