Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / topology / lineclean / fmap / LineCleanGeoprocess.java @ 8659

History | View | Annotate | Download (8.33 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 8659 2006-11-09 21:08:32Z azabala $
47
* $Log$
48
* Revision 1.4  2006-11-09 21:08:32  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.3  2006/10/19 16:06:48  azabala
52
* *** empty log message ***
53
*
54
* Revision 1.2  2006/10/17 18:25:53  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.1  2006/10/10 18:50:17  azabala
58
* First version in CVS
59
*
60
*
61
*/
62
package com.iver.cit.gvsig.topology.lineclean.fmap;
63

    
64
import java.io.File;
65
import java.util.Map;
66

    
67
import com.iver.andami.PluginServices;
68
import com.iver.cit.gvsig.fmap.MapContext;
69
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
70
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
71
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
72
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
73
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
74
import com.iver.cit.gvsig.fmap.edition.IWriter;
75
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
76
import com.iver.cit.gvsig.fmap.edition.writers.shp.MultiShpWriter;
77
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
78
import com.iver.cit.gvsig.fmap.layers.FBitSet;
79
import com.iver.cit.gvsig.fmap.layers.FLayer;
80
import com.iver.cit.gvsig.fmap.layers.FLayers;
81
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
82
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
83
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
84
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess;
85
import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils;
86
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2;
87
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
88
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
89
import com.iver.cit.gvsig.project.documents.view.gui.View;
90
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
91
import com.iver.utiles.swing.threads.IMonitorableTask;
92

    
93
public class LineCleanGeoprocess extends AbstractGeoprocess {
94

    
95
        /**
96
         * Schema of the result layer
97
         */
98
        private ILayerDefinition resultLayerDefinition;
99

    
100
        
101
        /**
102
         * flag to only clip selection of input layer
103
         */
104
        private boolean onlyFirstLayerSelection = false;
105
        
106
        /**
107
         * Processes features (writing them)
108
         */
109
        FeaturePersisterProcessor2 processor;
110

    
111
        /**
112
         * Writer to save in a temporal layer intersections
113
         */
114
        private IWriter intersectionsWriter;
115
        FeaturePersisterProcessor2 intersectsProcessor;
116
        
117
        
118
        public LineCleanGeoprocess(FLyrVect inputLayer){
119
                this.firstLayer = inputLayer;
120
        }
121
        
122
        
123
        public void setParameters(Map params) throws GeoprocessException {
124
                Boolean firstLayerSelection = (Boolean) params.get("firstlayerselection");
125
                if (firstLayerSelection != null)
126
                        this.onlyFirstLayerSelection = 
127
                                firstLayerSelection.booleanValue();
128

    
129
        }
130

    
131
        public void checkPreconditions() throws GeoprocessException {
132
                if (firstLayer == null)
133
                        throw new GeoprocessException("CLEAN: capa de entrada a null");
134
                if (this.writer == null || this.schemaManager == null) {
135
                        throw new GeoprocessException(
136
                                        "Operacion de CLEAN sin especificar capa de resultados");
137
                }
138
        }
139

    
140
        public void process() throws GeoprocessException {
141
                try {
142
                        createTask().run();
143
                } catch (Exception e) {
144
                        throw new GeoprocessException("Error al ejecutar el geoproceso");
145
                }
146
        }
147

    
148
        public ILayerDefinition createLayerDefinition() {
149
                if (resultLayerDefinition == null) {
150
                        try {
151
                                resultLayerDefinition = DefinitionUtils.
152
                                                        createLayerDefinition(firstLayer);
153
                        } catch (Exception e) {
154
                                e.printStackTrace();
155
                        }
156
                }
157
                return resultLayerDefinition;
158
        }
159
        
160
        
161
        class LineCleanTask extends AbstractMonitorableTask {
162

    
163
                private LineCleanTask() {
164
                        setInitialStep(0);
165
                        try {
166
                                if (onlyFirstLayerSelection) {
167
                                        int numSelected = firstLayer.getRecordset().getSelection()
168
                                                        .cardinality();
169
                                        setFinalStep(numSelected);
170
                                } else {
171
                                        int numShapes = firstLayer.getSource().getShapeCount();
172
                                        setFinalStep(numShapes);
173
                                }// else
174
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
175
                                // TODO Auto-generated catch block
176
                                e.printStackTrace();
177
                        } catch (DriverIOException e) {
178
                                // TODO Auto-generated catch block
179
                                e.printStackTrace();
180
                        }
181
                        setDeterminatedProcess(true);
182
                        setStatusMessage(PluginServices.getText(this,
183
                                        "LineClean._Progress_Message"));
184

    
185
                }
186

    
187
                public void run() throws Exception {
188
                        processor =
189
                                new FeaturePersisterProcessor2(writer);
190
                        
191
                        intersectionsWriter = new ShpWriter();
192
                        String temp = System.getProperty("java.io.tmpdir") + 
193
                                        "/intersections_" + 
194
                                        System.currentTimeMillis() +
195
                                        ".shp";
196
                        File newFile = new File(temp);
197
                        ((ShpWriter) intersectionsWriter).setFile(newFile);
198
                        
199
                        ILayerDefinition intersectDefinition = new SHPLayerDefinition();
200
                        intersectDefinition.setShapeType(XTypes.POINT);
201
                        FieldDescription[] intersectFields = new FieldDescription[2];
202
                        intersectFields[0] = new FieldDescription();
203
                        intersectFields[0].setFieldLength(10);
204
                        intersectFields[0].setFieldDecimalCount(0);
205
                        intersectFields[0].setFieldName("FID1");
206
                        intersectFields[0].setFieldType(XTypes.INTEGER);
207
                        intersectFields[1] = new FieldDescription();
208
                        intersectFields[1].setFieldLength(10);
209
                        intersectFields[1].setFieldDecimalCount(0);
210
                        intersectFields[1].setFieldName("FID2");
211
                        intersectFields[1].setFieldType(XTypes.INTEGER);
212
                        intersectDefinition.setFieldsDesc(intersectFields);
213
                        
214
                        ((ShpWriter) intersectionsWriter).initialize(
215
                                        (LayerDefinition) intersectDefinition);
216
                        ((SHPLayerDefinition) intersectDefinition).setFile(newFile);
217
                        
218
                        ShpSchemaManager interSchMg = 
219
                                new ShpSchemaManager(newFile.getAbsolutePath());
220
                        interSchMg.createSchema(intersectDefinition);
221
                        
222
                        intersectsProcessor = new 
223
                                FeaturePersisterProcessor2(intersectionsWriter);
224
                        
225
                        
226
                        FBitSet selection = null;
227
                        LineCleanVisitor visitor = 
228
                                new LineCleanVisitor(processor,
229
                                                             intersectsProcessor,
230
                                                onlyFirstLayerSelection, 
231
                                                resultLayerDefinition,
232
                                                intersectDefinition);
233
                        Strategy strategy = StrategyManager.getStrategy(firstLayer);
234
                        try {
235
                                strategy.process(visitor, this);
236
                        } catch (Exception e) {
237
                                throw new GeoprocessException("Error al ejecutar el geoproceso line clean ", e);
238
                        }
239
                }
240

    
241
                // TODO INTERNACIONALIZAR LOS MENSAJES
242
                public String getNote() {
243
                        return "Limpiando l?neas..." + " " + getCurrentStep() + " "
244
                                        + "de" + " " + getFinishStep();
245
                }
246

    
247
                public void cancel() {
248
                        setCanceled(true);
249
                        LineCleanGeoprocess.this.cancel();
250
                }
251
        }
252

    
253
        
254
        public IMonitorableTask createTask() {
255
                return new LineCleanTask();
256
        }
257
        
258
        
259
        public FLayer getResult() throws GeoprocessException {
260
                
261
                FLyrVect cleanedLayer = (FLyrVect) createLayerFrom(this.writer);
262
                FLyrVect pseudoNodes = (FLyrVect) createLayerFrom(this.intersectionsWriter);
263
                try {
264
                        if(pseudoNodes.getSource().getShapeCount() == 0){
265
                                return cleanedLayer;
266
                        }else{
267
                                MapContext map = ((View)PluginServices.
268
                                                getMDIManager().
269
                                                getActiveWindow()).
270
                                                getModel().
271
                                                getMapContext();
272
                                FLayers solution = new FLayers(map, null);
273
                                solution.setName(this.firstLayer.getName()+"_cleaned");
274
                                solution.addLayer(cleanedLayer);
275
                                solution.addLayer(pseudoNodes);
276
                                return solution;
277
                        }
278
                } catch (DriverIOException e) {
279
                        throw new GeoprocessException("Error de lectura de datos");
280
                }
281
                
282
                
283
                
284
                        
285
        }
286

    
287
}
288