Statistics
| Revision:

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

History | View | Annotate | Download (8.74 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 13593 2007-09-10 08:11:47Z  $
47
* $Log$
48
* Revision 1.4.2.3  2007-07-12 16:54:35  azabala
49
* bug solved when try to do a one to many spatial join with layers without numeric fields
50
*
51
* Revision 1.4.2.2  2006/11/29 12:51:36  jmvivo
52
* Se ha a?adido mas informaci?n al mensaje de error para los GeoprocessException: e.getMessage()
53
*
54
* Revision 1.4.2.1  2006/11/15 00:08:17  jjdelcerro
55
* *** empty log message ***
56
*
57
* Revision 1.5  2006/10/23 10:29:18  caballero
58
* ancho y alto del panel
59
*
60
* Revision 1.4  2006/08/11 16:30:38  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.3  2006/07/21 09:10:34  azabala
64
* fixed bug 608: user doesnt enter any result file to the geoprocess panel
65
*
66
* Revision 1.2  2006/06/29 07:33:57  fjp
67
* Cambios ISchemaManager y IFieldManager por terminar
68
*
69
* Revision 1.1  2006/06/20 18:20:45  azabala
70
* first version in cvs
71
*
72
* Revision 1.4  2006/06/12 19:15:38  azabala
73
* cambios para poder trabajar en geoprocessing con capas MULTI (dxf, jdbc, etc)
74
*
75
* Revision 1.3  2006/06/02 18:21:28  azabala
76
* *** empty log message ***
77
*
78
* Revision 1.2  2006/05/25 08:21:48  jmvivo
79
* A?adida peticion de confirmacion para sobreescribir el fichero de salida, si este ya existiera
80
*
81
* Revision 1.1  2006/05/24 21:09:47  azabala
82
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
83
*
84
* Revision 1.2  2006/05/08 15:35:32  azabala
85
* *** empty log message ***
86
*
87
* Revision 1.1  2006/04/11 17:55:51  azabala
88
* primera version en cvs
89
*
90
*
91
*/
92
package com.iver.cit.gvsig.geoprocess.impl.spatialjoin;
93

    
94
import java.io.File;
95
import java.io.FileNotFoundException;
96
import java.util.HashMap;
97
import java.util.Map;
98

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

    
117
public class SpatialJoinGeoprocessController extends
118
                AbstractGeoprocessController {
119

    
120
        private SpatialJoinPanelIF geoProcessingSpatialjoinPanel;
121
        private SpatialJoinGeoprocess spatialJoin;
122

    
123
        public void setView(IGeoprocessPanel viewPanel) {
124
                this.geoProcessingSpatialjoinPanel =
125
                        (SpatialJoinPanelIF) viewPanel;
126
        }
127
        public IGeoprocess getGeoprocess() {
128
                return spatialJoin;
129
        }
130

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

    
177
                boolean onlyFirstSelection = geoProcessingSpatialjoinPanel
178
                                .onlyFirstLayerSelected();
179
                boolean onlySecondSelection = geoProcessingSpatialjoinPanel
180
                                .onlySecondLayerSelected();
181
                boolean nearest = geoProcessingSpatialjoinPanel.isNearestSelected();
182

    
183

    
184
                SpatialJoinGeoprocess join = new SpatialJoinGeoprocess(inputLayer);
185
                join.setSecondOperand(secondLayer);
186
                join.setFields_sumFunctions(sumarizeFunctions);
187
                HashMap params = new HashMap();
188
                Boolean first = new Boolean(onlyFirstSelection);
189
                params.put("firstlayerselection", first);
190

    
191
                Boolean second = new Boolean(onlySecondSelection);
192
                params.put("secondlayerselection", second);
193
                if(! nearest){
194
                        if(!secondLayer.isSpatiallyIndexed()){
195
                                final IMonitorableTask sptIdxTask =
196
                                        geoProcessingSpatialjoinPanel.
197
                                        askForSpatialIndexCreation(secondLayer);
198
                                if(sptIdxTask != null){
199
                                        PluginServices.backgroundExecution(
200
                                                        new Runnable(){
201
                                                public void run() {
202
                                                        PluginServices.
203
                                                        cancelableBackgroundExecution(sptIdxTask);
204
                                                }}
205
                                        );
206
                                }        //if
207
                        }//if
208

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

    
242
                } catch (Exception e) {
243
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
244
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
245
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
246
                        return false;
247
                }
248
                return true;
249
        }
250
        public int getWidth() {
251
                return 700;
252
        }
253

    
254
        public int getHeight() {
255
                return 350;
256
        }
257
}
258