Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / gml / GMLFeaturesIterator.java @ 6433

History | View | Annotate | Download (6.81 KB)

1
package com.iver.cit.gvsig.fmap.drivers.gml;
2

    
3
import java.awt.geom.Point2D;
4
import java.io.IOException;
5
import java.util.ArrayList;
6
import java.util.Date;
7
import java.util.NoSuchElementException;
8
import java.util.Vector;
9

    
10
import org.cresques.cts.ICoordTrans;
11
import org.geotools.data.FeatureReader;
12
import org.geotools.feature.AttributeType;
13
import org.geotools.feature.Feature;
14
import org.geotools.feature.IllegalAttributeException;
15

    
16
import com.hardcode.gdbms.engine.values.Value;
17
import com.hardcode.gdbms.engine.values.ValueFactory;
18
import com.iver.cit.gvsig.fmap.DriverException;
19
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
20
import com.iver.cit.gvsig.fmap.core.IFeature;
21
import com.iver.cit.gvsig.fmap.core.IGeometry;
22
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
23
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
24

    
25
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
26
 *
27
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
 *
43
 * For more information, contact:
44
 *
45
 *  Generalitat Valenciana
46
 *   Conselleria d'Infraestructures i Transport
47
 *   Av. Blasco Ib??ez, 50
48
 *   46010 VALENCIA
49
 *   SPAIN
50
 *
51
 *      +34 963862235
52
 *   gvsig@gva.es
53
 *      www.gvsig.gva.es
54
 *
55
 *    or
56
 *
57
 *   IVER T.I. S.A
58
 *   Salamanca 50
59
 *   46005 Valencia
60
 *   Spain
61
 *
62
 *   +34 963163400
63
 *   dac@iver.es
64
 */
65
/* CVS MESSAGES:
66
 *
67
 * $Id: GMLFeaturesIterator.java 6433 2006-07-19 12:30:09Z jorpiell $
68
 * $Log$
69
 * Revision 1.1  2006-07-19 12:29:39  jorpiell
70
 * A?adido el driver de GML
71
 *
72
 *
73
 */
74
/**
75
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
76
 */
