Statistics
| Revision:

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

History | View | Annotate | Download (9.16 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 10626 2007-03-06 16:55:54Z caballero $
47
* $Log$
48
* Revision 1.4  2007-03-06 16:47:58  caballero
49
* Exceptions
50
*
51
* Revision 1.3  2006/07/26 17:22:23  azabala
52
* createTask and process method are consistent now
53
*
54
* Revision 1.2  2006/06/29 07:33:57  fjp
55
* Cambios ISchemaManager y IFieldManager por terminar
56
*
57
* Revision 1.1  2006/06/20 18:20:45  azabala
58
* first version in cvs
59
*
60
* Revision 1.1  2006/05/24 21:10:15  azabala
61
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
62
*
63
* Revision 1.6  2006/05/01 19:09:46  azabala
64
* 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)
65
*
66
* Revision 1.5  2006/03/23 21:05:11  azabala
67
* *** empty log message ***
68
*
69
* Revision 1.4  2006/03/17 19:53:34  azabala
70
* *** empty log message ***
71
*
72
* Revision 1.3  2006/03/15 18:34:16  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.2  2006/03/14 18:32:46  fjp
76
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
77
*
78
* Revision 1.1  2006/03/05 19:59:16  azabala
79
* *** empty log message ***
80
*
81
*
82
*/
83
package com.iver.cit.gvsig.geoprocess.impl.merge.fmap;
84

    
85
import java.util.Map;
86

    
87
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
88
import com.hardcode.gdbms.driver.exceptions.SchemaEditionException;
89
import com.iver.andami.PluginServices;
90
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
91
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
92
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
93
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
94
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
95
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
96
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
97
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
98
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
99
import com.iver.cit.gvsig.geoprocess.core.fmap.DeferredFeaturePersisterProcessor;
100
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
101
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
102
import com.iver.utiles.swing.threads.CancellableMonitorable;
103
import com.iver.utiles.swing.threads.DefaultCancellableMonitorable;
104
import com.iver.utiles.swing.threads.IMonitorableTask;
105
/**
106
 * This geoprocess is "equivalent" to UNION relational operator.
107
 * Output layer will have a geometry for each geometry of input layer.
108
 *
109
 * Also, output layer will preserve a schema of one of input layers.
110
 * @author azabala
111
 *
112
 * FIXME Hereda de AbstractGeoprocess firstLayer. REVISAR
113
 *
114
 */
115
public class MergeGeoprocess extends AbstractGeoprocess {
116
        /**
117
         * Input layers we are going to merge.
118
         */
119
        private FLyrVect[] inputLayers;
120
        /**
121
         * Input layer whose schema will retain result layer
122
         */
123
        private FLyrVect outputSchemaLayer;
124
        private ILayerDefinition resultLayerDefinition;
125

    
126
        public MergeGeoprocess(){
127

    
128
        }
129

    
130
        public void setParameters(Map params) throws GeoprocessException {
131
        }
132

    
133
        public void checkPreconditions() throws GeoprocessException {
134
                if(inputLayers == null)
135
                        throw new GeoprocessException("No se han especificado las capas a juntar en un merge");
136
                if(outputSchemaLayer == null)
137
                        throw new GeoprocessException("Merge: no se ha especificado que capa determinara los atributos");
138
                int shapeType = -1;
139
                try {
140
                        shapeType = inputLayers[0].getShapeType();
141
                        for(int i = 0; i < inputLayers.length; i++){
142
                                if(inputLayers[i] == null)
143
                                        throw new GeoprocessException("Merge: la capa "+i+" no est? definida -null");
144
                                if(inputLayers[i].getShapeType() != shapeType)
145
                                        throw new GeoprocessException("Las capas a juntar deben tener el mismo tipo de geometria");
146
                        }
147
                } catch (ReadDriverException e) {
148
                        throw new GeoprocessException("Error al tratar de determinar el tipo de geometria de las capas a mezclar");
149
                }
150
        }
151

    
152
        public void process() throws GeoprocessException {
153
                try {
154
                        createTask().run();
155
                } catch (Exception e) {
156
                        throw new GeoprocessException("Error al ejecutar el geoproceso merge");
157
                }
158
        }
159

    
160
        public void cancel() {
161
                try {
162
                        schemaManager.removeSchema("");
163
                } catch (SchemaEditionException e) {
164
                        // TODO Auto-generated catch block
165
                        e.printStackTrace();
166
                }
167
        }
168

    
169
        public ILayerDefinition createLayerDefinition() {
170
                if(this.resultLayerDefinition == null)
171
                {
172
                        try {
173
                                this.resultLayerDefinition =
174
                                        DefinitionUtils.
175
                                        createLayerDefinition(this.outputSchemaLayer);
176
                        } catch (Exception e) {
177
                                return null;
178
                        }
179
                }
180
                return this.resultLayerDefinition;
181
        }
182

    
183
        public void setInputLayers(FLyrVect[] inputLayers) {
184
                this.inputLayers = inputLayers;
185
                if(inputLayers != null && inputLayers.length > 0)
186
                        firstLayer = inputLayers[0];
187
        }
188

    
189
        public void setOutputSchemaLayer(FLyrVect outputSchemaLayer) {
190
                this.outputSchemaLayer = outputSchemaLayer;
191
        }
192

    
193
        public IMonitorableTask createTask() {
194
                        try {
195
                                return new MergeMonitorableTask();
196
                        } catch (ReadDriverException e) {
197
                                return null;
198
                        }
199
        }
200

    
201
        class MergeMonitorableTask implements IMonitorableTask {
202
                private CancellableMonitorable cancelMonitor = null;
203
                String MERGE_MESSAGE = PluginServices.getText(this, "Mensaje_juntar");
204
                String MERGE_NOTE = PluginServices.getText(this, "Mensaje_procesando_juntar");
205
                String OF = PluginServices.getText(this, "De");
206
                private boolean finished = false;
207

    
208
                MergeMonitorableTask() throws ReadDriverException {
209
                        initialize();
210
                }
211
                void initialize() throws ReadDriverException {
212
                        cancelMonitor = createCancelMonitor();
213
                }
214

    
215
                private CancellableMonitorable createCancelMonitor() throws ReadDriverException {
216
                        DefaultCancellableMonitorable monitor = new
217
                                                        DefaultCancellableMonitorable();
218
                        monitor.setInitialStep(0);
219
                        monitor.setDeterminatedProcess(true);
220
                        int numSteps = 0;
221
                        for(int i = 0; i < inputLayers.length; i++){
222
                                numSteps += inputLayers[i].getSource().getShapeCount();
223
                        }
224
                        monitor.setFinalStep(numSteps);
225
                        return monitor;
226
                }
227

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

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

    
236
                public int getCurrentStep() {
237
                        return cancelMonitor.getCurrentStep();
238
                }
239

    
240
                public String getStatusMessage() {
241
                        return MERGE_MESSAGE;
242
                }
243

    
244
                public String getNote() {
245
                        return MERGE_NOTE + " " +
246
                        getCurrentStep() + " "+
247
                        OF + " " + getFinishStep();
248
                }
249

    
250
                public void cancel() {
251
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
252
                        MergeGeoprocess.this.cancel();
253
                }
254

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

    
290
                }
291

    
292
                public boolean isDefined() {
293
                        return cancelMonitor.isDeterminatedProcess();
294
                }
295

    
296
                public boolean isCanceled() {
297
                        return cancelMonitor.isCanceled();
298
                }
299

    
300
                public boolean isFinished() {
301
                        return finished;
302
                }
303
        }
304

    
305
}
306