Statistics
| Revision:

root / trunk / extensions / extGeoprocessingExtensions / src / com / iver / cit / gvsig / geoprocess / impl / topology / lineclean / fmap / LineCleanGeoprocess.java @ 24094

History | View | Annotate | Download (11.4 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 24094 2008-10-19 07:44:04Z azabala $
47
* $Log$
48
* Revision 1.4  2007-07-12 11:10:24  azabala
49
* bug 2617 solved (clean fails with multilinestring geometries)
50
*
51
* Revision 1.3  2007/05/15 07:23:26  cesar
52
* Add the finished method for execution from Event Dispatch Thread
53
*
54
* Revision 1.2  2007/03/06 16:48:14  caballero
55
* Exceptions
56
*
57
* Revision 1.1  2006/12/21 17:23:27  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.1  2006/12/04 19:42:23  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.8  2006/11/14 18:34:16  azabala
64
* *** empty log message ***
65
*
66
* Revision 1.7  2006/11/14 18:00:57  azabala
67
* internationalized texts
68
*
69
* Revision 1.6  2006/11/13 20:41:08  azabala
70
* *** empty log message ***
71
*
72
* Revision 1.5  2006/11/10 13:22:57  azabala
73
* better syncronization of clean and build network (use of pipetask)
74
*
75
* Revision 1.4  2006/11/09 21:08:32  azabala
76
* *** empty log message ***
77
*
78
* Revision 1.3  2006/10/19 16:06:48  azabala
79
* *** empty log message ***
80
*
81
* Revision 1.2  2006/10/17 18:25:53  azabala
82
* *** empty log message ***
83
*
84
* Revision 1.1  2006/10/10 18:50:17  azabala
85
* First version in CVS
86
*
87
*
88
*/
89
package com.iver.cit.gvsig.geoprocess.impl.topology.lineclean.fmap;
90

    
91
import java.io.File;
92
import java.util.Map;
93

    
94
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
95
import com.iver.andami.PluginServices;
96
import com.iver.cit.gvsig.fmap.MapContext;
97
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
98
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
99
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
100
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
101
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
102
import com.iver.cit.gvsig.fmap.edition.IWriter;
103
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
104
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
105
import com.iver.cit.gvsig.fmap.layers.FBitSet;
106
import com.iver.cit.gvsig.fmap.layers.FLayer;
107
import com.iver.cit.gvsig.fmap.layers.FLayers;
108
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
109
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
110
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
111
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
112
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
113
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
114
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
115
import com.iver.cit.gvsig.project.documents.view.gui.View;
116
import com.iver.cit.gvsig.util.SnappingCoordinateMap;
117
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
118
import com.iver.utiles.swing.threads.IMonitorableTask;
119
import com.iver.utiles.swing.threads.IPipedTask;
120

    
121
public class LineCleanGeoprocess extends AbstractGeoprocess {
122

    
123
        /**
124
         * Schema of the result layer
125
         */
126
        private ILayerDefinition resultLayerDefinition;
127

    
128

    
129
        /**
130
         * flag to only clip selection of input layer
131
         */
132
        private boolean onlyFirstLayerSelection = false;
133
        
134
        /**
135
         * flag to mark if must create a layer with the detected
136
         * pseudonodes
137
         */
138
        private boolean createLyrsWithErrorGeometries = false;
139

    
140
        /**
141
         * Processes features (writing them)
142
         */
143
        FeaturePersisterProcessor2 processor;
144

    
145
        /**
146
         * Writer to save in a temporal layer intersections
147
         */
148
        private IWriter intersectionsWriter;
149
        /**
150
         * Processor that saves created features using the IWriter
151
         */
152
        FeaturePersisterProcessor2 intersectsProcessor;
153

    
154

    
155
        public LineCleanGeoprocess(FLyrVect inputLayer){
156
                this.firstLayer = inputLayer;
157
        }
158

    
159

    
160
        public void setParameters(Map params) throws GeoprocessException {
161
                Boolean firstLayerSelection = (Boolean) params.get("firstlayerselection");
162
                if (firstLayerSelection != null)
163
                        this.onlyFirstLayerSelection =
164
                                firstLayerSelection.booleanValue();
165
                
166
                Boolean createLyrsWithError = (Boolean) params.get("createlayerswitherrors");
167
                if (createLyrsWithError != null)
168
                        this.createLyrsWithErrorGeometries =
169
                                createLyrsWithError.booleanValue();
170

    
171
        }
172

    
173
        public void checkPreconditions() throws GeoprocessException {
174
                if (firstLayer == null)
175
                        throw new GeoprocessException("CLEAN: capa de entrada a null");
176
                if (this.writer == null || this.schemaManager == null) {
177
                        throw new GeoprocessException(
178
                                        "Operacion de CLEAN sin especificar capa de resultados");
179
                }
180
                try {
181
                        if(firstLayer.getSource().getShapeCount() == 0){
182
                                throw new GeoprocessException(
183
                                "Capa de entrada vacia");
184
                        }
185
                } catch (ReadDriverException e) {
186
                        throw new GeoprocessException(
187
                        "Error al verificar si la capa est? vac?a");
188
                }
189
        }
190

    
191
        public void process() throws GeoprocessException {
192
                try {
193
                        createTask().run();
194
                } catch (Exception e) {
195
                        throw new GeoprocessException("Error al ejecutar el geoproceso", e);
196
                }
197
        }
198

    
199
        public ILayerDefinition createLayerDefinition() {
200
                if (resultLayerDefinition == null) {
201
                        try {
202
                                resultLayerDefinition = DefinitionUtils.
203
                                                        createLayerDefinition(firstLayer);
204
                        } catch (Exception e) {
205
                                e.printStackTrace();
206
                        }
207
                }
208
                return resultLayerDefinition;
209
        }
210

    
211

    
212
        class LineCleanTask extends AbstractMonitorableTask implements IPipedTask{
213

    
214
                private LineCleanTask() {
215
                        setInitialStep(0);
216
                        try {
217
                                if (onlyFirstLayerSelection) {
218
                                        int numSelected = firstLayer.getRecordset().getSelection()
219
                                                        .cardinality();
220
                                        setFinalStep(numSelected);
221
                                } else {
222
                                        int numShapes = firstLayer.getSource().getShapeCount();
223
                                        setFinalStep(numShapes);
224
                                }// else
225
                        } catch (ReadDriverException e) {
226
                                // TODO Auto-generated catch block
227
                                e.printStackTrace();
228
                        }
229
                        setDeterminatedProcess(true);
230
                        setStatusMessage(PluginServices.getText(this,
231
                                        "LineClean._Progress_Message"));
232

    
233
                }
234

    
235
                /**
236
                 * Verifies cancelation events, and return a boolean flag if processes must
237
                 * be stopped for this cancelations events.
238
                 *
239
                 * @param cancel
240
                 * @param va
241
                 * @param visitor
242
                 * @return
243
                 * @throws DriverIOException
244
                 */
245
                protected boolean verifyCancelation(ReadableVectorial va) {
246
                        if (isCanceled()) {
247
                                try {
248
                                        va.stop();
249
                                } finally {
250
                                        return true;
251
                                }
252
                        }
253
                        return false;
254
                }
255

    
256

    
257
                public void run() throws Exception {
258
                        processor =
259
                                new FeaturePersisterProcessor2(writer);
260

    
261
                        intersectionsWriter = new ShpWriter();
262
                        String temp = System.getProperty("java.io.tmpdir") +
263
                                        "/intersections_" +
264
                                        System.currentTimeMillis() +
265
                                        ".shp";
266
                        File newFile = new File(temp);
267
                        ((ShpWriter) intersectionsWriter).setFile(newFile);
268

    
269
                        ILayerDefinition intersectDefinition = new SHPLayerDefinition();
270
                        intersectDefinition.setShapeType(XTypes.POINT);
271
                        FieldDescription[] intersectFields = new FieldDescription[2];
272
                        intersectFields[0] = new FieldDescription();
273
                        intersectFields[0].setFieldLength(10);
274
                        intersectFields[0].setFieldDecimalCount(0);
275
                        intersectFields[0].setFieldName("FID1");
276
                        intersectFields[0].setFieldType(XTypes.INTEGER);
277
                        intersectFields[1] = new FieldDescription();
278
                        intersectFields[1].setFieldLength(10);
279
                        intersectFields[1].setFieldDecimalCount(0);
280
                        intersectFields[1].setFieldName("FID2");
281
                        intersectFields[1].setFieldType(XTypes.INTEGER);
282
                        intersectDefinition.setFieldsDesc(intersectFields);
283

    
284
                        ((ShpWriter) intersectionsWriter).initialize(
285
                                        (LayerDefinition) intersectDefinition);
286
                        ((SHPLayerDefinition) intersectDefinition).setFile(newFile);
287

    
288
                        ShpSchemaManager interSchMg =
289
                                new ShpSchemaManager(newFile.getAbsolutePath());
290
                        interSchMg.createSchema(intersectDefinition);
291

    
292
                        intersectsProcessor = new
293
                                FeaturePersisterProcessor2(intersectionsWriter);
294

    
295
                        FBitSet selection = null;
296
                        SnappingCoordinateMap coordMap =
297
                                new SnappingCoordinateMap(LineCleanVisitor.DEFAULT_SNAP);
298
                        LineCleanVisitor visitor =
299
                                new LineCleanVisitor(processor,
300
                                                             intersectsProcessor,
301
                                                onlyFirstLayerSelection,
302
                                                resultLayerDefinition,
303
                                                intersectDefinition,
304
                                                firstLayer,
305
                                                firstLayer.getRecordset(), coordMap);
306

    
307
                        try {
308
                                processor.start();
309
                                intersectsProcessor.start();
310

    
311
                                ReadableVectorial va = firstLayer.getSource();
312
                                va.start();
313
                                for (int i = 0; i < va.getShapeCount(); i++) {// for each geometry
314
                                        if (verifyCancelation(va)) {
315
                                                intersectsProcessor.finish();
316
                                                return;
317
                                        }
318
                                        if(selection != null){
319
                                                if (selection.get(i)) {
320
                                                                reportStep();
321
                                                                visitor.visit(va.getShape(i), i);
322
                                                }
323

    
324
                                        }else{
325
                                                reportStep();
326
                                                visitor.visit(va.getShape(i), i);
327
                                        }
328
                                }// for
329
                                va.stop();
330
                                processor.finish();
331
                                intersectsProcessor.finish();
332

    
333

    
334
                        } catch (ReadDriverException e) {
335
                                e.printStackTrace();
336
                        }
337
                }
338

    
339
                // TODO INTERNACIONALIZAR LOS MENSAJES
340
                public String getNote() {
341
                        String cleaningText = PluginServices.getText(this, "Limpiando_lineas");
342
                        String of = PluginServices.getText(this, "de");
343
                        return cleaningText + " " + getCurrentStep() + " "
344
                                        + of + " " + getFinishStep();
345
                }
346

    
347
                public void cancel() {
348
                        setCanceled(true);
349
                        LineCleanGeoprocess.this.cancel();
350
                }
351

    
352
                /* (non-Javadoc)
353
                 * @see com.iver.utiles.swing.threads.IPipedTask#getResult()
354
                 */
355
                public Object getResult() {
356
                        try {
357
                                return LineCleanGeoprocess.this.getResult();
358
                        } catch (GeoprocessException e) {
359
                                return null;
360
                        }
361
                }
362

    
363
                /* (non-Javadoc)
364
                 * @see com.iver.utiles.swing.threads.IPipedTask#setEntry(java.lang.Object)
365
                 */
366
                public void setEntry(Object object) {
367
                        // TODO Auto-generated method stub
368

    
369
                }
370

    
371
                /* (non-Javadoc)
372
                 * @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
373
                 */
374
                public void finished() {
375
                        // TODO Auto-generated method stub
376

    
377
                }
378
        }
379

    
380

    
381
        public IMonitorableTask createTask() {
382
                return new LineCleanTask();
383
        }
384

    
385

    
386
        public FLayer getResult() throws GeoprocessException {
387
                FLyrVect cleanedLayer = (FLyrVect) createLayerFrom(this.writer);
388
                FLyrVect pseudonodes = null;
389
                
390
                if(this.createLyrsWithErrorGeometries){
391
                        pseudonodes = (FLyrVect) createLayerFrom(this.intersectionsWriter);
392
                        try {
393
                                if(pseudonodes.getSource().getShapeCount() != 0){
394
        
395
                                        MapContext map = ((View)PluginServices.getMDIManager().
396
                                                                                                                getActiveWindow()).
397
                                                                                                                getModel().
398
                                                                                                                getMapContext();
399
                                        FLayers solution = new FLayers();//(map,null);
400
                                        solution.setMapContext(map);
401
                                        solution.setName(this.firstLayer.getName()+"_cleaned");
402
                                        solution.addLayer(cleanedLayer);
403
                                        solution.addLayer(pseudonodes);
404
                                        return solution;
405
                                }
406
                        } catch (ReadDriverException e) {
407
                                throw new GeoprocessException("Error de lectura de datos");
408
                        }
409
                }//if
410
                
411
                return cleanedLayer;
412
        }
413

    
414
}
415