Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / convexhull / fmap / ConvexHullGeoprocess.java @ 5412

History | View | Annotate | Download (9.85 KB)

1 5412 azabala
/*
2
 * Created on 16-feb-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id$
47
* $Log$
48
* Revision 1.1  2006-05-24 21:13:31  azabala
49
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
50
*
51
* Revision 1.10  2006/05/01 19:16:22  azabala
52
* la cancelacion no solo para el ITask que ejecuta el geoproceso, adem?s llama al metodo cancel() del mismo (que se supone que deber?a hacer un drop() con los resultados del geoproceso inconcluso)
53
*
54
* Revision 1.9  2006/03/21 19:26:41  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.8  2006/03/17 19:52:54  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.7  2006/03/15 18:31:06  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.6  2006/03/14 18:32:46  fjp
64
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
65
*
66
* Revision 1.5  2006/03/07 21:01:33  azabala
67
* *** empty log message ***
68
*
69
* Revision 1.4  2006/03/06 19:48:39  azabala
70
* *** empty log message ***
71
*
72
* Revision 1.3  2006/03/05 19:57:25  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.2  2006/02/26 20:52:43  azabala
76
* *** empty log message ***
77
*
78
* Revision 1.1  2006/02/17 16:32:20  azabala
79
* *** empty log message ***
80
 *
81
 *
82
 */
83
84
package com.iver.cit.gvsig.geoprocess.convexhull.fmap;
85
86
import java.util.Map;
87
88
import com.iver.andami.PluginServices;
89
import com.iver.cit.gvsig.fmap.DriverException;
90
import com.iver.cit.gvsig.fmap.core.IFeature;
91
import com.iver.cit.gvsig.fmap.core.IGeometry;
92
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
93
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
94
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
95
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
96
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
97
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
98
import com.iver.cit.gvsig.fmap.edition.EditionException;
99
import com.iver.cit.gvsig.fmap.layers.FBitSet;
100
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
101
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
102
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
103
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
104
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
105
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory;
106
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
107
import com.iver.cit.gvsig.geoprocess.core.fmap.IOneLayerGeoprocess;
108
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
109
import com.iver.utiles.swing.threads.CancellableMonitorable;
110
import com.iver.utiles.swing.threads.DefaultCancellableMonitorable;
111
import com.iver.utiles.swing.threads.IMonitorableTask;
112
113
/**
114
 * Geoprocess that computes the convex hull of all geometries of the input
115
 * layer.
116
 *
117
 * @author azabala
118
 *
119
 */
