Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_892 / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / clip / ClipGeoprocessController.java @ 10278

History | View | Annotate | Download (7.54 KB)

1 5918 azabala
/*
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$
47
* $Log$
48 9100 jmvivo
* Revision 1.4.2.2  2006-11-29 12:51:36  jmvivo
49
* Se ha a?adido mas informaci?n al mensaje de error para los GeoprocessException: e.getMessage()
50
*
51
* Revision 1.4.2.1  2006/11/15 00:08:17  jjdelcerro
52 8765 jjdelcerro
* *** empty log message ***
53
*
54
* Revision 1.5  2006/10/23 10:28:45  caballero
55
* ancho y alto del panel
56
*
57
* Revision 1.4  2006/08/11 16:18:48  azabala
58 6747 azabala
* refactoring to make logic independent from UI
59
*
60
* Revision 1.3  2006/07/21 09:10:34  azabala
61 6489 azabala
* 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 6093 fjp
* Cambios ISchemaManager y IFieldManager por terminar
65
*
66
* Revision 1.1  2006/06/20 18:20:45  azabala
67 5918 azabala
* first version in cvs
68
*
69
* Revision 1.4  2006/06/12 19:15:38  azabala
70
* cambios para poder trabajar en geoprocessing con capas MULTI (dxf, jdbc, etc)
71
*
72
* Revision 1.3  2006/06/02 18:21:28  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.2  2006/05/25 08:21:48  jmvivo
76
* A?adida peticion de confirmacion para sobreescribir el fichero de salida, si este ya existiera
77
*
78
* Revision 1.1  2006/05/24 21:14:17  azabala
79
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
80
*
81
* Revision 1.2  2006/05/08 15:35:13  azabala
82
* refactoring of ITask api
83
*
84
* Revision 1.1  2006/04/11 17:55:51  azabala
85
* primera version en cvs
86
*
87
*
88
*/
89
package com.iver.cit.gvsig.geoprocess.impl.clip;
90
91
import java.io.File;
92 6489 azabala
import java.io.FileNotFoundException;
93 5918 azabala
import java.util.HashMap;
94
95
import com.iver.andami.PluginServices;
96
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
97
import com.iver.cit.gvsig.fmap.edition.IWriter;
98
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
99
import com.iver.cit.gvsig.fmap.layers.FLayers;
100
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
101
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
102
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
103
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
104
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
105
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
106 6747 azabala
import com.iver.cit.gvsig.geoprocess.core.gui.OverlayPanelIF;
107 5918 azabala
import com.iver.cit.gvsig.geoprocess.impl.clip.fmap.ClipGeoprocess;
108
import com.iver.utiles.swing.threads.IMonitorableTask;
109
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
110
111
public class ClipGeoprocessController extends AbstractGeoprocessController {
112
113 6747 azabala
        private OverlayPanelIF geoProcessingClipPanel;
114 5918 azabala
        private ClipGeoprocess clip;
115 8765 jjdelcerro
116 5918 azabala
        public void setView(IGeoprocessPanel viewPanel) {
117 8765 jjdelcerro
                this.geoProcessingClipPanel =
118 6747 azabala
                        (OverlayPanelIF) viewPanel;
119 5918 azabala
        }
120
121
        public IGeoprocess getGeoprocess() {
122
                return clip;
123
        }
124
125
        public boolean launchGeoprocess() {
126
                FLyrVect inputLayer = geoProcessingClipPanel.getInputLayer();
127
                FLayers layers = geoProcessingClipPanel.getFLayers();
128
                FLyrVect clippingLayer = geoProcessingClipPanel.getSecondLayer();
129
                /*
130 8765 jjdelcerro
                 * Esta parte hay que mejorarla, pues est? previsto que el
131 5918 azabala
                 * resultado de los geoprocesos se pueda guardar en distintos
132
                 * formatos:
133
                 * shp, dxf, dgn, JDBC, etc
134 8765 jjdelcerro
                 *
135 5918 azabala
                 * Por este motivo, lo mas normal es que el usuario eliga
136
                 * un driver de escritura, y el clipPanel devuelva una instancia
137
                 * de IWriter sin inicializar.
138
                 */
139 6489 azabala
                File outputFile = null;
140
                try {
141
                        outputFile = geoProcessingClipPanel.getOutputFile();
142
                } catch (FileNotFoundException e3) {
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 5918 azabala
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
149
                        String error = PluginServices.getText(this, "Error_entrada_datos");
150
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
151
                        geoProcessingClipPanel.error(errorDescription, error);
152
                        return false;
153
                }
154
                if (outputFile.exists()) {
155
                        if (!geoProcessingClipPanel.askForOverwriteOutputFile(outputFile)) {
156
                                return false;
157
                        }
158
                }
159 8765 jjdelcerro
160 5918 azabala
                ClipGeoprocess clip = new ClipGeoprocess(inputLayer);
161
                clip.setSecondOperand(clippingLayer);
162
                SHPLayerDefinition definition = (SHPLayerDefinition) clip
163
                                .createLayerDefinition();
164
                definition.setFile(outputFile);
165 6093 fjp
                ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
166 5918 azabala
                IWriter writer = null;
167
                try {
168
                        writer = getShpWriter(definition);
169
                } catch (Exception e1) {
170
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
171
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
172
                        geoProcessingClipPanel.error(errorDescription, error);
173
                        return false;
174 8765 jjdelcerro
                }
175 5918 azabala
                clip.setResultLayerProperties(writer, schemaManager);
176 8765 jjdelcerro
177 5918 azabala
                HashMap params = new HashMap();
178
                boolean onlySelectedFirst = geoProcessingClipPanel
179
                                .onlyFirstLayerSelected();
180
                boolean onlySelectedSecond = geoProcessingClipPanel
181
                                .onlySecondLayerSelected();
182
                Boolean first = new Boolean(onlySelectedFirst);
183
                params.put("firstlayerselection", first);
184
185
                Boolean second = new Boolean(onlySelectedSecond);
186
                params.put("secondlayerselection", second);
187
188
                try {
189
                        clip.setParameters(params);
190
                        clip.checkPreconditions();
191
                        IMonitorableTask task1 = clip.createTask();
192
                        AddResultLayerTask task2 = new AddResultLayerTask(clip);
193
                        task2.setLayers(layers);
194
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
195
                                        task2);
196
                        if(clippingLayer.getISpatialIndex() == null){
197 8765 jjdelcerro
                                final IMonitorableTask sptIdxTask =
198 5918 azabala
                                        geoProcessingClipPanel.askForSpatialIndexCreation(clippingLayer);
199
                                if(sptIdxTask != null){
200
                                        PluginServices.backgroundExecution(
201
                                                        new Runnable(){
202
                                                public void run() {
203
                                                        PluginServices.
204 8765 jjdelcerro
                                                        cancelableBackgroundExecution(sptIdxTask);
205 5918 azabala
                                                }}
206
                                        );
207 8765 jjdelcerro
                                }
208 5918 azabala
                        }//if
209
                        if (globalTask.preprocess())
210
                                PluginServices.cancelableBackgroundExecution(globalTask);
211
                } catch (GeoprocessException e) {
212
                        String error = PluginServices.getText(this, "Error_ejecucion");
213
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
214 9100 jmvivo
                        errorDescription = "<html>" + errorDescription + ":<br>" + e.getMessage()+ "</html>";
215 5918 azabala
                        geoProcessingClipPanel.error(errorDescription, error);
216
                        return false;
217 8765 jjdelcerro
                }
218 5918 azabala
                return true;
219
        }
220
221 8765 jjdelcerro
        public int getWidth() {
222
                return 700;
223
        }
224
225
        public int getHeight() {
226
                return 300;
227
        }
228
229 5918 azabala
}