Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.dissolve / src / main / java / org / gvsig / geoprocess / algorithm / dissolve / DissolveOperation.java @ 740

History | View | Annotate | Download (11 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
/*
24

25
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
26
 *
27
 * Copyright (C) 2010 Generalitat Valenciana.
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
 */
43
package org.gvsig.geoprocess.algorithm.dissolve;
44

    
45
import java.util.ArrayList;
46

    
47
import org.gvsig.fmap.dal.exception.DataException;
48
import org.gvsig.fmap.dal.feature.EditableFeature;
49
import org.gvsig.fmap.dal.feature.Feature;
50
import org.gvsig.fmap.dal.feature.FeatureSelection;
51
import org.gvsig.fmap.dal.feature.FeatureSet;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.geom.exception.CreateGeometryException;
54
import org.gvsig.fmap.geom.primitive.NullGeometry;
55
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
56
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
57
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
58

    
59
import com.vividsolutions.jts.geom.Geometry;
60

    
61
import es.unex.sextante.core.Sextante;
62
import java.util.Iterator;
63

    
64
/**
65
 * Dissolve operation
66
 *
67
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
68
 */
69
public class DissolveOperation extends GeometryOperation {
70

    
71
    private EditableFeature lastEditFeature = null;
72
    private ArrayList<Geometry> geometries = new ArrayList<Geometry>();
73
    private IDissolveRule criteria = null;
74
    private AtomicDissolveOperation atomicOperation = null;
75

    
76
    /**
77
     * Cada elemento representa una feature del vectorial de entrada. Cuando se
78
     * hace un dissolve de esa feature con otra su posici?n en el array se pone
79
     * a false para no volver a procesarla.
80
     */
81
        //private boolean[]                        analizedFeats    = null;
82
    public DissolveOperation(IDissolveRule criteria, AbstractSextanteGeoProcess p) {
83
        super(p);
84
        this.criteria = criteria;
85
    }
86

    
87
    public AtomicDissolveOperation getAtomicDissolveOperation() {
88
        if (atomicOperation == null) {
89
            atomicOperation = new AtomicDissolveOperation(process);
90
        }
91
        return atomicOperation;
92
    }
93

    
94
    /*
95
     * (non-Javadoc)
96
     * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
97
     */
98
    public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
99
        if (g == null) {
100
            return lastEditFeature;
101
        }
102
        getAtomicDissolveOperation().setFeature(feature);
103
        try {
104
            getAtomicDissolveOperation().computesOperation(selectedGeomInput);
105
            EditableFeature res = (EditableFeature) getAtomicDissolveOperation().getResult();
106
            lastEditFeature = persister.addFeature(res, res.getDefaultGeometry());
107
        } catch (DataException e) {
108
            Sextante.addErrorToLog(e);
109
        } catch (CreateGeometryException e) {
110
            Sextante.addErrorToLog(e);
111
        }
112

    
113
        return lastEditFeature;
114
    }
115

    
116
    /*
117
     * (non-Javadoc)
118
     * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
119
     */
120
    public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
121
        if (g == null) {
122
            return;
123
        }
124
    }
125

    
126
    /*
127
     * (non-Javadoc)
128
     * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
129
     */
130
    public Object getResult() {
131
        return lastEditFeature;
132
    }
133

    
134
    /**
135
     * Computes a complete operation over the input FeatureStore. The result of
136
     * this operation is stored in the output FeatureStore.
137
     *
138
     * @param inFeatStore Input FeatureStore
139
     * @param outFeatStore Output FeatureStore
140
     * @param attrNames List of attributes to build the output feature store
141
     * @param selectedGeom If it is true only the selected geometries will be
142
     * processed
143
     * @throws DataException
144
     */
145
    @SuppressWarnings({"deprecation"})
