root / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / fmap / drivers / sde / ArcSdeFeatureIterator.java @ 11197
History | View | Annotate | Download (12.4 KB)
1 | 2277 | vcaballero | /*
|
---|---|---|---|
2 | * Created on 11-mar-2005
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | 10942 | caballero | *
|
6 | 2277 | vcaballero | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 | 10942 | caballero | *
|
8 | 2277 | vcaballero | * 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 | 10942 | caballero | *
|
13 | 2277 | vcaballero | * 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 | 10942 | caballero | *
|
18 | 2277 | vcaballero | * 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 | 10942 | caballero | *
|
22 | 2277 | vcaballero | * 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 | 10942 | caballero | *
|
34 | 2277 | vcaballero | * or
|
35 | 10942 | caballero | *
|
36 | 2277 | vcaballero | * IVER T.I. S.A
|
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | 10942 | caballero | *
|
41 | 2277 | vcaballero | * +34 963163400
|
42 | * dac@iver.es
|
||
43 | */
|
||
44 | package com.iver.cit.gvsig.fmap.drivers.sde; |
||
45 | |||
46 | 11012 | caballero | import java.awt.geom.PathIterator; |
47 | 2277 | vcaballero | import java.sql.SQLException; |
48 | 11012 | caballero | import java.util.ArrayList; |
49 | 2277 | vcaballero | |
50 | 11012 | caballero | import com.esri.sde.sdk.client.SDEPoint; |
51 | 2277 | vcaballero | import com.esri.sde.sdk.client.SeColumnDefinition; |
52 | 11012 | caballero | import com.esri.sde.sdk.client.SeCoordinateReference; |
53 | 2277 | vcaballero | 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 | 10942 | caballero | import com.iver.cit.gvsig.fmap.DriverException; |
60 | 2277 | vcaballero | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
61 | 11012 | caballero | import com.iver.cit.gvsig.fmap.core.FMultiPoint2D; |
62 | 2277 | vcaballero | import com.iver.cit.gvsig.fmap.core.FNullGeometry; |
63 | import com.iver.cit.gvsig.fmap.core.FPoint2D; |
||
64 | 11012 | caballero | import com.iver.cit.gvsig.fmap.core.FShape; |
65 | 2277 | vcaballero | 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 | 10942 | caballero | * @author FJP
|
73 | 2277 | vcaballero | */
|
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 | 11012 | caballero | private int index=0; |
82 | 10942 | caballero | |
83 | 2277 | vcaballero | 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 | 10942 | caballero | { |
91 | 2277 | vcaballero | 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 | 10942 | caballero | } |
109 | 2277 | vcaballero | return gpx;
|
110 | } |
||
111 | 10942 | caballero | |
112 | 2277 | vcaballero | public static IGeometry getGeometry( SeShape shape ) { |
113 | 10942 | caballero | |
114 | 2277 | vcaballero | try {
|
115 | /*
|
||
116 | * Retrieve the shape type.
|
||
117 | */
|
||
118 | int type = -1; |
||
119 | type = shape.getType(); |
||
120 | 10942 | caballero | |
121 | 2277 | vcaballero | // Display the X and Y values
|
122 | 10942 | caballero | /* 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 | 2277 | vcaballero | System.out.println("X: " + points[partNo][subPartNo][pointNo] + "\tY: "
|
126 | 10942 | caballero | + points[partNo][subPartNo][(pointNo+1)] ); */
|
127 | 2277 | vcaballero | 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 | 10942 | caballero | |
134 | 2277 | vcaballero | 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 | 10942 | caballero | |
141 | case SeShape.TYPE_MULTI_POINT:
|
||
142 | 2277 | vcaballero | break;
|
143 | 10942 | caballero | |
144 | 2277 | vcaballero | case SeShape.TYPE_NIL:
|
145 | return new FNullGeometry(); |
||
146 | case SeShape.TYPE_POLYGON:
|
||
147 | 10942 | caballero | case SeShape.TYPE_MULTI_POLYGON:
|
148 | 2277 | vcaballero | // 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 | 10942 | caballero | } |
167 | |||
168 | 11012 | caballero | |
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 | 2277 | vcaballero | /**
|
256 | * @throws SQLException
|
||
257 | 10942 | caballero | *
|
258 | 2277 | vcaballero | */
|
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 | 11012 | caballero | 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 | 2277 | vcaballero | } catch (SeException e) {
|
279 | // TODO Auto-generated catch block
|
||
280 | e.printStackTrace(); |
||
281 | } |
||
282 | } |
||
283 | 10942 | caballero | |
284 | 2277 | vcaballero | /* (non-Javadoc)
|
285 | * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
|
||
286 | */
|
||
287 | 10942 | caballero | public boolean hasNext() throws DriverException { |
288 | 2277 | vcaballero | 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 | 10942 | caballero | |
299 | 2277 | vcaballero | return true; |
300 | } |
||
301 | 10942 | caballero | catch (SeException e) {
|
302 | e.printStackTrace(); |
||
303 | 2277 | vcaballero | // 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 | 10942 | caballero | public IFeature next() throws DriverException { |
312 | 2277 | vcaballero | SeShape spVal = null;
|
313 | 10942 | caballero | SeColumnDefinition[] colDefs = row.getColumns();
|
314 | 2277 | vcaballero | IFeature feat = null;
|
315 | try
|
||
316 | 10942 | caballero | { |
317 | 11012 | caballero | |
318 | 10942 | caballero | if ( row != null ) |
319 | { |
||
320 | 2277 | vcaballero | for (int colNum = 0; colNum < colDefs.length; colNum++) |
321 | 10942 | caballero | { |
322 | 2277 | vcaballero | 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 | 10942 | caballero | case SeColumnDefinition.TYPE_INT16:
|
329 | 2277 | vcaballero | regAtt[colNum-1] = ValueFactory.createValue(row.getShort(colNum).intValue());
|
330 | 10942 | caballero | break;
|
331 | 2277 | vcaballero | case SeColumnDefinition.TYPE_DATE:
|
332 | 10942 | caballero | regAtt[colNum-1] = ValueFactory.createValue(row.getTime(colNum).getTime());
|
333 | 2277 | vcaballero | break;
|
334 | 10942 | caballero | |
335 | case SeColumnDefinition.TYPE_INT32:
|
||
336 | case SeColumnDefinition.TYPE_INT64:
|
||
337 | 2277 | vcaballero | regAtt[colNum-1] = ValueFactory.createValue(row.getInteger(colNum).intValue());
|
338 | break;
|
||
339 | 10942 | caballero | |
340 | case SeColumnDefinition.TYPE_FLOAT32:
|
||
341 | 2277 | vcaballero | regAtt[colNum-1] = ValueFactory.createValue(row.getFloat(colNum).floatValue());
|
342 | break;
|
||
343 | 10942 | caballero | |
344 | case SeColumnDefinition.TYPE_FLOAT64:
|
||
345 | 2277 | vcaballero | regAtt[colNum-1] = ValueFactory.createValue(row.getDouble(colNum).doubleValue());
|
346 | break;
|
||
347 | 10942 | caballero | |
348 | 2277 | vcaballero | case SeColumnDefinition.TYPE_STRING:
|
349 | regAtt[colNum-1] = ValueFactory.createValue(row.getString(colNum));
|
||
350 | break;
|
||
351 | 10942 | caballero | |
352 | 2277 | vcaballero | case SeColumnDefinition.TYPE_SHAPE:
|
353 | 10942 | caballero | spVal = row.getShape(colNum); |
354 | 2277 | vcaballero | geom = getGeometry(spVal); |
355 | break;
|
||
356 | } // End switch
|
||
357 | } // End if
|
||
358 | } // for
|
||
359 | 11012 | caballero | //System.out.println("Dentro de next(): " + spVal.getFeatureId().longValue() + " " + regAtt[0]);
|
360 | 10942 | caballero | |
361 | 11012 | caballero | feat = new DefaultFeature(geom, regAtt,String.valueOf(regAtt[index-1]));//""+ spVal.getFeatureId().longValue()); |
362 | 2277 | vcaballero | } // if
|
363 | |||
364 | 10942 | caballero | |
365 | 2277 | vcaballero | } catch (SeException e)
|
366 | { |
||
367 | e.printStackTrace(); |
||
368 | } |
||
369 | 10942 | caballero | |
370 | |||
371 | 2277 | vcaballero | return feat;
|
372 | } |
||
373 | |||
374 | 10942 | caballero | 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 | 2277 | vcaballero | } |