Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.base / src / main / java / org / gvsig / sextante / app / algorithm / base / core / GeometryOperation.java @ 90

History | View | Annotate | Download (8 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 */
20
package org.gvsig.sextante.app.algorithm.base.core;
21

    
22
import java.util.ArrayList;
23
import java.util.Iterator;
24
import java.util.List;
25

    
26
import org.gvsig.fmap.dal.DataSet;
27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.EditableFeature;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.dal.feature.FeatureSet;
31
import org.gvsig.fmap.dal.feature.FeatureStore;
32
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection;
33
import org.gvsig.sextante.app.extension.core.IProgressModel;
34
import org.gvsig.tools.dispose.DisposableIterator;
35

    
36
/**
37
 * Each geometry operation have to inherit from this class.
38
 *  
39
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
40
 */
41
public abstract class GeometryOperation {
42
        protected DALFeaturePersister              persister          = null;
43
        protected FeatureStore                     inFeatureStore     = null;
44
        protected ArrayList<FeatureStore>          inFeatureStoreList = null;
45
        protected boolean                          selectedGeom       = false;
46
        protected int                              numberOfFeatures   = 0;
47
        protected EditableFeature                  lastEditFeature    = null;
48
        protected IProgressModel                   progressModel      = null;
49
        
50
        /**
51
         * Invokes this operation and returns an EditableFeature
52
         * @param g
53
         * @param featureInput
54
         * @return
55
         */
56
        public abstract EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput);
57
        
58
        /**
59
         * Invokes this operation
60
         * @param g
61
         * @param featureInput
62
         */
63
        public abstract void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput);
64
        
65
        /**
66
         * Sets the output FeatureType and the input attributes for this feature
67
         * @param out
68
         * @throws DataException 
69
         */
70
        public void setFeatureStore(FeatureStore out, String[] attrNames) throws DataException {
71
                persister = new DALFeaturePersister(out, attrNames);
72
        }
73
        
74
        /**
75
         * Gets the last result of this operation
76
         * @return
77
         */
78
        public Object getResult() {
79
                return lastEditFeature;
80
        }
81
        
82
        /**
83
         * Computes a complete operation over the input FeatureStore. The result of this operation
84
         * is stored in the output FeatureStore. This method will call once for each geometry.
85
         * @param inFeatStore
86
         *        Input FeatureStore
87
         * @param outFeatStore
88
         *        Output FeatureStore
89
         * @param attrNames
90
         *        List of attributes to build the output feature store
91
         * @param selectedGeom
92
         *        If it is true only the selected geometries will be processed
93
         * @throws DataException
94
         */
95
        @SuppressWarnings("unchecked")
96
        public void computesGeometryOperation(FeatureStore inFeatStore,
97
                                                                        FeatureStore outFeatStore,
98
                                                                        String[] attrNames,
99
                                                                        boolean selectedGeom,
100
                                                                        boolean closeOutStore) throws DataException {
101
                this.inFeatureStore = inFeatStore;
102
                this.selectedGeom = selectedGeom;
103
                FeatureSet featuresSet = null;
104
                featuresSet = inFeatStore.getFeatureSet();
105
                
106
                if(outFeatStore != null)
107
                        setFeatureStore(outFeatStore, attrNames);
108
                DisposableIterator it = null;
109

    
110
                if(selectedGeom) {
111
                        DataSet ds = inFeatStore.getSelection();
112
                        it = ((DefaultFeatureSelection)ds).iterator();
113
                } else
114
                        it = featuresSet.iterator();
115
                numberOfFeatures = (int)featuresSet.getSize();
116
                if(progressModel != null)
117
                        progressModel.setTotalNumberOfSteps(numberOfFeatures);
118
                
119
                int iCount = 0;
120
                while( it.hasNext() ) {
121
                        Feature feature = (Feature)it.next();
122
                        List geomList = feature.getGeometries();
123
                        
124
                        if(progressModel != null)
125
                                progressModel.setProgress(iCount);
126
                        iCount ++;
127
                        
128
                        if(geomList == null) {
129
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
130
                                invoke(geom, feature);
131
                                continue;
132
                        }
133

    
134
                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
135
                        boolean first = true;
136
                        EditableFeature editFeat = null;
137
                        while(itGeom.hasNext()) {
138
                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
139
                                if(first) {
140
                                        editFeat = invoke(g, feature);
141
                                        first = false;
142
                                } else
143
                                        invoke(g, editFeat);
144
                        }
145
                }
146
                
147
                if(closeOutStore && persister != null)
148
                        persister.end();
149
        }
