Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / postgis / PostGisDriver.java @ 1691

History | View | Annotate | Download (5.54 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.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.vividsolutions.jts.io.ParseException;
58

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

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

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