Statistics
| Revision:

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

History | View | Annotate | Download (8.94 KB)

1
/*
2
 * Created on 02-mar-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: MergeGeoprocess.java 5412 2006-05-24 21:15:07Z azabala $
47
* $Log$
48
* Revision 1.1  2006-05-24 21:10:15  azabala
49
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
50
*
51
* Revision 1.6  2006/05/01 19:09:46  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.5  2006/03/23 21:05:11  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.4  2006/03/17 19:53:34  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.3  2006/03/15 18:34:16  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.2  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.1  2006/03/05 19:59:16  azabala
67
* *** empty log message ***
68
*
69
*
70
*/
71
package com.iver.cit.gvsig.geoprocess.merge.fmap;
72

    
73
import java.util.Map;
74

    
75
import com.iver.andami.PluginServices;
76
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
77
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
78
import com.iver.cit.gvsig.fmap.edition.EditionException;
79
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
80
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
81
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
82
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
83
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
84
import com.iver.cit.gvsig.geoprocess.core.fmap.DeferredFeaturePersisterProcessor;
85
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
86
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
87
import com.iver.utiles.swing.threads.CancellableMonitorable;
88
import com.iver.utiles.swing.threads.DefaultCancellableMonitorable;
89
import com.iver.utiles.swing.threads.IMonitorableTask;
90
/**
91
 * This geoprocess is "equivalent" to UNION relational operator.
92
 * Output layer will have a geometry for each geometry of input layer.
93
 * 
94
 * Also, output layer will preserve a schema of one of input layers.
95
 * @author azabala
96
 * 
97
 * FIXME Hereda de AbstractGeoprocess firstLayer. REVISAR
98
 *
99
 */
