Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / spatialjoin / SpatialJoinGeoprocessController.java @ 6749

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

    
82
import java.io.File;
83
import java.io.FileNotFoundException;
84
import java.util.HashMap;
85
import java.util.Map;
86

    
87
import com.iver.andami.PluginServices;
88
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
89
import com.iver.cit.gvsig.fmap.edition.IWriter;
90
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
91
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
92
import com.iver.cit.gvsig.fmap.layers.FLayers;
93
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
94
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
95
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
96
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
97
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
98
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
99
import com.iver.cit.gvsig.geoprocess.impl.spatialjoin.fmap.SpatialJoinGeoprocess;
100
import com.iver.cit.gvsig.geoprocess.impl.spatialjoin.gui.SpatialJoinPanelIF;
101
import com.iver.utiles.swing.threads.IMonitorableTask;
102
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
103

    
104
public class SpatialJoinGeoprocessController extends
105
                AbstractGeoprocessController {
106

    
107
        private SpatialJoinPanelIF geoProcessingSpatialjoinPanel;
108
        private SpatialJoinGeoprocess spatialJoin;
109
        
110
        public void setView(IGeoprocessPanel viewPanel) {
111
                this.geoProcessingSpatialjoinPanel = 
112
                        (SpatialJoinPanelIF) viewPanel;
113
        }
114
        public IGeoprocess getGeoprocess() {
115
                return spatialJoin;
116
        }
117

    
118
        public boolean launchGeoprocess() {
119
//                 this open a modal dialog and sets sumarize functions
120
                Map sumarizeFunctions = null;
121
                if (!geoProcessingSpatialjoinPanel.isNearestSelected()){
122
                        boolean isOk = geoProcessingSpatialjoinPanel.openSumarizeFunction();
123
                        if(! isOk){
124
                                //Ok button wasnt pushed. Dialog was closed directly
125
                                return false;
126
                        }
127
                        sumarizeFunctions = geoProcessingSpatialjoinPanel
128
                                .getSumarizeFunctions();
129
                        if(sumarizeFunctions == null || sumarizeFunctions.size() == 0){
130
                                geoProcessingSpatialjoinPanel.error(PluginServices.getText(this, "Error_spjoinmn_sin_funcion"),
131
                                PluginServices.getText(this, "Error_entrada_datos"));
132
                                return false;
133
                        }
134
                }
135
                FLyrVect inputLayer = geoProcessingSpatialjoinPanel.getFirstLayer();
136
                FLyrVect secondLayer = geoProcessingSpatialjoinPanel.getSecondLayer();
137
                FLayers layers = geoProcessingSpatialjoinPanel.getFLayers();
138
                File outputFile = null;
139
                try {
140
                        outputFile = geoProcessingSpatialjoinPanel.getOutputFile();
141
                } catch (FileNotFoundException e3) {
142
                        String error = PluginServices.getText(this, "Error_entrada_datos");
143
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
144
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
145
                        return false;
146
                }
147
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
148
                        String error = PluginServices.getText(this, "Error_entrada_datos");
149
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
150
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
151
                        return false;
152
                }
153
                if (outputFile.exists()) {
154
                        if (!geoProcessingSpatialjoinPanel.askForOverwriteOutputFile(outputFile)) {
155
                                return false;
156
                        }
157
                }
158
                
159
                boolean onlyFirstSelection = geoProcessingSpatialjoinPanel
160
                                .onlyFirstLayerSelected();
161
                boolean onlySecondSelection = geoProcessingSpatialjoinPanel
162
                                .onlySecondLayerSelected();
163
                boolean nearest = geoProcessingSpatialjoinPanel.isNearestSelected();
164
                
165
                
166
                SpatialJoinGeoprocess join = new SpatialJoinGeoprocess(inputLayer);
167
                join.setSecondOperand(secondLayer);
168
                join.setFields_sumFunctions(sumarizeFunctions);
169
                HashMap params = new HashMap();
170
                Boolean first = new Boolean(onlyFirstSelection);
171
                params.put("firstlayerselection", first);
172

    
173
                Boolean second = new Boolean(onlySecondSelection);
174
                params.put("secondlayerselection", second);
175
                if(! nearest){
176
                        if(!secondLayer.isSpatiallyIndexed()){
177
                                final IMonitorableTask sptIdxTask = 
178
                                        geoProcessingSpatialjoinPanel.
179
                                        askForSpatialIndexCreation(secondLayer);
180
                                if(sptIdxTask != null){
181
                                        PluginServices.backgroundExecution(
182
                                                        new Runnable(){
183
                                                public void run() {
184
                                                        PluginServices.
185
                                                        cancelableBackgroundExecution(sptIdxTask);        
186
                                                }}
187
                                        );
188
                                }        //if
189
                        }//if
190
                        
191
                }//if nearest
192
                Boolean nearSel = new Boolean(nearest);
193
                params.put("nearest", nearSel);
194
                try {
195
                        ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
196
                        IWriter writer = new ShpWriter();
197
                        join.setResultLayerProperties(writer, schemaManager);
198
                        join.setParameters(params);
199
                        SHPLayerDefinition definition = 
200
                                (SHPLayerDefinition) join.createLayerDefinition();
201
                        definition.setFile(outputFile);
202
                        ((ShpWriter)writer).setFile(definition.getFile());
203
                        writer.initialize(definition);
204
                        writer = getShpWriter(definition);
205
                        join.checkPreconditions();
206
                        IMonitorableTask task1 = join.createTask();
207
                        if(task1 == null){
208
                                //mensaje de error
209
                                return false;
210
                        }
211
                        AddResultLayerTask task2 = new AddResultLayerTask(join);
212
                        task2.setLayers(layers);
213
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
214
                                        task2);
215
                        if (globalTask.preprocess())
216
                                PluginServices.cancelableBackgroundExecution(globalTask);
217
                } catch (GeoprocessException e) {
218
                        String error = PluginServices.getText(this, "Error_ejecucion");
219
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
220
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
221
                        return false;
222

    
223
                } catch (Exception e) {
224
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
225
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
226
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
227
                        return false;
228
                }
229
                return true;
230
        }
231

    
232
}
233