Statistics
| Revision:

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

History | View | Annotate | Download (6.93 KB)

1 6433 jorpiell
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 6507 jorpiell
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
25 6433 jorpiell
26
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 *
44
 * For more information, contact:
45
 *
46
 *  Generalitat Valenciana
47
 *   Conselleria d'Infraestructures i Transport
48
 *   Av. Blasco Ib??ez, 50
49
 *   46010 VALENCIA
50
 *   SPAIN
51
 *
52
 *      +34 963862235
53
 *   gvsig@gva.es
54
 *      www.gvsig.gva.es
55
 *
56
 *    or
57
 *
58
 *   IVER T.I. S.A
59
 *   Salamanca 50
60
 *   46005 Valencia
61
 *   Spain
62
 *
63
 *   +34 963163400
64
 *   dac@iver.es
65
 */
66
/* CVS MESSAGES:
67
 *
68
 * $Id$
69
 * $Log$
70 6507 jorpiell
 * Revision 1.2  2006-07-24 07:36:40  jorpiell
71
 * Se han hecho un cambio en los nombres de los metodos para clarificar
72
 *
73
 * Revision 1.1  2006/07/19 12:29:39  jorpiell
74 6433 jorpiell
 * A?adido el driver de GML
75
 *
76
 *
77
 */
78
/**
79
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
80
 */
81 6507 jorpiell
public class GMLFeaturesIterator implements IFeatureIterator{
82
        protected FeatureReader featureReader = null;
83 6433 jorpiell
        private Vector featureVector = new Vector();
84
        private boolean parserException = false;
85
        private boolean finished = false;
86
        private int position = -1;
87
        private int length = -2;
88 6507 jorpiell
        protected ICoordTrans coordTrans;
89 6433 jorpiell
90
        public GMLFeaturesIterator(FeatureReader featureReader){
91
                this.featureReader = featureReader;
92
        }
93
94
        /**
95
         * Gets the next feature
96
         * @return
97
         * @throws DriverException
98
         */
99
        public IFeature next() throws DriverException {
100
                if (!finished){
101
                        Feature feature = null;
102
                        try {
103
                                feature = featureReader.next();
104
                        } catch (NoSuchElementException e) {
105
                                e.printStackTrace();
106
                        } catch (IOException e) {
107
                                e.printStackTrace();
108
                        } catch (IllegalAttributeException e) {
109
                                e.printStackTrace();
110
                        }
111
                        Object[] attr = null;
112
                        attr = feature.getAttributes(attr);
113
                        Value[] values = getValues(attr);
114
115
                        try{
116
                                IGeometry g = ShapeFactory.createGeometry(FConverter.jts_to_java2d(feature.getDefaultGeometry()));
117
                                if (coordTrans != null){
118
                                        g.reProject(coordTrans);
119
                                }
120
                                IFeature outPutFeature = new DefaultFeature(g,values,feature.getID());
121
                                featureVector.add(outPutFeature);
122
                                return outPutFeature;
123
                        }catch (NullPointerException e){
124
                                //The geometry can't be created
125
                        }
126
                }else{
127
                        return (DefaultFeature)featureVector.get(position);
128
                }
129
                return null;
130
        }
131
132
        /**
133
         * Returns if there is a new feature to retrieve
134
         * @return
135
         */
136
        public boolean hasNext(){
137
                if (length == -2){
138
                        try {
139
                                if (featureReader.hasNext()){
140
                                        position++;
141
                                        return true;
142
                                }else{
143
                                        featureReader.close();
144
                                        finished = true;
145
                                        length = featureVector.size() - 1;
146
                                        return false;
147
                                }
148
                        } catch (IOException e) {
149
                                e.printStackTrace();
150
                        }
151
                }else{
152
                        if (position != length){
153
                                position++;
154
                                return true;
155
                        }else{
156
                                return false;
157
                        }
158
                }
159
                return false;
160
        }
161
162
        /**
163
         * Gets the attributes
164
         * @param attr
165
         * @return
166
         */
167
        private Value[] getValues(Object[] attr) {
168
                Value[] values = null;
169
                if (attr.length - 1 > 0){
170
                        values = new Value[attr.length - 1];
171
                }else{
172
                        values = new Value[0];
173
                }
174
                for (int i=1;i<attr.length;i++){
175
                        if (attr[i]!=null){
176
                                if (attr[i] instanceof Double){
177
                                        values[i-1]=ValueFactory.createValue(((Double)attr[i]).doubleValue());
178
                                }else if (attr[i] instanceof String){
179
                                        values[i-1]=ValueFactory.createValue(String.valueOf(attr[i]));
180
                                }else if (attr[i] instanceof Long){
181
                                        values[i-1]=ValueFactory.createValue(((Long)attr[i]).longValue());
182
                                }else if (attr[i] instanceof Integer){
183
                                        values[i-1]=ValueFactory.createValue(((Integer)attr[i]).intValue());
184
                                }else if (attr[i] instanceof Float){
185
                                        values[i-1]=ValueFactory.createValue(((Float)attr[i]).floatValue());
186
                                }else if (attr[i] instanceof Short){
187
                                        values[i-1]=ValueFactory.createValue(((Short)attr[i]).shortValue());
188
                                }else if (attr[i] instanceof Boolean){
189
                                        values[i-1]=ValueFactory.createValue(((Boolean)attr[i]).booleanValue());
190
                                }else if (attr[i] instanceof Date){
191
                                        values[i-1]=ValueFactory.createValue(((Date)attr[i]));
192
                                }
193
                        }else{
194
                                values[i-1]=ValueFactory.createValue("");
195
                        }
196
                }
197
                return values;
198
        }
199
200
        /*
201
         *  (non-Javadoc)
202
         * @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
203
         */
204
        public void closeIterator() throws DriverException {
205
                // TODO Auto-generated method stub
206
        }
207
208
        /**
209
         * This method must be executed before to start an iteration
210
         * process
211
         */
212
        public void startIteration(){
213
                position = -1;
214
        }
215
216
        /**
217
         * @return the coordTrans
218
         */
219
        public ICoordTrans getCoordTrans() {
220
                return coordTrans;
221
        }
222
223
        /**
224
         * @return Returns the parserException.
225
         */
226
        public boolean isParserException() {
227
                return parserException;
228
        }
229
230
        /**
231
         * @param parserException The parserException to set.
232
         */
233
        public void setParserException(boolean parserException) {
234
                this.parserException = parserException;
235
        }
236
237
        /**
238
         * Return the attributes type
239
         *
240
         */
241
        public AttributeType[] getAttributeTypes(){
242
                try{
243
                        return featureReader.getFeatureType().getAttributeTypes();
244
                }catch(NullPointerException e){
245
                        return new AttributeType[0];
246
                }
247
        }
248
249
        /**
250
         * Returns the attributes name
251
         * @return
252
         */
253
        public ArrayList getAttributeNames(){
254
                AttributeType[] attr = getAttributeTypes();
255
                ArrayList arrayFields = new ArrayList();
256
                for (int i=0 ; i<attr.length ; i++){
257
                        if (!(GMLUtils.isGeometry(attr[i].getType()))){
258
                                arrayFields.add(attr[i].getName());
259
                        }
260
                }
261
                return arrayFields;
262
        }
263
264 6507 jorpiell
        /**
265
         * @param coordTrans The coordTrans to set.
266
         */
267
        public void setCoordTrans(ICoordTrans coordTrans) {
268
                this.coordTrans = coordTrans;
269
        }
270
271 6433 jorpiell
}