Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / selectionByTheme / MySelectionByThemeListener.java @ 33205

History | View | Annotate | Download (11.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.app.gui.selectionByTheme;
42

    
43
import org.cresques.cts.IProjection;
44
import org.gvsig.andami.messages.NotificationManager;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.exception.ReadException;
47
import org.gvsig.tools.dispose.DisposableIterator;
48
import org.gvsig.fmap.dal.feature.Feature;
49
import org.gvsig.fmap.dal.feature.FeatureQuery;
50
import org.gvsig.fmap.dal.feature.FeatureSelection;
51
import org.gvsig.fmap.dal.feature.FeatureSet;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.dal.feature.FeatureType;
54
import org.gvsig.fmap.geom.Geometry;
55
import org.gvsig.fmap.mapcontext.layers.FLayer;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.ContainsGeometryEvaluator;
57
import org.gvsig.fmap.mapcontext.layers.vectorial.CrossesGeometryEvaluator;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.DisjointGeometryEvaluator;
59
import org.gvsig.fmap.mapcontext.layers.vectorial.EqualsGeometryEvaluator;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
61
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator;
62
import org.gvsig.fmap.mapcontext.layers.vectorial.OverlapsGeometryEvaluator;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.TouchesGeometryEvaluator;
64
import org.gvsig.fmap.mapcontext.layers.vectorial.WithinGeometryEvaluator;
65
import org.gvsig.tools.dispose.DisposeUtils;
66
import org.gvsig.tools.evaluator.Evaluator;
67
import org.gvsig.tools.evaluator.EvaluatorData;
68
import org.gvsig.tools.evaluator.EvaluatorException;
69
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
70

    
71

    
72
/**
73
 * DOCUMENT ME!
74
 *
75
 * @author Fernando Gonz?lez Cort?s
76
 */
77
//TODO comentado para que compile
78
public class MySelectionByThemeListener implements SelectionByThemeListener {
79
        /**
80
         * @see org.gvsig.app.gui.selectionByTheme.SelectionByThemeListener#newSet(int,
81
         *      int, int)
82
         */
83
        public void newSet(FLayer[] toSelect, FLayer selectionLayer, int action) {
84
                long t1 = System.currentTimeMillis();
85

    
86
                if (selectionLayer instanceof FLyrVect) {
87
                        try {
88
                                FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection();
89
                                SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
90

    
91
                                for (int i = 0; i < toSelect.length; i++) {
92
                                        if (toSelect[i] instanceof FLyrVect) {
93
//                                                if (!selectionLayer.getFullEnvelope().intersects(toSelect[i].getFullEnvelope())){
94
//                                                        continue;
95
//                                                }
96
                                                FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore();
97
                                                FeatureQuery fQuery = storeToQuery
98
                                                                .createFeatureQuery();
99
                                                filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),null);
100

    
101
                                                fQuery.setFilter(filter);
102
                                                FeatureSet featureSet = null;
103
                                                try {
104
                                                        featureSet = storeToQuery.getFeatureSet(fQuery);
105
                                                        ((FeatureSelection) storeToQuery.getSelection())
106
                                                                        .deselectAll();
107
                                                        storeToQuery.setSelection(featureSet);
108
                                                } finally {
109
                                                        if (featureSet != null) {
110
                                                                featureSet.dispose();
111
                                                        }
112
                                                }
113
                                        }
114
                                }
115
                        } catch (ReadException e) {
116
                                NotificationManager.addError("error_selection_by_theme", e);
117
                        } catch (DataException e) {
118
                                NotificationManager.addError("error_selection_by_theme", e);
119
                        }
120

    
121
                }
122
                long t2 = System.currentTimeMillis();
123
                System.out
124
                                .println("Tiempo de consulta: " + (t2 - t1) + " milisegundos");
125
                // doSelection(toSelect, selectionLayer, action, false);
126
        }
