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

History | View | Annotate | Download (10.4 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
/*
25

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

    
45
package org.gvsig.geoprocess.algorithm.dissolve;
46

    
47
import java.util.ArrayList;
48

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

    
62
import com.vividsolutions.jts.geom.Geometry;
63

    
64
import es.unex.sextante.core.Sextante;
65
/**
66
 * Dissolve operation
67
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
68
 */
69
public class DissolveOperation extends GeometryOperation {
70
        private EditableFeature                  lastEditFeature  = null;
71
        private ArrayList<Geometry>              geometries       = new ArrayList<Geometry>();
72
        private IDissolveRule                    criteria         = null;
73
        private AtomicDissolveOperation          atomicOperation  = null;
74
        /**
75
         * Cada elemento representa una feature del vectorial de entrada. Cuando se hace un dissolve de
76
         * esa feature con otra su posici?n en el array se pone a false para no volver a procesarla.
77
         */
78
        //private boolean[]                        analizedFeats    = null;
79
        
80
        public DissolveOperation(IDissolveRule criteria, AbstractSextanteGeoProcess p) {
81
                super(p);
82
                this.criteria = criteria;
83
        }
84
        
85
        public AtomicDissolveOperation getAtomicDissolveOperation() {
86
                if(atomicOperation == null)
87
                        atomicOperation = new AtomicDissolveOperation(process);
88
                return atomicOperation;
89
        }
90

    
91
        /*
92
         * (non-Javadoc)
93
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
94
         */
95
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
96
                if(g == null)
97
                        return lastEditFeature;
98
                getAtomicDissolveOperation().setFeature(feature);
99
                try {
100
                        getAtomicDissolveOperation().computesOperation(selectedGeom);
101
                        EditableFeature res = (EditableFeature)getAtomicDissolveOperation().getResult();
102
                        lastEditFeature = persister.addFeature(res, res.getDefaultGeometry());                                
103
                } catch (DataException e) {
104
                        Sextante.addErrorToLog(e);
105
                } catch (CreateGeometryException e) {
106
                        Sextante.addErrorToLog(e);
107
                }
108
                
109
                return lastEditFeature;
110
        }
111
        
112
        /*
113
         * (non-Javadoc)
114
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
115
         */
116
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
117
                if(g == null)
118
                        return;
119
        }
120
        
121
        /*
122
         * (non-Javadoc)
123
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
124
         */
125
        public Object getResult() {
126
                return lastEditFeature;
127
        }
128
        
129
        /**
130
         * Computes a complete operation over the input FeatureStore. The result of this operation
131
         * is stored in the output FeatureStore. 
132
         * @param inFeatStore
133
         *        Input FeatureStore
134
         * @param outFeatStore
135
         *        Output FeatureStore
136
         * @param attrNames
137
         *        List of attributes to build the output feature store
138
         * @param selectedGeom
139
         *        If it is true only the selected geometries will be processed
140
         * @throws DataException
141
         */
142
        @SuppressWarnings({ "deprecation" })
143
        public void computesGeometryOperation(FeatureStore inFeatStore,
144
                                                                        FeatureStore outFeatStore,
145
                                                                        String[] attrNames,
146
                                                                        boolean selectedGeom,
147
                                                                        boolean closeOutStore) throws DataException {
148
                this.inFeatureStore = inFeatStore;
149
                this.selectedGeom = selectedGeom;
150
                FeatureSet featuresSet = null;
151
                featuresSet = inFeatStore.getFeatureSet();
152
                
153
                setFeatureStore(outFeatStore, attrNames);
154
                DisposableIterator it = null;
155

    
156
                if(selectedGeom) {
157
            FeatureSelection ds = inFeatStore.getFeatureSelection();
158
            it = ds.iterator();
159
            numberOfFeatures = (int) ds.getSelectedCount();
160
                } else {
161
                        it = featuresSet.iterator();
162
                        numberOfFeatures = (int)featuresSet.getSize();
163
                }
164
                
165
        if (status != null && process != null) {
166
            status.setRangeOfValues(0, numberOfFeatures);
167
            process.setProgress(0, numberOfFeatures);
168
        }
169
                
170
                ArrayList<Feature> featList = new ArrayList<Feature>();
171
                
172
                int iCount = 0;
173
                while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
174
                        featList.add((Feature)it.next());
175
            if (status != null && process != null) {
176
                status.setCurValue(iCount);
177
                process.setProgress(iCount, numberOfFeatures);
178
            }
179
                        iCount ++;
180
                }
181
                
182
                getAtomicDissolveOperation().setTaskStatus(status);
183
                getAtomicDissolveOperation().setGeoProcess(process, numberOfFeatures);
184
                getAtomicDissolveOperation().setCriteria(criteria);
185
                getAtomicDissolveOperation().setGeometryList(geometries);
186
                getAtomicDissolveOperation().setFeatureStore(persister.getOutputFeatureStore(), null);
187
                getAtomicDissolveOperation().setFeatureList(featList);
188
                
189
                iCount = 0;
190
                while(featList.size() > 0 && !process.getTaskMonitor().isCanceled()) {
191
                        Feature feature = featList.remove(0);
192

    
193
                        geometries.clear();
194

    
195
                        if (status != null && process != null) {
196
                                status.setCurValue(numberOfFeatures - featList.size());
197
                                process.setProgress(numberOfFeatures - featList.size(), numberOfFeatures);
198
                        }
199

    
200
                        org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
201
                        if(!(geom instanceof NullGeometry)) {
202
                                geometries.add(GeometryUtil.geomToJTS(geom));
203
                                invoke(geom, feature);
204
                        }
205
                        continue;
206

    
207
                }
208

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