Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extTopology / src / org / gvsig / topology / ComputeBuildAndCleanExtension.java @ 32785

History | View | Annotate | Download (10.4 KB)

1
/*
2
 * Created on 10-abr-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: 
47
 * $Log: 
48
 */
49
package org.gvsig.topology;
50

    
51
import java.io.File;
52
import java.io.FileNotFoundException;
53
import java.util.List;
54

    
55
import org.gvsig.fmap.core.FGeometryUtil;
56
import org.gvsig.fmap.core.FLyrUtil;
57
import org.gvsig.topology.ui.util.GUIUtil;
58

    
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.plugins.Extension;
62
import com.iver.cit.gvsig.fmap.MapContext;
63
import com.iver.cit.gvsig.fmap.layers.FLayer;
64
import com.iver.cit.gvsig.fmap.layers.FLayers;
65
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
66
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
67
import com.iver.cit.gvsig.geoprocess.impl.lineclean.ILineCleanGeoprocessUserEntries;
68
import com.iver.cit.gvsig.geoprocess.impl.lineclean.LineCleanGeoprocessController;
69
import com.iver.cit.gvsig.geoprocess.impl.polygonbuild.IPolygonBuildGeoprocessUserEntries;
70
import com.iver.cit.gvsig.geoprocess.impl.polygonbuild.PolygonBuildGeoprocessController;
71
import com.iver.cit.gvsig.project.documents.view.IProjectView;
72
import com.iver.cit.gvsig.project.documents.view.gui.View;
73

    
74
/**
75
 * Extension to launch BUILD and CLEAN geoprocesses for lineal layers.
76
 * 
77
 * @author Alvaro Zabala
78
 * 
79
 */
