Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / impl / convexhull / ConvexHullGeoprocess.java @ 4447

History | View | Annotate | Download (9.61 KB)

1
/*
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: ConvexHullGeoprocess.java 4447 2006-03-15 18:35:56Z azabala $
47
* $Log$
48
* Revision 1.7  2006-03-15 18:31:06  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.6  2006/03/14 18:32:46  fjp
52
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
53
*
54
* Revision 1.5  2006/03/07 21:01:33  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.4  2006/03/06 19:48:39  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.3  2006/03/05 19:57:25  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.2  2006/02/26 20:52:43  azabala
64
* *** empty log message ***
65
*
66
* Revision 1.1  2006/02/17 16:32:20  azabala
67
* *** empty log message ***
68
 *
69
 *
70
 */
71

    
72
package com.iver.gvsig.geoprocessing.impl.convexhull;
73

    
74
import java.util.Map;
75

    
76
import com.iver.cit.gvsig.fmap.DriverException;
77
import com.iver.cit.gvsig.fmap.core.IFeature;
78
import com.iver.cit.gvsig.fmap.core.IGeometry;
79
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
80
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
81
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
82
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
83
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
84
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
85
import com.iver.cit.gvsig.fmap.edition.EditionException;
86
import com.iver.cit.gvsig.fmap.layers.FBitSet;
87
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
88
import com.iver.cit.gvsig.fmap.operations.CancellableMonitorable;
89
import com.iver.cit.gvsig.fmap.operations.DefaultCancellableMonitorable;
90
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
91
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
92
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
93
import com.iver.gvsig.geoprocessing.impl.AbstractGeoprocess;
94
import com.iver.gvsig.geoprocessing.model.GeoprocessException;
95
import com.iver.gvsig.geoprocessing.model.IOneLayerGeoprocess;
96
import com.iver.gvsig.geoprocessing.schemabuilder.FeatureFactory;
97
import com.iver.gvsig.geoprocessing.schemabuilder.XTypes;
98
import com.iver.utiles.swing.threads.IMonitorableTask;
99

    
100
/**
101
 * Geoprocess that computes the convex hull of all geometries of the input
102
 * layer.
103
 * 
104
 * @author azabala
105
 * 
106
 */