146
    public void computesGeometryOperation(FeatureStore inFeatStore,
147
            FeatureStore outFeatStore,
148
            String[] attrNames,
149
            boolean selectedGeomInput,
150
            boolean selectedGeomOutput,
151
            boolean closeOutStore) throws DataException {
152
        this.inFeatureStore = inFeatStore;
153
        this.selectedGeomInput = selectedGeomInput;
154
        this.selectedGeomOverlay = selectedGeomOutput;
155
        FeatureSet featuresSet = null;
156
        featuresSet = inFeatStore.getFeatureSet();
157

    
158
        setFeatureStore(outFeatStore, attrNames);
159
        Iterator it = null;
160

    
161
        if (selectedGeomInput) {
162
            FeatureSelection ds = inFeatStore.getFeatureSelection();
163
            it = ds.iterator();
164
            numberOfFeatures = (int) ds.getSelectedCount();
165
        } else {
166
            it = featuresSet.iterator();
167
            numberOfFeatures = (int) featuresSet.getSize();
168
        }
169

    
170
        if (status != null && process != null) {
171
            status.setRangeOfValues(0, numberOfFeatures);
172
            process.setProgress(0, numberOfFeatures);
173
        }
174

    
175
        ArrayList<Feature> featList = new ArrayList<Feature>();
176

    
177
        int iCount = 0;
178
        while (it.hasNext() && !process.getTaskMonitor().isCanceled()) {
179
            featList.add((Feature) it.next());
180
            if (status != null && process != null) {
181
                status.setCurValue(iCount);
182
                process.setProgress(iCount, numberOfFeatures);
183
            }
184
            iCount++;
185
        }
186

    
187
        getAtomicDissolveOperation().setTaskStatus(status);
188
        getAtomicDissolveOperation().setGeoProcess(process, numberOfFeatures);
189
        getAtomicDissolveOperation().setCriteria(criteria);
190
        getAtomicDissolveOperation().setGeometryList(geometries);
191
        getAtomicDissolveOperation().setFeatureStore(persister.getOutputFeatureStore(), null);
192
        getAtomicDissolveOperation().setFeatureList(featList);
193

    
194
        iCount = 0;
195
        while (featList.size() > 0 && !process.getTaskMonitor().isCanceled()) {
196
            Feature feature = featList.remove(0);
197

    
198
            geometries.clear();
199

    
200
            if (status != null && process != null) {
201
                status.setCurValue(numberOfFeatures - featList.size());
202
                process.setProgress(numberOfFeatures - featList.size(), numberOfFeatures);
203
            }
204

    
205
            org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
206
            if (!(geom instanceof NullGeometry)) {
207
                geometries.add(GeometryUtil.geomToJTS(geom));
208
                invoke(geom, feature);
209
            }
210
            continue;
211

    
212
        }
213

    
214
        if (closeOutStore && persister != null) {
215
            persister.end();
216
        }
217
    }
218

    
219
    /**
220
     * Computes a complete operation over the input FeatureStore. The result of
221
     * this operation is stored in the output FeatureStore.
222
     *
223
     * @param inFeatStore Input FeatureStore
224
     * @param outFeatStore Output FeatureStore
225
     * @param attrNames List of attributes to build the output feature store
226
     * @param selectedGeom If it is true only the selected geometries will be
227
     * processed
228
     * @throws DataException
229
     */
230
//        public void computesGeometryOperation(FeatureStore inFeatStore,
231
//                                                                        FeatureStore outFeatStore,
232
//                                                                        String[] attrNames,
233
//                                                                        boolean selectedGeom,
234
//                                                                        boolean closeOutStore) throws DataException {
235
//                this.inFeatureStore = inFeatStore;
236
//                this.selectedGeom = selectedGeom;
237
//                FeatureSet featuresSet = null;
238
//                featuresSet = inFeatStore.getFeatureSet();
239
//                
240
//                setFeatureStore(outFeatStore, attrNames);
241
//                DisposableIterator it = null;
242
//
243
//                if(selectedGeom) {
244
//            FeatureSelection ds = inFeatStore.getFeatureSelection();
245
//            it = ds.iterator();
246
//            numberOfFeatures = (int) ds.getSelectedCount();
247
//                } else {
248
//                        it = featuresSet.iterator();
249
//                        numberOfFeatures = (int)featuresSet.getSize();
250
//                }
251
//                
252
//        if (status != null && process != null) {
253
//            status.setRangeOfValues(0, numberOfFeatures);
254
//            process.setProgress(0, numberOfFeatures);
255
//        }
256
//                analizedFeats = new boolean[numberOfFeatures];
257
//                
258
//                getAtomicDissolveOperation().setCriteria(criteria);
259
//                getAtomicDissolveOperation().setGeometryList(geometries);
260
//                getAtomicDissolveOperation().setFeatureStore(persister.getOutputFeatureStore(), null);
261
//                getAtomicDissolveOperation().setAnalizedFeatureList(analizedFeats);
262
//                getAtomicDissolveOperation().setFeatureStore(inFeatureStore);
263
//                
264
//                int iCount = 0;
265
//                int featAnalized = 0;
266
//                while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
267
//                        Feature feature = (Feature)it.next();
268
//                        
269
//                        if(analizedFeats[featAnalized]) {
270
//                                featAnalized ++;
271
//                                continue;
272
//                        }
273
//                        
274
//                        analizedFeats[featAnalized] = true;
275
//                        List geomList = feature.getGeometries();
276
//                        geometries.clear();
277
//                        
278
//            if (status != null && process != null) {
279
//                status.setCurValue(iCount);
280
//                process.setProgress(iCount, numberOfFeatures);
281
//            }
282
//                        iCount ++;
283
//                        
284
//                        if(geomList == null) {
285
//                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
286
//                                geometries.add(GeometryUtil.geomToJTS(geom));
287
//                                invoke(geom, feature);
288
//                                featAnalized ++;
289
//                                continue;
290
//                        }
291
//
292
//                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
293
//                        while(itGeom.hasNext() && !process.getTaskMonitor().isCanceled()) {
294
//                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
295
//                                geometries.add(GeometryUtil.geomToJTS(g));
296
//                                invoke(g, feature);
297
//                        }
298
//                        
299
//                        featAnalized ++;
300
//                }
301
//        
302
//                if(closeOutStore && persister != null)
303
//                        persister.end();
304
//        }
305
}