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 @ 48

History | View | Annotate | Download (7.95 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.DisposableIterator;
29
import org.gvsig.fmap.dal.feature.EditableFeature;
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureSet;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection;
34
import org.gvsig.sextante.app.extension.core.IProgressModel;
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
        private 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 abstract void setFeatureStore(FeatureStore out, String[] attrNames) throws DataException;
71
        
72
        /**
73
         * Gets the last result of this operation
74
         * @return
75
         */
76
        public Object getResult() {
77
                return lastEditFeature;
78
        }
79
        
80
        /**
81
         * Computes a complete operation over the input FeatureStore. The result of this operation
82
         * is stored in the output FeatureStore. This method will call once for each geometry.
83
         * @param inFeatStore
84
         *        Input FeatureStore
85
         * @param outFeatStore
86
         *        Output FeatureStore
87
         * @param attrNames
88
         *        List of attributes to build the output feature store
89
         * @param selectedGeom
90
         *        If it is true only the selected geometries will be processed
91
         * @throws DataException
92
         */
93
        @SuppressWarnings("unchecked")
94
        public void computesGeometryOperation(FeatureStore inFeatStore,
95
                                                                        FeatureStore outFeatStore,
96
                                                                        String[] attrNames,
97
                                                                        boolean selectedGeom,
98
                                                                        boolean closeOutStore) throws DataException {
99
                this.inFeatureStore = inFeatStore;
100
                this.selectedGeom = selectedGeom;
101
                FeatureSet featuresSet = null;
102
                featuresSet = inFeatStore.getFeatureSet();
103
                
104
                if(outFeatStore != null)
105
                        setFeatureStore(outFeatStore, attrNames);
106
                DisposableIterator it = null;
107

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

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

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

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

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