Statistics
| Revision:

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

History | View | Annotate | Download (6.68 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 5628 2006-06-02 18:21:28Z azabala $
47
* $Log$
48
* Revision 1.3  2006-06-02 18:21:28  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.2  2006/05/25 08:21:48  jmvivo
52
* A?adida peticion de confirmacion para sobreescribir el fichero de salida, si este ya existiera
53
*
54
* Revision 1.1  2006/05/24 21:09:47  azabala
55
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
56
*
57
* Revision 1.2  2006/05/08 15:35:32  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.1  2006/04/11 17:55:51  azabala
61
* primera version en cvs
62
*
63
*
64
*/
65
package com.iver.cit.gvsig.geoprocess.spatialjoin;
66

    
67
import java.io.File;
68
import java.util.HashMap;
69
import java.util.Map;
70

    
71
import com.iver.andami.PluginServices;
72
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
73
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
74
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
75
import com.iver.cit.gvsig.fmap.layers.FLayers;
76
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
77
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
78
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
79
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
80
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
81
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
82
import com.iver.cit.gvsig.geoprocess.spatialjoin.fmap.SpatialJoinGeoprocess;
83
import com.iver.cit.gvsig.geoprocess.spatialjoin.gui.GeoProcessingSpatialjoinPanel;
84
import com.iver.utiles.swing.threads.IMonitorableTask;
85
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
86

    
87
public class SpatialJoinGeoprocessController extends
88
                AbstractGeoprocessController {
89

    
90
        private GeoProcessingSpatialjoinPanel geoProcessingSpatialjoinPanel;
91
        private SpatialJoinGeoprocess spatialJoin;
92
        
93
        public void setView(IGeoprocessPanel viewPanel) {
94
                this.geoProcessingSpatialjoinPanel = 
95
                        (GeoProcessingSpatialjoinPanel) viewPanel;
96
        }
97
        public IGeoprocess getGeoprocess() {
98
                return spatialJoin;
99
        }
100

    
101
        public boolean launchGeoprocess() {
102
//                 this open a modal dialog and sets sumarize functions
103
                Map sumarizeFunctions = null;
104
                if (!geoProcessingSpatialjoinPanel.isNearestSelected()){
105
                        boolean isOk = geoProcessingSpatialjoinPanel.openSumarizeFunction();
106
                        if(! isOk){
107
                                //Ok button wasnt pushed. Dialog was closed directly
108
                                return false;
109
                        }
110
                        sumarizeFunctions = geoProcessingSpatialjoinPanel
111
                                .getSumarizeFunctions();
112
                        if(sumarizeFunctions == null || sumarizeFunctions.size() == 0){
113
                                geoProcessingSpatialjoinPanel.error(PluginServices.getText(this, "Error_spjoinmn_sin_funcion"),
114
                                PluginServices.getText(this, "Error_entrada_datos"));
115
                        }
116
                }
117
                FLyrVect inputLayer = geoProcessingSpatialjoinPanel.getFirstLayer();
118
                FLyrVect secondLayer = geoProcessingSpatialjoinPanel.getSecondLayer();
119
                FLayers layers = geoProcessingSpatialjoinPanel.getFLayers();
120
                File outputFile = geoProcessingSpatialjoinPanel.getOutputFile();
121
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
122
                        String error = PluginServices.getText(this, "Error_entrada_datos");
123
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
124
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
125
                        return false;
126
                }
127
                if (outputFile.exists()) {
128
                        if (!geoProcessingSpatialjoinPanel.askForOverwriteOutputFile(outputFile)) {
129
                                return false;
130
                        }
131
                }
132
                
133
                boolean onlyFirstSelection = geoProcessingSpatialjoinPanel
134
                                .onlyFirstLayerSelected();
135
                boolean onlySecondSelection = geoProcessingSpatialjoinPanel
136
                                .onlySecondLayerSelected();
137
                boolean nearest = geoProcessingSpatialjoinPanel.isNearestSelected();
138
                
139
                
140
                SpatialJoinGeoprocess join = new SpatialJoinGeoprocess(inputLayer);
141
                join.setSecondOperand(secondLayer);
142
                join.setFields_sumFunctions(sumarizeFunctions);
143
                HashMap params = new HashMap();
144
                Boolean first = new Boolean(onlyFirstSelection);
145
                params.put("firstlayerselection", first);
146

    
147
                Boolean second = new Boolean(onlySecondSelection);
148
                params.put("secondlayerselection", second);
149

    
150
                Boolean nearSel = new Boolean(nearest);
151
                params.put("nearest", nearSel);
152
                try {
153
                        ShpSchemaManager schemaManager = new ShpSchemaManager();
154
                        ShpWriter writer = new ShpWriter();
155
                        join.setResultLayerProperties(writer, schemaManager);
156
                        join.setParameters(params);
157
                        SHPLayerDefinition definition = 
158
                                (SHPLayerDefinition) join.createLayerDefinition();
159
                        definition.setFile(outputFile);
160
                        writer.setFile(definition.getFile());
161
                        writer.initialize(definition);
162
                        writer = getShpWriter(definition);
163
                        join.checkPreconditions();
164
                        IMonitorableTask task1 = join.createTask();
165
                        if(task1 == null){
166
                                //mensaje de error
167
                                return false;
168
                        }
169
                        AddResultLayerTask task2 = new AddResultLayerTask(join);
170
                        task2.setLayers(layers);
171
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
172
                                        task2);
173
                        if (globalTask.preprocess())
174
                                PluginServices.cancelableBackgroundExecution(globalTask);
175
                } catch (GeoprocessException e) {
176
                        String error = PluginServices.getText(this, "Error_ejecucion");
177
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
178
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
179
                        return false;
180

    
181
                } catch (Exception e) {
182
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
183
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
184
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
185
                        return false;
186
                }
187
                return true;
188
        }
189

    
190
}
191