Statistics
| Revision:

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

History | View | Annotate | Download (9.7 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.DriverException;
56
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
57
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
58
import com.iver.cit.gvsig.fmap.core.FPoint2D;
59
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
60
import com.iver.cit.gvsig.fmap.core.IFeature;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
63
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
64

    
65
/**
66
 * @author   FJP
67
 */
68
public class ArcSdeFeatureIterator implements IFeatureIterator {
69
    IGeometry geom;
70
    int numColumns;
71
    private SeQuery query = null;
72
    private boolean bFirst;
73
    Value[] regAtt;
74
    SeRow row;
75

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

    
99
                }
100
            }
101
        }
102
        return gpx;
103
    }
104

    
105
    public static IGeometry getGeometry( SeShape shape ) {
106

    
107
        try {
108
            /*
109
             *   Retrieve the shape type.
110
             */
111
            int type = -1;
112
            type = shape.getType();
113

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

    
127
                case SeShape.TYPE_LINE:
128
                case SeShape.TYPE_MULTI_LINE:
129
                case SeShape.TYPE_MULTI_SIMPLE_LINE:
130
                case SeShape.TYPE_SIMPLE_LINE:
131
                    GeneralPathX gpx = new GeneralPathX(shape.toGeneralPath());
132
                    return ShapeFactory.createPolyline2D(gpx);
133

    
134
                case SeShape.TYPE_MULTI_POINT:
135
                    break;
136

    
137
                case SeShape.TYPE_NIL:
138
                    return new FNullGeometry();
139
                case SeShape.TYPE_POLYGON:
140
                case SeShape.TYPE_MULTI_POLYGON:
141
                    // GeneralPathX gpx2 = new GeneralPathX(shape.toGeneralPath());
142
                    GeneralPathX gpx2 = convertSeShapeToGeneralPathX(shape);
143
                    /* SeExtent r = shape.getExtent();
144
                    GeneralPathX gpx2 = new GeneralPathX();
145
                    gpx2.moveTo(r.getMinX(), r.getMinY());
146
                    gpx2.lineTo(r.getMaxX(), r.getMinY());
147
                    gpx2.lineTo(r.getMaxX(), r.getMaxY());
148
                    gpx2.lineTo(r.getMinX(), r.getMaxY());
149
                    gpx2.closePath(); */
150
                    return ShapeFactory.createPolygon2D(gpx2);
151

    
152
            } // End switch
153
        }
154
        catch (SeException e)
155
        {
156
            e.printStackTrace();
157
        }
158
        return new FNullGeometry();
159
    }
160

    
161
    /**
162
     * @throws SQLException
163
     *
164
     */
165
    public ArcSdeFeatureIterator(SeQuery query) {
166
        // Debe ser forward only
167
        this.query = query;
168
        try {
169
            row = query.fetch();
170
                        if (row == null)
171
            {
172
                bFirst = true;
173
                return;
174
            }
175
            numColumns = row.getNumColumns();
176
            regAtt = new Value[numColumns-1];
177
            bFirst = true;
178
        } catch (SeException e) {
179
            // TODO Auto-generated catch block
180
            e.printStackTrace();
181
        }
182
    }
183

    
184
    /* (non-Javadoc)
185
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
186
     */
187
    public boolean hasNext() throws DriverException {
188
        try {
189
            if (bFirst)
190
                bFirst = false;
191
            else
192
                row = query.fetch();
193
            if (row == null)
194
            {
195
                query.close();
196
                return false;
197
            }
198

    
199
            return true;
200
        }
201
        catch (SeException e) {
202
            e.printStackTrace();
203
            // throw new SQLException(e.getMessage());
204
        }
205
        return false;
206
    }
207

    
208
    /* (non-Javadoc)
209
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
210
     */
211
    public IFeature next() throws DriverException {
212
        SeShape spVal = null;
213
        SeColumnDefinition[] colDefs = row.getColumns();
214
        IFeature feat = null;
215
        try
216
        {
217
            if ( row != null )
218
            {
219
                for (int colNum = 0; colNum < colDefs.length; colNum++)
220
                {
221
                    SeColumnDefinition colDef = colDefs[colNum];
222
                    int dataType = colDef.getType();
223
                    if ( row.getIndicator((short)colNum) != SeRow.SE_IS_NULL_VALUE)
224
                    {
225
                        switch( dataType )
226
                        {
227
                            case SeColumnDefinition.TYPE_INT16:
228
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getShort(colNum).intValue());
229
                                break;
230
                            case SeColumnDefinition.TYPE_DATE:
231
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getTime(colNum).getTime());
232
                                break;
233

    
234
                            case SeColumnDefinition.TYPE_INT32:
235
                            case SeColumnDefinition.TYPE_INT64:
236
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getInteger(colNum).intValue());
237
                                break;
238

    
239
                            case SeColumnDefinition.TYPE_FLOAT32:
240
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getFloat(colNum).floatValue());
241
                                break;
242

    
243
                            case SeColumnDefinition.TYPE_FLOAT64:
244
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getDouble(colNum).doubleValue());
245
                                break;
246

    
247
                            case SeColumnDefinition.TYPE_STRING:
248
                                regAtt[colNum-1] =  ValueFactory.createValue(row.getString(colNum));
249
                                break;
250

    
251
                            case SeColumnDefinition.TYPE_SHAPE:
252
                                spVal = row.getShape(colNum);
253
                                geom = getGeometry(spVal);
254
                                break;
255
                        } // End switch
256
                    } // End if
257
                } // for
258
                // System.out.println("Dentro de next(): " + spVal.getFeatureId().toString() + " " + regAtt[0]);
259

    
260
                feat = new DefaultFeature(geom, regAtt, "" + spVal.getFeatureId().longValue());
261
            } // if
262

    
263

    
264
        } catch (SeException e)
265
        {
266
            e.printStackTrace();
267
        }
268

    
269

    
270
        return feat;
271
    }
272

    
273
        public void closeIterator() throws DriverException {
274
                try {
275
                        bFirst=false;
276
                        query.close();
277
                } catch (SeException e) {
278
                        // TODO Auto-generated catch block
279
                        e.printStackTrace();
280
                }
281

    
282
        }
283

    
284
}