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

History | View | Annotate | Download (9.94 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 int                              numberOfFeatures    = 0;
53
        protected EditableFeature                  lastEditFeature     = null;
54
    protected SimpleTaskStatus                 status              = null;
55
    protected AbstractSextanteGeoProcess       process             = null;
56
    protected boolean                          selectedGeomInput   = false;
57
    protected boolean                          selectedGeomOverlay = false;
58
    protected int                              procesSize          = 0;
59

    
60
    public GeometryOperation(AbstractSextanteGeoProcess process) {
61
            this.process = process;
62
    }
63

    
64
    /**
65
     * Assigns the flag to use only the selected geometries
66
     * in the overlay layer
67
     */
68
    public void setSelectedGeomOverlayLayer(boolean setSelected) {
69
            this.selectedGeomOverlay = setSelected;
70
    }
71

    
72
        /**
73
         * Invokes this operation and returns an EditableFeature
74
         * @param g
75
         * @param featureInput
76
         * @return
77
         */
78
        public abstract EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput);
79

    
80
        /**
81
         * Invokes this operation
82
         * @param g
83
         * @param featureInput
84
         */
85
        public abstract void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput);
86

    
87
        /**
88
         * Sets the output FeatureType and the input attributes for this feature
89
         * @param out
90
         * @throws DataException
91
         */
92
        public void setFeatureStore(FeatureStore out, String[] attrNames) throws DataException {
93
                persister = new DALFeaturePersister(out, attrNames);
94
        }
95

    
96
        /**
97
         * Gets the last result of this operation
98
         * @return
99
         */
100
        public Object getResult() {
101
                return lastEditFeature;
102
        }
103

    
104
        /**
105
         * Computes a complete operation over the input FeatureStore. The result of this operation
106
         * is stored in the output FeatureStore. This method will call once for each geometry.
107
         * @param inFeatStore
108
         *        Input FeatureStore
109
         * @param outFeatStore
110
         *        Output FeatureStore
111
         * @param attrNames
112
         *        List of attributes to build the output feature store
113
         * @param selectedGeom
114
         *        If it is true only the selected geometries will be processed
115
         * @throws DataException
116
         */
117
        @SuppressWarnings({ "unchecked" })
118
        public void computesGeometryOperation(FeatureStore inFeatStore,
119
                                                                        FeatureStore outFeatStore,
120
                                                                        String[] attrNames,
121
                                                                        boolean selectedGeomInput,
122
                                                                        boolean selectedGeomOverlay,
123
                                                                        boolean closeOutStore) throws DataException {
124
                this.inFeatureStore = inFeatStore;
125
                FeatureSet featuresSet = null;
126
                this.selectedGeomInput = selectedGeomInput;
127
                this.selectedGeomOverlay = selectedGeomOverlay;
128
                if(outFeatStore != null)
129
                        setFeatureStore(outFeatStore, attrNames);
130
                DisposableIterator it = null;
131

    
132
                if(selectedGeomInput) {
133
                        featuresSet = (FeatureSet)inFeatStore.getSelection();
134
                } else {
135
                        featuresSet = inFeatStore.getFeatureSet();
136
                }
137

    
138
                it = featuresSet.iterator();
139
                numberOfFeatures = (int)featuresSet.getSize();
140
        if (status != null && process != null) {
141
            status.setRangeOfValues(0, numberOfFeatures);
142
            process.setProgress(0, numberOfFeatures);
143
        }
144

    
145
                int iCount = 0;
146
                while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
147
                        Feature feature = (Feature)it.next();
148
                        List geomList = feature.getGeometries();
149

    
150
            if (status != null && process != null) {
151
                status.setCurValue(iCount);
152
                process.setProgress(iCount, numberOfFeatures);
153
            }
154
                        iCount ++;
155

    
156
                        if(geomList == null) {
157
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
158
                                invoke(geom, feature);
159
                                continue;
160
                        }
161

    
162
                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
163
                        boolean first = true;
164
                        EditableFeature editFeat = null;
165
                        while(itGeom.hasNext() && !process.getTaskMonitor().isCanceled()) {
166
                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
167
                                if(first) {
168
                                        editFeat = invoke(g, feature);
169
                                        first = false;
170
                                } else
171
                                        invoke(g, editFeat);
172
                        }
173
                }
174
        process.setProgress(iCount, numberOfFeatures);
175

    
176
                if(closeOutStore && persister != null)
177
                        persister.end();
178
        }
179

    
180
        /**
181
         * Computes a complete operation over the input FeatureStore. The result of this operation
182
         * is stored in the output FeatureStore. This method will call once for each feature.
183
         * @param inFeatStore
184
         *        Input FeatureStore
185
         * @param outFeatStore
186
         *        Output FeatureStore
187
         * @param attrNames
188
         *        List of attributes to build the output feature store
189
         * @param selectedGeom
190
         *        If it is true only the selected geometries will be processed
191
         * @throws DataException
192
         */
