Statistics
| Revision:

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

History | View | Annotate | Download (7.23 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: ClipGeoprocessController.java 8235 2006-10-23 10:29:18Z caballero $
47
* $Log$
48
* Revision 1.5  2006-10-23 10:28:45  caballero
49
* ancho y alto del panel
50
*
51
* Revision 1.4  2006/08/11 16:18:48  azabala
52
* refactoring to make logic independent from UI
53
*
54
* Revision 1.3  2006/07/21 09:10:34  azabala
55
* fixed bug 608: user doesnt enter any result file to the geoprocess panel
56
*
57
* Revision 1.2  2006/06/29 07:33:57  fjp
58
* Cambios ISchemaManager y IFieldManager por terminar
59
*
60
* Revision 1.1  2006/06/20 18:20:45  azabala
61
* first version in cvs
62
*
63
* Revision 1.4  2006/06/12 19:15:38  azabala
64
* cambios para poder trabajar en geoprocessing con capas MULTI (dxf, jdbc, etc)
65
*
66
* Revision 1.3  2006/06/02 18:21:28  azabala
67
* *** empty log message ***
68
*
69
* Revision 1.2  2006/05/25 08:21:48  jmvivo
70
* A?adida peticion de confirmacion para sobreescribir el fichero de salida, si este ya existiera
71
*
72
* Revision 1.1  2006/05/24 21:14:17  azabala
73
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
74
*
75
* Revision 1.2  2006/05/08 15:35:13  azabala
76
* refactoring of ITask api
77
*
78
* Revision 1.1  2006/04/11 17:55:51  azabala
79
* primera version en cvs
80
*
81
*
82
*/
83
package com.iver.cit.gvsig.geoprocess.impl.clip;
84

    
85
import java.io.File;
86
import java.io.FileNotFoundException;
87
import java.util.HashMap;
88

    
89
import com.iver.andami.PluginServices;
90
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
91
import com.iver.cit.gvsig.fmap.edition.IWriter;
92
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
93
import com.iver.cit.gvsig.fmap.layers.FLayers;
94
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
95
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
96
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
97
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
98
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
99
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
100
import com.iver.cit.gvsig.geoprocess.core.gui.OverlayPanelIF;
101
import com.iver.cit.gvsig.geoprocess.impl.clip.fmap.ClipGeoprocess;
102
import com.iver.utiles.swing.threads.IMonitorableTask;
103
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
104

    
105
public class ClipGeoprocessController extends AbstractGeoprocessController {
106

    
107
        private OverlayPanelIF geoProcessingClipPanel;
108
        private ClipGeoprocess clip;
109

    
110
        public void setView(IGeoprocessPanel viewPanel) {
111
                this.geoProcessingClipPanel =
112
                        (OverlayPanelIF) viewPanel;
113
        }
114

    
115
        public IGeoprocess getGeoprocess() {
116
                return clip;
117
        }
118

    
119
        public boolean launchGeoprocess() {
120
                FLyrVect inputLayer = geoProcessingClipPanel.getInputLayer();
121
                FLayers layers = geoProcessingClipPanel.getFLayers();
122
                FLyrVect clippingLayer = geoProcessingClipPanel.getSecondLayer();
123
                /*
124
                 * Esta parte hay que mejorarla, pues est? previsto que el
125
                 * resultado de los geoprocesos se pueda guardar en distintos
126
                 * formatos:
127
                 * shp, dxf, dgn, JDBC, etc
128
                 *
129
                 * Por este motivo, lo mas normal es que el usuario eliga
130
                 * un driver de escritura, y el clipPanel devuelva una instancia
131
                 * de IWriter sin inicializar.
132
                 */
133
                File outputFile = null;
134
                try {
135
                        outputFile = geoProcessingClipPanel.getOutputFile();
136
                } catch (FileNotFoundException e3) {
137
                        String error = PluginServices.getText(this, "Error_entrada_datos");
138
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
139
                        geoProcessingClipPanel.error(errorDescription, error);
140
                        return false;
141
                }
142
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
143
                        String error = PluginServices.getText(this, "Error_entrada_datos");
144
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
145
                        geoProcessingClipPanel.error(errorDescription, error);
146
                        return false;
147
                }
148
                if (outputFile.exists()) {
149
                        if (!geoProcessingClipPanel.askForOverwriteOutputFile(outputFile)) {
150
                                return false;
151
                        }
152
                }
153

    
154
                ClipGeoprocess clip = new ClipGeoprocess(inputLayer);
155
                clip.setSecondOperand(clippingLayer);
156
                SHPLayerDefinition definition = (SHPLayerDefinition) clip
157
                                .createLayerDefinition();
158
                definition.setFile(outputFile);
159
                ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
160
                IWriter writer = null;
161
                try {
162
                        writer = getShpWriter(definition);
163
                } catch (Exception e1) {
164
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
165
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
166
                        geoProcessingClipPanel.error(errorDescription, error);
167
                        return false;
168
                }
169
                clip.setResultLayerProperties(writer, schemaManager);
170

    
171
                HashMap params = new HashMap();
172
                boolean onlySelectedFirst = geoProcessingClipPanel
173
                                .onlyFirstLayerSelected();
174
                boolean onlySelectedSecond = geoProcessingClipPanel
175
                                .onlySecondLayerSelected();
176
                Boolean first = new Boolean(onlySelectedFirst);
177
                params.put("firstlayerselection", first);
178

    
179
                Boolean second = new Boolean(onlySelectedSecond);
180
                params.put("secondlayerselection", second);
181

    
182
                try {
183
                        clip.setParameters(params);
184
                        clip.checkPreconditions();
185
                        IMonitorableTask task1 = clip.createTask();
186
                        AddResultLayerTask task2 = new AddResultLayerTask(clip);
187
                        task2.setLayers(layers);
188
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
189
                                        task2);
190
                        if(clippingLayer.getISpatialIndex() == null){
191
                                final IMonitorableTask sptIdxTask =
192
                                        geoProcessingClipPanel.askForSpatialIndexCreation(clippingLayer);
193
                                if(sptIdxTask != null){
194
                                        PluginServices.backgroundExecution(
195
                                                        new Runnable(){
196
                                                public void run() {
197
                                                        PluginServices.
198
                                                        cancelableBackgroundExecution(sptIdxTask);
199
                                                }}
200
                                        );
201
                                }
202
                        }//if
203
                        if (globalTask.preprocess())
204
                                PluginServices.cancelableBackgroundExecution(globalTask);
205
                } catch (GeoprocessException e) {
206
                        String error = PluginServices.getText(this, "Error_ejecucion");
207
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
208
                        geoProcessingClipPanel.error(errorDescription, error);
209
                        return false;
210
                }
211
                return true;
212
        }
213

    
214
        public int getWidth() {
215
                return 700;
216
        }
217

    
218
        public int getHeight() {
219
                return 300;
220
        }
221

    
222
}
223