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

History | View | Annotate | Download (7.69 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
import java.util.Iterator;
49
import java.util.List;
50

    
51
import com.vividsolutions.jts.geom.Geometry;
52

    
53
import es.unex.sextante.core.Sextante;
54

    
55
import org.gvsig.fmap.dal.exception.DataException;
56
import org.gvsig.fmap.dal.feature.EditableFeature;
57
import org.gvsig.fmap.dal.feature.Feature;
58
import org.gvsig.fmap.dal.feature.FeatureSelection;
59
import org.gvsig.fmap.dal.feature.FeatureSet;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
62
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
63
import org.gvsig.tools.dispose.DisposableIterator;
64
/**
65
 * Atomic dissolve operation
66
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
67
 */
68
public class AtomicDissolveOperation extends GeometryOperation {
69
        private IDissolveRule                    rule             = null;
70
        private ArrayList<Geometry>              geometryList     = null;
71
        private Feature                          feature          = null;
72
        private EditableFeature                  result           = null;
73
        private boolean[]                        analizedFeats    = null;
74
        private FeatureStore                     outFeatureStore  = null;
75
        private int                              featAnalized     = 0;
76
        private int                              newFeatID        = 0;
77
        private Summary                          summary          = null;                       
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 current feature
98
         * @param feat
99
         */
100
        public void setFeature(Feature feat) {
101
                this.feature = feat;
102
        }
103
        
104
        /**
105
         * Sets the list of analized Geoms
106
         * @param analizedGeoms
107
         */
108
        public void setAnalizedFeatureList(boolean[] analizedGeoms) {
109
                this.analizedFeats = analizedGeoms;
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
         * Sets the input FeatureStore
123
         * @param out
124
         * @throws DataException 
125
         */
126
        public void setFeatureStore(FeatureStore in) {
127
                this.inFeatureStore = in;
128
        }
129

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

    
155
                                result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom));
156
                                geometryList.clear();
157
                                geometryList.add(newGeom);
158
                                analizedFeats[featAnalized] = true;
159
                        } catch (DataException e) {
160
                                Sextante.addErrorToLog(e);
161
                        }
162
                }
163
                return result;
164
        }
165
        
166
        /*
167
         * (non-Javadoc)
168
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
169
         */
170
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
171
        }
172
        
173
        /*
174
         * (non-Javadoc)
175
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
176
         */
177
        public Object getResult() {
178
                newFeatID ++;
179
                return result;
180
        }
181
        
182
        /**
183
         * Computes this operation without build an output layer. When the operation
184
         * finish the result is got with getResult method.
185
         * @param selectedGeom
186
         * @throws DataException
187
         */
188
        @SuppressWarnings("unchecked")
189
        public void computesOperation(boolean selectedGeom) throws DataException {
190
                FeatureSet featuresSet = null;
191
                featuresSet = inFeatureStore.getFeatureSet();
192
                result = null;
193
                DisposableIterator it = null;
194
                
195
                summary = new Summary(rule, outFeatureStore.getDefaultFeatureType());
196
                summary.loadDefaultSummarizes(this.feature);
197

    
198
                if(selectedGeom) {
199
            FeatureSelection ds = inFeatureStore.getFeatureSelection();
200
            it = ds.iterator();
201
        } else {
202
                        it = featuresSet.iterator();
203
        }
204
                
205
                featAnalized = 0;
206
                while( it.hasNext() ) {
207
                        Feature feature = (Feature)it.next();
208
                        if(analizedFeats[featAnalized]) {
209
                                featAnalized ++;
210
                                continue;
211
                        }
212
                        
213
                        List geomList = feature.getGeometries();
214
                        
215
                        if(geomList == null) {
216
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
217
                                invoke(geom, feature);
218
                                featAnalized ++;
219
                                continue;
220
                        }
221

    
222
                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
223
                        while(itGeom.hasNext()) {
224
                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
225
                                invoke(g, feature);
226
                        }
227
                        featAnalized ++;
228
                }
229
                
230
                //Si el resultado es nulo es que no se ha hecho un dissolve porque no hab?a geometrias que unir
231
                //por lo que el resultado ser?n las geometrias de entrada
232
                if(result == null) {
233
                        try {
234
                                Geometry newGeom = GeometryUtil.geometryUnion(geometryList, feature.getDefaultGeometry().getGeometryType().getType());
235
                                result = outFeatureStore.createNewFeature();
236
                                result.setDouble(0, newFeatID);
237
                                result.set(1, feature.get(rule.getIndexField()));
238
                                summary.updateValues(feature);
239
                                summary.loadEditableFeature(result);
240
                                result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom));
241
                        } catch (DataException e) {
242
                                Sextante.addErrorToLog(e);
243
                        }
244
                }
245
        }
246

    
247
}
248