Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.base / src / main / java / org / gvsig / sextante / app / algorithm / base / core / GeometryOperation.java @ 41

History | View | Annotate | Download (6.89 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.sextante.app.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.DisposableIterator;
29
import org.gvsig.fmap.dal.feature.EditableFeature;
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureSet;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection;
34

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

    
103
                if(selectedGeom) {
104
                        DataSet ds = inFeatStore.getSelection();
105
                        it = ((DefaultFeatureSelection)ds).iterator();
106
                } else
107
                        it = featuresSet.iterator();
108
                numberOfFeatures = (int)featuresSet.getSize();
109
                
110
                while( it.hasNext() ) {
111
                        Feature feature = (Feature)it.next();
112
                        List geomList = feature.getGeometries();
113
                        if(geomList == null) {
114
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
115
                                invoke(geom, feature);
116
                                continue;
117
                        }
118

    
119
                        Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
120
                        boolean first = true;
121
                        EditableFeature editFeat = null;
122
                        while(itGeom.hasNext()) {
123
                                org.gvsig.fmap.geom.Geometry g = itGeom.next();
124
                                if(first) {
125
                                        editFeat = invoke(g, feature);
126
                                        first = false;
127
                                } else
128
                                        invoke(g, editFeat);
129
                        }
130
                }
131
                
132
                if(closeOutStore && persister != null)
133
                        persister.end();
134
        }
135
        
136
        /**
137
         * Computes a complete operation over the input FeatureStore. The result of this operation
138
         * is stored in the output FeatureStore. This method will call once for each feature.
139
         * @param inFeatStore
140
         *        Input FeatureStore
141
         * @param outFeatStore
142
         *        Output FeatureStore
143
         * @param attrNames
144
         *        List of attributes to build the output feature store
145
         * @param selectedGeom
146
         *        If it is true only the selected geometries will be processed
147
         * @throws DataException
148
         */
149
        public void computesFeatureOperation(FeatureStore inFeatStore,
150
                                                                        FeatureStore outFeatStore,
151
                                                                        String[] attrNames,
152
                                                                        boolean selectedGeom,
153
                                                                        boolean closeOutStore) throws DataException {
154
                this.inFeatureStore = inFeatStore;
155
                this.selectedGeom = selectedGeom;
156
                FeatureSet featuresSet = null;
157
                featuresSet = inFeatStore.getFeatureSet();
158
                
159
                if(outFeatStore != null)
160
                        setFeatureStore(outFeatStore, attrNames);
161
                DisposableIterator it = null;
162

    
163
                if(selectedGeom) {
164
                        DataSet ds = inFeatStore.getSelection();
165
                        it = ((DefaultFeatureSelection)ds).iterator();
166
                } else
167
                        it = featuresSet.iterator();
168
                numberOfFeatures = (int)featuresSet.getSize();
169
                
170
                while( it.hasNext() ) {
171
                        Feature feature = (Feature)it.next();
172
                        invoke(null, feature);
173
                }
174
                
175
                if(closeOutStore && persister != null)
176
                        persister.end();
177
        }
178
        
179
        /**
180
         * Computes a complete operation over the input list of FeatureStore. The result of this operation
181
         * is stored in the output FeatureStore. This method will call once for each geometry.
182
         * @param inFeatStoreList
183
         *        Input FeatureStore list
184
         * @param outFeatStore
185
         *        Output FeatureStore
186
         * @param attrNames
187
         *        List of attributes to build the output feature store
188
         * @param selectedGeom
189
         *        If it is true only the selected geometries will be processed
190
         * @throws DataException
191
         */
192
        public void computesGeometryOperationInAList(ArrayList<FeatureStore> inFeatStoreList,
193
                                                                        FeatureStore outFeatStore,
194
                                                                        String[] attrNames,
195
                                                                        boolean selectedGeom,
196
                                                                        boolean closeOutStore) throws DataException {
197
                this.inFeatureStoreList = inFeatStoreList;
198
                for (int i = 0; i < inFeatStoreList.size(); i++) {
199
                        computesGeometryOperation(inFeatStoreList.get(i), 
200
                                                                                i == 0 ? outFeatStore : null, 
201
                                                                                attrNames, 
202
                                                                                selectedGeom, 
203
                                                                                false);
204
                }
205
                if(closeOutStore && persister != null)
206
                        persister.end();
207
        }
208
        
209
        /**
210
         * Ends the edition and closes the FeatureStore
211
         * @deprecated
212
         */
213
        public void end() {
214
                persister.end();
215
        }
216
        
217
}