Statistics
| Revision:

root / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / fmap / drivers / sde / ArcSdeFeatureIterator.java @ 11012

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
import java.util.List;
50

    
51
import com.esri.sde.sdk.client.SDEPoint;
52
import com.esri.sde.sdk.client.SeColumnDefinition;
53
import com.esri.sde.sdk.client.SeCoordinateReference;
54
import com.esri.sde.sdk.client.SeException;
55
import com.esri.sde.sdk.client.SeQuery;
56
import com.esri.sde.sdk.client.SeRow;
57
import com.esri.sde.sdk.client.SeShape;
58
import com.hardcode.gdbms.engine.values.Value;
59
import com.hardcode.gdbms.engine.values.ValueFactory;
60
import com.iver.cit.gvsig.fmap.DriverException;
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
            e.printStackTrace();
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
                        e.printStackTrace();
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
                        }
278
                }
279
        } catch (SeException e) {
280
            // TODO Auto-generated catch block
281
            e.printStackTrace();
282
        }
283
    }
284

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

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

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

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

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

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

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

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

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

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

    
365

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

    
371

    
372
        return feat;
373
    }
374

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

    
384
        }
385

    
386
}