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

History | View | Annotate | Download (7.56 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
/*
22

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

    
42
package org.gvsig.geoprocess.algorithm.dissolve;
43

    
44
import java.util.ArrayList;
45
import java.util.Iterator;
46
import java.util.List;
47

    
48
import com.vividsolutions.jts.geom.Geometry;
49

    
50
import es.unex.sextante.core.Sextante;
51

    
52
import org.gvsig.fmap.dal.exception.DataException;
53
import org.gvsig.fmap.dal.feature.EditableFeature;
54
import org.gvsig.fmap.dal.feature.Feature;
55
import org.gvsig.fmap.dal.feature.FeatureSelection;
56
import org.gvsig.fmap.dal.feature.FeatureSet;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
59
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
60
import org.gvsig.tools.dispose.DisposableIterator;
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 Feature                          feature          = null;
69
        private EditableFeature                  result           = null;
70
        private boolean[]                        analizedFeats    = null;
71
        private FeatureStore                     outFeatureStore  = null;
72
        private int                              featAnalized     = 0;
73
        private int                              newFeatID        = 0;
74
        private Summary                          summary          = null;                       
75
        
76
        /**
77
         * Sets IDissolveCriteria. This object will choose what geometry
78
         * will be dissolved   
79
         * @param criteria
80
         */
81
        public void setCriteria(IDissolveRule rule) {
82
                this.rule = rule;
83
        }
84
        
85
        /**
86
         * Sets the list of geometries
87
         * @param geoList
88
         */
89
        public void setGeometryList(ArrayList<Geometry> geoList) {
90
                this.geometryList = geoList;
91
        }
92
        
93
        /**
94
         * Sets the current feature
95
         * @param feat
96
         */
97
        public void setFeature(Feature feat) {
98
                this.feature = feat;
99
        }
100
        
101
        /**
102
         * Sets the list of analized Geoms
103
         * @param analizedGeoms
104
         */
105
        public void setAnalizedFeatureList(boolean[] analizedGeoms) {
106
                this.analizedFeats = analizedGeoms;
107
        }
108
        
109
        /**
110
         * Sets the output FeatureStore
111
         * @param out
112
         * @throws DataException 
113
         */
114
        public void setFeatureStore(FeatureStore out, String[] attrNames) {
115
                this.outFeatureStore = out;
116
        }
117
        
118
        /**
119
         * Sets the input FeatureStore
120
         * @param out
121
         * @throws DataException 
122
         */
123
        public void setFeatureStore(FeatureStore in) {
124
                this.inFeatureStore = in;
125
        }
126

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

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

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

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

    
244
}
245