127

    
128
        /**
129
         * @see org.gvsig.app.gui.selectionByTheme.SelectionByThemeListener#addToSet(int,
130
         *      int, int)
131
         */
132
        public void addToSet(FLayer[] toSelect, FLayer selectionLayer, int action) {
133
                long t1 = System.currentTimeMillis();
134

    
135
                if (selectionLayer instanceof FLyrVect) {
136
                        try {
137
                                FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection();
138
                                SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
139

    
140
                                for (int i = 0; i < toSelect.length; i++) {
141
                                        if (toSelect[i] instanceof FLyrVect) {
142
                                                if (!selectionLayer.getFullEnvelope().intersects(toSelect[i].getFullEnvelope())){
143
                                                        continue;
144
                                                }
145
                                                FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore();
146
                                                FeatureQuery fQuery = storeToQuery
147
                                                                .createFeatureQuery();
148
                                                filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),null);
149

    
150
                                                fQuery.setFilter(filter);
151
                                                FeatureSet featureSet = null;
152
                                                try {
153
                                                        featureSet = storeToQuery.getFeatureSet(fQuery);
154
                                                        storeToQuery.setSelection(featureSet);
155
                                                } finally {
156
                                                        if (featureSet != null) {
157
                                                                featureSet.dispose();
158
                                                        }
159
                                                }
160
                                        }
161
                                }
162
                        } catch (ReadException e) {
163
                                NotificationManager.addError("error_selection_by_theme", e);
164
                        } catch (DataException e) {
165
                                NotificationManager.addError("error_selection_by_theme", e);
166
                        }
167

    
168
                }
169
                long t2 = System.currentTimeMillis();
170
                System.out
171
                                .println("Tiempo de consulta: " + (t2 - t1) + " milisegundos");
172
        }
173

    
174
        /**
175
         * @see org.gvsig.app.gui.selectionByTheme.SelectionByThemeListener#fromSet(int,
176
         *      int, int)
177
         */
178
        public void fromSet(FLayer[] toSelect, FLayer selectionLayer, int action) {
179
                long t1 = System.currentTimeMillis();
180
                if (selectionLayer instanceof FLyrVect) {
181
                        try {
182
                                FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection();
183
                                SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
184

    
185
                                for (int i = 0; i < toSelect.length; i++) {
186
                                        if (toSelect[i] instanceof FLyrVect) {
187
                                                if (!selectionLayer.getFullEnvelope().intersects(toSelect[i].getFullEnvelope())){
188
                                                        continue;
189
                                                }
190
                                                FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore();
191
                                                FeatureQuery fQuery = storeToQuery
192
                                                                .createFeatureQuery();
193
                                                filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),(FeatureSelection)storeToQuery.getSelection());
194

    
195
                                                fQuery.setFilter(filter);
196
                                                FeatureSet featureSet = null;
197
                                                try {
198
                                                        featureSet = storeToQuery.getFeatureSet(fQuery);
199
                                                        ((FeatureSelection) storeToQuery.getSelection())
200
                                                                        .deselectAll();
201
                                                        storeToQuery.setSelection(featureSet);
202
                                                } finally {
203
                                                        if (featureSet != null) {
204
                                                                featureSet.dispose();
205
                                                        }
206

    
207
                                                }
208
                                        }
209
                                }
210
                        } catch (ReadException e) {
211
                                NotificationManager.addError("error_selection_by_theme", e);
212
                        } catch (DataException e) {
213
                                NotificationManager.addError("error_selection_by_theme", e);
214
                        }
215

    
216
                }
217
                long t2 = System.currentTimeMillis();
218
                System.out
219
                                .println("Tiempo de consulta: " + (t2 - t1) + " milisegundos");
220
        }
