Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.base / src / main / java / org / gvsig / geoprocess / algorithm / base / core / GeometryOperation.java @ 191

History | View | Annotate | Download (8.1 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 */
20
package org.gvsig.geoprocess.algorithm.base.core;
21

    
22
import java.util.ArrayList;
23
import java.util.Iterator;
24
import java.util.List;
25

    
26
import org.gvsig.fmap.dal.DataSet;
27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.EditableFeature;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.dal.feature.FeatureSelection;
31
import org.gvsig.fmap.dal.feature.FeatureSet;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.geoprocess.ProgressModel;
34
import org.gvsig.geoprocess.core.IProgressModel;
35
import org.gvsig.tools.dispose.DisposableIterator;
36

    
37
/**
38
 * Each geometry operation have to inherit from this class.
39
 *  
40
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
41
 */
42
public abstract class GeometryOperation {
43
        protected DALFeaturePersister              persister          = null;
44
        protected FeatureStore                     inFeatureStore     = null;
45
        protected ArrayList<FeatureStore>          inFeatureStoreList = null;
46
        protected boolean                          selectedGeom       = false;
47
        protected int                              numberOfFeatures   = 0;
48
        protected EditableFeature                  lastEditFeature    = null;
49
    protected ProgressModel progressModel = null;
50
        
51
        /**
52
         * Invokes this operation and returns an EditableFeature
53
         * @param g
54
         * @param featureInput
55
         * @return
56
         */
57
        public abstract EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput);
58
        
59
        /**
60
         * Invokes this operation
61
         * @param g
62
         * @param featureInput
63
         */
64
        public abstract void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput);
65
        
66
        /**
67
         * Sets the output FeatureType and the input attributes for this feature
68
         * @param out
69
         * @throws DataException 
70
         */
71
        public void setFeatureStore(FeatureStore out, String[] attrNames) throws DataException {
72
                persister = new DALFeaturePersister(out, attrNames);
73
        }
74
        
75
        /**
76
         * Gets the last result of this operation
77
         * @return
78
         */
79
        public Object getResult() {
80
                return lastEditFeature;
81
        }
82
        
83
        /**
84
         * Computes a complete operation over the input FeatureStore. The result of this operation
85
         * is stored in the output FeatureStore. This method will call once for each geometry.
86
         * @param inFeatStore
87
         *        Input FeatureStore
88
         * @param outFeatStore
89
         *        Output FeatureStore
90
         * @param attrNames
91
         *        List of attributes to build the output feature store
92
         * @param selectedGeom
93
         *        If it is true only the selected geometries will be processed
94
         * @throws DataException
95
         */
96
        @SuppressWarnings("unchecked")
97
        public void computesGeometryOperation(FeatureStore inFeatStore,
98
                                                                        FeatureStore outFeatStore,
99
                                                                        String[] attrNames,
100
                                                                        boolean selectedGeom,
101
                                                                        boolean closeOutStore) throws DataException {
102
                this.inFeatureStore = inFeatStore;
103
                this.selectedGeom = selectedGeom;
104
                FeatureSet featuresSet = null;
105
                featuresSet = inFeatStore.getFeatureSet();
106
                
107
                if(outFeatStore != null)
108
                        setFeatureStore(outFeatStore, attrNames);
109
                DisposableIterator it = null;
110

    
111
                if(selectedGeom) {
112
                        DataSet ds = inFeatStore.getSelection();
113
            it = ((FeatureSelection) ds).iterator();
114
                } else
115
                        it = featuresSet.iterator();
116
                numberOfFeatures = (int)featuresSet.getSize();
117
                if(progressModel != null)
118
                        progressModel.setTotalNumberOfSteps(numberOfFeatures);
119
                
120
                int iCount = 0;
121
                while( it.hasNext() ) {
122
                        Feature feature = (Feature)it.next();
123
                        List geomList = feature.getGeometries();
124
                        
125
                        if(progressModel != null)
126
                                progressModel.setProgress(iCount);
127
                        iCount ++;
128
                        
129
                        if(geomList == null) {
130
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
131
                                invoke(geom, feature);
132
                                continue;
133
                        }
134

    
135
                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
136
                        boolean first = true;
137
                        EditableFeature editFeat = null;
138
                        while(itGeom.hasNext()) {
139
                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
140
                                if(first) {
141
                                        editFeat = invoke(g, feature);
142
                                        first = false;
143
                                } else
144
                                        invoke(g, editFeat);
145
                        }
146
                }
147
                
148
                if(closeOutStore && persister != null)
149
                        persister.end();
150
        }
