Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / convexhull / fmap / ConvexHullGeoprocess.java @ 10626

History | View | Annotate | Download (10.3 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 10626 2007-03-06 16:55:54Z caballero $
47
* $Log$
48
* Revision 1.2  2007-03-06 16:47:58  caballero
49
* Exceptions
50
*
51
* Revision 1.1  2006/06/20 18:20:45  azabala
52
* first version in cvs
53
*
54
* Revision 1.1  2006/05/24 21:13:31  azabala
55
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
56
*
57
* Revision 1.10  2006/05/01 19:16:22  azabala
58
* 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)
59
*
60
* Revision 1.9  2006/03/21 19:26:41  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.8  2006/03/17 19:52:54  azabala
64
* *** empty log message ***
65
*
66
* Revision 1.7  2006/03/15 18:31:06  azabala
67
* *** empty log message ***
68
*
69
* Revision 1.6  2006/03/14 18:32:46  fjp
70
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
71
*
72
* Revision 1.5  2006/03/07 21:01:33  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.4  2006/03/06 19:48:39  azabala
76
* *** empty log message ***
77
*
78
* Revision 1.3  2006/03/05 19:57:25  azabala
79
* *** empty log message ***
80
*
81
* Revision 1.2  2006/02/26 20:52:43  azabala
82
* *** empty log message ***
83
*
84
* Revision 1.1  2006/02/17 16:32:20  azabala
85
* *** empty log message ***
86
 *
87
 *
88
 */
89

    
90
package com.iver.cit.gvsig.geoprocess.impl.convexhull.fmap;
91

    
92
import java.util.Map;
93

    
94
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
95
import com.iver.andami.PluginServices;
96
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
97
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
98
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
99
import com.iver.cit.gvsig.fmap.core.IFeature;
100
import com.iver.cit.gvsig.fmap.core.IGeometry;
101
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
102
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
103
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
104
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
105
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
106
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
107
import com.iver.cit.gvsig.fmap.layers.FBitSet;
108
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
109
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
110
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
111
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
112
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory;
113
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
114
import com.iver.cit.gvsig.geoprocess.core.fmap.IOneLayerGeoprocess;
115
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
116
import com.iver.utiles.swing.threads.CancellableMonitorable;
117
import com.iver.utiles.swing.threads.DefaultCancellableMonitorable;
118
import com.iver.utiles.swing.threads.IMonitorableTask;
119

    
120
/**
121
 * Geoprocess that computes the convex hull of all geometries of the input
122
 * layer.
123
 *
124
 * @author azabala
125
 *
126
 */
