Statistics
| Revision:

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

History | View | Annotate | Download (10.5 KB)

1
/*
2
 * Created on 23-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: DissolveGeoprocess.java 5412 2006-05-24 21:15:07Z azabala $
47
* $Log$
48
* Revision 1.1  2006-05-24 21:11:14  azabala
49
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
50
*
51
* Revision 1.10  2006/05/08 15:37:26  azabala
52
* added alphanumeric dissolve
53
*
54
* Revision 1.9  2006/05/01 19:14:50  azabala
55
* 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)
56
*
57
* Revision 1.8  2006/03/23 21:03:45  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.7  2006/03/17 19:53:14  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.6  2006/03/15 18:33:24  azabala
64
* *** empty log message ***
65
*
66
* Revision 1.5  2006/03/14 18:32:46  fjp
67
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
68
*
69
* Revision 1.4  2006/03/07 21:01:33  azabala
70
* *** empty log message ***
71
*
72
* Revision 1.3  2006/03/06 19:48:39  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.2  2006/03/05 19:58:20  azabala
76
* *** empty log message ***
77
*
78
* Revision 1.1  2006/02/26 20:53:44  azabala
79
* *** empty log message ***
80
*
81
*
82
*/
83
package com.iver.cit.gvsig.geoprocess.dissolve.fmap;
84

    
85
import java.util.Map;
86

    
87
import com.iver.andami.PluginServices;
88
import com.iver.cit.gvsig.fmap.DriverException;
89
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
90
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
91
import com.iver.cit.gvsig.fmap.edition.EditionException;
92
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
93
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
94
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
95
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
96
import com.iver.cit.gvsig.geoprocess.core.fmap.IOneLayerGeoprocess;
97
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
98
import com.iver.utiles.swing.threads.CancellableMonitorable;
99
import com.iver.utiles.swing.threads.DefaultCancellableMonitorable;
100
import com.iver.utiles.swing.threads.IMonitorableTask;
101
/**
102
 * Processes each geometry of a polygonal vectorial layer, looking for
103
 * its adjacent polygons. If this adjacent polygons has the same specified 
104
 * field value as processed geometry, this polygons will be dissolved.
105
 * 
106
 * 
107
 * @author azabala
108
 *
109
 */
