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

History | View | Annotate | Download (10.9 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 2 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.geoprocess.algorithm.base.core;
24

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

    
29
import org.gvsig.fmap.dal.DataSet;
30
import org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.feature.EditableFeature;
32
import org.gvsig.fmap.dal.feature.Feature;
33
import org.gvsig.fmap.dal.feature.FeatureSelection;
34
import org.gvsig.fmap.dal.feature.FeatureSet;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.geoprocess.core.IProgressModel;
37
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
38
import org.gvsig.tools.dispose.DisposableIterator;
39
import org.gvsig.tools.namestranslator.NamesTranslator;
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

    
50
    protected DALFeaturePersister persister = null;
51
    protected FeatureStore inFeatureStore = null;
52
    protected ArrayList<FeatureStore> inFeatureStoreList = null;
53
    protected int numberOfFeatures = 0;
54
    protected EditableFeature lastEditFeature = null;
55
    protected SimpleTaskStatus status = null;
56
    protected AbstractSextanteGeoProcess process = null;
57
    protected boolean selectedGeomInput = false;
58
    protected boolean selectedGeomOverlay = false;
59
    protected int procesSize = 0;
60

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

    
65
    /**
66
     * Assigns the flag to use only the selected geometries 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
     *
75
     * @param g
76
     * @param featureInput
77
     * @return
78
     */
79
    public abstract EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput);
80

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

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

    
99
    /**
100
     * Gets the last result of this operation
101
     *
102
     * @return
103
     */
104
    public Object getResult() {
105
        return lastEditFeature;
106
    }
107

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

    
136
        if (this.selectedGeomInput) {
137
            featuresSet = (FeatureSet) inFeatStore.getSelection();
138
        } else {
139
            featuresSet = inFeatStore.getFeatureSet();
140
        }
141

    
142
        it = featuresSet.iterator();
143
        numberOfFeatures = (int) featuresSet.getSize();
144
        if (status != null && process != null) {
145
            status.setRangeOfValues(0, numberOfFeatures);
146
            process.setProgress(0, numberOfFeatures);
147
        }
148

    
149
        int iCount = 0;
150
        while (it.hasNext() && !process.getTaskMonitor().isCanceled()) {
151
            Feature feature = (Feature) it.next();
152
            List geomList = feature.getGeometries();
153

    
154
            if (status != null && process != null) {
155
                status.setCurValue(iCount);
156
                process.setProgress(iCount, numberOfFeatures);
157
            }
158
            iCount++;
159

    
160
            if (geomList == null) {
161
                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
162
                if(geom!=null){
163
                    invoke(geom, feature);
164
                }
165
                continue;
166
            }
167

    
168
            Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
169
            boolean first = true;
170
            EditableFeature editFeat = null;
171
            while (itGeom.hasNext() && !process.getTaskMonitor().isCanceled()) {
172
                org.gvsig.fmap.geom.Geometry g = itGeom.next();
173
                if (first) {
174
                    editFeat = invoke(g, feature);
175
                    first = false;
176
                } else {
177
                    invoke(g, editFeat);
178
                }
179
            }
180
        }
181
        process.setProgress(iCount, numberOfFeatures);
182

    
183
        if (closeOutStore && persister != null) {
184
            persister.end();
185
        }
186
        
187
    }
188

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

    
213
        if (outFeatStore != null) {
214
            setFeatureStore(outFeatStore, attrNames);
215
        }
216
        DisposableIterator it = null;
217

    
218
        if (this.selectedGeomInput) {
219
            DataSet ds = inFeatStore.getSelection();
220
            it = ((FeatureSelection) ds).fastIterator();
221
        } else {
222
            it = featuresSet.fastIterator();
223
        }
224
        numberOfFeatures = (int) featuresSet.getSize();
225

    
226
        if (status != null && process != null) {
227
            status.setRangeOfValues(0, numberOfFeatures);
228
            process.setProgress(0, numberOfFeatures);
229
        }
230

    
231
        int iCount = 0;
232
        while (it.hasNext() && !process.getTaskMonitor().isCanceled()) {
233
            Feature feature = (Feature) it.next();
234
            invoke(null, feature);
235
            if (status != null && process != null) {
236
                status.setCurValue(iCount);
237
                process.setProgress(iCount, numberOfFeatures);
238
            }
239
            iCount++;
240
        }
241

    
242
        if (closeOutStore && persister != null) {
243
            persister.end();
244
        }
245
    }
246

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

    
269
        if (status != null && process != null) {
270
            status.setRangeOfValues(0, inFeatStoreList.size());
271
            process.setProgress(0, inFeatStoreList.size());
272
        }
273

    
274
        int iCount = 0;
275
        for (int i = 0; i < inFeatStoreList.size(); i++) {
276
            if (status != null && process != null) {
277
                status.setCurValue(iCount);
278
                process.setProgress(iCount, numberOfFeatures);
279
            }
280
            iCount++;
281
            computesGeometryOperation(inFeatStoreList.get(i),
282
                    i == 0 ? outFeatStore : null,
283
                    attrNames,
284
                    selectedGeomInput,
285
                    selectedGeomOverlay,
286
                    false);
287
            if (process.getTaskMonitor().isCanceled()) {
288
                return;
289
            }
290
        }
291

    
292
        if (closeOutStore && persister != null) {
293
            persister.end();
294
        }
295
    }
296

    
297
    /**
298
     * Ends the edition and closes the FeatureStore
299
     *
300
     * @deprecated
301
     */
302
    public void end() {
303
        persister.end();
304
    }
305

    
306
    /**
307
     * @deprecated use {@link #setTaskStatus(SimpleTaskStatus)} instead.
308
     */
309
    public void setProgressModel(IProgressModel pModel) {
310
        // Ignore
311
    }
312

    
313
    public void setTaskStatus(SimpleTaskStatus status) {
314
        this.status = status;
315
    }
316

    
317
    public void setGeoProcess(AbstractSextanteGeoProcess geoprocess, int size) {
318
        this.process = geoprocess;
319
        this.procesSize = size;
320
    }
321

    
322
}