|
From: James M. <jma...@us...> - 2001-12-04 09:24:12
|
Update of /cvsroot/geotools/geotools/src/uk/ac/leeds/ccg/sfsql
In directory usw-pr-cvs1:/tmp/cvs-serv6155
Added Files:
PostGISDataSource.java
Log Message:
Provides a connection to a postgis database:
MultiPolygons only at the moment.
--- NEW FILE: PostGISDataSource.java ---
/*
* PostGISDataSource.java
*
* Created on 01 December 2001, 00:15
*/
package uk.ac.leeds.ccg.sfsql;
import java.sql.*;
import org.postgis.*;
import uk.ac.leeds.ccg.geotools.*;
/**
*
* @author James Macgill
* @version
*/
public class PostGISDataSource {
Connection connection;
/** Creates new PostGISDataSource */
public PostGISDataSource() {
}
public void connect(String server,String database,String user,String passwd) throws SQLException{
try {
Class.forName("org.postgresql.Driver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("DaS->ClassNotFoundException: ");
System.err.println("DaS->"+e.getMessage());
}
connection = DriverManager.getConnection("jdbc:postgresql://"+server+"/"+database,user,passwd);
((org.postgresql.Connection)connection).addDataType("geometry","org.postgis.PGgeometry");
((org.postgresql.Connection)connection).addDataType("box3d","org.postgis.PGbox3d");
}
public MixedLayer getFeatures(GeoRectangle bounds) throws SQLException{
MixedLayer l = new MixedLayer();
addFeatures(l,bounds);
return l;
}
public void addFeatures(MixedLayer l,GeoRectangle bounds)throws SQLException{
Statement s = connection.createStatement();
//PGbox3d box3d = new PGbox3d(new org.postgis.Point(400000,400000),new org.postgis.Point(410000,410000));
StringBuffer box3d = new StringBuffer("BOX3D (");
box3d.append(bounds.x);
box3d.append(" ");
box3d.append(bounds.y);
box3d.append(",");
box3d.append(bounds.x+bounds.width);
box3d.append(" ");
box3d.append(bounds.y+bounds.height);
int notNeeded[] = l.getIDs(bounds,SelectionManager.CROSSES);
StringBuffer idList = new StringBuffer();
if(notNeeded.length>0){
idList.append("and oid not in (");
idList.append(notNeeded[0]);
for(int i=1;i<notNeeded.length;i++){
idList.append(",");
idList.append(notNeeded[i]);
}
idList.append(")");
}
System.out.println("select geo_value,oid from yh_eds where '"+box3d+"'::box3d && geo_value "+idList);
ResultSet r = s.executeQuery("select geo_value,oid from yh_eds where '"+box3d+"'::box3d && geo_value "+idList);
// ResultSet r = s.executeQuery("select geo_value,oid from yh_eds where geo_value && '"+box3d+"'::box3d AND oid not in set (8223251,8223252)");
l.setStatus(l.LOADING);
while( r.next() ) {
/*
* Retrieve the geometry as an object then cast it to the geometry type.
* Print things out.
*/
PGgeometry geom = (PGgeometry)r.getObject(1);
int id = r.getInt(2);
//System.out.println("Row " + id + ":");
if(l.getGeoShape(id)!=null)continue;
//System.out.println(geom.getGeoType());
switch (geom.getGeoType()){
case Geometry.POINT:
case Geometry.MULTIPOINT:
case Geometry.GEOMETRYCOLLECTION:
case Geometry.LINESTRING:
case Geometry.MULTILINESTRING:
System.out.println("Geometry type not supported");
break;
case Geometry.POLYGON:
System.out.println("A polygon!");
break;
case Geometry.MULTIPOLYGON:
GeoPolygon poly = convertMultiPolygon(id,(MultiPolygon)geom.getGeometry());
//System.out.println("poly bounds "+poly.getBounds());
l.addGeoPolygon(poly);
//System.out.println("multipolygon converted");
break;
default :
System.out.println("Unkown geom type");
}
//System.out.println(geom.toString());
}
l.setStatus(l.COMPLETED);
}
public GeoPolygon convertMultiPolygon(int id,MultiPolygon p){
GeoPolygon gp=null;
GeoPolygon part;
int count = p.numPolygons();
for(int i=0;i<count;i++){
org.postgis.Polygon poly = p.getPolygon(i);
int rings = poly.numRings();
for(int j=0;j<rings;j++){
LinearRing ring = poly.getRing(j);
double x[] = new double[ring.points.length];
double y[] = new double[ring.points.length];
for(int k=0;k<x.length;k++){
x[k] = ring.points[k].x;
y[k] = ring.points[k].y;
}
part = new GeoPolygon(id,x,y,x.length);
if(i==0 && j==0){
gp = part;
}
else{
gp.addSubPart(part);
}
}
}
return gp;
}
}
|