Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / dissolve / DissolveGeoprocessController.java @ 8235

History | View | Annotate | Download (7.57 KB)

1
/*
2
 * Created on 11-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: DissolveGeoprocessController.java 8235 2006-10-23 10:29:18Z caballero $
47
* $Log$
48
* Revision 1.7  2006-10-23 10:29:18  caballero
49
* ancho y alto del panel
50
*
51
* Revision 1.6  2006/10/20 14:29:46  azabala
52
* removed unused imports
53
*
54
* Revision 1.5  2006/08/11 16:20:24  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.4  2006/07/24 15:00:27  azabala
58
* fixed bug in IWriter creation.
59
*
60
* Revision 1.3  2006/07/21 09:10:34  azabala
61
* fixed bug 608: user doesnt enter any result file to the geoprocess panel
62
*
63
* Revision 1.2  2006/06/29 07:33:57  fjp
64
* Cambios ISchemaManager y IFieldManager por terminar
65
*
66
* Revision 1.1  2006/06/20 18:20:45  azabala
67
* first version in cvs
68
*
69
* Revision 1.3  2006/06/02 18:21:28  azabala
70
* *** empty log message ***
71
*
72
* Revision 1.2  2006/05/25 08:21:48  jmvivo
73
* A?adida peticion de confirmacion para sobreescribir el fichero de salida, si este ya existiera
74
*
75
* Revision 1.1  2006/05/24 21:11:14  azabala
76
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
77
*
78
* Revision 1.2  2006/05/08 15:35:32  azabala
79
* *** empty log message ***
80
*
81
* Revision 1.1  2006/04/11 17:55:51  azabala
82
* primera version en cvs
83
*
84
*
85
*/
86
package com.iver.cit.gvsig.geoprocess.impl.dissolve;
87

    
88
import java.io.File;
89
import java.io.FileNotFoundException;
90
import java.util.HashMap;
91
import java.util.Map;
92

    
93
import com.iver.andami.PluginServices;
94
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
95
import com.iver.cit.gvsig.fmap.edition.IWriter;
96
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
97
import com.iver.cit.gvsig.fmap.layers.FLayers;
98
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
99
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
100
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
101
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
102
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
103
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
104
import com.iver.cit.gvsig.geoprocess.impl.dissolve.fmap.DissolveGeoprocess;
105
import com.iver.cit.gvsig.geoprocess.impl.dissolve.gui.DissolvePanelIF;
106
import com.iver.utiles.swing.threads.IMonitorableTask;
107
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
108

    
109
public class DissolveGeoprocessController extends AbstractGeoprocessController {
110
        private DissolvePanelIF geoProcessingDissolvePanel;
111
        private DissolveGeoprocess dissolve;
112

    
113
        public void setView(IGeoprocessPanel viewPanel) {
114
                this.geoProcessingDissolvePanel =
115
                        (DissolvePanelIF) viewPanel;
116
        }
117

    
118
        public IGeoprocess getGeoprocess() {
119
                return dissolve;
120
        }
121

    
122
        public boolean launchGeoprocess() {
123
                FLyrVect inputLayer = geoProcessingDissolvePanel.getInputLayer();
124
                FLayers layers = geoProcessingDissolvePanel.getFLayers();
125
                File outputFile = null;
126
                try {
127
                        outputFile = geoProcessingDissolvePanel.getOutputFile();
128
                } catch (FileNotFoundException e3) {
129
                        String error = PluginServices.getText(this, "Error_entrada_datos");
130
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
131
                        geoProcessingDissolvePanel.error(errorDescription, error);
132
                        return false;
133
                }
134
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
135
                        String error = PluginServices.getText(this, "Error_entrada_datos");
136
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
137
                        geoProcessingDissolvePanel.error(errorDescription, error);
138
                        return false;
139
                }
140
                if (outputFile.exists()) {
141
                        if (!geoProcessingDissolvePanel.askForOverwriteOutputFile(outputFile)) {
142
                                return false;
143
                        }
144
                }
145
                String dissolveField = geoProcessingDissolvePanel
146
                                .getDissolveFieldName();
147
                boolean onlySelection = geoProcessingDissolvePanel
148
                                .isDissolveOnlySelected();
149
                boolean onlyAdjacents = geoProcessingDissolvePanel.onlyAdjacentSelected();
150
                DissolveGeoprocess dissolve = new DissolveGeoprocess(inputLayer,
151
                                dissolveField);
152

    
153
                // Antes de crear el esquema vemos la seleccion del usuario
154
                Map fields_functions = geoProcessingDissolvePanel.getFieldFunctionMap();
155
                dissolve.setFieldsFunctions(fields_functions);
156

    
157
                HashMap params = new HashMap();
158
                params.put("layer_selection", new Boolean(onlySelection));
159
                params.put("only_adjacents", new Boolean(onlyAdjacents));
160
                try {
161
                        dissolve.setParameters(params);
162
                } catch (GeoprocessException e1) {
163
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
164
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
165
                        geoProcessingDissolvePanel.error(errorDescription, error);
166
                        return false;
167
                }
168

    
169
                SHPLayerDefinition definition = (SHPLayerDefinition) dissolve
170
                                .createLayerDefinition();
171
                definition.setFile(outputFile);
172
                ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
173

    
174

    
175
                IWriter writer = null;
176
                try {
177
                        writer = getShpWriter(definition);
178
                } catch (Exception e1) {
179
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
180
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
181
                        geoProcessingDissolvePanel.error(errorDescription, error);
182
                        return false;
183
                }
184

    
185
                dissolve.setResultLayerProperties(writer, schemaManager);
186

    
187
                try {
188

    
189
                        dissolve.checkPreconditions();
190
                        IMonitorableTask task1 = dissolve.createTask();
191
                        AddResultLayerTask task2 = new AddResultLayerTask(dissolve);
192
                        task2.setLayers(layers);
193
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
194
                                        task2);
195
                        //if dissolving is spatial based, checks spatial
196
                        //index existency
197
                        if(geoProcessingDissolvePanel.onlyAdjacentSelected()){
198
                                if(inputLayer.getISpatialIndex() == null){
199
                                        final IMonitorableTask sptIdxTask =
200
                                                geoProcessingDissolvePanel.askForSpatialIndexCreation(inputLayer);
201
                                        if(sptIdxTask != null){
202
                                                PluginServices.backgroundExecution(
203
                                                                new Runnable(){
204
                                                        public void run() {
205
                                                                PluginServices.
206
                                                                cancelableBackgroundExecution(sptIdxTask);
207
                                                        }}
208
                                                );
209
                                        }
210
                                }//if
211
                        }
212
                        if (globalTask.preprocess())
213
                                PluginServices.cancelableBackgroundExecution(globalTask);
214
                } catch (GeoprocessException e) {
215
                        String error = PluginServices.getText(this, "Error_ejecucion");
216
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
217
                        geoProcessingDissolvePanel.error(errorDescription, error);
218
                        return false;
219
                }
220
                return true;
221
        }
222
        public int getWidth() {
223
                return 700;
224
        }
225

    
226
        public int getHeight() {
227
                return 500;
228
        }
229
}
230