193
        public void computesFeatureOperation(FeatureStore inFeatStore,
194
                                                                        FeatureStore outFeatStore,
195
                                                                        String[] attrNames,
196
                                                                        boolean selectedGeomInput,
197
                                                                        boolean selectedGeomOverlay,
198
                                                                        boolean closeOutStore) throws DataException {
199
                this.inFeatureStore = inFeatStore;
200
                FeatureSet featuresSet = null;
201
                featuresSet = inFeatStore.getFeatureSet();
202
                this.selectedGeomInput = selectedGeomInput;
203
                this.selectedGeomOverlay = selectedGeomOverlay;
204

    
205
                if(outFeatStore != null)
206
                        setFeatureStore(outFeatStore, attrNames);
207
                DisposableIterator it = null;
208

    
209
                if(selectedGeomInput) {
210
                        DataSet ds = inFeatStore.getSelection();
211
            it = ((FeatureSelection) ds).fastIterator();
212
                } else
213
                        it = featuresSet.fastIterator();
214
                numberOfFeatures = (int)featuresSet.getSize();
215

    
216
                if (status != null && process != null) {
217
            status.setRangeOfValues(0, numberOfFeatures);
218
            process.setProgress(0, numberOfFeatures);
219
        }
220

    
221
                int iCount = 0;
222
                while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
223
                        Feature feature = (Feature)it.next();
224
                        invoke(null, feature);
225
                        if (status != null && process != null) {
226
                                status.setCurValue(iCount);
227
                                process.setProgress(iCount, numberOfFeatures);
228
                        }
229
                        iCount ++;
230
                }
231

    
232
                if(closeOutStore && persister != null)
233
                        persister.end();
234
        }
235

    
236
        /**
237
         * Computes a complete operation over the input list of FeatureStore. The result of this operation
238
         * is stored in the output FeatureStore. This method will call once for each geometry.
239
         * @param inFeatStoreList
240
         *        Input FeatureStore list
241
         * @param outFeatStore
242
         *        Output FeatureStore
243
         * @param attrNames
244
         *        List of attributes to build the output feature store
245
         * @param selectedGeom
246
         *        If it is true only the selected geometries will be processed
247
         * @throws DataException
248
         */
249
        public void computesGeometryOperationInAList(ArrayList<FeatureStore> inFeatStoreList,
250
                                                                        FeatureStore outFeatStore,
251
                                                                        String[] attrNames,
252
                                                                        boolean selectedGeomInput,
253
                                                                        boolean selectedGeomOverlay,
254
                                                                        boolean closeOutStore) throws DataException {
255
                this.inFeatureStoreList = inFeatStoreList;
256
                this.selectedGeomInput = selectedGeomInput;
257
                this.selectedGeomOverlay = selectedGeomOverlay;
258

    
259
                if (status != null && process != null) {
260
            status.setRangeOfValues(0, inFeatStoreList.size());
261
            process.setProgress(0, inFeatStoreList.size());
262
        }
263

    
264
                int iCount = 0;
265
                for (int i = 0; i < inFeatStoreList.size(); i++) {
266
                        if (status != null && process != null) {
267
                                status.setCurValue(iCount);
268
                                process.setProgress(iCount, numberOfFeatures);
269
                        }
270
                        iCount ++;
271
                        computesGeometryOperation(inFeatStoreList.get(i),
272
                                                                                i == 0 ? outFeatStore : null,
273
                                                                                attrNames,
274
                                                                                selectedGeomInput,
275
                                                                                selectedGeomOverlay,
276
                                                                                false);
277
                        if(process.getTaskMonitor().isCanceled())
278
                                return;
279
                }
280

    
281
        if (closeOutStore && persister != null) {
282
                        persister.end();
283
        }
284
        }
285

    
286
        /**
287
         * Ends the edition and closes the FeatureStore
288
         * @deprecated
289
         */
290
        public void end() {
291
                persister.end();
292
        }
293

    
294
    /**
295
     * @deprecated use {@link #setTaskStatus(SimpleTaskStatus)} instead.
296
     */
297
        public void setProgressModel(IProgressModel pModel) {
298
        // Ignore
299
        }
300

    
301
    public void setTaskStatus(SimpleTaskStatus status) {
302
        this.status = status;
303
    }
304

    
305
    public void setGeoProcess(AbstractSextanteGeoProcess geoprocess, int size) {
306
                this.process = geoprocess;
307
                this.procesSize = size;
308
        }
309

    
310
}