Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.dissolve / src / main / java / org / gvsig / sextante / app / algorithm / dissolve / AtomicDissolveOperation.java @ 172

History | View | Annotate | Download (6.76 KB)

1
/*
2

3
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
4
 *
5
 * Copyright (C) 2010 Generalitat Valenciana.
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
20
 */
21

    
22
package org.gvsig.sextante.app.algorithm.dissolve;
23

    
24
import java.util.ArrayList;
25
import java.util.Iterator;
26
import java.util.List;
27

    
28
import com.vividsolutions.jts.geom.Geometry;
29

    
30
import es.unex.sextante.core.Sextante;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.EditableFeature;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureSelection;
36
import org.gvsig.fmap.dal.feature.FeatureSet;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.sextante.app.algorithm.base.core.GeometryOperation;
39
import org.gvsig.sextante.app.algorithm.base.util.GeometryUtil;
40
import org.gvsig.tools.dispose.DisposableIterator;
41
/**
42
 * Atomic dissolve operation
43
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
44
 */
45
public class AtomicDissolveOperation extends GeometryOperation {
46
        private IDissolveRule                    rule             = null;
47
        private ArrayList<Geometry>              geometryList     = null;
48
        private Feature                          feature          = null;
49
        private EditableFeature                  result           = null;
50
        private boolean[]                        analizedFeats    = null;
51
        private FeatureStore                     outFeatureStore  = null;
52
        private int                              featAnalized     = 0;
53
        private int                              newFeatID        = 0;
54
        private Summary                          summary          = null;                       
55
        
56
        /**
57
         * Sets IDissolveCriteria. This object will choose what geometry
58
         * will be dissolved   
59
         * @param criteria
60
         */
61
        public void setCriteria(IDissolveRule rule) {
62
                this.rule = rule;
63
        }
64
        
65
        /**
66
         * Sets the list of geometries
67
         * @param geoList
68
         */
69
        public void setGeometryList(ArrayList<Geometry> geoList) {
70
                this.geometryList = geoList;
71
        }
72
        
73
        /**
74
         * Sets the current feature
75
         * @param feat
76
         */
77
        public void setFeature(Feature feat) {
78
                this.feature = feat;
79
        }
80
        
81
        /**
82
         * Sets the list of analized Geoms
83
         * @param analizedGeoms
84
         */
85
        public void setAnalizedFeatureList(boolean[] analizedGeoms) {
86
                this.analizedFeats = analizedGeoms;
87
        }
88
        
89
        /**
90
         * Sets the output FeatureStore
91
         * @param out
92
         * @throws DataException 
93
         */
94
        public void setFeatureStore(FeatureStore out, String[] attrNames) {
95
                this.outFeatureStore = out;
96
        }
97
        
98
        /**
99
         * Sets the input FeatureStore
100
         * @param out
101
         * @throws DataException 
102
         */
103
        public void setFeatureStore(FeatureStore in) {
104
                this.inFeatureStore = in;
105
        }
106

    
107
        /*
108
         * (non-Javadoc)
109
         * @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
110
         */
111
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
112
                Geometry g1 = geometryList.get(0);
113
                if(g1 == null)
114
                        return null;
115
                
116
                com.vividsolutions.jts.geom.Geometry g2 = GeometryUtil.geomToJTS(g);
117
                
118
                //Si son iguales las geometrias no se opera
119
                if(g1.compareTo(g2) == 0)
120
                        return null;
121
                
122
                if(rule.verifyIfDissolve(g1, g2, this.feature, feature)) {
123
                        geometryList.add(g2);
124
                        Geometry newGeom = GeometryUtil.geometryUnion(geometryList, g.getGeometryType().getType());
125
                        try {
126
                                result = outFeatureStore.createNewFeature();
127
                                result.setDouble(0, newFeatID);
128
                                result.set(1, feature.get(rule.getIndexField()));
129
                                summary.updateValues(feature);
130
                                summary.loadEditableFeature(result);
131

    
132
                                result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom));
133
                                geometryList.clear();
134
                                geometryList.add(newGeom);
135
                                analizedFeats[featAnalized] = true;
136
                        } catch (DataException e) {
137
                                Sextante.addErrorToLog(e);
138
                        }
139
                }
140
                return result;
141
        }
142
        
143
        /*
144
         * (non-Javadoc)
145
         * @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
146
         */
147
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
148
        }
149
        
150
        /*
151
         * (non-Javadoc)
152
         * @see org.gvsig.sextante.app.algorithm.base.core.IOperation#getResult()
153
         */
154
        public Object getResult() {
155
                newFeatID ++;
156
                return result;
157
        }
158
        
159
        /**
160
         * Computes this operation without build an output layer. When the operation
161
         * finish the result is got with getResult method.
162
         * @param selectedGeom
163
         * @throws DataException
164
         */
165
        @SuppressWarnings("unchecked")
166
        public void computesOperation(boolean selectedGeom) throws DataException {
167
                FeatureSet featuresSet = null;
168
                featuresSet = inFeatureStore.getFeatureSet();
169
                result = null;
170
                DisposableIterator it = null;
171
                
172
                summary = new Summary(rule, outFeatureStore.getDefaultFeatureType());
173
                summary.loadDefaultSummarizes(this.feature);
174

    
175
                if(selectedGeom) {
176
            FeatureSelection ds = inFeatureStore.getFeatureSelection();
177
            it = ds.iterator();
178
        } else {
179
                        it = featuresSet.iterator();
180
        }
181
                
182
                featAnalized = 0;
183
                while( it.hasNext() ) {
184
                        Feature feature = (Feature)it.next();
185
                        if(analizedFeats[featAnalized]) {
186
                                featAnalized ++;
187
                                continue;
188
                        }
189
                        
190
                        List geomList = feature.getGeometries();
191
                        
192
                        if(geomList == null) {
193
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
194
                                invoke(geom, feature);
195
                                featAnalized ++;
196
                                continue;
197
                        }
198

    
199
                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
200
                        while(itGeom.hasNext()) {
201
                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
202
                                invoke(g, feature);
203
                        }
204
                        featAnalized ++;
205
                }
206
                
207
                //Si el resultado es nulo es que no se ha hecho un dissolve porque no hab?a geometrias que unir
208
                //por lo que el resultado ser?n las geometrias de entrada
209
                if(result == null) {
210
                        try {
211
                                Geometry newGeom = GeometryUtil.geometryUnion(geometryList, feature.getDefaultGeometry().getGeometryType().getType());
212
                                result = outFeatureStore.createNewFeature();
213
                                result.setDouble(0, newFeatID);
214
                                result.set(1, feature.get(rule.getIndexField()));
215
                                summary.updateValues(feature);
216
                                summary.loadEditableFeature(result);
217
                                result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom));
218
                        } catch (DataException e) {
219
                                Sextante.addErrorToLog(e);
220
                        }
221
                }
222
        }
223

    
224
}
225