[postgis-users] Buffering with JDBC

Leticia lgomez at itba.edu.ar
Fri May 11 08:49:05 PDT 2007


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-bounces at postgis.refractions.net
[mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of
shojka at web.de
Sent: Friday, May 11, 2007 11:45 AM
To: postgis-users at 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

 


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


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; 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20070511/041c79d6/attachment.html>


More information about the postgis-users mailing list