100
public class MergeGeoprocess extends AbstractGeoprocess {
101
        /**
102
         * Input layers we are going to merge.
103
         */
104
        private FLyrVect[] inputLayers;
105
        /**
106
         * Input layer whose schema will retain result layer
107
         */
108
        private FLyrVect outputSchemaLayer;
109
        private ILayerDefinition resultLayerDefinition;
110
        
111
        public MergeGeoprocess(){
112
                
113
        }
114
        
115
        public void setParameters(Map params) throws GeoprocessException {
116
        }
117

    
118
        public void checkPreconditions() throws GeoprocessException {
119
                if(inputLayers == null)
120
                        throw new GeoprocessException("No se han especificado las capas a juntar en un merge");
121
                if(outputSchemaLayer == null)
122
                        throw new GeoprocessException("Merge: no se ha especificado que capa determinara los atributos");
123
                int shapeType = -1;
124
                try {
125
                        shapeType = inputLayers[0].getShapeType();
126
                        for(int i = 0; i < inputLayers.length; i++){
127
                                if(inputLayers[i] == null)
128
                                        throw new GeoprocessException("Merge: la capa "+i+" no est? definida -null");
129
                                if(inputLayers[i].getShapeType() != shapeType)
130
                                        throw new GeoprocessException("Las capas a juntar deben tener el mismo tipo de geometria");
131
                        }
132
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
133
                        throw new GeoprocessException("Error al tratar de determinar el tipo de geometria de las capas a mezclar");
134
                }
135
        }
136

    
137
        public void process() throws GeoprocessException {
138
                DeferredFeaturePersisterProcessor processor =
139
                        new DeferredFeaturePersisterProcessor(writer);
140
                MergeVisitor merge = new MergeVisitor(createLayerDefinition(), 
141
                                                        processor);
142
                for(int i = 0; i < inputLayers.length; i++){
143
                        Strategy strategy = StrategyManager.
144
                                        getStrategy(inputLayers[i]);
145
                        try {
146
                                strategy.process(merge);
147
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
148
                                throw new GeoprocessException("Merge: error al leer la capa "+inputLayers[i].getName());
149
                        } catch (VisitException e) {
150
                                throw new GeoprocessException("Merge: error al procesar la capa "+inputLayers[i].getName());
151
                        }
152
                }//for
153
        }
154

    
155
        public void cancel() {
156
                try {
157
                        schemaManager.drop();
158
                } catch (EditionException e) {
159
                        // TODO Auto-generated catch block
160
                        e.printStackTrace();
161
                }
162
        }
163

    
164
        public ILayerDefinition createLayerDefinition() {
165
                if(this.resultLayerDefinition == null)
166
                {
167
                        try {
168
                                this.resultLayerDefinition = 
169
                                        DefinitionUtils.
170
                                        createLayerDefinition(this.outputSchemaLayer);
171
                        } catch (Exception e) {
172
                                return null;
173
                        }
174
                }
175
                return this.resultLayerDefinition;
176
        }
177

    
178
        public void setInputLayers(FLyrVect[] inputLayers) {
179
                this.inputLayers = inputLayers;
180
                if(inputLayers != null && inputLayers.length > 0)
181
                        firstLayer = inputLayers[0];
182
        }
183

    
184
        public void setOutputSchemaLayer(FLyrVect outputSchemaLayer) {
185
                this.outputSchemaLayer = outputSchemaLayer;
186
        }
187

    
188
        public IMonitorableTask createTask() {
189
                try {
190
                        return new MergeMonitorableTask();
191
                } catch (DriverIOException e) {
192
                        return null;
193
                }
194
        }
195
        
196
        class MergeMonitorableTask implements IMonitorableTask {
197
                private CancellableMonitorable cancelMonitor = null;
198
                String MERGE_MESSAGE = PluginServices.getText(this, "Mensaje_juntar");
199
                String MERGE_NOTE = PluginServices.getText(this, "Mensaje_procesando_juntar");
200
                String OF = PluginServices.getText(this, "De");
201
                private boolean finished = false;
202

    
203
                MergeMonitorableTask() throws DriverIOException {
204
                        initialize();
205
                }
206
                void initialize() throws DriverIOException {
207
                        cancelMonitor = createCancelMonitor();
208
                }
209

    
210
                private CancellableMonitorable createCancelMonitor()
211
                                throws DriverIOException {
212
                        DefaultCancellableMonitorable monitor = new 
213
                                                        DefaultCancellableMonitorable();
214
                        monitor.setInitialStep(0);
215
                        monitor.setDeterminatedProcess(true);
216
                        int numSteps = 0;
217
                        for(int i = 0; i < inputLayers.length; i++){
218
                                numSteps += inputLayers[i].getSource().getShapeCount();
219
                        }
220
                        monitor.setFinalStep(numSteps);
221
                        return monitor;
222
                }
223

    
224
                public int getInitialStep() {
225
                        return cancelMonitor.getInitialStep();
226
                }
227

    
228
                public int getFinishStep() {
229
                        return cancelMonitor.getFinalStep();
230
                }
231

    
232
                public int getCurrentStep() {
233
                        return cancelMonitor.getCurrentStep();
234
                }
235

    
236
                public String getStatusMessage() {
237
                        return MERGE_MESSAGE;
238
                }
239

    
240
                public String getNote() {
241
                        return MERGE_NOTE + " " +
242
                        getCurrentStep() + " "+
243
                        OF + " " + getFinishStep();
244
                }
245

    
246
                public void cancel() {
247
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
248
                        MergeGeoprocess.this.cancel();
249
                }
250

    
251
                public void run() throws GeoprocessException {
252
                        DeferredFeaturePersisterProcessor processor =
253
                                new DeferredFeaturePersisterProcessor(writer);
254
                        try {
255
                                writer.preProcess();
256
                        } catch (EditionException e1) {
257
                                finished = true;
258
                                throw new GeoprocessException("Error al preparar la capa resultado");
259
                        }
260
                        MergeVisitor merge = new MergeVisitor(createLayerDefinition(), 
261
                                                                processor);
262
                        for(int i = 0; i < inputLayers.length; i++){
263
                                Strategy strategy = StrategyManager.
264
                                                getStrategy(inputLayers[i]);
265
                                try {
266
                                        strategy.process(merge, cancelMonitor);
267
                                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
268
                                        finished = true;
269
                                        throw new GeoprocessException("Merge: error al leer la capa "+inputLayers[i].getName());
270
                                } catch (VisitException e) {
271
                                        finished = true;
272
                                        throw new GeoprocessException("Merge: error al procesar la capa "+inputLayers[i].getName());
273
                                }
274
                        }//for
275
                        try {
276
                                writer.postProcess();
277
                        } catch (EditionException e) {
278
                                finished = true;
279
                                throw new GeoprocessException("Error al preparar la capa resultado");
280
                        }
281
                        finished = true;
282
                        
283
                }
284

    
285
                public boolean isDefined() {
286
                        return cancelMonitor.isDeterminatedProcess();
287
                }
288

    
289
                public boolean isCanceled() {
290
                        return cancelMonitor.isCanceled();
291
                }
292

    
293
                public boolean isFinished() {
294
                        return finished;
295
                }
296
        }
297

    
298
}
299