Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / intersection / fmap / IntersectionGeoprocess.java @ 13874

History | View | Annotate | Download (11.3 KB)

1
/*
2
 * Created on 21-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: IntersectionGeoprocess.java 13874 2007-09-19 16:12:18Z jaume $
47
 * $Log$
48
 * Revision 1.6  2007-09-19 16:07:28  jaume
49
 * ReadExpansionFileException removed from this context
50
 *
51
 * Revision 1.5  2007/08/07 16:07:06  azabala
52
 * centrilizing JTS in JTSFacade and allowing all geometry types (not only Polygon)
53
 *
54
 * Revision 1.4  2007/05/15 07:24:19  cesar
55
 * Add the finished method for execution from Event Dispatch Thread
56
 *
57
 * Revision 1.3  2007/03/06 16:47:58  caballero
58
 * Exceptions
59
 *
60
 * Revision 1.2  2006/06/29 07:33:57  fjp
61
 * Cambios ISchemaManager y IFieldManager por terminar
62
 *
63
 * Revision 1.1  2006/06/20 18:20:45  azabala
64
 * first version in cvs
65
 *
66
 * Revision 1.2  2006/06/08 18:25:20  azabala
67
 * modificaciones para admitir capas de shapeType MULTI
68
 *
69
 * Revision 1.1  2006/05/24 21:10:40  azabala
70
 * primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
71
 *
72
 * Revision 1.9  2006/05/01 19:14:06  azabala
73
 * 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)
74
 *
75
 * Revision 1.8  2006/03/23 21:04:36  azabala
76
 * *** empty log message ***
77
 *
78
 * Revision 1.7  2006/03/21 19:29:18  azabala
79
 * *** empty log message ***
80
 *
81
 * Revision 1.6  2006/03/17 19:53:22  azabala
82
 * *** empty log message ***
83
 *
84
 * Revision 1.5  2006/03/15 18:33:36  azabala
85
 * *** empty log message ***
86
 *
87
 * Revision 1.4  2006/03/14 18:32:46  fjp
88
 * Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
89
 *
90
 * Revision 1.3  2006/03/07 21:01:33  azabala
91
 * *** empty log message ***
92
 *
93
 * Revision 1.2  2006/03/06 19:48:39  azabala
94
 * *** empty log message ***
95
 *
96
 * Revision 1.1  2006/02/26 20:54:25  azabala
97
 * *** empty log message ***
98
 *
99
 *
100
 */
101
package com.iver.cit.gvsig.geoprocess.impl.intersection.fmap;
102

    
103
import java.util.Map;
104

    
105
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
106
import com.hardcode.gdbms.driver.exceptions.SchemaEditionException;
107
import com.iver.andami.PluginServices;
108
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
109
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
110
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
111
import com.iver.cit.gvsig.fmap.core.FShape;
112
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
113
import com.iver.cit.gvsig.fmap.layers.FBitSet;
114
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
115
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
116
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
117
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
118
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
119
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
120
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
121
import com.iver.cit.gvsig.geoprocess.core.fmap.IOverlayGeoprocess;
122
import com.iver.utiles.swing.threads.CancellableMonitorable;
123
import com.iver.utiles.swing.threads.DefaultCancellableMonitorable;
124
import com.iver.utiles.swing.threads.IMonitorableTask;
125
/**
126
 * Computes intersection between two layers.
127
 *
128
 * @author azabala
129
 *
130
 */