151
        
152
        /**
153
         * Computes a complete operation over the input FeatureStore. The result of this operation
154
         * is stored in the output FeatureStore. This method will call once for each feature.
155
         * @param inFeatStore
156
         *        Input FeatureStore
157
         * @param outFeatStore
158
         *        Output FeatureStore
159
         * @param attrNames
160
         *        List of attributes to build the output feature store
161
         * @param selectedGeom
162
         *        If it is true only the selected geometries will be processed
163
         * @throws DataException
164
         */
165
        public void computesFeatureOperation(FeatureStore inFeatStore,
166
                                                                        FeatureStore outFeatStore,
167
                                                                        String[] attrNames,
168
                                                                        boolean selectedGeom,
169
                                                                        boolean closeOutStore) throws DataException {
170
                this.inFeatureStore = inFeatStore;
171
                this.selectedGeom = selectedGeom;
172
                FeatureSet featuresSet = null;
173
                featuresSet = inFeatStore.getFeatureSet();
174
                
175
                if(outFeatStore != null)
176
                        setFeatureStore(outFeatStore, attrNames);
177
                DisposableIterator it = null;
178

    
179
                if(selectedGeom) {
180
                        DataSet ds = inFeatStore.getSelection();
181
            it = ((FeatureSelection) ds).iterator();
182
                } else
183
                        it = featuresSet.iterator();
184
                numberOfFeatures = (int)featuresSet.getSize();
185

    
186
                if(progressModel != null)
187
                        progressModel.setTotalNumberOfSteps(numberOfFeatures);
188
                
189
                int iCount = 0;
190
                while( it.hasNext() ) {
191
                        Feature feature = (Feature)it.next();
192
                        invoke(null, feature);
193
                        if(progressModel != null)
194
                                progressModel.setProgress(iCount);
195
                        iCount ++;
196
                }
197
                
198
                if(closeOutStore && persister != null)
199
                        persister.end();
200
        }
201
        
202
        /**
203
         * Computes a complete operation over the input list of FeatureStore. The result of this operation
204
         * is stored in the output FeatureStore. This method will call once for each geometry.
205
         * @param inFeatStoreList
206
         *        Input FeatureStore list
207
         * @param outFeatStore
208
         *        Output FeatureStore
209
         * @param attrNames
210
         *        List of attributes to build the output feature store
211
         * @param selectedGeom
212
         *        If it is true only the selected geometries will be processed
213
         * @throws DataException
214
         */
215
        public void computesGeometryOperationInAList(ArrayList<FeatureStore> inFeatStoreList,
216
                                                                        FeatureStore outFeatStore,
217
                                                                        String[] attrNames,
218
                                                                        boolean selectedGeom,
219
                                                                        boolean closeOutStore) throws DataException {
220
                this.inFeatureStoreList = inFeatStoreList;
221
        ProgressModel pModel = progressModel;
222
                
223
                if(progressModel != null) {
224
                        progressModel.setTotalNumberOfSteps(inFeatStoreList.size());
225
                        pModel = progressModel;
226
                        progressModel = null;  //Esto es para que computesGeometryOperation no actualice la barra
227
                }
228
                
229
                int iCount = 0;
230
                for (int i = 0; i < inFeatStoreList.size(); i++) {
231
                        if(pModel != null)
232
                                pModel.setProgress(iCount);
233
                        iCount ++;
234
                        computesGeometryOperation(inFeatStoreList.get(i), 
235
                                                                                i == 0 ? outFeatStore : null, 
236
                                                                                attrNames, 
237
                                                                                selectedGeom, 
238
                                                                                false);
239
                }
240
                
241
                this.progressModel = pModel;
242
                
243
                if(closeOutStore && persister != null)
244
                        persister.end();
245
        }
246
        
247
        /**
248
         * Ends the edition and closes the FeatureStore
249
         * @deprecated
250
         */
251
        public void end() {
252
                persister.end();
253
        }
254

    
255
        public void setProgressModel(IProgressModel pModel) {
256
                this.progressModel = pModel;
257
        }
258
        
259
    public void setProgressModel(ProgressModel pModel) {
260
        this.progressModel = pModel;
261
    }
262
}