Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGisDriver.java @ 1704

History | View | Annotate | Download (6.33 KB)

1
/*
2
 * Created on 04-mar-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 * 
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *  
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 * 
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *  
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 * 
34
 *    or
35
 * 
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 * 
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.drivers.jdbc.postgis;
45

    
46
import java.awt.geom.Rectangle2D;
47
import java.sql.Connection;
48
import java.sql.ResultSet;
49
import java.sql.SQLException;
50
import java.sql.Statement;
51

    
52
import org.postgis.PGbox3d;
53

    
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
56
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
57
import com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator;
58
import com.vividsolutions.jts.io.ParseException;
59

    
60
/**
61
 * @author FJP
62
 *
63
 * TODO To change the template for this generated type comment go to
64
 * Window - Preferences - Java - Code Generation - Code and Comments
65
 */
66
public class PostGisDriver extends DefaultDBDriver {
67
    private WKTParser parser = new WKTParser();
68
    private int fetch_min=-1;
69
    private int fetch_max=-1;
70
    private Statement st;
71
    private Rectangle2D fullExtent = null;
72
    private String strAux;
73
    
74

    
75
    /**
76
     * 
77
     */
78
    public PostGisDriver() {
79
    }
80
    /* (non-Javadoc)
81
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
82
     */
83
    public DriverAttributes getDriverAttributes() {
84
        return null;
85
    }
86

    
87
    /* (non-Javadoc)
88
     * @see com.hardcode.driverManager.Driver#getName()
89
     */
90
    public String getName() {
91
        return "PostGIS JDBC Driver";
92
    }
93
    
94
        /**
95
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
96
         */
97
        public IGeometry getShape(int index) {
98
            IGeometry geom = null;
99
            boolean resul;
100
                try {
101
                    // EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
102
                    // FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
103
                    // LO HAGA
104
                    // System.out.println("getShape " + index);
105
                    if (index < fetch_min)
106
                    {
107
                        rs.close();
108
                            
109
                            rs = st.executeQuery(sqlOrig);
110
                        fetch_min = 0;
111
                        fetch_max = rs.getFetchSize();
112
                    }
113
                    while (index >= fetch_max)
114
                    {
115
                        rs.last();
116
                        // forzamos una carga
117
                        rs.next();
118
                        fetch_min = fetch_max;
119
                        fetch_max = fetch_max + rs.getFetchSize();
120
                        // System.out.println("fetchSize = " + rs.getFetchSize() + " " + fetch_min + "-" + fetch_max);
121
                    }
122
                    rs.absolute(index+1 - fetch_min);
123
                    strAux = rs.getString(1);                
124
                    geom = parser.read(strAux);                
125
            } catch (SQLException e) {
126
                e.printStackTrace();
127
            } catch (ParseException e) {
128
                e.printStackTrace();
129
            }
130
                
131
            return geom;
132
        }
133
        /**
134
         * @param conn
135
         * @param tableName
136
         * @param fields OJO: EL PRIMER CAMPO HA DE SER EL DE GEOMETRIA
137
         * @param whereClause
138
         */
139
        public void setData(Connection conn, String tableName, String fields, String whereClause)
140
        {
141
            this.conn = conn;            
142
            this.tableName = tableName;
143
            this.fields = fields;
144
            this.whereClause = whereClause;
145
            this.sqlOrig = "SELECT " + fields + " FROM " + tableName + " " + whereClause;
146
            try {
147
                st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
148
                st.setFetchSize(5000);
149
                rs = st.executeQuery(sqlOrig);
150
            fetch_min = 0;
151
            fetch_max = rs.getFetchSize();
152
            metaData = rs.getMetaData();
153
        } catch (SQLException e) {
154
            // TODO Auto-generated catch block
155
            e.printStackTrace();
156
        }
157
        }
158
        
159
        /**
160
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
161
         */
162
        public Rectangle2D getFullExtent(){
163
            if (fullExtent == null)
164
            {
165
                try
166
            {
167
                    Statement s = conn.createStatement();                    
168
                    ResultSet r = s.executeQuery("SELECT extent(the_geom) AS FullExtent FROM " + tableName);
169
                    r.next();
170
                    String strAux = r.getString(1);
171
                    System.out.println("fullExtent = " + strAux);
172
                    PGbox3d regeom = new PGbox3d(strAux.toString());
173
                    double x = regeom.getLLB().x;
174
                    double y = regeom.getLLB().y;
175
                    double w = regeom.getURT().x -x;
176
                    double h = regeom.getURT().y - y;
177
                fullExtent = new Rectangle2D.Double(x, y, w, h);
178
            }
179
                catch (SQLException e)
180
                {
181
                    System.err.println(e.getMessage());
182
                }
183
                
184
            }
185
            return fullExtent;
186
        }
187
    /* (non-Javadoc)
188
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
189
     */
190
    public GeometryIterator getGeometryIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException {
191
        Statement st;
192
        PostGisGeometryIterator geomIterator = null;
193
        try {
194
            st = conn.createStatement();
195
            st.setFetchSize(2000);
196
            ResultSet rs = st.executeQuery(sql);
197
            geomIterator = new PostGisGeometryIterator(rs);
198
        } catch (SQLException e) {
199
            e.printStackTrace();
200
            throw new com.iver.cit.gvsig.fmap.DriverException(e);
201
        }
202
            
203
        return geomIterator;
204
    }
205
        
206
    
207
    
208
}