Statistics
| Revision:

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

History | View | Annotate | Download (12.5 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.driver.exceptions.ReadDriverException;
58
import com.hardcode.gdbms.engine.values.Value;
59
import com.hardcode.gdbms.engine.values.ValueFactory;
60
import com.iver.andami.messages.NotificationManager;
61
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
62
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
63
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
64
import com.iver.cit.gvsig.fmap.core.FPoint2D;
65
import com.iver.cit.gvsig.fmap.core.FShape;
66
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
67
import com.iver.cit.gvsig.fmap.core.IFeature;
68
import com.iver.cit.gvsig.fmap.core.IGeometry;
69
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
70
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
71

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

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

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

    
113
    public static IGeometry getGeometry( SeShape shape ) {
114

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

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

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

    
142
                case SeShape.TYPE_MULTI_POINT:
143
                    break;
144

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

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

    
169

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

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

    
177
                }
178

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

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

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

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

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

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

    
232
                return shape;
233
        }
234

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

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

    
253

    
254

    
255

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

    
282
                NotificationManager.addError(e);
283
        }
284
    }
285

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

    
301
            return true;
302
        }
303
        catch (SeException e) {
304
                NotificationManager.addError(e);
305

    
306
        }
307
        return false;
308
    }
309

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

    
320
            if ( row != null )
321
            {
322
                    int hasgeom=0;
323
                for (int colNum = 0; colNum < colDefs.length; colNum++)
324
                {
325
                    SeColumnDefinition colDef = colDefs[colNum];
326
                    int dataType = colDef.getType();
327
                    if ( row.getIndicator((short)colNum) != SeRow.SE_IS_NULL_VALUE)
328
                    {
329
                        switch( dataType )
330
                        {
331
                                case SeColumnDefinition.TYPE_SHAPE:
332
                                        spVal = row.getShape(colNum);
333
                                        geom = getGeometry(spVal);
334
                                        hasgeom=-1;
335
                                        break;
336
                            case SeColumnDefinition.TYPE_INT16:
337
                                regAtt[colNum+hasgeom] =  ValueFactory.createValue(row.getShort(colNum).intValue());
338
                                break;
339
                            case SeColumnDefinition.TYPE_DATE:
340
                                regAtt[colNum+hasgeom] =  ValueFactory.createValue(row.getTime(colNum).getTime());
341
                                break;
342

    
343
                            case SeColumnDefinition.TYPE_INT32:
344
                            case SeColumnDefinition.TYPE_INT64:
345
                                regAtt[colNum+hasgeom] =  ValueFactory.createValue(row.getInteger(colNum).intValue());
346
                                break;
347

    
348
                            case SeColumnDefinition.TYPE_FLOAT32:
349
                                regAtt[colNum+hasgeom] =  ValueFactory.createValue(row.getFloat(colNum).floatValue());
350
                                break;
351

    
352
                            case SeColumnDefinition.TYPE_FLOAT64:
353
                                regAtt[colNum+hasgeom] =  ValueFactory.createValue(row.getDouble(colNum).doubleValue());
354
                                break;
355

    
356
                            case SeColumnDefinition.TYPE_STRING:
357
                            case SeColumnDefinition.TYPE_NSTRING:
358
                                regAtt[colNum+hasgeom] =  ValueFactory.createValue(row.getString(colNum));
359
                                break;
360

    
361

    
362
                        } // End switch
363
                    } // End if
364
                } // for
365
                //System.out.println("Dentro de next(): " + spVal.getFeatureId().longValue() + " " + regAtt[0]);
366

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

    
370

    
371
        } catch (SeException e)
372
        {
373
                NotificationManager.addError(e);
374
        }
375

    
376

    
377
        return feat;
378
    }
379

    
380
        public void closeIterator() throws ReadDriverException {
381
                try {
382
                        bFirst=false;
383
                        query.close();
384
                } catch (SeException e) {
385
                        throw new ReadDriverException("ArcSDE",e);
386
                }
387

    
388
        }
389

    
390
}