221
        class SelectByTheme implements Evaluator{
222
                private FeatureSelection selection=null;
223
                private IProjection projection;
224
                private FeatureType featureType;
225
                private String geomName;
226
                private EvaluatorFieldsInfo info;
227
                private String nameSelectedGeom;
228
                private FeatureSelection featureSelection;
229
                private int action;
230
                private int index;
231

    
232
                public SelectByTheme(FeatureSelection selection, IProjection projection,String nameSelectedGeom, int action){
233
                        this.selection=selection;
234

    
235
                        this.projection=projection;
236
                        this.nameSelectedGeom=nameSelectedGeom;
237
                        this.action=action;
238
                        this.index=selection.getDefaultFeatureType().getIndex(nameSelectedGeom);
239
                }
240
                public void setData(FeatureType featureType, String geomName, FeatureSelection featureSelection){
241
                        this.featureType=featureType;
242
                        this.geomName=geomName;
243
                        this.featureSelection=featureSelection;
244
                        this.info=new EvaluatorFieldsInfo();
245
                        this.info.addFieldValue(geomName);
246
                }
247

    
248
                public Object evaluate(EvaluatorData data) throws EvaluatorException {
249
                        if (featureSelection!=null && !featureSelection.isSelected(((Feature)data.getContextValue("feature")))){
250
                                return new Boolean(false);
251
                        }
252
                        DisposableIterator features = null;
253
                        try {
254
                                features = selection.iterator();
255

    
256
                                while (features.hasNext()) {
257
                                        Feature feature = (Feature) features.next();
258
                                        Geometry geometry = feature.getGeometry(index);
259
                                        Evaluator eval = null;
260
                                        switch (action) {
261
                                        case SelectionByTheme.INTERSECTS:
262
                                                eval = new IntersectsGeometryEvaluator(geometry,
263
                                                                projection, featureType, geomName);
264
                                                break;
265
                                        case SelectionByTheme.CONTAINS:
266
                                                eval = new ContainsGeometryEvaluator(geometry,
267
                                                                projection, featureType, geomName);
268
                                                break;
269
                                        case SelectionByTheme.CROSSES:
270
                                                eval = new CrossesGeometryEvaluator(geometry,
271
                                                                projection, featureType, geomName);
272
                                                break;
273
                                        case SelectionByTheme.DISJOINT:
274
                                                eval = new DisjointGeometryEvaluator(geometry,
275
                                                                projection, featureType, geomName);
276
                                                break;
277
                                        case SelectionByTheme.EQUALS:
278
                                                eval = new EqualsGeometryEvaluator(geometry,
279
                                                                projection, featureType, geomName);
280
                                                break;
281
                                        case SelectionByTheme.OVERLAPS:
282
                                                eval = new OverlapsGeometryEvaluator(geometry,
283
                                                                projection, featureType, geomName);
284
                                                break;
285
                                        case SelectionByTheme.TOUCHES:
286
                                                eval = new TouchesGeometryEvaluator(geometry,
287
                                                                projection, featureType, geomName);
288
                                                break;
289
                                        case SelectionByTheme.WITHIN:
290
                                                eval = new WithinGeometryEvaluator(geometry,
291
                                                                projection, featureType, geomName);
292
                                                break;
293

    
294
                                        default:
295
                                                eval = new IntersectsGeometryEvaluator(geometry,
296
                                                                projection, featureType, geomName);
297
                                        break;
298
                                        }
299

    
300
                                        Boolean evaluate = (Boolean) eval.evaluate(data);
301
                                        if ((evaluate).booleanValue()) {
302
                                                return evaluate;
303
                                        }
304
                                }
305
                        } catch (DataException e) {
306
                                NotificationManager.addError("error_selection_by_theme", e);
307
                        } finally {
308
                                DisposeUtils.dispose(features);
309
                        }
310
                        return new Boolean(false);
311
                }
312

    
313
                public String getName() {
314
                        return "select by theme";
315
                }
316

    
317
                public String getSQL() {
318
                        // TODO Auto-generated method stub
319
                        return null;
320
                }
321
                public String getDescription() {
322
                        return getName();
323
                }
324
                public EvaluatorFieldsInfo getFieldsInfo() {
325
                        return info;
326
                }
327
        }
328
}