Statistics
| Revision:

svn-gvsig-desktop / tags / Root_v10 / extensions / extSDE / src / com / iver / cit / gvsig / fmap / drivers / sde / ArcSdeFeatureIterator.java @ 13670

History | View | Annotate | Download (10 KB)

1
/*
2
 * Created on 11-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.sde;
45

    
46
import java.sql.SQLException;
47

    
48
import com.esri.sde.sdk.client.SeColumnDefinition;
49
import com.esri.sde.sdk.client.SeException;
50
import com.esri.sde.sdk.client.SeQuery;
51
import com.esri.sde.sdk.client.SeRow;
52
import com.esri.sde.sdk.client.SeShape;
53
import com.hardcode.gdbms.engine.values.Value;
54
import com.hardcode.gdbms.engine.values.ValueFactory;
55
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
57
import com.iver.cit.gvsig.fmap.core.FPoint2D;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.IFeature;
60
import com.iver.cit.gvsig.fmap.core.IGeometry;
61
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
62
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
63

    
64
/**
65
 * @author FJP
66
 *
67
 * TODO To change the template for this generated type comment go to
68
 * Window - Preferences - Java - Code Generation - Code and Comments
69
 */
70
public class ArcSdeFeatureIterator implements IFeatureIterator {
71
    IGeometry geom;
72
    int numColumns;
73
    private SeQuery query = null;
74
    private boolean bFirst;
75
    Value[] regAtt;
76
    SeRow row;
77
    
78
    static GeneralPathX convertSeShapeToGeneralPathX(SeShape spVal) throws SeException
79
    {
80
        double[][][] points = spVal.getAllCoords(SeShape.TURN_RIGHT);
81
        GeneralPathX gpx = new GeneralPathX();
82
        // Display the X and Y values
83
        boolean bStartPart;
84
        for( int partNo = 0 ; partNo < points.length ; partNo++)
85
        {            
86
            for( int subPartNo = 0 ; subPartNo < points[partNo].length ; subPartNo++)
87
            {
88
                bStartPart = true;
89
                for( int pointNo = 0 ; pointNo < points[partNo][subPartNo].length ; pointNo+=2)
90
                {
91
                    if (bStartPart)
92
                    {
93
                        bStartPart = false;
94
                        gpx.moveTo(points[partNo][subPartNo][pointNo],
95
                                points[partNo][subPartNo][(pointNo+1)]);
96
                    }
97
                    else
98
                        gpx.lineTo(points[partNo][subPartNo][pointNo],
99
                                points[partNo][subPartNo][(pointNo+1)]);
100

    
101
                }
102
            }
103
        }  
104
        return gpx;
105
    }
106
    
107
    public static IGeometry getGeometry( SeShape shape ) {
108
        
109
        try {
110
            /*
111
             *   Retrieve the shape type.
112
             */
113
            int type = -1;
114
            type = shape.getType();
115
            
116
            // Display the X and Y values
117
            /* for( int partNo = 0 ; partNo < points.length ; partNo++, System.out.println("") ) 
118
                for( int subPartNo = 0 ; subPartNo < points[partNo].length ; subPartNo++, System.out.println("") ) 
119
                    for( int pointNo = 0 ; pointNo < points[partNo][subPartNo].length ; pointNo+=2) 
120
                        System.out.println("X: " + points[partNo][subPartNo][pointNo] + "\tY: "
121
                                                 + points[partNo][subPartNo][(pointNo+1)] ); */            
122
            switch( type )
123
            {
124
                case SeShape.TYPE_POINT:
125
                    double[][][] points = shape.getAllCoords();
126
                    FPoint2D p =  new FPoint2D(points[0][0][0],points[0][0][1]);
127
                    return ShapeFactory.createPoint2D(p);
128
                
129
                case SeShape.TYPE_LINE:
130
                case SeShape.TYPE_MULTI_LINE:
131
                case SeShape.TYPE_MULTI_SIMPLE_LINE:
132
                case SeShape.TYPE_SIMPLE_LINE:
133
                    GeneralPathX gpx = new GeneralPathX(shape.toGeneralPath());
134
                    return ShapeFactory.createPolyline2D(gpx);
135
                    
136
                case SeShape.TYPE_MULTI_POINT:                    
137
                    break;
138
                
139
                case SeShape.TYPE_NIL:
140
                    return new FNullGeometry();
141
                case SeShape.TYPE_POLYGON:
142
                case SeShape.TYPE_MULTI_POLYGON:                     
143
                    // GeneralPathX gpx2 = new GeneralPathX(shape.toGeneralPath());
144
                    GeneralPathX gpx2 = convertSeShapeToGeneralPathX(shape);
145
                    /* SeExtent r = shape.getExtent();
146
                    GeneralPathX gpx2 = new GeneralPathX();
147
                    gpx2.moveTo(r.getMinX(), r.getMinY());
148
                    gpx2.lineTo(r.getMaxX(), r.getMinY());
149
                    gpx2.lineTo(r.getMaxX(), r.getMaxY());
150
                    gpx2.lineTo(r.getMinX(), r.getMaxY());
151
                    gpx2.closePath(); */
152
                    return ShapeFactory.createPolygon2D(gpx2);
153

    
154
            } // End switch
155
        }
156
        catch (SeException e)
157
        {
158
            e.printStackTrace();
159
        }
160
        return new FNullGeometry();
161
    }     
162
    
163
    /**
164
     * @throws SQLException
165
     * 
166
     */
167
    public ArcSdeFeatureIterator(SeQuery query) {
168
        // Debe ser forward only
169
        this.query = query;
170
        try {
171
            row = query.fetch();
172
                        if (row == null)
173
            {
174
                bFirst = true;
175
                return;
176
            }
177
            numColumns = row.getNumColumns();
178
            regAtt = new Value[numColumns-1];
179
            bFirst = true;
180
        } catch (SeException e) {
181
            // TODO Auto-generated catch block
182
            e.printStackTrace();
183
        }
184
    }
185
    
186
    /* (non-Javadoc)
187
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
188
     */
189
    public boolean hasNext() throws SQLException {
190
        try {
191
            if (bFirst)
192
                bFirst = false;
193
            else
194
                row = query.fetch();
195
            if (row == null)
196
            {
197
                query.close();
198
                return false;
199
            }
200
            
201
            return true;
202
        }
203
        catch (SeException e) {            
204
            e.printStackTrace();            
205
            // throw new SQLException(e.getMessage());
206
        }
207
        return false;
208
    }
209

    
210
    /* (non-Javadoc)
211
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
212
     */
213
    public IFeature next() throws SQLException {
214
        SeShape spVal = null;
215
        SeColumnDefinition[] colDefs = row.getColumns(); 
216
        IFeature feat = null;
217
        try
218
        {            
219
            if ( row != null ) 
220
            { 
221
                for (int colNum = 0; colNum < colDefs.length; colNum++)
222
                {                
223
                    SeColumnDefinition colDef = colDefs[colNum];
224
                    int dataType = colDef.getType();
225
                    if ( row.getIndicator((short)colNum) != SeRow.SE_IS_NULL_VALUE)
226
                    {
227
                        switch( dataType )
228
                        {
229
                            case SeColumnDefinition.TYPE_SMALLINT:
230
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getShort(colNum).intValue());
231
                                break;                                
232
                            case SeColumnDefinition.TYPE_DATE:
233
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getDate(colNum));
234
                                break;
235
                                
236
                            case SeColumnDefinition.TYPE_INTEGER:
237
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getInteger(colNum).intValue());
238
                                break;
239
                                                            
240
                            case SeColumnDefinition.TYPE_FLOAT:
241
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getFloat(colNum).floatValue());
242
                                break;
243
                                
244
                            case SeColumnDefinition.TYPE_DOUBLE:
245
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getDouble(colNum).doubleValue());
246
                                break;
247
                                
248
                            case SeColumnDefinition.TYPE_STRING:
249
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getString(colNum));
250
                                break;
251
                                
252
                            case SeColumnDefinition.TYPE_SHAPE:
253
                                spVal = row.getShape(colNum);                               
254
                                geom = getGeometry(spVal);
255
                                break;
256
                        } // End switch
257
                    } // End if
258
                } // for
259
                // System.out.println("Dentro de next(): " + spVal.getFeatureId().toString() + " " + regAtt[0]);
260
            
261
                feat = new DefaultFeature(geom, regAtt, "" + spVal.getFeatureId().longValue());
262
            } // if
263

    
264
            
265
        } catch (SeException e)
266
        {
267
            e.printStackTrace();
268
        }
269
              
270
        
271
        return feat;
272
    }
273

    
274
}