77
public class GMLFeaturesIterator {
78
        private FeatureReader featureReader = null;
79
        private Vector featureVector = new Vector();
80
        private boolean parserException = false;
81
        private boolean finished = false;
82
        private int position = -1;
83
        private int length = -2;
84
        private ICoordTrans coordTrans;        
85
        
86
        public GMLFeaturesIterator(FeatureReader featureReader){
87
                this.featureReader = featureReader;                
88
        }
89
        
90
        /**
91
         * Gets the next feature
92
         * @return
93
         * @throws DriverException
94
         */
95
        public IFeature next() throws DriverException {
96
                if (!finished){
97
                        Feature feature = null;
98
                        try {
99
                                feature = featureReader.next();
100
                        } catch (NoSuchElementException e) {
101
                                e.printStackTrace();
102
                        } catch (IOException e) {
103
                                e.printStackTrace();
104
                        } catch (IllegalAttributeException e) {
105
                                e.printStackTrace();
106
                        }
107
                        Object[] attr = null;
108
                        attr = feature.getAttributes(attr);
109
                        Value[] values = getValues(attr);
110
                        
111
                        try{
112
                                IGeometry g = ShapeFactory.createGeometry(FConverter.jts_to_java2d(feature.getDefaultGeometry()));
113
                                if (coordTrans != null){
114
                                        g.reProject(coordTrans);
115
                                }
116
                                IFeature outPutFeature = new DefaultFeature(g,values,feature.getID());
117
                                featureVector.add(outPutFeature);
118
                                return outPutFeature;
119
                        }catch (NullPointerException e){
120
                                //The geometry can't be created
121
                        }
122
                }else{
123
                        return (DefaultFeature)featureVector.get(position);                        
124
                }
125
                return null;
126
        }
127
        
128
        /**
129
         * Returns if there is a new feature to retrieve
130
         * @return
131
         */
132
        public boolean hasNext(){
133
                if (length == -2){
134
                        try {
135
                                if (featureReader.hasNext()){
136
                                        position++;
137
                                        return true;
138
                                }else{
139
                                        featureReader.close();
140
                                        finished = true;
141
                                        length = featureVector.size() - 1;
142
                                        return false;
143
                                }
144
                        } catch (IOException e) {
145
                                e.printStackTrace();
146
                        }
147
                }else{
148
                        if (position != length){
149
                                position++;
150
                                return true;
151
                        }else{
152
                                return false;
153
                        }
154
                }
155
                return false;
156
        }
157
        
158
        /**
159
         * Gets the attributes
160
         * @param attr
161
         * @return
162
         */
163
        private Value[] getValues(Object[] attr) {        
164
                Value[] values = null;
165
                if (attr.length - 1 > 0){
166
                        values = new Value[attr.length - 1];
167
                }else{
168
                        values = new Value[0];
169
                }
170
                for (int i=1;i<attr.length;i++){
171
                        if (attr[i]!=null){
172
                                if (attr[i] instanceof Double){
173
                                        values[i-1]=ValueFactory.createValue(((Double)attr[i]).doubleValue());
174
                                }else if (attr[i] instanceof String){
175
                                        values[i-1]=ValueFactory.createValue(String.valueOf(attr[i]));
176
                                }else if (attr[i] instanceof Long){
177
                                        values[i-1]=ValueFactory.createValue(((Long)attr[i]).longValue());
178
                                }else if (attr[i] instanceof Integer){
179
                                        values[i-1]=ValueFactory.createValue(((Integer)attr[i]).intValue());
180
                                }else if (attr[i] instanceof Float){
181
                                        values[i-1]=ValueFactory.createValue(((Float)attr[i]).floatValue());
182
                                }else if (attr[i] instanceof Short){
183
                                        values[i-1]=ValueFactory.createValue(((Short)attr[i]).shortValue());
184
                                }else if (attr[i] instanceof Boolean){
185
                                        values[i-1]=ValueFactory.createValue(((Boolean)attr[i]).booleanValue());
186
                                }else if (attr[i] instanceof Date){
187
                                        values[i-1]=ValueFactory.createValue(((Date)attr[i]));
188
                                }                                        
189
                        }else{
190
                                values[i-1]=ValueFactory.createValue("");
191
                        }
192
                }
193
                return values;
194
        }
195

    
196
        /*
197
         *  (non-Javadoc)
198
         * @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
199
         */
200
        public void closeIterator() throws DriverException {
201
                // TODO Auto-generated method stub                
202
        }
203
        
204
        /**
205
         * This method must be executed before to start an iteration
206
         * process
207
         */
208
        public void startIteration(){
209
                position = -1;                
210
        }
211
        
212
        /**
213
         * Reprojects the new point
214
         * @param ptOrig
215
         * Origin point
216
         * @return
217
         * FPoint2D
218
         */
219
        private Point2D getReprojectedPoint(Point2D ptOrigin){
220
                Point2D ptDest = null;
221
                return getCoordTrans().convert(ptOrigin, ptDest);
222
        }
223
        
224
        /**
225
         * @return the coordTrans
226
         */
227
        public ICoordTrans getCoordTrans() {
228
                return coordTrans;
229
        }
230

    
231
        /**
232
         * @return Returns the parserException.
233
         */
234
        public boolean isParserException() {
235
                return parserException;
236
        }
237

    
238
        /**
239
         * @param parserException The parserException to set.
240
         */
241
        public void setParserException(boolean parserException) {
242
                this.parserException = parserException;
243
        }
244
        
245
        /**
246
         * Return the attributes type
247
         *
248
         */
249
        public AttributeType[] getAttributeTypes(){
250
                try{
251
                        return featureReader.getFeatureType().getAttributeTypes();
252
                }catch(NullPointerException e){
253
                        return new AttributeType[0];
254
                }
255
        }
256
        
257
        /**
258
         * Returns the attributes name
259
         * @return
260
         */
261
        public ArrayList getAttributeNames(){
262
                AttributeType[] attr = getAttributeTypes();
263
                ArrayList arrayFields = new ArrayList();
264
                for (int i=0 ; i<attr.length ; i++){
265
                        if (!(GMLUtils.isGeometry(attr[i].getType()))){
266
                                arrayFields.add(attr[i].getName());
267
                        }
268
                }
269
                return arrayFields;
270
        }
271

    
272
}