Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / featureiterators / DefaultFeatureIterator.java @ 19509

History | View | Annotate | Download (7.41 KB)

1
/*
2
 * Created on 11-abr-2007
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
/* CVS MESSAGES:
45
*
46
* $Id: DefaultFeatureIterator.java 14713 2007-10-18 07:06:07Z jdominguez $
47
* $Log$
48
* Revision 1.5  2007-09-19 16:25:04  jaume
49
* ReadExpansionFileException removed from this context
50
*
51
* Revision 1.4  2007/06/07 11:49:39  azabala
52
* added default constructor
53
*
54
* Revision 1.3  2007/06/07 10:19:56  azabala
55
* closeIterator calls to ReadableVectorial.stop
56
*
57
* Revision 1.2  2007/06/06 18:03:34  azabala
58
* when String[] param is null, instead of return all fields it returns zero fields
59
*
60
* Revision 1.1  2007/05/29 19:08:11  azabala
61
* first version in cvs
62
*
63
* Revision 1.1  2007/04/19 17:27:58  azabala
64
* first version in cvs
65
*
66
*
67
*/
68
package com.iver.cit.gvsig.fmap.drivers.featureiterators;
69

    
70
import org.cresques.cts.ICoordTrans;
71
import org.cresques.cts.IProjection;
72

    
73
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
74
import com.hardcode.gdbms.engine.values.Value;
75
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
76
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
77
import com.iver.cit.gvsig.fmap.core.IFeature;
78
import com.iver.cit.gvsig.fmap.core.IGeometry;
79
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
80
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
81
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
82

    
83
/**
84
 * 
85
 * Iterator over all the features of a vectorial adapter.
86
 * It is thinked for data sources which dont have capabilities
87
 * of querying or reprojection.
88
 * 
89
 * 
90
 * @author Alvaro Zabala
91
 * */
92
public class DefaultFeatureIterator implements IFeatureIterator {
93
        
94
        /**
95
         * Projection of the layer on which this iterator iterates.
96
         * TODO Move projection from layer to adapter or driver
97
         * */
98
        protected IProjection sourceProjection;
99
    /**
100
     * If its setted, all features returned by this iterator
101
     * will be previously reprojected to this target projection
102
     */
103
        protected IProjection targetProjection;
104
        /**
105
         * If its setted, returned features only will have these alphanumeric attributes
106
         */
107
        protected String[] fieldNames;
108
        
109
        /**
110
         * vectorial data source. It reads geometries
111
         * and has the recordset
112
         */
113
        protected ReadableVectorial source;
114
        /**
115
         * recordset, it reads alphanumeric attributes
116
         */
117
        protected SelectableDataSource recordset;
118
        
119
        /**
120
         * index of the next feature that will be returned by this
121
         * iterator
122
         */
123
        protected int currentFeature;
124
        
125
        /**
126
         * Default constructor. 
127
         * Creates an iterator which will return features in the data source projection
128
         * (without reprojection) and with all the alphanumeric attributes
129
         * @throws ReadDriverException 
130
         *
131
         */
132
        public DefaultFeatureIterator( ReadableVectorial source) throws ReadDriverException{
133
                this.source = source;
134
                //needed for layers in edition status
135
                this.source.start();
136
                this.recordset = source.getRecordset();
137
                currentFeature = 0;
138
        }
139
        
140
        /**
141
         * Constructor.
142
         * The iterator will reproject the geometry of the features to the specified target projection,
143
         * and with the specified attribute fields.
144
         * */
145
        public DefaultFeatureIterator(ReadableVectorial source, 
146
                                                                        IProjection sourceProj, 
147
                                                                        IProjection targetProj, 
148
                                                                        String[] fieldNames) throws ReadDriverException{
149
                this(source);
150
                this.sourceProjection = sourceProj;
151
                //check to avoid reprojections with the same projection
152
                if(targetProj != null){
153
                         // FJP: Si la capa original no sabemos qu? proyecci?n tiene, no hacemos nada
154
                        if (sourceProj != null) {
155
                                if(!(targetProj.getAbrev().equalsIgnoreCase(sourceProjection.getAbrev())))
156
                                        this.targetProjection = targetProj;
157
                        }
158
                }
159
                this.fieldNames = fieldNames;
160
        }
161
        
162
        /**
163
         * Default constructor.
164
         *
165
         */
166
        public DefaultFeatureIterator(){
167
        }
168
        
169
        public boolean hasNext() throws ReadDriverException {
170
                boolean bMore = (currentFeature < source.getShapeCount());
171
                return bMore;
172
        }
173

    
174
        public IFeature next() throws ReadDriverException {
175
                
176
                try {
177
                        IGeometry geom = source.getShape(currentFeature);
178
                        reprojectIfNecessary(geom);
179
                        Value[] regAtt = getValues(currentFeature);
180
                        IFeature feat  = new DefaultFeature(geom, regAtt, currentFeature + "");
181
                        currentFeature++;
182
                        return feat;
183
                } catch (ExpansionFileReadException e) {
184
                        throw new ReadDriverException("",e);
185
                } 
186
        }
187

    
188
        public void closeIterator() throws ReadDriverException {
189
                this.source.stop();
190
        }
191

    
192
        public String[] getFieldNames() {
193
                return fieldNames;
194
        }
195

    
196
        public void setFieldNames(String[] fieldNames) {
197
                this.fieldNames = fieldNames;
198
        }
199

    
200
        public IProjection getTargetProjection() {
201
                return targetProjection;
202
        }
203

    
204
        public void setTargetProjection(IProjection targetProjection) {
205
                this.targetProjection = targetProjection;
206
        }
207

    
208
        public IProjection getSourceProjection() {
209
                return sourceProjection;
210
        }
211

    
212
        public void setSourceProjection(IProjection sourceProjection) {
213
                this.sourceProjection = sourceProjection;
214
        }
215
        
216
        /**
217
         * 
218
         * Checks if must reproject the given geom
219
         * and reprojects it if true
220
         * @param geom
221
         */
222
        protected void reprojectIfNecessary(IGeometry geom){
223
                if(this.targetProjection != null && 
224
                   this.sourceProjection != null &&
225
                   this.targetProjection.getAbrev() != this.sourceProjection.getAbrev()){
226
                        ICoordTrans trans = sourceProjection.getCT(targetProjection);
227
                        geom.reProject(trans);
228
                }
229
        }
230
        
231

    
232
        
233
        protected Value[] getValues(int featureIdx) throws ReadDriverException{
234
                Value[] regAtt = null;
235
                if(fieldNames == null){
236
                        //TODO Duda: cual es el comportamiento deseado cuando fieldNames sea null
237
                        //devolverlo todo o no devolver nada?????
238
//                        regAtt = new Value[recordset.getFieldCount()];//igual optimiza reutilizar y hacer copias (en vez de alocar array)
239
//                        for (int fieldId = 0; fieldId < recordset.getFieldCount(); fieldId++) {
240
//                                regAtt[fieldId] = recordset.getFieldValue(featureIdx, fieldId);
241
//                        }
242
                        regAtt = new Value[0];
243
                }else{
244
                        regAtt = new Value[fieldNames.length];
245
                        for (int fieldId = 0; fieldId < fieldNames.length; fieldId++) {
246
                                int fieldCode = recordset.getFieldIndexByName(fieldNames[fieldId]);
247
                                regAtt[fieldId] = recordset.getFieldValue(featureIdx, fieldCode);
248
                        }
249
                }
250
                return regAtt;
251
        }
252
        
253
}
254