svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / selectionByTheme / MySelectionByThemeListener.java @ 43847
History | View | Annotate | Download (12.7 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.tools.dispose.DisposableIterator; |
30 |
import org.gvsig.fmap.dal.feature.Feature; |
31 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
33 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
35 |
import org.gvsig.fmap.geom.Geometry; |
36 |
import org.gvsig.fmap.geom.GeometryUtils; |
37 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
38 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
39 |
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory; |
40 |
import org.gvsig.tools.dispose.DisposeUtils; |
41 |
import org.gvsig.tools.evaluator.Evaluator; |
42 |
import org.gvsig.tools.evaluator.EvaluatorData; |
43 |
import org.gvsig.tools.evaluator.EvaluatorException; |
44 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
45 |
|
46 |
public class MySelectionByThemeListener implements SelectionByThemeListener { |
47 |
|
48 |
@Override
|
49 |
public void newSet(FLayer[] layersToSelect, FLayer selectionLayer, int action) { |
50 |
if (selectionLayer instanceof FLyrVect) { |
51 |
try {
|
52 |
FeatureSelection selection = ((FLyrVect) selectionLayer).getFeatureStore().getFeatureSelection(); |
53 |
SelectByTheme filter = new SelectByTheme(
|
54 |
selection, |
55 |
selectionLayer.getMapContext().getProjection(), |
56 |
action |
57 |
); |
58 |
|
59 |
for (FLayer layerToSelect : layersToSelect) {
|
60 |
if (layerToSelect instanceof FLyrVect) { |
61 |
FeatureStore storeToQuery = ((FLyrVect) layerToSelect).getFeatureStore(); |
62 |
FeatureQuery fQuery = storeToQuery.createFeatureQuery(); |
63 |
filter.setData(storeToQuery, null);
|
64 |
fQuery.setFilter(filter); |
65 |
FeatureSet featureSet = null;
|
66 |
try {
|
67 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
68 |
storeToQuery.getFeatureSelection().deselectAll(); |
69 |
storeToQuery.setSelection(featureSet); |
70 |
} finally {
|
71 |
DisposeUtils.disposeQuietly(featureSet); |
72 |
} |
73 |
} |
74 |
} |
75 |
} catch (Exception e) { |
76 |
NotificationManager.addError("error_selection_by_theme", e);
|
77 |
} |
78 |
} |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void addToSet(FLayer[] layersToSelect, FLayer selectionLayer, int action) { |
83 |
if (selectionLayer instanceof FLyrVect) { |
84 |
try {
|
85 |
FeatureSelection selection = ((FLyrVect) selectionLayer).getFeatureStore().getFeatureSelection(); |
86 |
SelectByTheme filter = new SelectByTheme(
|
87 |
selection, |
88 |
selectionLayer.getMapContext().getProjection(), |
89 |
action |
90 |
); |
91 |
|
92 |
for (FLayer layerToSelect : layersToSelect) {
|
93 |
if (layerToSelect instanceof FLyrVect) { |
94 |
if (! GeometryUtils.intersects(selectionLayer.getFullEnvelope(),layerToSelect.getFullEnvelope()) ) {
|
95 |
continue;
|
96 |
} |
97 |
FeatureStore storeToQuery = ((FLyrVect) layerToSelect).getFeatureStore(); |
98 |
FeatureQuery fQuery = storeToQuery.createFeatureQuery(); |
99 |
filter.setData(storeToQuery, null);
|
100 |
fQuery.setFilter(filter); |
101 |
FeatureSet newFeatureSet = null;
|
102 |
FeatureSelection previousFeatureSet = null;
|
103 |
try {
|
104 |
newFeatureSet = storeToQuery.getFeatureSet(fQuery); |
105 |
previousFeatureSet = storeToQuery.getFeatureSelection(); |
106 |
previousFeatureSet.select(newFeatureSet); |
107 |
storeToQuery.setSelection(previousFeatureSet); |
108 |
} finally {
|
109 |
DisposeUtils.disposeQuietly(newFeatureSet); |
110 |
DisposeUtils.disposeQuietly(previousFeatureSet); |
111 |
} |
112 |
} |
113 |
} |
114 |
} catch (Exception e) { |
115 |
NotificationManager.addError("error_selection_by_theme", e);
|
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void fromSet(FLayer[] layersToSelect, FLayer selectionLayer, int action) { |
122 |
if (selectionLayer instanceof FLyrVect) { |
123 |
try {
|
124 |
FeatureSelection selection = ((FLyrVect) selectionLayer).getFeatureStore().getFeatureSelection(); |
125 |
SelectByTheme filter = new SelectByTheme(
|
126 |
selection, |
127 |
selectionLayer.getMapContext().getProjection(), |
128 |
action |
129 |
); |
130 |
|
131 |
for (FLayer layerToSelect : layersToSelect) {
|
132 |
if (layerToSelect instanceof FLyrVect) { |
133 |
if (!GeometryUtils.intersects(selectionLayer.getFullEnvelope(),layerToSelect.getFullEnvelope())) {
|
134 |
continue;
|
135 |
} |
136 |
FeatureStore storeToQuery = ((FLyrVect) layerToSelect).getFeatureStore(); |
137 |
FeatureQuery fQuery = storeToQuery.createFeatureQuery(); |
138 |
filter.setData( |
139 |
storeToQuery, |
140 |
(FeatureSelection) storeToQuery.getSelection() |
141 |
); |
142 |
fQuery.setFilter(filter); |
143 |
FeatureSet featureSet = null;
|
144 |
try {
|
145 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
146 |
storeToQuery.getFeatureSelection().deselectAll(); |
147 |
storeToQuery.setSelection(featureSet); |
148 |
} finally {
|
149 |
DisposeUtils.disposeQuietly(featureSet); |
150 |
} |
151 |
} |
152 |
} |
153 |
} catch (Exception e) { |
154 |
NotificationManager.addError("error_selection_by_theme", e);
|
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
class SelectByTheme implements Evaluator { |
160 |
|
161 |
private FeatureSelection selection = null; |
162 |
private final IProjection projection; |
163 |
private FeatureStore store;
|
164 |
private String geomName; |
165 |
private EvaluatorFieldsInfo info;
|
166 |
private FeatureSelection featureSelection;
|
167 |
private final int action; |
168 |
|
169 |
|
170 |
public SelectByTheme(
|
171 |
FeatureSelection selection, |
172 |
IProjection projection, |
173 |
int action
|
174 |
) { |
175 |
this.selection = selection;
|
176 |
this.projection = projection;
|
177 |
this.action = action;
|
178 |
} |
179 |
|
180 |
public void setData( |
181 |
FeatureStore store, |
182 |
FeatureSelection featureSelection |
183 |
) throws DataException {
|
184 |
this.store = store;
|
185 |
this.geomName = store.getDefaultFeatureType().getDefaultGeometryAttributeName();
|
186 |
this.featureSelection = featureSelection;
|
187 |
this.info = new EvaluatorFieldsInfo(); |
188 |
this.info.addFieldValue(this.geomName); |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
193 |
if (featureSelection != null && !featureSelection.isSelected(((Feature) data.getContextValue("feature")))) { |
194 |
return false; |
195 |
} |
196 |
DisposableIterator features = null;
|
197 |
try {
|
198 |
features = selection.fastIterator(); |
199 |
|
200 |
while (features.hasNext()) {
|
201 |
Feature feature = (Feature) features.next(); |
202 |
Geometry geometry = feature.getGeometry(geomName); |
203 |
Evaluator eval; |
204 |
switch (action) {
|
205 |
case SelectionByTheme.INTERSECTS:
|
206 |
eval = SpatialEvaluatorsFactory.getInstance().intersects( |
207 |
geometry, |
208 |
projection, |
209 |
store |
210 |
); |
211 |
break;
|
212 |
case SelectionByTheme.CONTAINS:
|
213 |
eval = SpatialEvaluatorsFactory.getInstance().contains( |
214 |
geometry, |
215 |
projection, |
216 |
store |
217 |
); |
218 |
break;
|
219 |
case SelectionByTheme.CROSSES:
|
220 |
eval = SpatialEvaluatorsFactory.getInstance().crosses( |
221 |
geometry, |
222 |
projection, |
223 |
store |
224 |
); |
225 |
break;
|
226 |
case SelectionByTheme.DISJOINT:
|
227 |
eval = SpatialEvaluatorsFactory.getInstance().disjoint( |
228 |
geometry, |
229 |
projection, |
230 |
store |
231 |
); |
232 |
break;
|
233 |
case SelectionByTheme.EQUALS:
|
234 |
eval = SpatialEvaluatorsFactory.getInstance().equals( |
235 |
geometry, |
236 |
projection, |
237 |
store |
238 |
); |
239 |
break;
|
240 |
case SelectionByTheme.OVERLAPS:
|
241 |
eval = SpatialEvaluatorsFactory.getInstance().overlaps( |
242 |
geometry, |
243 |
projection, |
244 |
store |
245 |
); |
246 |
break;
|
247 |
case SelectionByTheme.TOUCHES:
|
248 |
eval = SpatialEvaluatorsFactory.getInstance().touches( |
249 |
geometry, |
250 |
projection, |
251 |
store |
252 |
); |
253 |
break;
|
254 |
case SelectionByTheme.WITHIN:
|
255 |
eval = SpatialEvaluatorsFactory.getInstance().within( |
256 |
geometry, |
257 |
projection, |
258 |
store |
259 |
); |
260 |
break;
|
261 |
|
262 |
default:
|
263 |
eval = SpatialEvaluatorsFactory.getInstance().intersects( |
264 |
geometry, |
265 |
projection, |
266 |
store |
267 |
); |
268 |
break;
|
269 |
} |
270 |
|
271 |
Boolean evaluate = (Boolean) eval.evaluate(data); |
272 |
if ( evaluate) {
|
273 |
return evaluate;
|
274 |
} |
275 |
} |
276 |
} catch (DataException e) {
|
277 |
NotificationManager.addError("error_selection_by_theme", e);
|
278 |
} finally {
|
279 |
DisposeUtils.dispose(features); |
280 |
} |
281 |
return false; |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public String getName() { |
286 |
return "select by theme"; |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public String getSQL() { |
291 |
return null; |
292 |
} |
293 |
|
294 |
@Override
|
295 |
public String getDescription() { |
296 |
return getName();
|
297 |
} |
298 |
|
299 |
@Override
|
300 |
public EvaluatorFieldsInfo getFieldsInfo() {
|
301 |
return info;
|
302 |
} |
303 |
} |
304 |
} |