[postgis-users] JAVA and post GIS

Emily Gouge egouge at refractions.net
Mon Jan 30 15:39:05 PST 2006


Fatemeh,

If you are using an attribute type of Circle you are not using the PostgreSQL data types and not the 
PostGIS geometry objects.  So if you want to use the PostGIS geometry functions you need to create 
PostGIS Geometry objects using the AddGeometryColumns() function.

For further information see:  http://postgis.refractions.net/docs/ch04.html#id2787299


The following code will return PGGeometries; however you must have the postgis.jar, 
postgresql-7.4.1-jdbc3.jar on your class path.  If you are using postgres 8.1 then you can have the 
postgres81 jar in replace of the 7.4.1 jar, however you will note that the addDataType function will 
be depreciated (the newer version takes a class instead of a String).

Class.forName("org.postgresql.Driver");
Connection c= DriverManager.getConnection("jdbc:postgresql://turtle:5432/cwb_devel","egouge", "" );
((PGConnection) c).addDataType("geometry", "org.postgis.PGgeometry" ); //for 8.1 change to 
addDataType("geometry", org.postgis.PGgeometry.class );

ResultSet rs = c.createStatement().executeQuery("SELECT the_geom from table_name");
while(rs.next()){
	System.out.println(rs.getObject(1).getClass());
}


-Emily

Fatemeh Abbasinejad wrote:
> Hi.
>   I sae the code and tried to execute it on my own tables.Ther were some problems:
>   First when trying to add data types it would give me a warning that the method addDataType has been deprecated.
>   Also when I tried to cast to PGeometry  it would throw "ClassCastException".Here is my code:
>    
>   ((PGConnection)conn).addDataType("geometry","org.postgis.PGgeometry");
> 
>   Statement s = conn.createStatement(); 
> ResultSet r = s.executeQuery("select center(geometry)as mycenter from land"); 
> 
>   while( r.next() ) 
>       { 
>         PGgeometry mycenter = (PGgeometry)r.getObject(1); 
>         System.out.println(mycenter.toString()); 
>       }
>    
>   I have a table called "land" which has an attribute "geometry" of type Circle.I want to retreive the center of the circle which is of type "Point".
>   Have I casted wrong? I also tried casting to "Point" but it would give the same exception. What should I do?
>    
>   
> 
> Emily Gouge <egouge at refractions.net> wrote:
>   Hi,
> 
> There is some documentation and an example on connecting to PostGIS using Java. See:
> http://postgis.refractions.net/docs/ch04.html#id2789526
> 
> You can add a datatype to the postgres connection and then the getObject() call will return a 
> PGGeometry object:
> ((org.postgresql.Connection)conn).addDataType("geometry","org.postgis.PGgeometry");
> 
> Additionally you can also check out the postgis_jts jar, which provides the ability to return a 
> JtsGeometry object (and subsequently to convert this object to a JTS Geometry object).
> 
> Emily
> 
> 
> 
> Fatemeh Abbasinejad wrote:
>> I am new to PostGIS. Does anyone know how we can retrieve our defined data types 
>> (like point,circle,...) from a ResultSet in java. I used getObject but it didn't work. 
>> -fatemeh
>>
>>
>> ---------------------------------
>> Bring words and photos together (easily) with
>> PhotoMail - it's free and works with Yahoo! Mail.
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> postgis-users mailing list
>> postgis-users at postgis.refractions.net
>> http://postgis.refractions.net/mailman/listinfo/postgis-users
> 
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
>   
> 
> 
> 		
> ---------------------------------
>  
>  What are the most popular cars? Find out at Yahoo! Autos 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users




More information about the postgis-users mailing list