Statistics
| Revision:

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

History | View | Annotate | Download (10.7 KB)

1 21272 azabala
/*
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.topology.lineclean.ILineCleanGeoprocessUserEntries;
68
import com.iver.cit.gvsig.geoprocess.impl.topology.lineclean.LineCleanGeoprocessController;
69
import com.iver.cit.gvsig.geoprocess.impl.topology.polygonbuild.IPolygonBuildGeoprocessUserEntries;
70
import com.iver.cit.gvsig.geoprocess.impl.topology.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,
83
                        "Desea_calcular_el_build_solo_con_las_polilineas_seleccionadas");
84
85 24859 azabala
        String msgErrorLyrBuild = PluginServices.getText(this,
86 21272 azabala
                        "Desea_a?adir_capa_con_geometrias_que_no_forman_parte_de_poligonos");
87 24859 azabala
88
        String msgErrorLyrClean = PluginServices.getText(this,
89
                        "Desea_a?adir_capa_con_pseudonodos");
90 21272 azabala
91
        String title = PluginServices.getText(this,
92
                                                "Introduccion_de_datos");
93
94
        public void execute(String actionCommand) {
95
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices
96
                                .getMDIManager().getActiveWindow();
97
                View vista = (View) f;
98
                IProjectView model = vista.getModel();
99
                final MapContext mapContext = model.getMapContext();
100
                List<FLyrVect> activeVectorialLyrs = FLyrUtil.getActiveVectorialLyrs(mapContext);
101
                FLyrVect lyrOfLines = null;
102
                for (FLyrVect lyr : activeVectorialLyrs) {
103
                                int shapeType;
104
                                try {
105
                                        shapeType = lyr.getShapeType();
106
                                } catch (ReadDriverException e) {
107
                                        e.printStackTrace();
108
                                        continue;
109
                                }
110
                                int numDim = FGeometryUtil.getDimensions(shapeType);
111
                                if (numDim != 1)
112
                                        continue;
113
                                else {
114
                                        lyrOfLines = lyr;
115
                                        break;
116
                                }
117
                }
118
                if (lyrOfLines != null) {
119
                        final FLyrVect inputLyr = lyrOfLines;
120
                        if (actionCommand.equalsIgnoreCase("COMPUTE_BUILD")) {
121
                                PolygonBuildGeoprocessController polygonBuild = new PolygonBuildGeoprocessController();
122
                                polygonBuild.setView(createBuildPolygonUserEntries(mapContext,inputLyr));
123
                                polygonBuild.launchGeoprocess();
124
                        } else if (actionCommand.equalsIgnoreCase("COMPUTE_CLEAN")) {
125
                                LineCleanGeoprocessController lineCleanGp = new LineCleanGeoprocessController();
126
                                lineCleanGp.setView(createLineCleanUserEntries(mapContext, inputLyr));
127
                                lineCleanGp.launchGeoprocess();
128
                        }
129
                }else {
130
                        GUIUtil.getInstance().messageBox(
131
                                        PluginServices.getText(this,
132
                                                                        "Debe_seleccionar_una_capa_de_lineas_en_el_TOC"),
133
                                        PluginServices.getText(this, "Error"));
134
                }
135
        }
136
137
        public void initialize() {
138
                PluginServices.getIconTheme().registerDefault(
139
                                "compute-clean",
140
                                this.getClass().getClassLoader().getResource(
141
                                                "images/compute-clean.png"));
142
143
                PluginServices.getIconTheme().registerDefault(
144
                                "compute-build",
145
                                this.getClass().getClassLoader().getResource(
146
                                                "images/compute-build.png"));
147
        }
148
149
        public boolean isEnabled() {
150
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices
151
                                .getMDIManager().getActiveWindow();
152
                if (f == null) {
153
                        return false;
154
                }
155
                if (f instanceof View) {
156
                        View vista = (View) f;
157
                        MapContext mapContext = vista.getModel().getMapContext();
158
                        FLayers layers = mapContext.getLayers();
159
                        int numLayers = layers.getLayersCount();
160
                        for (int i = 0; i < numLayers; i++) {
161
                                FLayer layer = layers.getLayer(i);
162
                                if (layer instanceof FLyrVect && layer.isAvailable()
163
                                                && layer.isActive()) {
164
                                        int shapeType;
165
                                        try {
166
                                                shapeType = ((FLyrVect) layer).getShapeType();
167
                                        } catch (ReadDriverException e) {
168
                                                e.printStackTrace();
169
                                                continue;
170
                                        }
171
                                        int numDim = FGeometryUtil.getDimensions(shapeType);
172
                                        if (numDim == 1)
173
                                                return true;
174
175
                                }
176
                        }
177
                }
178
                return false;
179
        }
180
181
        public boolean isVisible() {
182
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices
183
                                .getMDIManager().getActiveWindow();
184
                if (f == null) {
185
                        return false;
186
                }
187
                if (f instanceof View) {
188
                        View vista = (View) f;
189
                        IProjectView model = vista.getModel();
190
                        FLayers layers = model.getMapContext().getLayers();
191
                        int numLayers = layers.getLayersCount();
192
                        for (int i = 0; i < numLayers; i++) {
193
                                FLayer layer = layers.getLayer(i);
194 24850 azabala
                                if (layer instanceof FLyrVect || layer instanceof Topology)
195 21272 azabala
                                        return true;
196
                        }
197
                        return false;
198
                }
199
                return false;
200
        }
201
202
        /**
203
         * Generates a shp output file (in the topology's temp files directory)
204
         * for the layer resulting of a clean or build operation.
205
         *
206
         * @param inputLyr original layer to compute the clean or build geoprocess
207
         * @return
208
         */