150
        
151
        /**
152
         * Computes a complete operation over the input FeatureStore. The result of this operation
153
         * is stored in the output FeatureStore. This method will call once for each feature.
154
         * @param inFeatStore
155
         *        Input FeatureStore
156
         * @param outFeatStore
157
         *        Output FeatureStore
158
         * @param attrNames
159
         *        List of attributes to build the output feature store
160
         * @param selectedGeom
161
         *        If it is true only the selected geometries will be processed
162
         * @throws DataException
163
         */
164
        public void computesFeatureOperation(FeatureStore inFeatStore,
165
                                                                        FeatureStore outFeatStore,
166
                                                                        String[] attrNames,
167
                                                                        boolean selectedGeom,
168
                                                                        boolean closeOutStore) throws DataException {
169
                this.inFeatureStore = inFeatStore;
170
                this.selectedGeom = selectedGeom;
171
                FeatureSet featuresSet = null;
172
                featuresSet = inFeatStore.getFeatureSet();
173
                
174
                if(outFeatStore != null)
175
                        setFeatureStore(outFeatStore, attrNames);
176
                DisposableIterator it = null;
177

    
178
                if(selectedGeom) {
179
                        DataSet ds = inFeatStore.getSelection();
180
                        it = ((DefaultFeatureSelection)ds).iterator();
181
                } else
182
                        it = featuresSet.iterator();
183
                numberOfFeatures = (int)featuresSet.getSize();
184

    
185
                if(progressModel != null)
186
                        progressModel.setTotalNumberOfSteps(numberOfFeatures);
187
                
188
                int iCount = 0;
189
                while( it.hasNext() ) {
190
                        Feature feature = (Feature)it.next();
191
                        invoke(null, feature);
192
                        if(progressModel != null)
193
                                progressModel.setProgress(iCount);
194
                        iCount ++;
195
                }
196
                
197
                if(closeOutStore && persister != null)
198
                        persister.end();
199
        }
200
        
201
        /**
202
         * Computes a complete operation over the input list of FeatureStore. The result of this operation
203
         * is stored in the output FeatureStore. This method will call once for each geometry.
204
         * @param inFeatStoreList
205
         *        Input FeatureStore list
206
         * @param outFeatStore
207
         *        Output FeatureStore
208
         * @param attrNames
209
         *        List of attributes to build the output feature store
210
         * @param selectedGeom
211
         *        If it is true only the selected geometries will be processed
212
         * @throws DataException
213
         */
214
        public void computesGeometryOperationInAList(ArrayList<FeatureStore> inFeatStoreList,
215
                                                                        FeatureStore outFeatStore,
216
                                                                        String[] attrNames,
217
                                                                        boolean selectedGeom,
218
                                                                        boolean closeOutStore) throws DataException {
219
                this.inFeatureStoreList = inFeatStoreList;
220
                IProgressModel  pModel = progressModel;
221
                
222
                if(progressModel != null) {
223
                        progressModel.setTotalNumberOfSteps(inFeatStoreList.size());
224
                        pModel = progressModel;
225
                        progressModel = null;  //Esto es para que computesGeometryOperation no actualice la barra
226
                }
227
                
228
                int iCount = 0;
229
                for (int i = 0; i < inFeatStoreList.size(); i++) {
230
                        if(pModel != null)
231
                                pModel.setProgress(iCount);
232
                        iCount ++;
233
                        computesGeometryOperation(inFeatStoreList.get(i), 
234
                                                                                i == 0 ? outFeatStore : null, 
235
                                                                                attrNames, 
236
                                                                                selectedGeom, 
237
                                                                                false);
238
                }
239
                
240
                this.progressModel = pModel;
241
                
242
                if(closeOutStore && persister != null)
243
                        persister.end();
244
        }
245
        
246
        /**
247
         * Ends the edition and closes the FeatureStore
248
         * @deprecated
249
         */
250
        public void end() {
251
                persister.end();
252
        }
253

    
254
        public void setProgressModel(IProgressModel pModel) {
255
                this.progressModel = pModel;
256
        }
257
        
258
}