110
public class DissolveGeoprocess extends AbstractGeoprocess
111
                                                        implements IOneLayerGeoprocess {
112

    
113
        private String dissolveField;
114
        private boolean dissolveOnlySelection;
115
        private ILayerDefinition resultLayerDefinition;
116
        private Map fields_functions;
117
        
118
        //AZABALA
119
        /*
120
        private DissolveVisitor visitor;
121
        */
122
private FeatureDissolver visitor;        
123
private IDissolveCriteria criteria;        
124
private int dissolveType = FeatureDissolver.ALPHANUMERIC_DISSOLVE;
125
        
126
        
127
        
128
        
129
        
130
        private boolean dissolveOnlyAdjacents;
131
        
132
        
133
        /**
134
         * Constructor.
135
         * 
136
         * @param inputLayer Layer whose geometries we are 
137
         * going to dissolve
138
         */
139
        public DissolveGeoprocess(FLyrVect inputLayer, String dissolveField) {
140
                setFirstOperand(inputLayer);
141
                this.dissolveField = dissolveField;
142
        }
143
        
144
        public void setFieldsFunctions(Map fieldsFunctions){
145
                this.fields_functions = fieldsFunctions;
146
        }
147

    
148
        public void setFirstOperand(FLyrVect inputLayer) {
149
                this.firstLayer = inputLayer;
150

    
151
        }
152

    
153
        public void setParameters(Map params) throws GeoprocessException {
154
                Boolean onlySelection =
155
                        (Boolean) params.get("layer_selection");
156
                if(onlySelection != null)
157
                        dissolveOnlySelection = onlySelection.booleanValue();
158
                
159
                Boolean onlyAdjacents =
160
                        (Boolean) params.get("only_adjacents");
161
                if(onlyAdjacents != null)
162
                        dissolveOnlyAdjacents = onlyAdjacents.booleanValue();
163
                
164
                
165
                try {
166
                        if(dissolveOnlyAdjacents){
167
                                dissolveType = FeatureDissolver.SPATIAL_DISSOLVE;
168
                                criteria = 
169
                                        new SingleFieldAdjacencyDissolveCriteria(dissolveField,
170
                                                        firstLayer);
171
//                        visitor = new DissolveVisitor(dissolveField, processor);        
172
                        }else{
173
//                        visitor = new AlphanumericDissolveVisitor(dissolveField, processor);
174
                                criteria = new SingleFieldDissolveCriteria(dissolveField,
175
                                                firstLayer);        
176
                        }
177
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
178
                        throw new GeoprocessException("Error preparando la lectura del dissolve field", e);
179
                }
180
                
181
        }
182

    
183
        public void checkPreconditions() throws GeoprocessException {
184
                if (firstLayer == null)
185
                        throw new GeoprocessException("Buffer con capa de entrada a null");
186
                if(this.writer == null || 
187
                   this.schemaManager == null){
188
                        throw new GeoprocessException("Operacion de dissolve sin especificar capa de resultados");
189
                }
190
                try {
191
                        if(firstLayer.getShapeType() != XTypes.POLYGON)
192
                                throw new GeoprocessException("La capa a disolver debe ser de pol?gonos");
193
                } catch (DriverException e) {
194
                        throw new GeoprocessException("Error intentando verificar el tipo de geometria de la capa a disolver");
195
                } 
196
                if(this.dissolveField == null)
197
                        throw new GeoprocessException("No se ha proporcionado el campo para dissolver");
198

    
199
        }
200

    
201
        public void process() throws GeoprocessException {
202
                /*
203
                try {
204
                        FeaturePersisterProcessor2 processor =
205
                                new FeaturePersisterProcessor2(writer);
206
                        
207
                        if(dissolveOnlyAdjacents){
208
                                visitor = new AlphanumericDissolveVisitor(dissolveField, processor);
209
                        }else{
210
                                visitor = new DissolveVisitor(this.dissolveField, processor);        
211
                        }
212
                        visitor.setDissolvedAttributesInfo(fields_functions);
213
                        Strategy strategy =
214
                                StrategyManager.getStrategy(firstLayer);
215
                        visitor.setStrategy(strategy);
216
                        if(this.dissolveOnlySelection){
217
                                strategy.process(visitor, firstLayer.
218
                                                                                           getRecordset().
219
                                                                                           getSelection());
220
                        }else{
221
                                strategy.process(visitor);
222
                        }
223
                        
224
                        
225
                        
226
                } catch (DriverException e) {
227
                        throw new GeoprocessException("Error accediendo a los datos durante un dissolve");
228
                } catch (VisitException e) {
229
                        throw new GeoprocessException("Error al procesar los elementos de la capa de entrada durante un dissolve");
230
                }
231
                
232
                */
233
        }
234
        
235
        
236

    
237
        public void cancel() {
238
                try {
239
                        schemaManager.drop();
240
                } catch (EditionException e) {
241
                        // TODO Auto-generated catch block
242
                        e.printStackTrace();
243
                }
244
        }
245
        
246
        public ILayerDefinition createLayerDefinition() {
247
                if(resultLayerDefinition == null)
248
                        resultLayerDefinition = criteria.createLayerDefinition(fields_functions);
249
                return resultLayerDefinition;
250
        }
251
        
252
        
253

    
254
        public IMonitorableTask createTask() {
255
                try {
256
                        return new DissolveMonitorableTask();
257
                } catch (DriverIOException e) {
258
                        return null;
259
                }
260
        }
261
        
262
        /**
263
         * IMonitorableTask that allows to run diff geoprocess in background,
264
         * with cancelation requests.
265
         * 
266
         * @author azabala
267
         * 
268
         */
269
        class DissolveMonitorableTask implements IMonitorableTask {
270
                private CancellableMonitorable cancelMonitor = null;
271

    
272
                String DISSOLVE_MESSAGE = PluginServices.getText(this, "Mensaje_dissolve");
273
                String DISSOLVE_NOTE = PluginServices.getText(this, "Mensaje_procesando_dissolves");
274
                String OF = PluginServices.getText(this, "De");
275
                private boolean finished = false;
276
                
277
                FeatureDissolver dissolver = null;
278

    
279
                DissolveMonitorableTask() throws DriverIOException {
280
                        initialize();
281
                }
282
                void initialize() throws DriverIOException{
283
                        cancelMonitor = createCancelMonitor();
284
                }
285

    
286
                private CancellableMonitorable createCancelMonitor() throws DriverIOException{
287
                        DefaultCancellableMonitorable monitor = new 
288
                                                        DefaultCancellableMonitorable();
289
                        monitor.setInitialStep(0);
290
                        //monitor.setDeterminatedProcess(false);
291
                        
292
                        
293
                        //FIXME: Si solo dissolvemos la seleccion, esto esta mal
294
                        monitor.setFinalStep(firstLayer.getSource().getShapeCount());
295
                        monitor.setDeterminatedProcess(true);
296
                        return monitor;
297
                }
298

    
299
                public int getInitialStep() {
300
                        return cancelMonitor.getInitialStep();
301
                }
302

    
303
                public int getFinishStep() {
304
                        return cancelMonitor.getFinalStep();
305
                }
306

    
307
                public int getCurrentStep() {
308
                        //return cancelMonitor.getCurrentStep();
309
//                        if(visitor == null)
310
//                                return getInitialStep();
311
//                        else
312
//                                return visitor.getNumProcessedGeometries();
313
                        if(dissolver == null)
314
                                return getInitialStep();
315
                        else
316
                                return dissolver.getNumProcessedGeometries();
317
                }
318

    
319
                public String getStatusMessage() {
320
                        return DISSOLVE_MESSAGE;
321
                }
322

    
323
                public String getNote() {
324
                        return DISSOLVE_NOTE + " "+
325
                        getCurrentStep() + " " +
326
                        OF + " " +
327
                        getFinishStep();
328
                }
329

    
330
                public void cancel() {
331
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
332
                        DissolveGeoprocess.this.cancel();
333
                }
334

    
335
                public void run() throws GeoprocessException {
336
                        try {
337
                                FeaturePersisterProcessor2 processor =
338
                                        new FeaturePersisterProcessor2(writer);
339
                                
340
                                dissolver = new
341
                                        FeatureDissolver(processor, 
342
                                                        firstLayer, 
343
                                                        fields_functions,
344
                                                        criteria,
345
                                                        dissolveType);
346
                                dissolver.dissolve(cancelMonitor);
347
                                
348
                                
349
//                                visitor.setDissolvedAttributesInfo(fields_functions);
350
//                                Strategy strategy =
351
//                                        StrategyManager.getStrategy(firstLayer);
352
//                                visitor.setStrategy(strategy);                        
353
//                                if(dissolveOnlySelection){
354
//                                        strategy.process(visitor, firstLayer.
355
//                                                                                                   getRecordset().
356
//                                                                                                   getSelection(),
357
//                                                                                                   cancelMonitor);
358
//                                }else{
359
//                                        strategy.process(visitor, cancelMonitor);
360
//                                }
361
                        }catch(Exception e){
362
                                e.printStackTrace();
363
                        
364
                        } finally{
365
                                finished = true;
366
                        }
367
                }
368

    
369
                public boolean isDefined() {
370
                        return cancelMonitor.isDeterminatedProcess();
371
                }
372

    
373
                public boolean isCanceled() {
374
                        return cancelMonitor.isCanceled();
375
                }
376

    
377
                public boolean isFinished() {
378
                        return finished;
379
                }
380
        }
381

    
382
}
383