120
public class ConvexHullGeoprocess extends AbstractGeoprocess implements
121
                IOneLayerGeoprocess {
122
123
        /**
124
         * Schema of the result layer
125
         */
126
        private LayerDefinition resultLayerDefinition;
127
128
        /**
129
         * Iterates over geometries computing convex hull.
130
         * TODO Comparative Memory
131
         * vs Scalable
132
         */
133
        private ScalableConvexHullVisitor visitor;
134
135
        /**
136
         * If only consideer selection to compute convex hull
137
         */
138
        private boolean convexSelection;
139
140
        public ConvexHullGeoprocess() {
141
                visitor = new ScalableConvexHullVisitor();
142
        }
143
144
        public void setParameters(Map params) throws GeoprocessException {
145
                Boolean onlySelection = (Boolean) params.get("layer_selection");
146
                if (onlySelection != null)
147
                        convexSelection = onlySelection.booleanValue();
148
149
        }
150
151
        public void checkPreconditions() throws GeoprocessException {
152
                if (firstLayer == null)
153
                        throw new GeoprocessException(
154
                                        "Convex Hull con capa de entrada a null");
155
                if (this.writer == null || this.schemaManager == null) {
156
                        throw new GeoprocessException(
157
                                        "Operacion de convex hull sin especificar capa de resultados");
158
                }
159
160
        }
161
162
        public void process() throws GeoprocessException {
163
                Strategy strategy = StrategyManager.getStrategy(firstLayer);
164
                try {
165
                        if (convexSelection) {
166
                                strategy.process(visitor, firstLayer.getRecordset()
167
                                                .getSelection());
168
                        } else {
169
                                strategy.process(visitor);
170
                        }
171
172
                        IGeometry convexHull = visitor.getConvexHull();
173
                        Object[] attrs = new Object[1];
174
                        attrs[0] = new Long(0);
175
                        IFeature feature = FeatureFactory.createFeature(attrs, convexHull,
176
                                        resultLayerDefinition);
177
                        writer.preProcess();
178
                        DefaultRowEdited editedFeature = new DefaultRowEdited(feature,
179
                                        DefaultRowEdited.STATUS_ADDED, 0);
180
                        writer.process(editedFeature);
181
                        writer.postProcess();
182
183
                } catch (DriverException e) {
184
                        throw new GeoprocessException(
185
                                        "Problemas con el driver calculando el convex hull");
186
                } catch (VisitException e) {
187
                        throw new GeoprocessException(
188
                                        "Problemas durante el proceso de calculo del convex hull");
189
                } catch (EditionException e) {
190
                        throw new GeoprocessException(
191
                                        "Problemas al guardar la capa resultado del convex hull");
192
                }
193
        }
194
195
        public ILayerDefinition createLayerDefinition() {
196
                if(resultLayerDefinition == null){
197
                        resultLayerDefinition = new SHPLayerDefinition();
198
                        resultLayerDefinition.setShapeType(XTypes.POLYGON);
199
                        FieldDescription[] fields = new FieldDescription[1];
200
                        fields[0] = new FieldDescription();
201
                        fields[0].setFieldLength(10);
202
                        fields[0].setFieldName("FID");
203
                        fields[0].setFieldType(XTypes.BIGINT);
204
                        resultLayerDefinition.setFieldsDesc(fields);
205
                }
206
                return resultLayerDefinition;
207
        }
208
209
        public void setFirstOperand(FLyrVect firstLayer) {
210
                this.firstLayer = firstLayer;
211
        }
212
213
        public IMonitorableTask createTask() {
214
                try {
215
                        return new ConvexHullMonitorableTask();
216
                } catch (Exception e) {
217
                        return null;
218
                }
219
        }
220
221
        /**
222
         * IMonitorableTask that allows to run convex hull geoprocess in background,
223
         * with cancelation requests.
224
         *
225
         * @author azabala
226
         *
227
         */
228
        class ConvexHullMonitorableTask implements IMonitorableTask {
229
                private CancellableMonitorable cancelMonitor = null;
230
                String MAIN_MESSAGE = PluginServices.getText(this, "Mensaje_convexhull");
231
                String HULL_MESSAGE = PluginServices.getText(this, "Mensaje_procesando_convexhull");
232
                String of = PluginServices.getText(this, "De");
233
                private boolean finished = false;
234
235
                ConvexHullMonitorableTask() throws DriverIOException, DriverException{
236
                        initialize();
237
                }
238
                void initialize() throws DriverIOException, DriverException {
239
                        cancelMonitor = createCancelMonitor();
240
                }
241
242
                private CancellableMonitorable createCancelMonitor()
243
                                throws DriverIOException, DriverException {
244
                        DefaultCancellableMonitorable monitor = new DefaultCancellableMonitorable();
245
                        monitor.setInitialStep(0);
246
                        monitor.setDeterminatedProcess(true);
247
                        int numSteps = 0;
248
                        if (convexSelection) {
249
                                FBitSet selection = firstLayer.getRecordset().getSelection();
250
                                numSteps = selection.cardinality();
251
                        } else {
252
                                numSteps = firstLayer.getSource().getShapeCount();
253
                        }
254
                        monitor.setFinalStep(numSteps);
255
                        return monitor;
256
                }
257
258
                public int getInitialStep() {
259
                        return cancelMonitor.getInitialStep();
260
                }
261
262
                public int getFinishStep() {
263
                        return cancelMonitor.getFinalStep();
264
                }
265
266
                public int getCurrentStep() {
267
                        return cancelMonitor.getCurrentStep();
268
                }
269
270
                public String getStatusMessage() {
271
                        return MAIN_MESSAGE;
272
                }
273
274
                public String getNote() {
275
                        return HULL_MESSAGE + " " +
276
                        getCurrentStep()+ " " +
277
                        of  + " "+
278
                        getFinishStep();
279
                }
280
281
                public void cancel() {
282
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
283
                        ConvexHullGeoprocess.this.cancel();
284
                }
285
286
                public void run() throws GeoprocessException {
287
288
                        Strategy strategy = StrategyManager.getStrategy(firstLayer);
289
                        try {
290
                                if (convexSelection) {
291
                                        strategy.process(visitor, firstLayer.getRecordset()
292
                                                        .getSelection(), cancelMonitor);
293
                                } else {
294
                                        strategy.process(visitor, cancelMonitor);
295
                                }
296
297
                                IGeometry convexHull = visitor.getConvexHull();
298
                                Object[] attrs = new Object[1];
299
                                attrs[0] = new Long(0);
300
                                IFeature feature = FeatureFactory.createFeature(attrs,
301
                                                convexHull, resultLayerDefinition);
302
                                writer.preProcess();
303
                                DefaultRowEdited editedFeature = new DefaultRowEdited(feature,
304
                                                DefaultRowEdited.STATUS_ADDED, 0);
305
                                writer.process(editedFeature);
306
                                writer.postProcess();
307
308
                        } catch (DriverException e) {
309
                                throw new GeoprocessException(
310
                                                "Problemas con el driver calculando el convex hull");
311
                        } catch (VisitException e) {
312
                                throw new GeoprocessException(
313
                                                "Problemas durante el proceso de calculo del convex hull");
314
                        } catch (EditionException e) {
315
                                throw new GeoprocessException(
316
                                                "Problemas al guardar la capa resultado del convex hull");
317
                        }
318
                        finished = true;
319
                }
320
321
                public boolean isDefined() {
322
                        return cancelMonitor.isDeterminatedProcess();
323
                }
324
325
                public boolean isCanceled() {
326
                        return cancelMonitor.isCanceled();
327
                }
328
329
                public boolean isFinished() {
330
                        return finished;
331
                }
332
333
        }
334
335
}