Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / topology / lineclean / fmap / LineCleanGeoprocess.java @ 8712

History | View | Annotate | Download (10.6 KB)

1
/*
2
 * Created on 10-oct-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: LineCleanGeoprocess.java 8712 2006-11-13 20:41:08Z azabala $
47
* $Log$
48
* Revision 1.6  2006-11-13 20:41:08  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.5  2006/11/10 13:22:57  azabala
52
* better syncronization of clean and build network (use of pipetask)
53
*
54
* Revision 1.4  2006/11/09 21:08:32  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.3  2006/10/19 16:06:48  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.2  2006/10/17 18:25:53  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.1  2006/10/10 18:50:17  azabala
64
* First version in CVS
65
*
66
*
67
*/
68
package com.iver.cit.gvsig.topology.lineclean.fmap;
69

    
70
import java.io.File;
71
import java.io.IOException;
72
import java.util.Map;
73

    
74
import com.iver.andami.PluginServices;
75
import com.iver.cit.gvsig.fmap.DriverException;
76
import com.iver.cit.gvsig.fmap.MapContext;
77
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
78
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
79
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
80
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
81
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
82
import com.iver.cit.gvsig.fmap.edition.EditionException;
83
import com.iver.cit.gvsig.fmap.edition.IWriter;
84
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
85
import com.iver.cit.gvsig.fmap.edition.writers.shp.MultiShpWriter;
86
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
87
import com.iver.cit.gvsig.fmap.layers.FBitSet;
88
import com.iver.cit.gvsig.fmap.layers.FLayer;
89
import com.iver.cit.gvsig.fmap.layers.FLayers;
90
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
91
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
92
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
93
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
94
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
95
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
96
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
97
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
98
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
99
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
100
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
101
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
102
import com.iver.cit.gvsig.project.documents.view.gui.View;
103
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
104
import com.iver.utiles.swing.threads.Cancellable;
105
import com.iver.utiles.swing.threads.IMonitorableTask;
106
import com.iver.utiles.swing.threads.IPipedTask;
107

    
108
public class LineCleanGeoprocess extends AbstractGeoprocess {
109

    
110
        /**
111
         * Schema of the result layer
112
         */
113
        private ILayerDefinition resultLayerDefinition;
114

    
115
        
116
        /**
117
         * flag to only clip selection of input layer
118
         */
119
        private boolean onlyFirstLayerSelection = false;
120
        
121
        /**
122
         * Processes features (writing them)
123
         */
124
        FeaturePersisterProcessor2 processor;
125

    
126
        /**
127
         * Writer to save in a temporal layer intersections
128
         */
129
        private IWriter intersectionsWriter;
130
        FeaturePersisterProcessor2 intersectsProcessor;
131
        
132
        
133
        public LineCleanGeoprocess(FLyrVect inputLayer){
134
                this.firstLayer = inputLayer;
135
        }
136
        
137
        
138
        public void setParameters(Map params) throws GeoprocessException {
139
                Boolean firstLayerSelection = (Boolean) params.get("firstlayerselection");
140
                if (firstLayerSelection != null)
141
                        this.onlyFirstLayerSelection = 
142
                                firstLayerSelection.booleanValue();
143

    
144
        }
145

    
146
        public void checkPreconditions() throws GeoprocessException {
147
                if (firstLayer == null)
148
                        throw new GeoprocessException("CLEAN: capa de entrada a null");
149
                if (this.writer == null || this.schemaManager == null) {
150
                        throw new GeoprocessException(
151
                                        "Operacion de CLEAN sin especificar capa de resultados");
152
                }
153
                try {
154
                        if(firstLayer.getSource().getShapeCount() == 0){
155
                                throw new GeoprocessException(
156
                                "Capa de entrada vacia");
157
                        }
158
                } catch (DriverIOException e) {
159
                        throw new GeoprocessException(
160
                        "Error al verificar si la capa est? vac?a");
161
                } 
162
        }
163

    
164
        public void process() throws GeoprocessException {
165
                try {
166
                        createTask().run();
167
                } catch (Exception e) {
168
                        throw new GeoprocessException("Error al ejecutar el geoproceso");
169
                }
170
        }
171

    
172
        public ILayerDefinition createLayerDefinition() {
173
                if (resultLayerDefinition == null) {
174
                        try {
175
                                resultLayerDefinition = DefinitionUtils.
176
                                                        createLayerDefinition(firstLayer);
177
                        } catch (Exception e) {
178
                                e.printStackTrace();
179
                        }
180
                }
181
                return resultLayerDefinition;
182
        }
183
        
184
        
185
        class LineCleanTask extends AbstractMonitorableTask implements IPipedTask{
186

    
187
                private LineCleanTask() {
188
                        setInitialStep(0);
189
                        try {
190
                                if (onlyFirstLayerSelection) {
191
                                        int numSelected = firstLayer.getRecordset().getSelection()
192
                                                        .cardinality();
193
                                        setFinalStep(numSelected);
194
                                } else {
195
                                        int numShapes = firstLayer.getSource().getShapeCount();
196
                                        setFinalStep(numShapes);
197
                                }// else
198
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
199
                                // TODO Auto-generated catch block
200
                                e.printStackTrace();
201
                        } catch (DriverIOException e) {
202
                                // TODO Auto-generated catch block
203
                                e.printStackTrace();
204
                        }
205
                        setDeterminatedProcess(true);
206
                        setStatusMessage(PluginServices.getText(this,
207
                                        "LineClean._Progress_Message"));
208

    
209
                }
210
        
211
                /**
212
                 * Verifies cancelation events, and return a boolean flag if processes must
213
                 * be stopped for this cancelations events.
214
                 * 
215
                 * @param cancel
216
                 * @param va
217
                 * @param visitor
218
                 * @return
219
                 * @throws DriverIOException
220
                 */
221
                protected boolean verifyCancelation(ReadableVectorial va) {
222
                        if (isCanceled()) {
223
                                try {
224
                                        va.stop();
225
                                } finally {
226
                                        return true;
227
                                }
228
                        }
229
                        return false;
230
                }
231
                
232
                
233
                public void run() throws Exception {
234
                        processor =
235
                                new FeaturePersisterProcessor2(writer);
236
                        
237
                        intersectionsWriter = new ShpWriter();
238
                        String temp = System.getProperty("java.io.tmpdir") + 
239
                                        "/intersections_" + 
240
                                        System.currentTimeMillis() +
241
                                        ".shp";
242
                        File newFile = new File(temp);
243
                        ((ShpWriter) intersectionsWriter).setFile(newFile);
244
                        
245
                        ILayerDefinition intersectDefinition = new SHPLayerDefinition();
246
                        intersectDefinition.setShapeType(XTypes.POINT);
247
                        FieldDescription[] intersectFields = new FieldDescription[2];
248
                        intersectFields[0] = new FieldDescription();
249
                        intersectFields[0].setFieldLength(10);
250
                        intersectFields[0].setFieldDecimalCount(0);
251
                        intersectFields[0].setFieldName("FID1");
252
                        intersectFields[0].setFieldType(XTypes.INTEGER);
253
                        intersectFields[1] = new FieldDescription();
254
                        intersectFields[1].setFieldLength(10);
255
                        intersectFields[1].setFieldDecimalCount(0);
256
                        intersectFields[1].setFieldName("FID2");
257
                        intersectFields[1].setFieldType(XTypes.INTEGER);
258
                        intersectDefinition.setFieldsDesc(intersectFields);
259
                        
260
                        ((ShpWriter) intersectionsWriter).initialize(
261
                                        (LayerDefinition) intersectDefinition);
262
                        ((SHPLayerDefinition) intersectDefinition).setFile(newFile);
263
                        
264
                        ShpSchemaManager interSchMg = 
265
                                new ShpSchemaManager(newFile.getAbsolutePath());
266
                        interSchMg.createSchema(intersectDefinition);
267
                        
268
                        intersectsProcessor = new 
269
                                FeaturePersisterProcessor2(intersectionsWriter);
270
                        
271
                        FBitSet selection = null;
272
                        LineCleanVisitor visitor = 
273
                                new LineCleanVisitor(processor,
274
                                                             intersectsProcessor,
275
                                                onlyFirstLayerSelection, 
276
                                                resultLayerDefinition,
277
                                                intersectDefinition,
278
                                                firstLayer,
279
                                                firstLayer.getRecordset());
280
                        
281
                        try {
282
                                processor.start();
283
                                intersectsProcessor.start();
284
                                
285
                                ReadableVectorial va = firstLayer.getSource();
286
                                va.start();
287
                                for (int i = 0; i < va.getShapeCount(); i++) {// for each geometry
288
                                        if (verifyCancelation(va)) {
289
                                                intersectsProcessor.finish();
290
                                                return;
291
                                        }
292
                                        if(selection != null){
293
                                                if (selection.get(i)) {
294
                                                                reportStep();
295
                                                                visitor.visit(va.getShape(i), i);
296
                                                }
297
                                                        
298
                                        }else{
299
                                                reportStep();
300
                                                visitor.visit(va.getShape(i), i);
301
                                        }
302
                                }// for
303
                                va.stop();
304
                                processor.finish();
305
                                intersectsProcessor.finish();
306
                                
307
                                
308
                        } catch (DriverIOException e) {
309
                                e.printStackTrace();
310
                        }
311
                }
312

    
313
                // TODO INTERNACIONALIZAR LOS MENSAJES
314
                public String getNote() {
315
                        return "Limpiando l?neas..." + " " + getCurrentStep() + " "
316
                                        + "de" + " " + getFinishStep();
317
                }
318

    
319
                public void cancel() {
320
                        setCanceled(true);
321
                        LineCleanGeoprocess.this.cancel();
322
                }
323

    
324
                /* (non-Javadoc)
325
                 * @see com.iver.utiles.swing.threads.IPipedTask#getResult()
326
                 */
327
                public Object getResult() {
328
                        try {
329
                                return LineCleanGeoprocess.this.getResult();
330
                        } catch (GeoprocessException e) {
331
                                return null;
332
                        }
333
                }
334

    
335
                /* (non-Javadoc)
336
                 * @see com.iver.utiles.swing.threads.IPipedTask#setEntry(java.lang.Object)
337
                 */
338
                public void setEntry(Object object) {
339
                        // TODO Auto-generated method stub
340
                        
341
                }
342
        }
343

    
344
        
345
        public IMonitorableTask createTask() {
346
                return new LineCleanTask();
347
        }
348
        
349
        
350
        public FLayer getResult() throws GeoprocessException {
351
                
352
                FLyrVect cleanedLayer = (FLyrVect) createLayerFrom(this.writer);
353
                FLyrVect pseudoNodes = (FLyrVect) createLayerFrom(this.intersectionsWriter);
354
                try {
355
                        if(pseudoNodes.getSource().getShapeCount() == 0){
356
                                return cleanedLayer;
357
                        }else{
358
                                MapContext map = ((View)PluginServices.
359
                                                getMDIManager().
360
                                                getActiveWindow()).
361
                                                getModel().
362
                                                getMapContext();
363
                                FLayers solution = new FLayers(map, null);
364
                                solution.setName(this.firstLayer.getName()+"_cleaned");
365
                                solution.addLayer(cleanedLayer);
366
                                solution.addLayer(pseudoNodes);
367
                                return solution;
368
                        }
369
                } catch (DriverIOException e) {
370
                        throw new GeoprocessException("Error de lectura de datos");
371
                }
372
                
373
                
374
                
375
                        
376
        }
377

    
378
}
379