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