127
public class ConvexHullGeoprocess extends AbstractGeoprocess implements
128
                IOneLayerGeoprocess {
129

    
130
        /**
131
         * Schema of the result layer
132
         */
133
        private LayerDefinition resultLayerDefinition;
134

    
135
        /**
136
         * Iterates over geometries computing convex hull.
137
         * TODO Comparative Memory
138
         * vs Scalable
139
         */
140
        private ScalableConvexHullVisitor visitor;
141

    
142
        /**
143
         * If only consideer selection to compute convex hull
144
         */
145
        private boolean convexSelection;
146

    
147
        public ConvexHullGeoprocess() {
148
                visitor = new ScalableConvexHullVisitor();
149
        }
150

    
151
        public void setParameters(Map params) throws GeoprocessException {
152
                Boolean onlySelection = (Boolean) params.get("layer_selection");
153
                if (onlySelection != null)
154
                        convexSelection = onlySelection.booleanValue();
155

    
156
        }
157

    
158
        public void checkPreconditions() throws GeoprocessException {
159
                if (firstLayer == null)
160
                        throw new GeoprocessException(
161
                                        "Convex Hull con capa de entrada a null");
162
                if (this.writer == null || this.schemaManager == null) {
163
                        throw new GeoprocessException(
164
                                        "Operacion de convex hull sin especificar capa de resultados");
165
                }
166

    
167
        }
168

    
169
        public void process() throws GeoprocessException {
170
                Strategy strategy = StrategyManager.getStrategy(firstLayer);
171
                try {
172
                        if (convexSelection) {
173
                                strategy.process(visitor, firstLayer.getRecordset()
174
                                                .getSelection());
175
                        } else {
176
                                strategy.process(visitor);
177
                        }
178

    
179
                        IGeometry convexHull = visitor.getConvexHull();
180
                        Object[] attrs = new Object[1];
181
                        attrs[0] = new Long(0);
182
                        IFeature feature = FeatureFactory.createFeature(attrs, convexHull,
183
                                        resultLayerDefinition);
184
                        writer.preProcess();
185
                        DefaultRowEdited editedFeature = new DefaultRowEdited(feature,
186
                                        IRowEdited.STATUS_ADDED, 0);
187
                        writer.process(editedFeature);
188
                        writer.postProcess();
189

    
190
                } catch (ProcessVisitorException e) {
191
                        throw new GeoprocessException(
192
                                        "Problemas durante el proceso de calculo del convex hull");
193
                } catch (ReadDriverException e) {
194
                        throw new GeoprocessException(
195
                                "Problemas con el driver calculando el convex hull");
196
                } catch (ExpansionFileReadException e) {
197
                        throw new GeoprocessException(
198
                                "Problemas con el driver calculando el convex hull");
199
                } catch (VisitorException e) {
200
                        throw new GeoprocessException(
201
                                "Problemas con el driver calculando el convex hull");
202
                }
203
        }
204

    
205
        public ILayerDefinition createLayerDefinition() {
206
                if(resultLayerDefinition == null){
207
                        resultLayerDefinition = new SHPLayerDefinition();
208
                        resultLayerDefinition.setShapeType(XTypes.POLYGON);
209
                        FieldDescription[] fields = new FieldDescription[1];
210
                        fields[0] = new FieldDescription();
211
                        fields[0].setFieldLength(10);
212
                        fields[0].setFieldName("FID");
213
                        fields[0].setFieldType(XTypes.BIGINT);
214
                        resultLayerDefinition.setFieldsDesc(fields);
215
                }
216
                return resultLayerDefinition;
217
        }
218

    
219
        public void setFirstOperand(FLyrVect firstLayer) {
220
                this.firstLayer = firstLayer;
221
        }
222

    
223
        public IMonitorableTask createTask() {
224
                try {
225
                        return new ConvexHullMonitorableTask();
226
                } catch (Exception e) {
227
                        return null;
228
                }
229
        }
230

    
231
        /**
232
         * IMonitorableTask that allows to run convex hull geoprocess in background,
233
         * with cancelation requests.
234
         *
235
         * @author azabala
236
         *
237
         */
238
        class ConvexHullMonitorableTask implements IMonitorableTask {
239
                private CancellableMonitorable cancelMonitor = null;
240
                String MAIN_MESSAGE = PluginServices.getText(this, "Mensaje_convexhull");
241
                String HULL_MESSAGE = PluginServices.getText(this, "Mensaje_procesando_convexhull");
242
                String of = PluginServices.getText(this, "De");
243
                private boolean finished = false;
244

    
245
                ConvexHullMonitorableTask() throws ReadDriverException{
246
                        initialize();
247
                }
248
                void initialize() throws ReadDriverException {
249
                        cancelMonitor = createCancelMonitor();
250
                }
251

    
252
                private CancellableMonitorable createCancelMonitor() throws ReadDriverException {
253
                        DefaultCancellableMonitorable monitor = new DefaultCancellableMonitorable();
254
                        monitor.setInitialStep(0);
255
                        monitor.setDeterminatedProcess(true);
256
                        int numSteps = 0;
257
                        if (convexSelection) {
258
                                FBitSet selection = firstLayer.getRecordset().getSelection();
259
                                numSteps = selection.cardinality();
260
                        } else {
261
                                numSteps = firstLayer.getSource().getShapeCount();
262
                        }
263
                        monitor.setFinalStep(numSteps);
264
                        return monitor;
265
                }
266

    
267
                public int getInitialStep() {
268
                        return cancelMonitor.getInitialStep();
269
                }
270

    
271
                public int getFinishStep() {
272
                        return cancelMonitor.getFinalStep();
273
                }
274

    
275
                public int getCurrentStep() {
276
                        return cancelMonitor.getCurrentStep();
277
                }
278

    
279
                public String getStatusMessage() {
280
                        return MAIN_MESSAGE;
281
                }
282

    
283
                public String getNote() {
284
                        return HULL_MESSAGE + " " +
285
                        getCurrentStep()+ " " +
286
                        of  + " "+
287
                        getFinishStep();
288
                }
289

    
290
                public void cancel() {
291
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
292
                        ConvexHullGeoprocess.this.cancel();
293
                }
294

    
295
                public void run() throws GeoprocessException {
296

    
297
                        Strategy strategy = StrategyManager.getStrategy(firstLayer);
298
                        try {
299
                                if (convexSelection) {
300
                                        strategy.process(visitor, firstLayer.getRecordset()
301
                                                        .getSelection(), cancelMonitor);
302
                                } else {
303
                                        strategy.process(visitor, cancelMonitor);
304
                                }
305

    
306
                                IGeometry convexHull = visitor.getConvexHull();
307
                                Object[] attrs = new Object[1];
308
                                attrs[0] = new Long(0);
309
                                IFeature feature = FeatureFactory.createFeature(attrs,
310
                                                convexHull, resultLayerDefinition);
311
                                writer.preProcess();
312
                                DefaultRowEdited editedFeature = new DefaultRowEdited(feature,
313
                                                IRowEdited.STATUS_ADDED, 0);
314
                                writer.process(editedFeature);
315
                                writer.postProcess();
316

    
317
                        } catch (ProcessVisitorException e) {
318
                                throw new GeoprocessException(
319
                                                "Problemas durante el proceso de calculo del convex hull");
320
                        } catch (ReadDriverException e) {
321
                                throw new GeoprocessException(
322
                                        "Problemas con el driver calculando el convex hull");
323
                        } catch (ExpansionFileReadException e) {
324
                                throw new GeoprocessException(
325
                                        "Problemas con el driver calculando el convex hull");
326
                        } catch (VisitorException e) {
327
                                throw new GeoprocessException(
328
                                        "Problemas con el driver calculando el convex hull");
329
                        }
330
                        finished = true;
331
                }
332

    
333
                public boolean isDefined() {
334
                        return cancelMonitor.isDeterminatedProcess();
335
                }
336

    
337
                public boolean isCanceled() {
338
                        return cancelMonitor.isCanceled();
339
                }
340

    
341
                public boolean isFinished() {
342
                        return finished;
343
                }
344

    
345
        }
346

    
347
}