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

History | View | Annotate | Download (10.7 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.task.SimpleTaskStatus;
40

    
41
/**
42
 * Each geometry operation have to inherit from this class.
43
 *
44
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
45
 */
46
@SuppressWarnings("deprecation")
47
public abstract class GeometryOperation {
48

    
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 in the overlay layer
66
     */
67
    public void setSelectedGeomOverlayLayer(boolean setSelected) {
68
        this.selectedGeomOverlay = setSelected;
69
    }
70

    
71
    /**
72
     * Invokes this operation and returns an EditableFeature
73
     *
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
     *
83
     * @param g
84
     * @param featureInput
85
     */
86
    public abstract void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput);
87

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

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

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

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

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

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

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

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

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

    
180
        if (closeOutStore && persister != null) {
181
            persister.end();
182
        }
183
    }
184

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

    
209
        if (outFeatStore != null) {
210
            setFeatureStore(outFeatStore, attrNames);
211
        }
212
        DisposableIterator it = null;
213

    
214
        if (selectedGeomInput) {
215
            DataSet ds = inFeatStore.getSelection();
216
            it = ((FeatureSelection) ds).fastIterator();
217
        } else {
218
            it = featuresSet.fastIterator();
219
        }
220
        numberOfFeatures = (int) featuresSet.getSize();
221

    
222
        if (status != null && process != null) {
223
            status.setRangeOfValues(0, numberOfFeatures);
224
            process.setProgress(0, numberOfFeatures);
225
        }
226

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

    
238
        if (closeOutStore && persister != null) {
239
            persister.end();
240
        }
241
    }
242

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

    
265
        if (status != null && process != null) {
266
            status.setRangeOfValues(0, inFeatStoreList.size());
267
            process.setProgress(0, inFeatStoreList.size());
268
        }
269

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

    
288
        if (closeOutStore && persister != null) {
289
            persister.end();
290
        }
291
    }
292

    
293
    /**
294
     * Ends the edition and closes the FeatureStore
295
     *
296
     * @deprecated
297
     */
298
    public void end() {
299
        persister.end();
300
    }
301

    
302
    /**
303
     * @deprecated use {@link #setTaskStatus(SimpleTaskStatus)} instead.
304
     */
305
    public void setProgressModel(IProgressModel pModel) {
306
        // Ignore
307
    }
308

    
309
    public void setTaskStatus(SimpleTaskStatus status) {
310
        this.status = status;
311
    }
312

    
313
    public void setGeoProcess(AbstractSextanteGeoProcess geoprocess, int size) {
314
        this.process = geoprocess;
315
        this.procesSize = size;
316
    }
317

    
318
}