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 / AtomicDissolveOperation.java @ 1259

History | View | Annotate | Download (9.09 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.FeatureStore;
53
import org.gvsig.fmap.geom.primitive.NullGeometry;
54
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
55
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
56
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
57

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

    
60
import es.unex.sextante.core.Sextante;
61
/**
62
 * Atomic dissolve operation
63
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
64
 */
65
public class AtomicDissolveOperation extends GeometryOperation {
66
        private IDissolveRule                    rule             = null;
67
        private ArrayList<Geometry>              geometryList     = null;
68
        private ArrayList<Feature>               featList         = null;
69
        private Feature                          feature          = null;
70
        private EditableFeature                  result           = null;
71
        private FeatureStore                     outFeatureStore  = null;
72
        private int                              newFeatID        = 0;
73
        private Summary                          summary          = null;                       
74
        
75
        public AtomicDissolveOperation(AbstractSextanteGeoProcess p) {
76
                super(p);
77
        }
78
        
79
        /**
80
         * Sets IDissolveCriteria. This object will choose what geometry
81
         * will be dissolved   
82
         * @param criteria
83
         */
84
        public void setCriteria(IDissolveRule rule) {
85
                this.rule = rule;
86
        }
87
        
88
        /**
89
         * Sets the list of geometries
90
         * @param geoList
91
         */
92
        public void setGeometryList(ArrayList<Geometry> geoList) {
93
                this.geometryList = geoList;
94
        }
95
        
96
        /**
97
         * Sets the list of geometries
98
         * @param geoList
99
         */
100
        public void setFeatureList(ArrayList<Feature> featList) {
101
                this.featList = featList;
102
        }
103
        
104
        /**
105
         * Sets the current feature
106
         * @param feat
107
         */
108
        public void setFeature(Feature feat) {
109
                this.feature = feat;
110
        }
111
        
112
        /**
113
         * Sets the output FeatureStore
114
         * @param out
115
         * @throws DataException 
116
         */
117
        public void setFeatureStore(FeatureStore out, String[] attrNames) {
118
                this.outFeatureStore = out;
119
        }
120

    
121
        
122
        /*
123
         * (non-Javadoc)
124
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
125
         */
126
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
127
                Geometry g1 = geometryList.get(0);
128
                if(g1 == null || g instanceof NullGeometry) 
129
                        return null;
130
                
131
                com.vividsolutions.jts.geom.Geometry g2 = GeometryUtil.geomToJTS(g);
132
                
133
                //Si son iguales las geometrias no se opera
134
                if(g1 == null || g1.compareTo(g2) == 0)
135
                        return null;
136
                
137
                if(rule.verifyIfDissolve(g1, g2, this.feature, feature)) {
138
                        geometryList.add(g2);
139
                        Geometry newGeom = GeometryUtil.geometryUnion(geometryList, g.getGeometryType().getType());
140
                        try {
141
                                result = outFeatureStore.createNewFeature();
142
                                result.setDouble(0, newFeatID);
143
                                result.set(1, feature.get(rule.getFieldName()));
144
                                summary.updateValues(feature);
145
                                summary.loadEditableFeature(result);
146

    
147
                                result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom));
148
                                geometryList.clear();
149
                                geometryList.add(newGeom);
150
                                return result;
151
                        } catch (DataException e) {
152
                                Sextante.addErrorToLog(e);
153
                        }
154
                }
155
                return null;
156
        }
157
        
158
        /*
159
         * (non-Javadoc)
160
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
161
         */
162
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
163
        }
164
        
165
        /*
166
         * (non-Javadoc)
167
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
168
         */
169
        public Object getResult() {
170
                newFeatID ++;
171
                return result;
172
        }
173
        
174
        /**
175
         * Computes this operation without build an output layer. When the operation
176
         * finish the result is got with getResult method.
177
         * @param selectedGeom
178
         * @throws DataException
179
         */
