root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / selectionByTheme / MySelectionByThemeListener.java @ 33053
History | View | Annotate | Download (11.4 KB)
1 | 1103 | fjp | /* 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 | 29596 | jpiera | package org.gvsig.app.gui.selectionByTheme; |
42 | 934 | fernando | |
43 | 26435 | vcaballero | import org.cresques.cts.IProjection; |
44 | 29596 | jpiera | import org.gvsig.andami.messages.NotificationManager; |
45 | 26435 | vcaballero | import org.gvsig.fmap.dal.exception.DataException; |
46 | import org.gvsig.fmap.dal.exception.ReadException; |
||
47 | 27525 | jmvivo | import org.gvsig.fmap.dal.feature.DisposableIterator; |
48 | 26435 | vcaballero | 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 | 20994 | jmvivo | import org.gvsig.fmap.mapcontext.layers.FLayer; |
56 | 26435 | vcaballero | 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 | 20994 | jmvivo | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
61 | 26435 | vcaballero | 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 | 31284 | cordinyana | import org.gvsig.tools.dispose.DisposeUtils; |
66 | 26435 | vcaballero | 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 | 20994 | jmvivo | |
71 | 26435 | vcaballero | |
72 | 934 | fernando | /**
|
73 | * DOCUMENT ME!
|
||
74 | 10626 | caballero | *
|
75 | 934 | fernando | * @author Fernando Gonz?lez Cort?s
|
76 | */
|
||
77 | 23383 | vcaballero | //TODO comentado para que compile
|
78 | 934 | fernando | public class MySelectionByThemeListener implements SelectionByThemeListener { |
79 | 5669 | fjp | /**
|
80 | 29596 | jpiera | * @see org.gvsig.app.gui.selectionByTheme.SelectionByThemeListener#newSet(int,
|
81 | 5669 | fjp | * int, int)
|
82 | */
|
||
83 | public void newSet(FLayer[] toSelect, FLayer selectionLayer, int action) { |
||
84 | long t1 = System.currentTimeMillis(); |
||
85 | 26435 | vcaballero | |
86 | 5669 | fjp | if (selectionLayer instanceof FLyrVect) { |
87 | 26435 | vcaballero | try {
|
88 | FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection(); |
||
89 | SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
|
||
90 | 5669 | fjp | |
91 | 26435 | vcaballero | 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 | 27634 | jmvivo | FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore(); |
97 | FeatureQuery fQuery = storeToQuery |
||
98 | .createFeatureQuery(); |
||
99 | filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),null);
|
||
100 | 26435 | vcaballero | |
101 | fQuery.setFilter(filter); |
||
102 | 27634 | jmvivo | 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 | 26435 | vcaballero | } |
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 | 5669 | fjp | } |
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 | 29596 | jpiera | * @see org.gvsig.app.gui.selectionByTheme.SelectionByThemeListener#addToSet(int,
|
130 | 5669 | fjp | * int, int)
|
131 | */
|
||
132 | public void addToSet(FLayer[] toSelect, FLayer selectionLayer, int action) { |
||
133 | 26435 | vcaballero | long t1 = System.currentTimeMillis(); |
134 | |||
135 | 5669 | fjp | if (selectionLayer instanceof FLyrVect) { |
136 | 26435 | vcaballero | 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 | 27634 | jmvivo | FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore(); |
146 | FeatureQuery fQuery = storeToQuery |
||
147 | .createFeatureQuery(); |
||
148 | filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),null);
|
||
149 | 26435 | vcaballero | |
150 | fQuery.setFilter(filter); |
||
151 | 27634 | jmvivo | 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 | 26435 | vcaballero | } |
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 | 5669 | fjp | } |
169 | 26435 | vcaballero | long t2 = System.currentTimeMillis(); |
170 | System.out
|
||
171 | .println("Tiempo de consulta: " + (t2 - t1) + " milisegundos"); |
||
172 | 5669 | fjp | } |
173 | 934 | fernando | |
174 | 5669 | fjp | /**
|
175 | 29596 | jpiera | * @see org.gvsig.app.gui.selectionByTheme.SelectionByThemeListener#fromSet(int,
|
176 | 5669 | fjp | * int, int)
|
177 | */
|
||
178 | public void fromSet(FLayer[] toSelect, FLayer selectionLayer, int action) { |
||
179 | 26435 | vcaballero | long t1 = System.currentTimeMillis(); |
180 | 5669 | fjp | if (selectionLayer instanceof FLyrVect) { |
181 | 26435 | vcaballero | try {
|
182 | FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection(); |
||
183 | SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
|
||
184 | 5669 | fjp | |
185 | 26435 | vcaballero | 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 | 27634 | jmvivo | FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore(); |
191 | FeatureQuery fQuery = storeToQuery |
||
192 | .createFeatureQuery(); |
||
193 | filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),(FeatureSelection)storeToQuery.getSelection()); |
||
194 | 26435 | vcaballero | |
195 | fQuery.setFilter(filter); |
||
196 | 27634 | jmvivo | 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 | 26435 | vcaballero | } |
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 | 5669 | fjp | } |
217 | 26435 | vcaballero | long t2 = System.currentTimeMillis(); |
218 | System.out
|
||
219 | .println("Tiempo de consulta: " + (t2 - t1) + " milisegundos"); |
||
220 | 5669 | fjp | } |
221 | 26435 | vcaballero | 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 | 27525 | jmvivo | DisposableIterator features = null;
|
253 | 26435 | vcaballero | try {
|
254 | features = selection.iterator(); |
||
255 | |||
256 | 27525 | jmvivo | 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 | 26435 | vcaballero | |
294 | 27525 | jmvivo | default:
|
295 | eval = new IntersectsGeometryEvaluator(geometry,
|
||
296 | projection, featureType, geomName); |
||
297 | 26435 | vcaballero | break;
|
298 | 27525 | jmvivo | } |
299 | 26435 | vcaballero | |
300 | 27525 | jmvivo | Boolean evaluate = (Boolean) eval.evaluate(data); |
301 | if ((evaluate).booleanValue()) {
|
||
302 | return evaluate;
|
||
303 | } |
||
304 | 26435 | vcaballero | } |
305 | 27525 | jmvivo | } catch (DataException e) {
|
306 | NotificationManager.addError("error_selection_by_theme", e);
|
||
307 | } finally {
|
||
308 | 31284 | cordinyana | DisposeUtils.dispose(features); |
309 | 26435 | vcaballero | } |
310 | return new Boolean(false); |
||
311 | } |
||
312 | |||
313 | public String getName() { |
||
314 | return "select by theme"; |
||
315 | } |
||
316 | 33053 | cordinyana | |
317 | public String getSQL() { |
||
318 | 26435 | vcaballero | // 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 | 934 | fernando | } |