Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / DefaultFeatureIterator.java @ 11285

History | View | Annotate | Download (5.84 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 11285 2007-04-19 17:27:58Z azabala $
47
* $Log$
48
* Revision 1.1  2007-04-19 17:27:58  azabala
49
* first version in cvs
50
*
51
*
52
*/
53
package com.iver.cit.gvsig.fmap.drivers;
54

    
55
import org.cresques.cts.ICoordTrans;
56
import org.cresques.cts.IProjection;
57

    
58
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
59
import com.hardcode.gdbms.engine.values.Value;
60
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
61
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
62
import com.iver.cit.gvsig.fmap.core.IFeature;
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
65
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
66

    
67
/**
68
 * 
69
 * Iterator over all the features of a vectorial adapter.
70
 * It is thinked for data sources which dont have capabilities
71
 * of querying or reprojection.
72
 * 
73
 * 
74
 * @author Alvaro Zabala
75
 * */
76
public class DefaultFeatureIterator implements IFeatureIterator {
77
        
78
        /**
79
         * Projection of the layer on which this iterator iterates.
80
         * TODO Move projection from layer to adapter or driver
81
         * */
82
        protected IProjection sourceProjection;
83
    /**
84
     * If its setted, all features returned by this iterator
85
     * will be previously reprojected to this target projection
86
     */
87
        protected IProjection targetProjection;
88
        /**
89
         * If its setted, returned features only will have these alphanumeric attributes
90
         */
91
        protected String[] fieldNames;
92
        
93
        /**
94
         * vectorial data source. It reads geometries
95
         * and has the recordset
96
         */
97
        protected ReadableVectorial source;
98
        /**
99
         * recordset, it reads alphanumeric attributes
100
         */
101
        protected SelectableDataSource recordset;
102
        
103
        /**
104
         * index of the next feature that will be returned by this
105
         * iterator
106
         */
107
        protected int currentFeature;
108
        
109
        /**
110
         * Default constructor. 
111
         * Creates an iterator which will return features in the data source projection
112
         * (without reprojection) and with all the alphanumeric attributes
113
         * @throws ReadDriverException 
114
         *
115
         */
116
        public DefaultFeatureIterator( ReadableVectorial source) throws ReadDriverException{
117
                this.source = source;
118
                this.recordset = source.getRecordset();
119
                currentFeature = 0;
120
        }
121
        
122
        /**
123
         * Constructor.
124
         * The iterator will reproject the geometry of the features to the specified target projection,
125
         * and with the specified attribute fields.
126
         * */
127
        public DefaultFeatureIterator(ReadableVectorial source, 
128
                                                                        IProjection sourceProj, 
129
                                                                        IProjection targetProj, 
130
                                                                        String[] fieldNames) throws ReadDriverException{
131
                this(source);
132
                this.sourceProjection = sourceProj;
133
                this.targetProjection = targetProj;
134
                this.fieldNames = fieldNames;
135
        }
136
        
137
        public boolean hasNext() throws ReadDriverException,
138
                        ExpansionFileReadException {
139
                boolean bMore = (currentFeature < source.getShapeCount());
140
                return bMore;
141
        }
142

    
143
        public IFeature next() throws ReadDriverException {
144
                
145
                try {
146
                        IGeometry geom = getGeometry(currentFeature);
147
                        Value[] regAtt = getValues(currentFeature);
148
                        IFeature feat  = new DefaultFeature(geom, regAtt, currentFeature + "");
149
                        currentFeature++;
150
                        return feat;
151
                } catch (ExpansionFileReadException e) {
152
                        throw new ReadDriverException("",e);
153
                } 
154
        }
155

    
156
        public void closeIterator() throws ReadDriverException {
157
        }
158

    
159
        public String[] getFieldNames() {
160
                return fieldNames;
161
        }
162

    
163
        public void setFieldNames(String[] fieldNames) {
164
                this.fieldNames = fieldNames;
165
        }
166

    
167
        public IProjection getTargetProjection() {
168
                return targetProjection;
169
        }
170

    
171
        public void setTargetProjection(IProjection targetProjection) {
172
                this.targetProjection = targetProjection;
173
        }
174

    
175
        public IProjection getSourceProjection() {
176
                return sourceProjection;
177
        }
178

    
179
        public void setSourceProjection(IProjection sourceProjection) {
180
                this.sourceProjection = sourceProjection;
181
        }
182
        
183
        protected IGeometry getGeometry(int geomIdx) throws ExpansionFileReadException, 
184
                                                                                                                                ReadDriverException{
185
                IGeometry geom = source.getShape(geomIdx); 
186
                if(this.targetProjection != null && this.sourceProjection != null){
187
                        ICoordTrans trans = sourceProjection.getCT(targetProjection);
188
                        geom.reProject(trans);
189
                }
190
                return geom;
191
        }
192
        
193
        protected Value[] getValues(int featureIdx) throws ReadDriverException{
194
                Value[] regAtt = null;
195
                if(fieldNames == null){
196
                        regAtt = new Value[recordset.getFieldCount()];
197
                        for (int fieldId = 0; fieldId < recordset.getFieldCount(); fieldId++) {
198
                                regAtt[fieldId] = recordset.getFieldValue(featureIdx, fieldId);
199
                        }
200
                }else{
201
                        regAtt = new Value[fieldNames.length];
202
                        for (int fieldId = 0; fieldId < fieldNames.length; fieldId++) {
203
                                int fieldCode = recordset.getFieldIndexByName(fieldNames[fieldId]);
204
                                regAtt[fieldId] = recordset.getFieldValue(featureIdx, fieldCode);
205
                        }
206
                }
207
                return regAtt;
208
        }
209
        
210
}
211