Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.base / src / main / java / org / gvsig / geoprocess / algorithm / base / core / GeometryOperation.java @ 332

History | View | Annotate | Download (9.25 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.base.core;
25

    
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29

    
30
import org.gvsig.fmap.dal.DataSet;
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.EditableFeature;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureSelection;
35
import org.gvsig.fmap.dal.feature.FeatureSet;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.geoprocess.core.IProgressModel;
38
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
39
import org.gvsig.tools.dispose.DisposableIterator;
40
import org.gvsig.tools.task.SimpleTaskStatus;
41

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

    
122
                if(selectedGeom) {
123
                        featuresSet = (FeatureSet)inFeatStore.getSelection();
124
                } else {
125
                        featuresSet = inFeatStore.getFeatureSet();
126
                }
127
                
128
                it = featuresSet.iterator();
129
                numberOfFeatures = (int)featuresSet.getSize();
130
        if (status != null && process != null) {
131
            status.setRangeOfValues(0, numberOfFeatures);
132
            process.setProgress(0, numberOfFeatures);
133
        }
134
                
135
                int iCount = 0;
136
                while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
137
                        Feature feature = (Feature)it.next();
138
                        List geomList = feature.getGeometries();
139
                        
140
            if (status != null && process != null) {
141
                status.setCurValue(iCount);
142
                process.setProgress(iCount, numberOfFeatures);
143
            }
144
                        iCount ++;
145
                        
146
                        if(geomList == null) {
147
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
148
                                invoke(geom, feature);
149
                                continue;
150
                        }
151

    
152
                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
153
                        boolean first = true;
154
                        EditableFeature editFeat = null;
155
                        while(itGeom.hasNext() && !process.getTaskMonitor().isCanceled()) {
156
                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
157
                                if(first) {
158
                                        editFeat = invoke(g, feature);
159
                                        first = false;
160
                                } else
161
                                        invoke(g, editFeat);
162
                        }
163
                }
164
                
165
                if(closeOutStore && persister != null)
166
                        persister.end();
167
        }
168
        
169
        /**
170
         * Computes a complete operation over the input FeatureStore. The result of this operation
171
         * is stored in the output FeatureStore. This method will call once for each feature.
172
         * @param inFeatStore
173
         *        Input FeatureStore
174
         * @param outFeatStore
175
         *        Output FeatureStore
176
         * @param attrNames
177
         *        List of attributes to build the output feature store
178
         * @param selectedGeom
179
         *        If it is true only the selected geometries will be processed
180
         * @throws DataException
181
         */
182
        public void computesFeatureOperation(FeatureStore inFeatStore,
183
                                                                        FeatureStore outFeatStore,
184
                                                                        String[] attrNames,
185
                                                                        boolean selectedGeom,
186
                                                                        boolean closeOutStore) throws DataException {
187
                this.inFeatureStore = inFeatStore;
188
                this.selectedGeom = selectedGeom;
189
                FeatureSet featuresSet = null;
190
                featuresSet = inFeatStore.getFeatureSet();
191
                
192
                if(outFeatStore != null)
193
                        setFeatureStore(outFeatStore, attrNames);
194
                DisposableIterator it = null;
195

    
196
                if(selectedGeom) {
197
                        DataSet ds = inFeatStore.getSelection();
198
            it = ((FeatureSelection) ds).fastIterator();
199
                } else
200
                        it = featuresSet.fastIterator();
201
                numberOfFeatures = (int)featuresSet.getSize();
202

    
203
                if (status != null && process != null) {
204
            status.setRangeOfValues(0, numberOfFeatures);
205
            process.setProgress(0, numberOfFeatures);
206
        }
207
                
208
                int iCount = 0;
209
                while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
210
                        Feature feature = (Feature)it.next();
211
                        invoke(null, feature);
212
                        if (status != null && process != null) {
213
                                status.setCurValue(iCount);
214
                                process.setProgress(iCount, numberOfFeatures);
215
                        }
216
                        iCount ++;
217
                }
218
                
219
                if(closeOutStore && persister != null)
220
                        persister.end();
221
        }
222
        
223
        /**
224
         * Computes a complete operation over the input list of FeatureStore. The result of this operation
225
         * is stored in the output FeatureStore. This method will call once for each geometry.
226
         * @param inFeatStoreList
227
         *        Input FeatureStore list
228
         * @param outFeatStore
229
         *        Output FeatureStore
230
         * @param attrNames
231
         *        List of attributes to build the output feature store
232
         * @param selectedGeom
233
         *        If it is true only the selected geometries will be processed
234
         * @throws DataException
235
         */
236
        public void computesGeometryOperationInAList(ArrayList<FeatureStore> inFeatStoreList,
237
                                                                        FeatureStore outFeatStore,
238
                                                                        String[] attrNames,
239
                                                                        boolean selectedGeom,
240
                                                                        boolean closeOutStore) throws DataException {
241
                this.inFeatureStoreList = inFeatStoreList;
242
                
243
                if (status != null && process != null) {
244
            status.setRangeOfValues(0, inFeatStoreList.size());
245
            process.setProgress(0, inFeatStoreList.size());
246
        }
247

    
248
                int iCount = 0;
249
                for (int i = 0; i < inFeatStoreList.size(); i++) {
250
                        if (status != null && process != null) {
251
                                status.setCurValue(iCount);
252
                                process.setProgress(iCount, numberOfFeatures);
253
                        }
254
                        iCount ++;
255
                        computesGeometryOperation(inFeatStoreList.get(i), 
256
                                                                                i == 0 ? outFeatStore : null, 
257
                                                                                attrNames, 
258
                                                                                selectedGeom, 
259
                                                                                false);
260
                        if(process.getTaskMonitor().isCanceled())
261
                                return;
262
                }
263
                
264
        if (closeOutStore && persister != null) {
265
                        persister.end();
266
        }
267
        }
268
        
269
        /**
270
         * Ends the edition and closes the FeatureStore
271
         * @deprecated
272
         */
273
        public void end() {
274
                persister.end();
275
        }
276

    
277
    /**
278
     * @deprecated use {@link #setTaskStatus(SimpleTaskStatus)} instead.
279
     */
280
        public void setProgressModel(IProgressModel pModel) {
281
        // Ignore
282
        }
283

    
284
    public void setTaskStatus(SimpleTaskStatus status) {
285
        this.status = status;
286
    }
287
    
288
    public void setGeoProcess(AbstractSextanteGeoProcess geoprocess, int size) {
289
                this.process = geoprocess;
290
                this.procesSize = size;
291
        }
292

    
293
}