107
public class ConvexHullGeoprocess extends AbstractGeoprocess implements
108
                IOneLayerGeoprocess {
109

    
110
        /**
111
         * Schema of the result layer
112
         */
113
        private LayerDefinition resultLayerDefinition;
114

    
115
        /**
116
         * Iterates over geometries computing convex hull. 
117
         * TODO Comparative Memory
118
         * vs Scalable
119
         */
120
        private ScalableConvexHullVisitor visitor;
121

    
122
        /**
123
         * If only consideer selection to compute convex hull
124
         */
125
        private boolean convexSelection;
126

    
127
        public ConvexHullGeoprocess() {
128
                visitor = new ScalableConvexHullVisitor();
129
        }
130

    
131
        public void setParameters(Map params) throws GeoprocessException {
132
                Boolean onlySelection = (Boolean) params.get("layer_selection");
133
                if (onlySelection != null)
134
                        convexSelection = onlySelection.booleanValue();
135

    
136
        }
137

    
138
        public void checkPreconditions() throws GeoprocessException {
139
                if (firstLayer == null)
140
                        throw new GeoprocessException(
141
                                        "Convex Hull con capa de entrada a null");
142
                if (this.writer == null || this.schemaManager == null) {
143
                        throw new GeoprocessException(
144
                                        "Operacion de convex hull sin especificar capa de resultados");
145
                }
146

    
147
        }
148

    
149
        public void process() throws GeoprocessException {
150
                Strategy strategy = StrategyManager.getStrategy(firstLayer);
151
                try {
152
                        if (convexSelection) {
153
                                strategy.process(visitor, firstLayer.getRecordset()
154
                                                .getSelection());
155
                        } else {
156
                                strategy.process(visitor);
157
                        }
158

    
159
                        IGeometry convexHull = visitor.getConvexHull();
160
                        Object[] attrs = new Object[1];
161
                        attrs[0] = new Long(0);
162
                        IFeature feature = FeatureFactory.createFeature(attrs, convexHull,
163
                                        resultLayerDefinition);
164
                        writer.preProcess();
165
                        DefaultRowEdited editedFeature = new DefaultRowEdited(feature,
166
                                        DefaultRowEdited.STATUS_ADDED, 0);
167
                        writer.process(editedFeature);
168
                        writer.postProcess();
169

    
170
                } catch (DriverException e) {
171
                        throw new GeoprocessException(
172
                                        "Problemas con el driver calculando el convex hull");
173
                } catch (VisitException e) {
174
                        throw new GeoprocessException(
175
                                        "Problemas durante el proceso de calculo del convex hull");
176
                } catch (EditionException e) {
177
                        throw new GeoprocessException(
178
                                        "Problemas al guardar la capa resultado del convex hull");
179
                }
180
        }
181

    
182
        public void cancel() {
183
                // TODO Implementar la cancelacion
184
                //llamar a ISchemaManager.drop() para borrar la capa
185
                //resultado
186

    
187
        }
188

    
189

    
190
        public ILayerDefinition createLayerDefinition() {
191
                if(resultLayerDefinition == null){
192
                        resultLayerDefinition = new SHPLayerDefinition();
193
                        resultLayerDefinition.setShapeType(XTypes.POLYGON);
194
                        FieldDescription[] fields = new FieldDescription[1];
195
                        fields[0] = new FieldDescription();
196
                        fields[0].setFieldLength(10);
197
                        fields[0].setFieldName("FID");
198
                        fields[0].setFieldType(XTypes.BIGINT);
199
                        resultLayerDefinition.setFieldsDesc(fields);
200
                }
201
                return resultLayerDefinition;
202
        }
203

    
204
        public void setFirstOperand(FLyrVect firstLayer) {
205
                this.firstLayer = firstLayer;
206
        }
207

    
208
        public IMonitorableTask createTask() {
209
                try {
210
                        return new ConvexHullMonitorableTask();
211
                } catch (DriverIOException e) {
212
                        return null;
213
                }
214
        }
215

    
216
        /**
217
         * IMonitorableTask that allows to run convex hull geoprocess in background,
218
         * with cancelation requests.
219
         * 
220
         * @author azabala
221
         * 
222
         */
223
        class ConvexHullMonitorableTask implements IMonitorableTask {
224
                private CancellableMonitorable cancelMonitor = null;
225

    
226
                // FIXME INTERNACIONALIZAR ESTO
227
                String HULL_MESSAGE = "Computing convex hull:";
228

    
229
                private boolean finished = false;
230

    
231
                ConvexHullMonitorableTask() throws DriverIOException{
232
                        initialize();
233
                }
234
                void initialize() throws DriverIOException {
235
                        cancelMonitor = createCancelMonitor();
236
                }
237

    
238
                private CancellableMonitorable createCancelMonitor()
239
                                throws DriverIOException {
240
                        DefaultCancellableMonitorable monitor = new DefaultCancellableMonitorable();
241
                        monitor.setInitialStep(0);
242
                        monitor.setDeterminatedProcess(true);
243
                        int numSteps = 0;
244
                        if (convexSelection) {
245
                                FBitSet selection = firstLayer.getRecordset().getSelection();
246
                                numSteps = selection.cardinality();
247
                        } else {
248
                                numSteps = firstLayer.getSource().getShapeCount();
249
                        }
250
                        monitor.setFinalStep(numSteps);
251
                        return monitor;
252
                }
253

    
254
                public int getInitialStep() {
255
                        return cancelMonitor.getInitialStep();
256
                }
257

    
258
                public int getFinishStep() {
259
                        return cancelMonitor.getFinalStep();
260
                }
261

    
262
                public int getCurrentStep() {
263
                        return cancelMonitor.getCurrentStep();
264
                }
265

    
266
                public String getStatusMessage() {
267
                        // FIXME Cambiar esto por un mecanismo de eventos,
268
                        // de forma que la tarea lo que tenga sea un escuchador
269
                        // que cambie el mensaje de estado segun los eventos
270
                        // de tareas que se est?n realizando
271
                        return "Convex Hull Geoprocess...";
272
                }
273

    
274
                public String getNote() {
275
                        // FIXME Cambiar esto por un mecanismo de eventos,
276
                        // de forma que la tarea lo que tenga sea un escuchador
277
                        // que cambie el mensaje de estado segun los eventos
278
                        // de tareas que se est?n realizando
279
                        return HULL_MESSAGE +
280
                        getCurrentStep()+ 
281
                        " de "  +
282
                        getFinishStep();
283
                }
284

    
285
                public void cancel() {
286
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
287
                }
288

    
289
                public void run() throws GeoprocessException {
290

    
291
                        Strategy strategy = StrategyManager.getStrategy(firstLayer);
292
                        try {
293
                                if (convexSelection) {
294
                                        strategy.process(visitor, firstLayer.getRecordset()
295
                                                        .getSelection(), cancelMonitor);
296
                                } else {
297
                                        strategy.process(visitor, cancelMonitor);
298
                                }
299

    
300
                                IGeometry convexHull = visitor.getConvexHull();
301
                                Object[] attrs = new Object[1];
302
                                attrs[0] = new Long(0);
303
                                IFeature feature = FeatureFactory.createFeature(attrs,
304
                                                convexHull, resultLayerDefinition);
305
                                writer.preProcess();
306
                                DefaultRowEdited editedFeature = new DefaultRowEdited(feature,
307
                                                DefaultRowEdited.STATUS_ADDED, 0);
308
                                writer.process(editedFeature);
309
                                writer.postProcess();
310

    
311
                        } catch (DriverException e) {
312
                                throw new GeoprocessException(
313
                                                "Problemas con el driver calculando el convex hull");
314
                        } catch (VisitException e) {
315
                                throw new GeoprocessException(
316
                                                "Problemas durante el proceso de calculo del convex hull");
317
                        } catch (EditionException e) {
318
                                throw new GeoprocessException(
319
                                                "Problemas al guardar la capa resultado del convex hull");
320
                        }
321
                        finished = true;
322
                }
323

    
324
                public boolean isDefined() {
325
                        return cancelMonitor.isDeterminatedProcess();
326
                }
327

    
328
                public boolean isCanceled() {
329
                        return cancelMonitor.isCanceled();
330
                }
331

    
332
                public boolean isFinished() {
333
                        return finished;
334
                }
335

    
336
        }
337

    
338
}