Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1003 / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / spatialjoin / SpatialJoinGeoprocessController.java @ 12271

History | View | Annotate | Download (8.47 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: SpatialJoinGeoprocessController.java 12271 2007-06-21 10:50:33Z  $
47
* $Log$
48
* 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
* *** empty log message ***
53
*
54
* Revision 1.5  2006/10/23 10:29:18  caballero
55
* ancho y alto del panel
56
*
57
* Revision 1.4  2006/08/11 16:30:38  azabala
58
* *** empty log message ***
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.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:09:47  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:32  azabala
82
* *** empty log message ***
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.spatialjoin;
90

    
91
import java.io.File;
92
import java.io.FileNotFoundException;
93
import java.util.HashMap;
94
import java.util.Map;
95

    
96
import com.iver.andami.PluginServices;
97
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
98
import com.iver.cit.gvsig.fmap.edition.IWriter;
99
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
100
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
101
import com.iver.cit.gvsig.fmap.layers.FLayers;
102
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
103
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
104
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
105
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
106
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
107
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
108
import com.iver.cit.gvsig.geoprocess.impl.spatialjoin.fmap.SpatialJoinGeoprocess;
109
import com.iver.cit.gvsig.geoprocess.impl.spatialjoin.gui.SpatialJoinPanelIF;
110
import com.iver.utiles.swing.threads.IMonitorableTask;
111
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
112

    
113
public class SpatialJoinGeoprocessController extends
114
                AbstractGeoprocessController {
115

    
116
        private SpatialJoinPanelIF geoProcessingSpatialjoinPanel;
117
        private SpatialJoinGeoprocess spatialJoin;
118

    
119
        public void setView(IGeoprocessPanel viewPanel) {
120
                this.geoProcessingSpatialjoinPanel =
121
                        (SpatialJoinPanelIF) viewPanel;
122
        }
123
        public IGeoprocess getGeoprocess() {
124
                return spatialJoin;
125
        }
126

    
127
        public boolean launchGeoprocess() {
128
//                 this open a modal dialog and sets sumarize functions
129
                Map sumarizeFunctions = null;
130
                if (!geoProcessingSpatialjoinPanel.isNearestSelected()){
131
                        boolean isOk = geoProcessingSpatialjoinPanel.openSumarizeFunction();
132
                        if(! isOk){
133
                                //Ok button wasnt pushed. Dialog was closed directly
134
                                return false;
135
                        }
136
                        sumarizeFunctions = geoProcessingSpatialjoinPanel
137
                                .getSumarizeFunctions();
138
                        if(sumarizeFunctions == null || sumarizeFunctions.size() == 0){
139
                                geoProcessingSpatialjoinPanel.error(PluginServices.getText(this, "Error_spjoinmn_sin_funcion"),
140
                                PluginServices.getText(this, "Error_entrada_datos"));
141
                                return false;
142
                        }
143
                }
144
                FLyrVect inputLayer = geoProcessingSpatialjoinPanel.getFirstLayer();
145
                FLyrVect secondLayer = geoProcessingSpatialjoinPanel.getSecondLayer();
146
                FLayers layers = geoProcessingSpatialjoinPanel.getFLayers();
147
                File outputFile = null;
148
                try {
149
                        outputFile = geoProcessingSpatialjoinPanel.getOutputFile();
150
                } catch (FileNotFoundException e3) {
151
                        String error = PluginServices.getText(this, "Error_entrada_datos");
152
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
153
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
154
                        return false;
155
                }
156
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
157
                        String error = PluginServices.getText(this, "Error_entrada_datos");
158
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
159
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
160
                        return false;
161
                }
162
                if (outputFile.exists()) {
163
                        if (!geoProcessingSpatialjoinPanel.askForOverwriteOutputFile(outputFile)) {
164
                                return false;
165
                        }
166
                }
167

    
168
                boolean onlyFirstSelection = geoProcessingSpatialjoinPanel
169
                                .onlyFirstLayerSelected();
170
                boolean onlySecondSelection = geoProcessingSpatialjoinPanel
171
                                .onlySecondLayerSelected();
172
                boolean nearest = geoProcessingSpatialjoinPanel.isNearestSelected();
173

    
174

    
175
                SpatialJoinGeoprocess join = new SpatialJoinGeoprocess(inputLayer);
176
                join.setSecondOperand(secondLayer);
177
                join.setFields_sumFunctions(sumarizeFunctions);
178
                HashMap params = new HashMap();
179
                Boolean first = new Boolean(onlyFirstSelection);
180
                params.put("firstlayerselection", first);
181

    
182
                Boolean second = new Boolean(onlySecondSelection);
183
                params.put("secondlayerselection", second);
184
                if(! nearest){
185
                        if(!secondLayer.isSpatiallyIndexed()){
186
                                final IMonitorableTask sptIdxTask =
187
                                        geoProcessingSpatialjoinPanel.
188
                                        askForSpatialIndexCreation(secondLayer);
189
                                if(sptIdxTask != null){
190
                                        PluginServices.backgroundExecution(
191
                                                        new Runnable(){
192
                                                public void run() {
193
                                                        PluginServices.
194
                                                        cancelableBackgroundExecution(sptIdxTask);
195
                                                }}
196
                                        );
197
                                }        //if
198
                        }//if
199

    
200
                }//if nearest
201
                Boolean nearSel = new Boolean(nearest);
202
                params.put("nearest", nearSel);
203
                try {
204
                        ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
205
                        IWriter writer = new ShpWriter();
206
                        join.setResultLayerProperties(writer, schemaManager);
207
                        join.setParameters(params);
208
                        SHPLayerDefinition definition =
209
                                (SHPLayerDefinition) join.createLayerDefinition();
210
                        definition.setFile(outputFile);
211
                        ((ShpWriter)writer).setFile(definition.getFile());
212
                        writer.initialize(definition);
213
                        writer = getShpWriter(definition);
214
                        join.checkPreconditions();
215
                        IMonitorableTask task1 = join.createTask();
216
                        if(task1 == null){
217
                                //mensaje de error
218
                                return false;
219
                        }
220
                        AddResultLayerTask task2 = new AddResultLayerTask(join);
221
                        task2.setLayers(layers);
222
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
223
                                        task2);
224
                        if (globalTask.preprocess())
225
                                PluginServices.cancelableBackgroundExecution(globalTask);
226
                } catch (GeoprocessException e) {
227
                        String error = PluginServices.getText(this, "Error_ejecucion");
228
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
229
                        errorDescription = "<html>" + errorDescription + ":<br>" + e.getMessage()+ "</html>";
230
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
231
                        return false;
232

    
233
                } catch (Exception e) {
234
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
235
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
236
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
237
                        return false;
238
                }
239
                return true;
240
        }
241
        public int getWidth() {
242
                return 700;
243
        }
244

    
245
        public int getHeight() {
246
                return 350;
247
        }
248
}
249