80
public class ComputeBuildAndCleanExtension extends Extension {
81
        
82
        String messageSelection = PluginServices.getText(this,"Desea_calcular_el_build_solo_con_las_polilineas_seleccionadas");
83

    
84
        String msgErrorLyrBuild = PluginServices.getText(this,"Desea_a?adir_capa_con_geometrias_que_no_forman_parte_de_poligonos");
85
        
86
        String msgErrorLyrClean = PluginServices.getText(this,"Desea_a?adir_capa_con_pseudonodos");
87

    
88
        String title = PluginServices.getText(this,"Introduccion_de_datos");
89

    
90
        public void execute(String actionCommand) {
91
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
92
                View vista = (View) f;
93
                IProjectView model = vista.getModel();
94
                final MapContext mapContext = model.getMapContext();
95
                List<FLyrVect> activeVectorialLyrs = FLyrUtil.getActiveVectorialLyrs(mapContext);
96
                FLyrVect lyrOfLines = null;
97
                for (FLyrVect lyr : activeVectorialLyrs) {
98
                        int shapeType;
99
                        try {
100
                                shapeType = lyr.getShapeType();
101
                        } catch (ReadDriverException e) {
102
                                e.printStackTrace();
103
                                continue;
104
                        }
105
                        int numDim = FGeometryUtil.getDimensions(shapeType);
106
                        if (numDim != 1)
107
                                continue;
108
                        else {
109
                                lyrOfLines = lyr;
110
                                break;
111
                        }
112
                }
113
                if (lyrOfLines != null) {
114
                        final FLyrVect inputLyr = lyrOfLines;
115
                        if (actionCommand.equalsIgnoreCase("COMPUTE_BUILD")) {
116
                                PolygonBuildGeoprocessController polygonBuild = new PolygonBuildGeoprocessController();
117
                                polygonBuild.setView(createBuildPolygonUserEntries(mapContext,inputLyr));
118
                                polygonBuild.launchGeoprocess();
119
                        } else if (actionCommand.equalsIgnoreCase("COMPUTE_CLEAN")) {
120
                                LineCleanGeoprocessController lineCleanGp = new LineCleanGeoprocessController();
121
                                lineCleanGp.setView(createLineCleanUserEntries(mapContext, inputLyr));
122
                                lineCleanGp.launchGeoprocess();
123
                        }        
124
                }else {
125
                        GUIUtil.getInstance().messageBox(PluginServices.getText(this,"Debe_seleccionar_una_capa_de_lineas_en_el_TOC"),PluginServices.getText(this, "Error"));
126
                }
127
        }
128

    
129
        public void initialize() {
130
                PluginServices.getIconTheme().registerDefault("compute-clean",this.getClass().getClassLoader().getResource("images/compute-clean.png"));
131

    
132
                PluginServices.getIconTheme().registerDefault("compute-build",this.getClass().getClassLoader().getResource("images/compute-build.png"));
133
        }
134

    
135
        public boolean isEnabled() {
136
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
137
                if (f == null) {
138
                        return false;
139
                }
140
                if (f instanceof View) {
141
                        View vista = (View) f;
142
                        MapContext mapContext = vista.getModel().getMapContext();
143
                        FLayers layers = mapContext.getLayers();
144
                        int numLayers = layers.getLayersCount();
145
                        for (int i = 0; i < numLayers; i++) {
146
                                FLayer layer = layers.getLayer(i);
147
                                if (layer instanceof FLyrVect && layer.isAvailable() && layer.isActive()) {
148
                                        int shapeType;
149
                                        try {
150
                                                shapeType = ((FLyrVect) layer).getShapeType();
151
                                        } catch (ReadDriverException e) {
152
                                                e.printStackTrace();
153
                                                continue;
154
                                        }
155
                                        int numDim = FGeometryUtil.getDimensions(shapeType);
156
                                        if (numDim == 1)
157
                                                return true;
158

    
159
                                }
160
                        }
161
                }
162
                return false;
163
        }
164

    
165
        public boolean isVisible() {
166
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
167
                if (f == null) {
168
                        return false;
169
                }
170
                if (f instanceof View) {
171
                        View vista = (View) f;
172
                        IProjectView model = vista.getModel();
173
                        FLayers layers = model.getMapContext().getLayers();
174
                        int numLayers = layers.getLayersCount();
175
                        for (int i = 0; i < numLayers; i++) {
176
                                FLayer layer = layers.getLayer(i);
177
                                if (layer instanceof FLyrVect || layer instanceof Topology)
178
                                        return true;
179
                        }
180
                        return false;
181
                }
182
                return false;
183
        }
184
        
185
        /**
186
         * Generates a shp output file (in the topology's temp files directory)
187
         * for the layer resulting of a clean or build operation.
188
         * 
189
         * @param inputLyr original layer to compute the clean or build geoprocess
190
         * @return
191
         */
192
        private File getOutputFileForLyr(final FLyrVect inputLyr, String sufix) {
193
                String filesDirectory = GUIUtil.getInstance().getFilesDirectory();
194
                if (!filesDirectory.endsWith("/"))
195
                        filesDirectory += "/";
196
                String fileName = inputLyr.getName();
197
                if (fileName.endsWith(".shp"))
198
                        fileName = fileName.substring(0,fileName.length() - 4);
199
                fileName += "_"+sufix+".shp";
200
                return new File(filesDirectory + fileName);
201
        }
202
        
203
        /**
204
         * Creates a IPolygonBuildGeoprocessUserEntries implementation to provides
205
         * PolygonBuildGeoprocessController user entries.
206
         * 
207
         * @param mapContext mapContext of the active view where the input lyr is added.
208
         * @param inputLyr
209
         */
210
        private IPolygonBuildGeoprocessUserEntries createBuildPolygonUserEntries(final MapContext mapContext, 
211
                                                                                                                                                                final FLyrVect inputLyr){
212
                return new IPolygonBuildGeoprocessUserEntries() {
213
                        
214
                        public boolean createLyrsWithErrorGeometries() {
215
                                return GUIUtil.getInstance().optionMessage(msgErrorLyrBuild, title);
216
                        }
217

    
218
                        public boolean applyDangleTolerance() {
219
                                return false;
220
                        }
221

    
222
                        public boolean applySnapTolerance() {
223
                                return false;
224
                        }
225

    
226
                        public boolean computeCleanBefore() {
227
                                return false;
228
                        }
229

    
230
                        public double getDangleTolerance() throws GeoprocessException {
231
                                return 0;
232
                        }
233

    
234
                        public boolean isFirstOnlySelected() {
235
                                boolean firstOnlySelected = false;
236
                                try {
237
                                        if (inputLyr.getRecordset().getSelection().cardinality() != 0) {
238
                                                firstOnlySelected = GUIUtil.getInstance().optionMessage(messageSelection,title);
239
                                        }
240
                                } catch (ReadDriverException e) {
241
                                        e.printStackTrace();
242
                                }
243
                                return firstOnlySelected;
244
                        }
245

    
246
                        public boolean askForOverwriteOutputFile(File outputFile) {
247
                                return GUIUtil.getInstance().askForOverwriteOutputFile(outputFile);
248
                        }
249

    
250
                        public void error(String message, String title) {
251
                                GUIUtil.getInstance().messageBox(message,title);
252
                        }
253

    
254
                        public FLayers getFLayers() {
255
                                return mapContext.getLayers();
256
                        }
257

    
258
                        public FLyrVect getInputLayer() {
259
                                return inputLyr;
260
                        }
261

    
262
                        public File getOutputFile()throws FileNotFoundException {
263
                                return getOutputFileForLyr(inputLyr, "build");
264
                        }
265

    
266
                        public void setFLayers(FLayers layers) {
267
                        }
268
                };        
269
        }
270
        
271
        /**
272
         * 
273
         *  Creates a ILineCleanGeoprocessUserEntries implementation to provides
274
         * LineCleanGeoprocessController user entries.
275
         * @param mapContext
276
         * @param inputLyr
277
         * @return
278
         */
279
        private ILineCleanGeoprocessUserEntries createLineCleanUserEntries(final MapContext mapContext, final FLyrVect inputLyr){
280
                return new ILineCleanGeoprocessUserEntries() {
281

    
282
                        String messageSelection = PluginServices.getText(this,
283
                                                        "Desea_calcular_el_clean_solo_con_las_polilineas_seleccionadas");
284

    
285
                        String title = PluginServices.getText(this,
286
                                        "Introduccion_de_datos");
287

    
288
                        public boolean cleanOnlySelection() {
289
                                boolean firstOnlySelected = false;
290
                                try {
291
                                        if (inputLyr.getRecordset().getSelection()
292
                                                        .cardinality() != 0) {
293
                                                firstOnlySelected = GUIUtil.getInstance()
294
                                                                .optionMessage(messageSelection,
295
                                                                                title);
296
                                        }
297
                                } catch (ReadDriverException e) {
298
                                        e.printStackTrace();
299
                                }
300
                                return firstOnlySelected;
301
                        }
302

    
303
                        public boolean askForOverwriteOutputFile(File outputFile) {
304
                                return GUIUtil.getInstance()
305
                                                .askForOverwriteOutputFile(outputFile);
306
                        }
307

    
308
                        public void error(String message, String title) {
309
                                GUIUtil.getInstance().messageBox(message, title);
310
                        }
311

    
312
                        public FLayers getFLayers() {
313
                                return mapContext.getLayers();
314
                        }
315

    
316
                        public FLyrVect getInputLayer() {
317
                                return inputLyr;
318
                        }
319

    
320
                        public File getOutputFile()
321
                                        throws FileNotFoundException {
322
                                return getOutputFileForLyr(inputLyr, "clean");
323
                        }
324

    
325
                        public void setFLayers(FLayers layers) {
326
                        }
327

    
328
                        public boolean createLyrsWithErrorGeometries() {
329
                                return GUIUtil.getInstance().optionMessage(msgErrorLyrClean, title);
330
                        }
331
                };
332
        
333
        }        
334

    
335
}