131
public class IntersectionGeoprocess extends AbstractGeoprocess
132
                                                                implements IOverlayGeoprocess {
133

    
134
        /**
135
         * overlay layer
136
         */
137
        private FLyrVect overlayLayer;
138

    
139
        /**
140
         * Schema of the result layer
141
         */
142
        private ILayerDefinition resultLayerDefinition;
143

    
144
        /**
145
         * flag to only clip selection of input layer
146
         */
147
        private boolean onlyFirstLayerSelection = false;
148

    
149
        /**
150
         * flag to only clip with selection of clipping layer
151
         */
152
        private boolean onlyClipLayerSelection = false;
153

    
154
        public IntersectionGeoprocess(FLyrVect inputLayer){
155
                setFirstOperand(inputLayer);
156
        }
157

    
158
        public void setSecondOperand(FLyrVect overlayLayer) {
159
                this.overlayLayer = overlayLayer;
160

    
161
        }
162

    
163
        public void setFirstOperand(FLyrVect firstLayer) {
164
                this.firstLayer = firstLayer;
165
        }
166

    
167
        public void setParameters(Map params) throws GeoprocessException {
168
                Boolean firstLayerSelection = (Boolean) params
169
                                .get("firstlayerselection");
170
                if (firstLayerSelection != null)
171
                        this.onlyFirstLayerSelection = firstLayerSelection.booleanValue();
172

    
173
                Boolean secondLayerSelection = (Boolean) params
174
                                .get("secondlayerselection");
175
                if (secondLayerSelection != null)
176
                        this.onlyClipLayerSelection = secondLayerSelection.booleanValue();
177

    
178
        }
179

    
180
        public void checkPreconditions() throws GeoprocessException {
181
                if (firstLayer == null)
182
                        throw new GeoprocessException("Interseccion: capa de entrada a null");
183
                if (overlayLayer == null)
184
                        throw new GeoprocessException("Interseccion: capa de union a null");
185
                if (this.writer == null || this.schemaManager == null) {
186
                        throw new GeoprocessException(
187
                                        "Operacion de interseccion sin especificar capa de resultados");
188
                }
189
                /*azabala: interseccion con cualquier tipo de capa
190
                try {
191
                        if ((firstLayer.getShapeType() != XTypes.POLYGON) &&
192
(firstLayer.getShapeType() != XTypes.MULTI)) {
193
                                throw new GeoprocessException(
194
                                                "Primera capa de interseccion no es de pol?gonos");
195
                        }
196
                        if ((overlayLayer.getShapeType() != XTypes.POLYGON) &&
197
(overlayLayer.getShapeType() != XTypes.MULTI))  {
198
                                throw new GeoprocessException(
199
                                                "Segunda capa de interseccion no es de pol?gonos");
200
                        }
201
                } catch (ReadDriverException e) {
202
                        throw new GeoprocessException(
203
                                        "Error al tratar de chequear si las capas a intersectar son de pol?gonos");
204
                }
205
                */
206

    
207
        }
208

    
209
        public void process() throws GeoprocessException {
210
                try {
211
                        new IntersectionMonitorableTask().run();
212
                } catch (ReadDriverException e) {
213
                        throw new GeoprocessException("Error de acceso a driver durante geoproceso interseccion");
214
                } 
215
        }
216

    
217

    
218
        public void cancel() {
219
                try {
220
                        schemaManager.removeSchema("");
221
                } catch (SchemaEditionException e) {
222
                        // TODO Auto-generated catch block
223
                        e.printStackTrace();
224
                }
225
        }
226

    
227
        public ILayerDefinition createLayerDefinition() {
228
                if (resultLayerDefinition == null) {
229
                        try {
230
                                resultLayerDefinition = DefinitionUtils.mergeLayerDefinitions(
231
                                                firstLayer, overlayLayer);
232
                                resultLayerDefinition.setShapeType(FShape.MULTI);
233
                        } catch (Exception e) {
234
                                // TODO Quizas createLayerDefinition deberia lanzar
235
                                // una excepcion
236
                                e.printStackTrace();
237
                        }
238
                }
239
                return resultLayerDefinition;
240
        }
241

    
242

    
243
        public IMonitorableTask createTask() {
244
                try {
245
                        return new IntersectionMonitorableTask();
246
                } catch (Exception e) {
247
                        return null;
248
                }
249
        }
250

    
251
        /**
252
         * IMonitorableTask that allows to run intersection geoprocess in background,
253
         * with cancelation requests.
254
         *
255
         * @author azabala
256
         *
257
         */
258
        class IntersectionMonitorableTask implements IMonitorableTask {
259
                private CancellableMonitorable cancelMonitor = null;
260
                String INTERSECTION_MESSAGE = PluginServices.getText(this, "Mensaje_interseccion");
261
                String INTERSECTION_NOTE = PluginServices.getText(this, "Mensaje_procesando_interseccion");
262
                String OF = PluginServices.getText(this, "De");
263
                private boolean finished = false;
264

    
265
                IntersectionMonitorableTask() throws ReadDriverException  {
266
                        initialize();
267
                }
268
                void initialize() throws ReadDriverException {
269
                        cancelMonitor = createCancelMonitor();
270
                }
271

    
272
                private CancellableMonitorable createCancelMonitor() throws ReadDriverException {
273
                        DefaultCancellableMonitorable monitor = new
274
                                                        DefaultCancellableMonitorable();
275
                        monitor.setInitialStep(0);
276
                        //Really its undeterminated, but  we must to process all
277
                        //elements of first layer (or selection) we are going to
278
                        //consideer determinated
279
                        monitor.setDeterminatedProcess(true);
280
                        int numSteps = 0;
281
                        if (onlyFirstLayerSelection) {
282
                                FBitSet selection = firstLayer.getRecordset().getSelection();
283
                                numSteps = selection.cardinality();
284
                        } else {
285
                                numSteps = firstLayer.getSource().getShapeCount();
286
                        }
287
                        monitor.setFinalStep(numSteps);
288
                        return monitor;
289
                }
290

    
291
                public int getInitialStep() {
292
                        return cancelMonitor.getInitialStep();
293
                }
294

    
295
                public int getFinishStep() {
296
                        return cancelMonitor.getFinalStep();
297
                }
298

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

    
303
                public String getStatusMessage() {
304
                        return INTERSECTION_MESSAGE;
305
                }
306

    
307
                public String getNote() {
308
                        return INTERSECTION_NOTE + " " +
309
                        getCurrentStep() + " " +
310
                        OF + " "
311
                        + getFinishStep();
312
                }
313

    
314
                public void cancel() {
315
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
316
                        //This does this geoprocess athomic. In this
317
                        //call we remove result files
318
                        IntersectionGeoprocess.this.cancel();
319
                }
320

    
321
                public void run() throws GeoprocessException {
322

    
323
                        try {
324
                                //Prepare the result
325
                                schemaManager.createSchema(createLayerDefinition());
326
                                writer.preProcess();
327
                                Strategy strategy =
328
                                        StrategyManager.getStrategy(firstLayer);
329
                                Strategy overlayStrategy =
330
                                        StrategyManager.getStrategy(overlayLayer);
331
                                FeaturePersisterProcessor2 featureProcessor =
332
                                        new FeaturePersisterProcessor2(writer);
333
                                IntersectVisitor visitor = new IntersectVisitor(overlayLayer,
334
                                                                                                                        featureProcessor,
335
                                                                                                                        overlayStrategy,
336
                                                                                                                        onlyClipLayerSelection);
337
                                if(onlyFirstLayerSelection){
338
                                        strategy.process(visitor,
339
                                                firstLayer.getRecordset().getSelection(),
340
                                                cancelMonitor);
341
                                }else{
342
                                        strategy.process(visitor, cancelMonitor);
343
                                }
344

    
345
                        } catch (ReadDriverException e) {
346
                                throw new GeoprocessException("Error de driver al calcular el geoproceso interseccion");
347
                        } catch (ProcessVisitorException e) {
348
                                throw new GeoprocessException("Error al procesar el feature de una capa durante el geoproceso interseccion");
349
                        } catch (VisitorException e) {
350
                                throw new GeoprocessException("Error de driver al calcular el geoproceso interseccion");
351
                        } catch (SchemaEditionException e) {
352
                                throw new GeoprocessException("Error al crear el esquema/fichero de la nueva capa");
353

    
354
                        }finally{
355
                                finished = true;
356
                        }
357
                }
358

    
359
                public boolean isDefined() {
360
                        return cancelMonitor.isDeterminatedProcess();
361
                }
362

    
363
                public boolean isCanceled() {
364
                        return cancelMonitor.isCanceled();
365
                }
366

    
367
                public boolean isFinished() {
368
                        return finished;
369
                }
370
                /* (non-Javadoc)
371
                 * @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
372
                 */
373
                public void finished() {
374
                        // TODO Auto-generated method stub
375
                        
376
                }
377
        }
378

    
379
}