209
        private File getOutputFileForLyr(final FLyrVect inputLyr, String sufix) {
210
                String filesDirectory = GUIUtil
211
                                .getInstance().getFilesDirectory();
212
                if (!filesDirectory.endsWith("/"))
213
                        filesDirectory += "/";
214
                String fileName = inputLyr.getName();
215
                if (fileName.endsWith(".shp"))
216
                        fileName = fileName.substring(0,
217
                                        fileName.length() - 4);
218
                fileName += "_"+sufix+".shp";
219
                return new File(filesDirectory + fileName);
220
        }
221
222
        /**
223
         * Creates a IPolygonBuildGeoprocessUserEntries implementation to provides
224
         * PolygonBuildGeoprocessController user entries.
225
         *
226
         * @param mapContext mapContext of the active view where the input lyr is added.
227
         * @param inputLyr
228
         */
229
        private IPolygonBuildGeoprocessUserEntries createBuildPolygonUserEntries(final MapContext mapContext,
230
                                                                                                                                                                final FLyrVect inputLyr){
231
                return new IPolygonBuildGeoprocessUserEntries() {
232
233
                        public boolean createLyrsWithErrorGeometries() {
234 24859 azabala
                                return GUIUtil.getInstance().optionMessage(msgErrorLyrBuild, title);
235 21272 azabala
                        }
236
237
                        public boolean applyDangleTolerance() {
238
                                return false;
239
                        }
240
241
                        public boolean applySnapTolerance() {
242
                                return false;
243
                        }
244
245
                        public boolean computeCleanBefore() {
246
                                return false;
247
                        }
248
249
                        public double getDangleTolerance()
250
                                        throws GeoprocessException {
251
                                return 0;
252
                        }
253
254
                        public boolean isFirstOnlySelected() {
255
                                boolean firstOnlySelected = false;
256
                                try {
257
                                        if (inputLyr.getRecordset()
258
                                                        .getSelection().cardinality() != 0) {
259
                                                firstOnlySelected = GUIUtil
260
                                                                .getInstance()
261
                                                                .optionMessage(
262
                                                                                messageSelection,
263
                                                                                title);
264
                                        }
265
                                } catch (ReadDriverException e) {
266
                                        e.printStackTrace();
267
                                }
268
                                return firstOnlySelected;
269
                        }
270
271
                        public boolean askForOverwriteOutputFile(
272
                                        File outputFile) {
273
                                return GUIUtil.getInstance()
274
                                                .askForOverwriteOutputFile(
275
                                                                outputFile);
276
                        }
277
278
                        public void error(String message, String title) {
279
                                GUIUtil.getInstance().messageBox(message,
280
                                                title);
281
                        }
282
283
                        public FLayers getFLayers() {
284
                                return mapContext.getLayers();
285
                        }
286
287
                        public FLyrVect getInputLayer() {
288
                                return inputLyr;
289
                        }
290
291
                        public File getOutputFile()
292
                                        throws FileNotFoundException {
293
294
                                return getOutputFileForLyr(inputLyr, "build");
295
                        }
296
297
                        public void setFLayers(FLayers layers) {
298
                        }
299
                };
300
        }
301
302
        /**
303
         *
304
         *  Creates a ILineCleanGeoprocessUserEntries implementation to provides
305
         * LineCleanGeoprocessController user entries.
306
         * @param mapContext
307
         * @param inputLyr
308
         * @return
309
         */
310
        private ILineCleanGeoprocessUserEntries createLineCleanUserEntries(final MapContext mapContext, final FLyrVect inputLyr){
311
                return new ILineCleanGeoprocessUserEntries() {
312
313
                        String messageSelection = PluginServices.getText(this,
314
                                                        "Desea_calcular_el_clean_solo_con_las_polilineas_seleccionadas");
315
316
                        String title = PluginServices.getText(this,
317
                                        "Introduccion_de_datos");
318
319
                        public boolean cleanOnlySelection() {
320
                                boolean firstOnlySelected = false;
321
                                try {
322
                                        if (inputLyr.getRecordset().getSelection()
323
                                                        .cardinality() != 0) {
324
                                                firstOnlySelected = GUIUtil.getInstance()
325
                                                                .optionMessage(messageSelection,
326
                                                                                title);
327
                                        }
328
                                } catch (ReadDriverException e) {
329
                                        e.printStackTrace();
330
                                }
331
                                return firstOnlySelected;
332
                        }
333
334
                        public boolean askForOverwriteOutputFile(File outputFile) {
335
                                return GUIUtil.getInstance()
336
                                                .askForOverwriteOutputFile(outputFile);
337
                        }
338
339
                        public void error(String message, String title) {
340
                                GUIUtil.getInstance().messageBox(message, title);
341
                        }
342
343
                        public FLayers getFLayers() {
344
                                return mapContext.getLayers();
345
                        }
346
347
                        public FLyrVect getInputLayer() {
348
                                return inputLyr;
349
                        }
350
351
                        public File getOutputFile()
352
                                        throws FileNotFoundException {
353
                                return getOutputFileForLyr(inputLyr, "clean");
354
                        }
355
356
                        public void setFLayers(FLayers layers) {
357
                        }
358
359
                        public boolean createLyrsWithErrorGeometries() {
360 24859 azabala
                                return GUIUtil.getInstance().optionMessage(msgErrorLyrClean, title);
361 21272 azabala
                        }
362
                };
363
364
        }
365
366
}