root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / gui / selectionByTheme / MySelectionByThemeListener.java @ 27634
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 com.iver.cit.gvsig.gui.selectionByTheme; |
42 |
|
43 |
import org.cresques.cts.IProjection; |
44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.exception.ReadException; |
46 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
47 |
import org.gvsig.fmap.dal.feature.Feature; |
48 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
49 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
50 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
51 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
52 |
import org.gvsig.fmap.dal.feature.FeatureType; |
53 |
import org.gvsig.fmap.geom.Geometry; |
54 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
55 |
import org.gvsig.fmap.mapcontext.layers.vectorial.ContainsGeometryEvaluator; |
56 |
import org.gvsig.fmap.mapcontext.layers.vectorial.CrossesGeometryEvaluator; |
57 |
import org.gvsig.fmap.mapcontext.layers.vectorial.DisjointGeometryEvaluator; |
58 |
import org.gvsig.fmap.mapcontext.layers.vectorial.EqualsGeometryEvaluator; |
59 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
60 |
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator; |
61 |
import org.gvsig.fmap.mapcontext.layers.vectorial.OverlapsGeometryEvaluator; |
62 |
import org.gvsig.fmap.mapcontext.layers.vectorial.TouchesGeometryEvaluator; |
63 |
import org.gvsig.fmap.mapcontext.layers.vectorial.WithinGeometryEvaluator; |
64 |
import org.gvsig.tools.evaluator.Evaluator; |
65 |
import org.gvsig.tools.evaluator.EvaluatorData; |
66 |
import org.gvsig.tools.evaluator.EvaluatorException; |
67 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
68 |
|
69 |
import com.iver.andami.messages.NotificationManager; |
70 |
|
71 |
/**
|
72 |
* DOCUMENT ME!
|
73 |
*
|
74 |
* @author Fernando Gonz?lez Cort?s
|
75 |
*/
|
76 |
//TODO comentado para que compile
|
77 |
public class MySelectionByThemeListener implements SelectionByThemeListener { |
78 |
/**
|
79 |
* @see com.iver.cit.gvsig.gui.selectionByTheme.SelectionByThemeListener#newSet(int,
|
80 |
* int, int)
|
81 |
*/
|
82 |
public void newSet(FLayer[] toSelect, FLayer selectionLayer, int action) { |
83 |
long t1 = System.currentTimeMillis(); |
84 |
|
85 |
if (selectionLayer instanceof FLyrVect) { |
86 |
try {
|
87 |
FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection(); |
88 |
SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
|
89 |
|
90 |
for (int i = 0; i < toSelect.length; i++) { |
91 |
if (toSelect[i] instanceof FLyrVect) { |
92 |
// if (!selectionLayer.getFullEnvelope().intersects(toSelect[i].getFullEnvelope())){
|
93 |
// continue;
|
94 |
// }
|
95 |
FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore(); |
96 |
FeatureQuery fQuery = storeToQuery |
97 |
.createFeatureQuery(); |
98 |
filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),null);
|
99 |
|
100 |
fQuery.setFilter(filter); |
101 |
FeatureSet featureSet = null;
|
102 |
try {
|
103 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
104 |
((FeatureSelection) storeToQuery.getSelection()) |
105 |
.deselectAll(); |
106 |
storeToQuery.setSelection(featureSet); |
107 |
} finally {
|
108 |
if (featureSet != null) { |
109 |
featureSet.dispose(); |
110 |
} |
111 |
} |
112 |
} |
113 |
} |
114 |
} catch (ReadException e) {
|
115 |
NotificationManager.addError("error_selection_by_theme", e);
|
116 |
} catch (DataException e) {
|
117 |
NotificationManager.addError("error_selection_by_theme", e);
|
118 |
} |
119 |
|
120 |
} |
121 |
long t2 = System.currentTimeMillis(); |
122 |
System.out
|
123 |
.println("Tiempo de consulta: " + (t2 - t1) + " milisegundos"); |
124 |
// doSelection(toSelect, selectionLayer, action, false);
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* @see com.iver.cit.gvsig.gui.selectionByTheme.SelectionByThemeListener#addToSet(int,
|
129 |
* int, int)
|
130 |
*/
|
131 |
public void addToSet(FLayer[] toSelect, FLayer selectionLayer, int action) { |
132 |
long t1 = System.currentTimeMillis(); |
133 |
|
134 |
if (selectionLayer instanceof FLyrVect) { |
135 |
try {
|
136 |
FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection(); |
137 |
SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
|
138 |
|
139 |
for (int i = 0; i < toSelect.length; i++) { |
140 |
if (toSelect[i] instanceof FLyrVect) { |
141 |
if (!selectionLayer.getFullEnvelope().intersects(toSelect[i].getFullEnvelope())){
|
142 |
continue;
|
143 |
} |
144 |
FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore(); |
145 |
FeatureQuery fQuery = storeToQuery |
146 |
.createFeatureQuery(); |
147 |
filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),null);
|
148 |
|
149 |
fQuery.setFilter(filter); |
150 |
FeatureSet featureSet = null;
|
151 |
try {
|
152 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
153 |
storeToQuery.setSelection(featureSet); |
154 |
} finally {
|
155 |
if (featureSet != null) { |
156 |
featureSet.dispose(); |
157 |
} |
158 |
} |
159 |
} |
160 |
} |
161 |
} catch (ReadException e) {
|
162 |
NotificationManager.addError("error_selection_by_theme", e);
|
163 |
} catch (DataException e) {
|
164 |
NotificationManager.addError("error_selection_by_theme", e);
|
165 |
} |
166 |
|
167 |
} |
168 |
long t2 = System.currentTimeMillis(); |
169 |
System.out
|
170 |
.println("Tiempo de consulta: " + (t2 - t1) + " milisegundos"); |
171 |
} |
172 |
|
173 |
/**
|
174 |
* @see com.iver.cit.gvsig.gui.selectionByTheme.SelectionByThemeListener#fromSet(int,
|
175 |
* int, int)
|
176 |
*/
|
177 |
public void fromSet(FLayer[] toSelect, FLayer selectionLayer, int action) { |
178 |
long t1 = System.currentTimeMillis(); |
179 |
if (selectionLayer instanceof FLyrVect) { |
180 |
try {
|
181 |
FeatureSelection selection=(FeatureSelection)((FLyrVect)selectionLayer).getFeatureStore().getSelection(); |
182 |
SelectByTheme filter=new SelectByTheme(selection,selectionLayer.getMapContext().getProjection(),selection.getDefaultFeatureType().getDefaultGeometryAttributeName(), action);
|
183 |
|
184 |
for (int i = 0; i < toSelect.length; i++) { |
185 |
if (toSelect[i] instanceof FLyrVect) { |
186 |
if (!selectionLayer.getFullEnvelope().intersects(toSelect[i].getFullEnvelope())){
|
187 |
continue;
|
188 |
} |
189 |
FeatureStore storeToQuery = ((FLyrVect) toSelect[i]).getFeatureStore(); |
190 |
FeatureQuery fQuery = storeToQuery |
191 |
.createFeatureQuery(); |
192 |
filter.setData(storeToQuery.getDefaultFeatureType(),storeToQuery.getDefaultFeatureType().getDefaultGeometryAttributeName(),(FeatureSelection)storeToQuery.getSelection()); |
193 |
|
194 |
fQuery.setFilter(filter); |
195 |
FeatureSet featureSet = null;
|
196 |
try {
|
197 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
198 |
((FeatureSelection) storeToQuery.getSelection()) |
199 |
.deselectAll(); |
200 |
storeToQuery.setSelection(featureSet); |
201 |
} finally {
|
202 |
if (featureSet != null) { |
203 |
featureSet.dispose(); |
204 |
} |
205 |
|
206 |
} |
207 |
} |
208 |
} |
209 |
} catch (ReadException e) {
|
210 |
NotificationManager.addError("error_selection_by_theme", e);
|
211 |
} catch (DataException e) {
|
212 |
NotificationManager.addError("error_selection_by_theme", e);
|
213 |
} |
214 |
|
215 |
} |
216 |
long t2 = System.currentTimeMillis(); |
217 |
System.out
|
218 |
.println("Tiempo de consulta: " + (t2 - t1) + " milisegundos"); |
219 |
} |
220 |
class SelectByTheme implements Evaluator{ |
221 |
private FeatureSelection selection=null; |
222 |
private IProjection projection;
|
223 |
private FeatureType featureType;
|
224 |
private String geomName; |
225 |
private EvaluatorFieldsInfo info;
|
226 |
private String nameSelectedGeom; |
227 |
private FeatureSelection featureSelection;
|
228 |
private int action; |
229 |
private int index; |
230 |
|
231 |
public SelectByTheme(FeatureSelection selection, IProjection projection,String nameSelectedGeom, int action){ |
232 |
this.selection=selection;
|
233 |
|
234 |
this.projection=projection;
|
235 |
this.nameSelectedGeom=nameSelectedGeom;
|
236 |
this.action=action;
|
237 |
this.index=selection.getDefaultFeatureType().getIndex(nameSelectedGeom);
|
238 |
} |
239 |
public void setData(FeatureType featureType, String geomName, FeatureSelection featureSelection){ |
240 |
this.featureType=featureType;
|
241 |
this.geomName=geomName;
|
242 |
this.featureSelection=featureSelection;
|
243 |
this.info=new EvaluatorFieldsInfo(); |
244 |
this.info.addFieldValue(geomName);
|
245 |
} |
246 |
|
247 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
248 |
if (featureSelection!=null && !featureSelection.isSelected(((Feature)data.getContextValue("feature")))){ |
249 |
return new Boolean(false); |
250 |
} |
251 |
DisposableIterator features = null;
|
252 |
try {
|
253 |
features = selection.iterator(); |
254 |
|
255 |
while (features.hasNext()) {
|
256 |
Feature feature = (Feature) features.next(); |
257 |
Geometry geometry = feature.getGeometry(index); |
258 |
Evaluator eval = null;
|
259 |
switch (action) {
|
260 |
case SelectionByTheme.INTERSECTS:
|
261 |
eval = new IntersectsGeometryEvaluator(geometry,
|
262 |
projection, featureType, geomName); |
263 |
break;
|
264 |
case SelectionByTheme.CONTAINS:
|
265 |
eval = new ContainsGeometryEvaluator(geometry,
|
266 |
projection, featureType, geomName); |
267 |
break;
|
268 |
case SelectionByTheme.CROSSES:
|
269 |
eval = new CrossesGeometryEvaluator(geometry,
|
270 |
projection, featureType, geomName); |
271 |
break;
|
272 |
case SelectionByTheme.DISJOINT:
|
273 |
eval = new DisjointGeometryEvaluator(geometry,
|
274 |
projection, featureType, geomName); |
275 |
break;
|
276 |
case SelectionByTheme.EQUALS:
|
277 |
eval = new EqualsGeometryEvaluator(geometry,
|
278 |
projection, featureType, geomName); |
279 |
break;
|
280 |
case SelectionByTheme.OVERLAPS:
|
281 |
eval = new OverlapsGeometryEvaluator(geometry,
|
282 |
projection, featureType, geomName); |
283 |
break;
|
284 |
case SelectionByTheme.TOUCHES:
|
285 |
eval = new TouchesGeometryEvaluator(geometry,
|
286 |
projection, featureType, geomName); |
287 |
break;
|
288 |
case SelectionByTheme.WITHIN:
|
289 |
eval = new WithinGeometryEvaluator(geometry,
|
290 |
projection, featureType, geomName); |
291 |
break;
|
292 |
|
293 |
default:
|
294 |
eval = new IntersectsGeometryEvaluator(geometry,
|
295 |
projection, featureType, geomName); |
296 |
break;
|
297 |
} |
298 |
|
299 |
Boolean evaluate = (Boolean) eval.evaluate(data); |
300 |
if ((evaluate).booleanValue()) {
|
301 |
return evaluate;
|
302 |
} |
303 |
} |
304 |
} catch (DataException e) {
|
305 |
NotificationManager.addError("error_selection_by_theme", e);
|
306 |
} finally {
|
307 |
if (features != null) { |
308 |
features.dispose(); |
309 |
} |
310 |
} |
311 |
return new Boolean(false); |
312 |
} |
313 |
|
314 |
public String getName() { |
315 |
return "select by theme"; |
316 |
} |
317 |
public String getCQL() { |
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 |
} |