Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / impl / intersection / IntersectionGeoprocess.java @ 4447

History | View | Annotate | Download (11 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 4447 2006-03-15 18:35:56Z azabala $
47
 * $Log$
48
 * Revision 1.5  2006-03-15 18:33:36  azabala
49
 * *** empty log message ***
50
 *
51
 * Revision 1.4  2006/03/14 18:32:46  fjp
52
 * Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
53
 *
54
 * Revision 1.3  2006/03/07 21:01:33  azabala
55
 * *** empty log message ***
56
 *
57
 * Revision 1.2  2006/03/06 19:48:39  azabala
58
 * *** empty log message ***
59
 *
60
 * Revision 1.1  2006/02/26 20:54:25  azabala
61
 * *** empty log message ***
62
 *
63
 *
64
 */
65
package com.iver.gvsig.geoprocessing.impl.intersection;
66

    
67
import java.util.Map;
68

    
69
import com.hardcode.gdbms.engine.data.driver.DriverException;
70
import com.iver.cit.gvsig.fmap.core.IFeature;
71
import com.iver.cit.gvsig.fmap.core.IGeometry;
72
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
73
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
74
import com.iver.cit.gvsig.fmap.edition.EditionException;
75
import com.iver.cit.gvsig.fmap.layers.FBitSet;
76
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
77
import com.iver.cit.gvsig.fmap.operations.CancellableMonitorable;
78
import com.iver.cit.gvsig.fmap.operations.DefaultCancellableMonitorable;
79
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
80
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
81
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
82
import com.iver.gvsig.geoprocessing.impl.AbstractGeoprocess;
83
import com.iver.gvsig.geoprocessing.impl.jtsprocessors.FeaturePersisterProcessor2;
84
import com.iver.gvsig.geoprocessing.model.GeoprocessException;
85
import com.iver.gvsig.geoprocessing.model.IOverlayGeoprocess;
86
import com.iver.gvsig.geoprocessing.schemabuilder.DefinitionUtils;
87
import com.iver.gvsig.geoprocessing.schemabuilder.XTypes;
88
import com.iver.utiles.swing.threads.IMonitorableTask;
89
/**
90
 * Computes intersection between two layers.
91
 * 
92
 * @author azabala
93
 *
94
 */
95
public class IntersectionGeoprocess extends AbstractGeoprocess
96
                                                                implements IOverlayGeoprocess {
97
        
98
        /**
99
         * overlay layer
100
         */
101
        private FLyrVect overlayLayer;
102

    
103
        /**
104
         * Schema of the result layer
105
         */
106
        private ILayerDefinition resultLayerDefinition;
107

    
108
        /**
109
         * flag to only clip selection of input layer
110
         */
111
        private boolean onlyFirstLayerSelection = false;
112

    
113
        /**
114
         * flag to only clip with selection of clipping layer
115
         */
116
        private boolean onlyClipLayerSelection = false;
117
        
118
        public IntersectionGeoprocess(FLyrVect inputLayer){
119
                setFirstOperand(inputLayer);
120
        }
121
        
122
        public void setSecondOperand(FLyrVect overlayLayer) {
123
                this.overlayLayer = overlayLayer;
124

    
125
        }
126

    
127
        public void setFirstOperand(FLyrVect firstLayer) {
128
                this.firstLayer = firstLayer;
129
        }
130

    
131
        public void setParameters(Map params) throws GeoprocessException {
132
                Boolean firstLayerSelection = (Boolean) params
133
                                .get("firstlayerselection");
134
                if (firstLayerSelection != null)
135
                        this.onlyFirstLayerSelection = firstLayerSelection.booleanValue();
136

    
137
                Boolean secondLayerSelection = (Boolean) params
138
                                .get("secondlayerselection");
139
                if (secondLayerSelection != null)
140
                        this.onlyClipLayerSelection = secondLayerSelection.booleanValue();
141

    
142
        }
143

    
144
        public void checkPreconditions() throws GeoprocessException {
145
                if (firstLayer == null)
146
                        throw new GeoprocessException("Interseccion: capa de entrada a null");
147
                if (overlayLayer == null)
148
                        throw new GeoprocessException("Interseccion: capa de union a null");
149
                if (this.writer == null || this.schemaManager == null) {
150
                        throw new GeoprocessException(
151
                                        "Operacion de interseccion sin especificar capa de resultados");
152
                }
153
                try {
154
                        if (firstLayer.getShapeType() != XTypes.POLYGON) {
155
                                throw new GeoprocessException(
156
                                                "Primera capa de interseccion no es de pol?gonos");
157
                        }
158
                        if (overlayLayer.getShapeType() != XTypes.POLYGON) {
159
                                throw new GeoprocessException(
160
                                                "Segunda capa de interseccion no es de pol?gonos");
161
                        }
162
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
163
                        throw new GeoprocessException(
164
                                        "Error al tratar de chequear si las capas a intersectar son de pol?gonos");
165
                }
166

    
167
        }
168

    
169
public void process() throws GeoprocessException {
170
        
171
                try {
172
                        //Prepare the result
173
                        this.schemaManager.createOrAlterSchema(createLayerDefinition());
174
                        writer.preProcess();
175
                        Strategy strategy = 
176
                                StrategyManager.getStrategy(firstLayer);
177
                        Strategy overlayStrategy = 
178
                                StrategyManager.getStrategy(overlayLayer);
179
                        FeaturePersisterProcessor2 featureProcessor =
180
                                new FeaturePersisterProcessor2(writer);
181
                        IntersectVisitor visitor = new IntersectVisitor(overlayLayer, 
182
                                                                                                                featureProcessor, 
183
                                                                                                                overlayStrategy,
184
                                                                                                                onlyClipLayerSelection);
185
                        if(this.onlyFirstLayerSelection){
186
                                strategy.process(visitor, 
187
                                        this.firstLayer.getRecordset().getSelection());
188
                        }else{
189
                                strategy.process(visitor);
190
                        }
191
                        
192
                } catch (EditionException e) {
193
                        throw new GeoprocessException("Error al crear el esquema/fichero de la nueva capa");
194
                } catch (DriverException e) {
195
                        throw new GeoprocessException("Error de driver al calcular el geoproceso interseccion");
196
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
197
                        throw new GeoprocessException("Error de driver al calcular el geoproceso interseccion");
198
                } catch (VisitException e) {
199
                        throw new GeoprocessException("Error al procesar el feature de una capa durante el geoproceso interseccion");
200
                }
201
        }        
202

    
203

    
204
        public void cancel() {
205
        }
206

    
207
        public ILayerDefinition createLayerDefinition() {
208
                if (resultLayerDefinition == null) {
209
                        try {
210
                                resultLayerDefinition = DefinitionUtils.mergeLayerDefinitions(
211
                                                firstLayer, overlayLayer);
212
                        } catch (Exception e) {
213
                                // TODO Quizas createLayerDefinition deberia lanzar
214
                                // una excepcion
215
                                e.printStackTrace();
216
                        }
217
                }
218
                return resultLayerDefinition;
219
        }
220

    
221
        public FBitSet getOverlays(IGeometry firstLayerGeometry, int index) {
222
                // TODO Auto-generated method stub
223
                return null;
224
        }
225

    
226
        public IFeature[] computeIntersection(IGeometry firsLayerGeometry,
227
                        int firstLayerFeatureIndex, IGeometry secondLayerGeometry,
228
                        int secondLayerFeatureIndex) {
229
                // TODO Auto-generated method stub
230
                return null;
231
        }
232

    
233
        public IMonitorableTask createTask() {
234
                try {
235
                        return new IntersectionMonitorableTask();
236
                } catch (DriverIOException e) {
237
                        return null;
238
                }
239
        }
240
        
241
        /**
242
         * IMonitorableTask that allows to run diff geoprocess in background,
243
         * with cancelation requests.
244
         * 
245
         * @author azabala
246
         * 
247
         */
248
        class IntersectionMonitorableTask implements IMonitorableTask {
249
                private CancellableMonitorable cancelMonitor = null;
250

    
251
                // FIXME INTERNACIONALIZAR ESTO
252
                String INTERSECTION_MESSAGE = "Computing intersections: ";
253

    
254
                private boolean finished = false;
255

    
256
                IntersectionMonitorableTask() throws DriverIOException {
257
                        initialize();
258
                }
259
                void initialize() throws DriverIOException {
260
                        cancelMonitor = createCancelMonitor();
261
                }
262

    
263
                private CancellableMonitorable createCancelMonitor()
264
                                throws DriverIOException {
265
                        DefaultCancellableMonitorable monitor = new 
266
                                                        DefaultCancellableMonitorable();
267
                        monitor.setInitialStep(0);
268
                        //Really its undeterminated, but  we must to process all
269
                        //elements of first layer (or selection) we are going to 
270
                        //consideer determinated
271
                        monitor.setDeterminatedProcess(true);
272
                        int numSteps = 0;
273
                        if (onlyFirstLayerSelection) {
274
                                FBitSet selection = firstLayer.getRecordset().getSelection();
275
                                numSteps = selection.cardinality();
276
                        } else {
277
                                numSteps = firstLayer.getSource().getShapeCount();
278
                        }
279
                        monitor.setFinalStep(numSteps);
280
                        return monitor;
281
                }
282

    
283
                public int getInitialStep() {
284
                        return cancelMonitor.getInitialStep();
285
                }
286

    
287
                public int getFinishStep() {
288
                        return cancelMonitor.getFinalStep();
289
                }
290

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

    
295
                public String getStatusMessage() {
296
                        // FIXME Cambiar esto por un mecanismo de eventos,
297
                        // de forma que la tarea lo que tenga sea un escuchador
298
                        // que cambie el mensaje de estado segun los eventos
299
                        // de tareas que se est?n realizando
300
                        return "Intersection Geoprocess...";
301
                }
302

    
303
                public String getNote() {
304
                        // FIXME Cambiar esto por un mecanismo de eventos,
305
                        // de forma que la tarea lo que tenga sea un escuchador
306
                        // que cambie el mensaje de estado segun los eventos
307
                        // de tareas que se est?n realizando
308
                        return INTERSECTION_MESSAGE + getCurrentStep() + "of " + getFinishStep();
309
                }
310

    
311
                public void cancel() {
312
                        ((DefaultCancellableMonitorable) cancelMonitor).setCanceled(true);
313
                }
314

    
315
                public void run() throws GeoprocessException {
316

    
317
                        try {
318
                                //Prepare the result
319
                                schemaManager.createOrAlterSchema(createLayerDefinition());
320
                                writer.preProcess();
321
                                Strategy strategy = 
322
                                        StrategyManager.getStrategy(firstLayer);
323
                                Strategy overlayStrategy = 
324
                                        StrategyManager.getStrategy(overlayLayer);
325
                                FeaturePersisterProcessor2 featureProcessor =
326
                                        new FeaturePersisterProcessor2(writer);
327
                                IntersectVisitor visitor = new IntersectVisitor(overlayLayer, 
328
                                                                                                                        featureProcessor, 
329
                                                                                                                        overlayStrategy,
330
                                                                                                                        onlyClipLayerSelection);
331
                                if(onlyFirstLayerSelection){
332
                                        strategy.process(visitor, 
333
                                                firstLayer.getRecordset().getSelection(),
334
                                                cancelMonitor);
335
                                }else{
336
                                        strategy.process(visitor, cancelMonitor);
337
                                }
338
                                
339
                        } catch (EditionException e) {
340
                                throw new GeoprocessException("Error al crear el esquema/fichero de la nueva capa");
341
                        } catch (DriverException e) {
342
                                throw new GeoprocessException("Error de driver al calcular el geoproceso interseccion");
343
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
344
                                throw new GeoprocessException("Error de driver al calcular el geoproceso interseccion");
345
                        } catch (VisitException e) {
346
                                throw new GeoprocessException("Error al procesar el feature de una capa durante el geoproceso interseccion");
347
                        }finally{
348
                                finished = true;
349
                        }
350
                }
351

    
352
                public boolean isDefined() {
353
                        return cancelMonitor.isDeterminatedProcess();
354
                }
355

    
356
                public boolean isCanceled() {
357
                        return cancelMonitor.isCanceled();
358
                }
359

    
360
                public boolean isFinished() {
361
                        return finished;
362
                }
363
        }
364

    
365
}