Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extOracleSpatial / src / es / prodevelop / cit / gvsig / fmap / drivers / jdbc / oracle / OracleSpatialFeatureIterator.java @ 17691

History | View | Annotate | Download (5.94 KB)

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

    
45
import com.hardcode.gdbms.engine.values.Value;
46

    
47
import com.iver.cit.gvsig.fmap.DriverException;
48
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
49
import com.iver.cit.gvsig.fmap.core.IFeature;
50
import com.iver.cit.gvsig.fmap.core.IGeometry;
51
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
52

    
53
// import oracle.spatial.geometry.JGeometry;
54

    
55
import oracle.sql.ROWID;
56
import oracle.sql.STRUCT;
57

    
58
import java.sql.ResultSet;
59
import java.sql.SQLException;
60
import java.sql.Statement;
61

    
62
import org.apache.log4j.Logger;
63

    
64

    
65
/**
66
 * Oracle feature iterator. An instance of this class is returned whaen gvSIG
67
 * asks for a feature iterator for a new viewport.
68
 *
69
 * @author jldominguez
70
 *
71
 */
72
public class OracleSpatialFeatureIterator implements IFeatureIterator {
73
        
74
        private static Logger logger = Logger.getLogger(OracleSpatialFeatureIterator.class.getName());
75
        
76
    private OracleSpatialDriver driver;
77
    private ResultSet rs = null;
78
    private int oneBasedGeoColInd = 0;
79
    private boolean useGeotools = false;
80
    private Statement st;
81

    
82
    /**
83
     * Constructor.
84
     *
85
     * @param parent the driver that creates it
86
     * @param _rs the result set to be iterated (already computed by the driver)
87
     * @param _st the statement that generated the result set. The iterator will close it.
88
     * @param geoColIndex index of the geometry field
89
     * @param _useGeotools a switch to decide if the geotools classes
90
     * must be used to deal with geometris
91
     */
92
    public OracleSpatialFeatureIterator(OracleSpatialDriver parent,
93
        ResultSet _rs, Statement _st, int geoColIndex, boolean _useGeotools) {
94
        driver = parent;
95
        rs = _rs;
96
        st = _st;
97
        useGeotools = _useGeotools;
98
        oneBasedGeoColInd = geoColIndex;
99
    }
100

    
101
    public boolean hasNext() throws DriverException {
102
        if (rs == null) {
103
            return false;
104
        }
105

    
106
        try {
107
            boolean _resp = rs.next();
108

    
109
            if (!_resp) {
110
                rs.close();
111
                st.close();
112
            }
113

    
114
            return _resp;
115
        }
116
        catch (SQLException se) {
117
            logger.error("Error while doing hasNext(): " + se.getMessage());
118
        }
119

    
120
        return false;
121
    }
122

    
123
    public IFeature next() throws DriverException {
124
        if (rs == null) {
125
            return null;
126
        }
127

    
128
        IFeature ife = null;
129

    
130
        try {
131
            ROWID ri = (ROWID) rs.getObject(1);
132
            Value[] atts = driver.getAttributes(rs, false);
133
            String gid = ri.stringValue();
134
            STRUCT _st = (oracle.sql.STRUCT) rs.getObject(oneBasedGeoColInd);
135
            IGeometry theGeom = driver.getGeometryUsing(_st, useGeotools);
136
            ife = new DefaultFeature(theGeom, atts, gid);
137
        }
138
        catch (SQLException se) {
139
            logger.error("Error while doing next(): " + se.getMessage());
140
        }
141

    
142
        return ife;
143
    }
144

    
145
    private void showGeometrySample(IFeature ife) {
146
            
147
            IGeometry geom = ife.getGeometry();
148
            
149
            int size = 80;
150
            
151
                        String wkt_str = geom.toJTSGeometry().toText();
152
                        if (wkt_str.length() <= size) {
153
                                logger.debug("Oracle driver returns geometry:\n" + wkt_str);
154
                        } else {
155
                                logger.debug("Oracle driver returns geometry:\n" + wkt_str.substring(0, size));
156
                        }
157
        }
158

    
159
        public void closeIterator() throws DriverException {
160
        try {
161
            rs.close();
162
            st.close();
163
        }
164
        catch (SQLException se) {
165
            throw new DriverException("SQL Exception: " + se.getMessage());
166
        }
167
    }
168

    
169
    /**
170
     * Utility method to get the oracle geometry type as a human-readable String.
171
     *
172
     * @param type the oracle geometry type
173
     * @return a human-readable String describing it.
174
     */
175
    public static String getJGeometryTypeName(int type) {
176
        String resp = "Unknown JGeometry type (" + type + ")";
177

    
178
        switch (type) {
179
        case OracleSpatialDriver.JGeometry_GTYPE_COLLECTION:
180
            resp = "Collection";
181

    
182
            break;
183

    
184
        case OracleSpatialDriver.JGeometry_GTYPE_CURVE:
185
            resp = "Curve";
186

    
187
            break;
188

    
189
        case OracleSpatialDriver.JGeometry_GTYPE_MULTICURVE:
190
            resp = "Multi-curve";
191

    
192
            break;
193

    
194
        case OracleSpatialDriver.JGeometry_GTYPE_MULTIPOINT:
195
            resp = "Multi-point";
196

    
197
            break;
198

    
199
        case OracleSpatialDriver.JGeometry_GTYPE_MULTIPOLYGON:
200
            resp = "Multi-polygon";
201

    
202
            break;
203

    
204
        case OracleSpatialDriver.JGeometry_GTYPE_POINT:
205
            resp = "Point";
206

    
207
            break;
208

    
209
        case OracleSpatialDriver.JGeometry_GTYPE_POLYGON:
210
            resp = "Polygon";
211

    
212
            break;
213
        }
214

    
215
        return resp;
216
    }
217
}