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

History | View | Annotate | Download (10.8 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 (this.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
                if(geom!=null){
162
                    invoke(geom, feature);
163
                }
164
                continue;
165
            }
166

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
311
    public void setTaskStatus(SimpleTaskStatus status) {
312
        this.status = status;
313
    }
314

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

    
320
}