Statistics
| Revision:

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

History | View | Annotate | Download (8.61 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 21232 2008-06-05 14:03:49Z azabala $
47
* $Log$
48
* Revision 1.7  2007-08-07 16:07:34  azabala
49
* bug solved when a layer vect hasnt numeric fields
50
*
51
* Revision 1.6  2006/11/29 13:11:23  jmvivo
52
* Se ha a?adido mas informaci?n al mensaje de error para los GeoprocessException: e.getMessage()
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.fmap.XTypes;
107
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
108
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessUserEntries;
109
import com.iver.cit.gvsig.geoprocess.impl.spatialjoin.fmap.SpatialJoinGeoprocess;
110
import com.iver.cit.gvsig.geoprocess.impl.spatialjoin.gui.SpatialJoinPanelIF;
111
import com.iver.utiles.swing.threads.IMonitorableTask;
112
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
113

    
114
public class SpatialJoinGeoprocessController extends
115
                AbstractGeoprocessController {
116

    
117
        private SpatialJoinPanelIF geoProcessingSpatialjoinPanel;
118
        private SpatialJoinGeoprocess spatialJoin;
119

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

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

    
171
                boolean onlyFirstSelection = geoProcessingSpatialjoinPanel
172
                                .onlyFirstLayerSelected();
173
                boolean onlySecondSelection = geoProcessingSpatialjoinPanel
174
                                .onlySecondLayerSelected();
175
                boolean nearest = geoProcessingSpatialjoinPanel.isNearestSelected();
176

    
177

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

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

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

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

    
248
        public int getHeight() {
249
                return 350;
250
        }
251
}
252