180
        public void computesOperation(boolean selectedGeom) throws DataException {
181
                result = null;
182
                summary = new Summary(rule, outFeatureStore.getDefaultFeatureType());
183
                summary.loadDefaultSummarizes(this.feature);
184

    
185
                int i = featList.size() - 1;
186
                while(i >= 0) {
187
                        if(process.getTaskMonitor().isCanceled()) 
188
                                return;
189

    
190
                        Feature feature = featList.get(i);
191

    
192
                        org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
193
                        EditableFeature ef = invoke(geom, feature);
194
                        if(ef != null) {
195
                                featList.remove(i);
196

    
197
                                if (process != null) 
198
                                        process.setProgress(numberOfFeatures - featList.size(), numberOfFeatures);
199

    
200
                                if(rule instanceof AdjacencyDissolveRule)
201
                                        i = featList.size();
202
                        }
203
                        i--;
204
                }
205
                
206
                //Si el resultado es nulo es que no se ha hecho un dissolve porque no hab?a geometrias que unir
207
                //por lo que el resultado ser?n las geometrias de entrada
208
                if(result == null) {
209
                        try {
210
                                Geometry newGeom = GeometryUtil.geometryUnion(geometryList, feature.getDefaultGeometry().getGeometryType().getType());
211
                                result = outFeatureStore.createNewFeature();
212
                                result.setDouble(0, newFeatID);
213
                                result.set(1, feature.get(rule.getFieldName()));
214
                                summary.updateValues(feature);
215
                                summary.loadEditableFeature(result);
216
                                result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom));
217
                        } catch (DataException e) {
218
                                Sextante.addErrorToLog(e);
219
                        }
220
                }
221
        }
222
        
223
        /**
224
         * Computes this operation without build an output layer. When the operation
225
         * finish the result is got with getResult method.
226
         * @param selectedGeom
227
         * @throws DataException
228
         */
229
//        public void computesOperation(boolean selectedGeom) throws DataException {
230
//                FeatureSet featuresSet = null;
231
//                featuresSet = inFeatureStore.getFeatureSet();
232
//                result = null;
233
//                DisposableIterator it = null;
234
//                
235
//                summary = new Summary(rule, outFeatureStore.getDefaultFeatureType());
236
//                summary.loadDefaultSummarizes(this.feature);
237
//
238
//                if(selectedGeom) {
239
//            FeatureSelection ds = inFeatureStore.getFeatureSelection();
240
//            it = ds.iterator();
241
//        } else {
242
//                        it = featuresSet.iterator();
243
//        }
244
//                
245
//                featAnalized = 0;
246
//                while( it.hasNext() ) {
247
//                        Feature feature = (Feature)it.next();
248
//                        if(analizedFeats[featAnalized]) {
249
//                                featAnalized ++;
250
//                                continue;
251
//                        }
252
//                        
253
//                        List geomList = feature.getGeometries();
254
//                        
255
//                        if(geomList == null) {
256
//                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
257
//                                invoke(geom, feature);
258
//                                featAnalized ++;
259
//                                continue;
260
//                        }
261
//
262
//                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
263
//                        while(itGeom.hasNext()) {
264
//                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
265
//                                invoke(g, feature);
266
//                        }
267
//                        featAnalized ++;
268
//                }
269
//                
270
//                //Si el resultado es nulo es que no se ha hecho un dissolve porque no hab?a geometrias que unir
271
//                //por lo que el resultado ser?n las geometrias de entrada
272
//                if(result == null) {
273
//                        try {
274
//                                Geometry newGeom = GeometryUtil.geometryUnion(geometryList, feature.getDefaultGeometry().getGeometryType().getType());
275
//                                result = outFeatureStore.createNewFeature();
276
//                                result.setDouble(0, newFeatID);
277
//                                result.set(1, feature.get(rule.getIndexField()));
278
//                                summary.updateValues(feature);
279
//                                summary.loadEditableFeature(result);
280
//                                result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom));
281
//                        } catch (DataException e) {
282
//                                Sextante.addErrorToLog(e);
283
//                        }
284
//                }
285
//        }
286

    
287
}
288