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