Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / SpatialJoinGeoprocessController.java @ 4820

History | View | Annotate | Download (5.86 KB)

1 4818 azabala
/*
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$
47
* $Log$
48
* Revision 1.1  2006-04-11 17:55:51  azabala
49
* primera version en cvs
50
*
51
*
52
*/
53
package com.iver.gvsig.geoprocessing;
54
55
import java.io.File;
56
import java.util.HashMap;
57
import java.util.Map;
58
59
import com.iver.andami.PluginServices;
60
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
61
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
62
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
63
import com.iver.cit.gvsig.fmap.layers.FLayers;
64
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
65
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingSpatialjoinPanel;
66
import com.iver.gvsig.geoprocessing.impl.spatialjoin.SpatialJoinGeoprocess;
67
import com.iver.gvsig.geoprocessing.model.GeoprocessException;
68
import com.iver.gvsig.geoprocessing.model.IGeoprocess;
69
import com.iver.utiles.swing.threads.IMonitorableTask;
70
import com.iver.utiles.swing.threads.MonitorableDecorator;
71
72
public class SpatialJoinGeoprocessController extends
73
                AbstractGeoprocessController {
74
75
        private GeoProcessingSpatialjoinPanel geoProcessingSpatialjoinPanel;
76
        private SpatialJoinGeoprocess spatialJoin;
77
78
        public void setView(IGeoprocessPanel viewPanel) {
79
                this.geoProcessingSpatialjoinPanel =
80
                        (GeoProcessingSpatialjoinPanel) viewPanel;
81
        }
82
        public IGeoprocess getGeoprocess() {
83
                return spatialJoin;
84
        }
85
86
        public boolean launchGeoprocess() {
87
//                 this open a modal dialog and sets sumarize functions
88
                Map sumarizeFunctions = null;
89
                if (!geoProcessingSpatialjoinPanel.isNearestSelected()){
90
                        boolean isOk = geoProcessingSpatialjoinPanel.openSumarizeFunction();
91
                        if(! isOk){
92
                                //Ok button wasnt pushed. Dialog was closed directly
93
                                return false;
94
                        }
95
                        sumarizeFunctions = geoProcessingSpatialjoinPanel
96
                                .getSumarizeFunctions();
97
                        if(sumarizeFunctions == null || sumarizeFunctions.size() == 0){
98
                                geoProcessingSpatialjoinPanel.error(PluginServices.getText(this, "Error_spjoinmn_sin_funcion"),
99
                                PluginServices.getText(this, "Error_entrada_datos"));
100
                        }
101
                }
102
                FLyrVect inputLayer = geoProcessingSpatialjoinPanel.getFirstLayer();
103
                FLyrVect secondLayer = geoProcessingSpatialjoinPanel.getSecondLayer();
104
                FLayers layers = geoProcessingSpatialjoinPanel.getFLayers();
105
                File outputFile = geoProcessingSpatialjoinPanel.getOutputFile();
106
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
107
                        String error = PluginServices.getText(this, "Error_entrada_datos");
108
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
109
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
110
                        return false;
111
                }
112
                boolean onlyFirstSelection = geoProcessingSpatialjoinPanel
113
                                .onlyFirstLayerSelected();
114
                boolean onlySecondSelection = geoProcessingSpatialjoinPanel
115
                                .onlySecondLayerSelected();
116
                boolean nearest = geoProcessingSpatialjoinPanel.isNearestSelected();
117
                SpatialJoinGeoprocess join = new SpatialJoinGeoprocess(inputLayer);
118
                join.setSecondOperand(secondLayer);
119
                join.setFields_sumFunctions(sumarizeFunctions);
120
                HashMap params = new HashMap();
121
                Boolean first = new Boolean(onlyFirstSelection);
122
                params.put("firstlayerselection", first);
123
124
                Boolean second = new Boolean(onlySecondSelection);
125
                params.put("secondlayerselection", second);
126
127
                Boolean nearSel = new Boolean(nearest);
128
                params.put("nearest", nearSel);
129
                try {
130
                        ShpSchemaManager schemaManager = new ShpSchemaManager();
131
                        ShpWriter writer = new ShpWriter();
132
                        join.setResultLayerProperties(writer, schemaManager);
133
                        join.setParameters(params);
134
                        SHPLayerDefinition definition =
135
                                (SHPLayerDefinition) join.createLayerDefinition();
136
                        definition.setFile(outputFile);
137
                        writer.setFile(definition.getFile());
138
                        writer.initialize(definition);
139
                        writer = getShpWriter(definition);
140
                        join.checkPreconditions();
141
                        IMonitorableTask task1 = join.createTask();
142
                        if(task1 == null){
143
                                //mensaje de error
144
                                return false;
145
                        }
146
                        AddResultLayerTask task2 = new AddResultLayerTask(join);
147
                        task2.setLayers(layers);
148
                        MonitorableDecorator globalTask = new MonitorableDecorator(task1,
149
                                        task2);
150
                        if (globalTask.preprocess())
151
                                PluginServices.cancelableBackgroundExecution(globalTask);
152
                } catch (GeoprocessException e) {
153
                        String error = PluginServices.getText(this, "Error_ejecucion");
154
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
155
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
156
                        return false;
157
158
                } catch (Exception e) {
159
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
160
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
161
                        geoProcessingSpatialjoinPanel.error(errorDescription, error);
162
                        return false;
163
                }
164
                return true;
165
        }
166
167
}