Statistics
| Revision:

root / trunk / extensions / extSDE / src / com / iver / cit / gvsig / fmap / drivers / sde / ArcSdeFeatureIterator.java @ 11193

History | View | Annotate | Download (12.4 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.awt.geom.PathIterator;
47
import java.sql.SQLException;
48
import java.util.ArrayList;
49

    
50
import com.esri.sde.sdk.client.SDEPoint;
51
import com.esri.sde.sdk.client.SeColumnDefinition;
52
import com.esri.sde.sdk.client.SeCoordinateReference;
53
import com.esri.sde.sdk.client.SeException;
54
import com.esri.sde.sdk.client.SeQuery;
55
import com.esri.sde.sdk.client.SeRow;
56
import com.esri.sde.sdk.client.SeShape;
57
import com.hardcode.gdbms.engine.values.Value;
58
import com.hardcode.gdbms.engine.values.ValueFactory;
59
import com.iver.cit.gvsig.fmap.DriverException;
60
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
61
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
62
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
63
import com.iver.cit.gvsig.fmap.core.FPoint2D;
64
import com.iver.cit.gvsig.fmap.core.FShape;
65
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
66
import com.iver.cit.gvsig.fmap.core.IFeature;
67
import com.iver.cit.gvsig.fmap.core.IGeometry;
68
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
69
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
70

    
71
/**
72
 * @author   FJP
73
 */
74
public class ArcSdeFeatureIterator implements IFeatureIterator {
75
    IGeometry geom;
76
    int numColumns;
77
    private SeQuery query = null;
78
    private boolean bFirst;
79
    Value[] regAtt;
80
    SeRow row;
81
    private  int index=0;
82

    
83
    static GeneralPathX convertSeShapeToGeneralPathX(SeShape spVal) throws SeException
84
    {
85
        double[][][] points = spVal.getAllCoords(SeShape.TURN_RIGHT);
86
        GeneralPathX gpx = new GeneralPathX();
87
        // Display the X and Y values
88
        boolean bStartPart;
89
        for( int partNo = 0 ; partNo < points.length ; partNo++)
90
        {
91
            for( int subPartNo = 0 ; subPartNo < points[partNo].length ; subPartNo++)
92
            {
93
                bStartPart = true;
94
                for( int pointNo = 0 ; pointNo < points[partNo][subPartNo].length ; pointNo+=2)
95
                {
96
                    if (bStartPart)
97
                    {
98
                        bStartPart = false;
99
                        gpx.moveTo(points[partNo][subPartNo][pointNo],
100
                                points[partNo][subPartNo][(pointNo+1)]);
101
                    }
102
                    else
103
                        gpx.lineTo(points[partNo][subPartNo][pointNo],
104
                                points[partNo][subPartNo][(pointNo+1)]);
105

    
106
                }
107
            }
108
        }
109
        return gpx;
110
    }
111

    
112
    public static IGeometry getGeometry( SeShape shape ) {
113

    
114
        try {
115
            /*
116
             *   Retrieve the shape type.
117
             */
118
            int type = -1;
119
            type = shape.getType();
120

    
121
            // Display the X and Y values
122
            /* for( int partNo = 0 ; partNo < points.length ; partNo++, System.out.println("") )
123
                for( int subPartNo = 0 ; subPartNo < points[partNo].length ; subPartNo++, System.out.println("") )
124
                    for( int pointNo = 0 ; pointNo < points[partNo][subPartNo].length ; pointNo+=2)
125
                        System.out.println("X: " + points[partNo][subPartNo][pointNo] + "\tY: "
126
                                                 + points[partNo][subPartNo][(pointNo+1)] ); */
127
            switch( type )
128
            {
129
                case SeShape.TYPE_POINT:
130
                    double[][][] points = shape.getAllCoords();
131
                    FPoint2D p =  new FPoint2D(points[0][0][0],points[0][0][1]);
132
                    return ShapeFactory.createPoint2D(p);
133

    
134
                case SeShape.TYPE_LINE:
135
                case SeShape.TYPE_MULTI_LINE:
136
                case SeShape.TYPE_MULTI_SIMPLE_LINE:
137
                case SeShape.TYPE_SIMPLE_LINE:
138
                    GeneralPathX gpx = new GeneralPathX(shape.toGeneralPath());
139
                    return ShapeFactory.createPolyline2D(gpx);
140

    
141
                case SeShape.TYPE_MULTI_POINT:
142
                    break;
143

    
144
                case SeShape.TYPE_NIL:
145
                    return new FNullGeometry();
146
                case SeShape.TYPE_POLYGON:
147
                case SeShape.TYPE_MULTI_POLYGON:
148
                    // GeneralPathX gpx2 = new GeneralPathX(shape.toGeneralPath());
149
                    GeneralPathX gpx2 = convertSeShapeToGeneralPathX(shape);
150
                    /* SeExtent r = shape.getExtent();
151
                    GeneralPathX gpx2 = new GeneralPathX();
152
                    gpx2.moveTo(r.getMinX(), r.getMinY());
153
                    gpx2.lineTo(r.getMaxX(), r.getMinY());
154
                    gpx2.lineTo(r.getMaxX(), r.getMaxY());
155
                    gpx2.lineTo(r.getMinX(), r.getMaxY());
156
                    gpx2.closePath(); */
157
                    return ShapeFactory.createPolygon2D(gpx2);
158

    
159
            } // End switch
160
        }
161
        catch (SeException e)
162
        {
163
            e.printStackTrace();
164
        }
165
        return new FNullGeometry();
166
    }
167

    
168

    
169
    public static SeShape constructShape(IGeometry geometry, SeCoordinateReference seSrs) {
170
                SeShape shape = null;
171

    
172
                try {
173
                        shape = new SeShape(seSrs);
174
                } catch (SeException ex) {
175

    
176
                }
177

    
178
//                if (geometry.isEmpty()) {
179
//                        return shape;
180
//                }
181

    
182
                int numParts=1;
183
//                GeometryCollection gcol = null;
184
//
185
//                if (geometry instanceof GeometryCollection) {
186
//                        gcol = (GeometryCollection) geometry;
187
//                } else {
188
//                        Geometry[] geoms = { geometry };
189
//                        gcol = new GeometryFactory().createGeometryCollection(geoms);
190
//                }
191

    
192
//                List allPoints = new ArrayList();
193
//                numParts = gcol.getNumGeometries();
194

    
195
                int[] partOffsets = new int[numParts];
196
//                Geometry geom;
197
//                Coordinate[] coords;
198
//                Coordinate c;
199
                SDEPoint[] points = getPoints(geometry);
200
                partOffsets[0]=points.length;
201
//                for (int currGeom = 0; currGeom < numParts; currGeom++) {
202
//                        partOffsets[currGeom] = allPoints.size();
203
//                        geom = gcol.getGeometryN(currGeom);
204
//
205
//                        coords = geom.getCoordinates();
206
//
207
//                        for (int i = 0; i < coords.length; i++) {
208
//                                c = coords[i];
209
//                                allPoints.add(new SDEPoint(c.x, c.y));
210
//                        }
211
//                }
212

    
213
//                SDEPoint[] points = new SDEPoint[allPoints.size()];
214
//                allPoints.toArray(points);
215

    
216
                try {
217
                        if (geometry.getGeometryType()==FShape.POINT || geometry instanceof FMultiPoint2D) {
218
                                shape.generatePoint(points.length, points);
219
                        } else if (geometry.getGeometryType()==FShape.LINE) {
220
                                shape
221
                                                .generateLine(points.length, numParts, partOffsets,
222
                                                                points);
223
                        } else {
224
                                shape.generatePolygon(points.length, numParts, partOffsets,
225
                                                points);
226
                        }
227
                } catch (SeException e) {
228
                        e.printStackTrace();
229
                }
230

    
231
                return shape;
232
        }
233

    
234
    private static SDEPoint[] getPoints(IGeometry g) {
235
//                if (FConstant.SHAPE_TYPE_MULTIPOINTZ == m_type){
236
//                        zs=((IGeometry3D)g).getZs();
237
//                }
238
                PathIterator theIterator = g.getPathIterator(null); //polyLine.getPathIterator(null, flatness);
239
                double[] theData = new double[6];
240
                ArrayList ps=new ArrayList();
241
                while (!theIterator.isDone()) {
242
                        //while not done
243
                        int theType = theIterator.currentSegment(theData);
244

    
245
                        ps.add(new SDEPoint(theData[0], theData[1]));
246
                        theIterator.next();
247
                } //end while loop
248
                SDEPoint[] points = (SDEPoint[])ps.toArray(new SDEPoint[0]);
249
                return points;
250
    }
251

    
252

    
253

    
254

    
255
    /**
256
     * @throws SQLException
257
     *
258
     */
259
    public ArcSdeFeatureIterator(SeQuery query) {
260
        // Debe ser forward only
261
        this.query = query;
262
        try {
263
            row = query.fetch();
264
                        if (row == null)
265
            {
266
                bFirst = true;
267
                return;
268
            }
269
            numColumns = row.getNumColumns();
270
            regAtt = new Value[numColumns-1];
271
            bFirst = true;
272
            SeColumnDefinition[] colDefs = row.getColumns();
273
                for (int i=0; i<colDefs.length;i++){
274
                        if (colDefs[i].getName().equals("OBJECTID")){
275
                                index=i;
276
                        }
277
                }
278
        } catch (SeException e) {
279
            // TODO Auto-generated catch block
280
            e.printStackTrace();
281
        }
282
    }
283

    
284
    /* (non-Javadoc)
285
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
286
     */
287
    public boolean hasNext() throws DriverException {
288
        try {
289
            if (bFirst)
290
                bFirst = false;
291
            else
292
                row = query.fetch();
293
            if (row == null)
294
            {
295
                query.close();
296
                return false;
297
            }
298

    
299
            return true;
300
        }
301
        catch (SeException e) {
302
            e.printStackTrace();
303
            // throw new SQLException(e.getMessage());
304
        }
305
        return false;
306
    }
307

    
308
    /* (non-Javadoc)
309
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
310
     */
311
    public IFeature next() throws DriverException {
312
        SeShape spVal = null;
313
        SeColumnDefinition[] colDefs = row.getColumns();
314
        IFeature feat = null;
315
        try
316
        {
317

    
318
            if ( row != null )
319
            {
320
                for (int colNum = 0; colNum < colDefs.length; colNum++)
321
                {
322
                    SeColumnDefinition colDef = colDefs[colNum];
323
                    int dataType = colDef.getType();
324
                    if ( row.getIndicator((short)colNum) != SeRow.SE_IS_NULL_VALUE)
325
                    {
326
                        switch( dataType )
327
                        {
328
                            case SeColumnDefinition.TYPE_INT16:
329
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getShort(colNum).intValue());
330
                                break;
331
                            case SeColumnDefinition.TYPE_DATE:
332
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getTime(colNum).getTime());
333
                                break;
334

    
335
                            case SeColumnDefinition.TYPE_INT32:
336
                            case SeColumnDefinition.TYPE_INT64:
337
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getInteger(colNum).intValue());
338
                                break;
339

    
340
                            case SeColumnDefinition.TYPE_FLOAT32:
341
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getFloat(colNum).floatValue());
342
                                break;
343

    
344
                            case SeColumnDefinition.TYPE_FLOAT64:
345
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getDouble(colNum).doubleValue());
346
                                break;
347

    
348
                            case SeColumnDefinition.TYPE_STRING:
349
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getString(colNum));
350
                                break;
351

    
352
                            case SeColumnDefinition.TYPE_SHAPE:
353
                                spVal = row.getShape(colNum);
354
                                geom = getGeometry(spVal);
355
                                break;
356
                        } // End switch
357
                    } // End if
358
                } // for
359
                //System.out.println("Dentro de next(): " + spVal.getFeatureId().longValue() + " " + regAtt[0]);
360

    
361
                feat = new DefaultFeature(geom, regAtt,String.valueOf(regAtt[index-1]));//""+ spVal.getFeatureId().longValue());
362
            } // if
363

    
364

    
365
        } catch (SeException e)
366
        {
367
            e.printStackTrace();
368
        }
369

    
370

    
371
        return feat;
372
    }
373

    
374
        public void closeIterator() throws DriverException {
375
                try {
376
                        bFirst=false;
377
                        query.close();
378
                } catch (SeException e) {
379
                        // TODO Auto-generated catch block
380
                        e.printStackTrace();
381
                }
382

    
383
        }
384

    
385
}