Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / buffer / BufferGeoprocessController.java @ 8235

History | View | Annotate | Download (9.07 KB)

1
/*
2
 * Created on 10-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: BufferGeoprocessController.java 8235 2006-10-23 10:29:18Z caballero $
47
* $Log$
48
* Revision 1.5  2006-10-23 10:27:38  caballero
49
* ancho y alto del panel
50
*
51
* Revision 1.4  2006/08/11 16:13:08  azabala
52
* refactoring to make logic independent from UI
53
*
54
* Revision 1.3  2006/07/21 09:10:34  azabala
55
* fixed bug 608: user doesnt enter any result file to the geoprocess panel
56
*
57
* Revision 1.2  2006/06/29 07:33:57  fjp
58
* Cambios ISchemaManager y IFieldManager por terminar
59
*
60
* Revision 1.1  2006/06/20 18:20:45  azabala
61
* first version in cvs
62
*
63
* Revision 1.4  2006/06/12 19:15:38  azabala
64
* cambios para poder trabajar en geoprocessing con capas MULTI (dxf, jdbc, etc)
65
*
66
* Revision 1.3  2006/06/02 18:20:04  azabala
67
* cuando el buffer es con dissolve se crea indice espacial para optimizar
68
*
69
* Revision 1.2  2006/05/25 08:21:48  jmvivo
70
* A?adida peticion de confirmacion para sobreescribir el fichero de salida, si este ya existiera
71
*
72
* Revision 1.1  2006/05/24 21:15:07  azabala
73
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
74
*
75
* Revision 1.3  2006/05/08 15:34:59  azabala
76
* refactoring of ITask api
77
*
78
* Revision 1.2  2006/05/01 19:20:14  azabala
79
* revisi?n general del buffer (a?adidos anillos concentricos, buffers interiores y exteriores, etc)
80
*
81
* Revision 1.1  2006/04/11 17:55:51  azabala
82
* primera version en cvs
83
*
84
*
85
*/
86
package com.iver.cit.gvsig.geoprocess.impl.buffer;
87

    
88
import java.io.File;
89
import java.io.FileNotFoundException;
90
import java.util.HashMap;
91

    
92
import com.iver.andami.PluginServices;
93
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
94
import com.iver.cit.gvsig.fmap.edition.IWriter;
95
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
96
import com.iver.cit.gvsig.fmap.layers.FLayers;
97
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
98
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
99
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
100
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
101
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
102
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
103
import com.iver.cit.gvsig.geoprocess.impl.buffer.fmap.BufferGeoprocess;
104
import com.iver.cit.gvsig.geoprocess.impl.buffer.fmap.BufferVisitor;
105
import com.iver.cit.gvsig.geoprocess.impl.buffer.gui.BufferPanelIF;
106
import com.iver.utiles.swing.threads.IMonitorableTask;
107
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
108
/**
109
 * Controller class for a Buffer Geoprocess
110
 * @author azabala
111
 *
112
 */
113
public class BufferGeoprocessController extends
114
                                        AbstractGeoprocessController {
115
        /**
116
         * UI class to read user entries.
117
         * By using UI interfaces, we are allowing geoprocessing
118
         * work with GUI and Command Line UI.
119
         */
120
        private BufferPanelIF bufferPanel;
121
        /**
122
         * Geoprocess we are going to launch with
123
         * launchGeoprocess() method
124
         */
125
        private BufferGeoprocess buffer;
126

    
127
        /**
128
         * Default constructor
129
         *
130
         */
131
        public BufferGeoprocessController(){}
132

    
133
        /**
134
         * Sets GUI panel to read user entries
135
         */
136
        public void setView(IGeoprocessPanel viewPanel) {
137
                this.bufferPanel = (BufferPanelIF) viewPanel;
138
        }
139

    
140
        public IGeoprocess getGeoprocess() {
141
                return buffer;
142
        }
143

    
144
        public boolean launchGeoprocess() {
145
                FLyrVect inputLayer = bufferPanel.getInputLayer();
146
                FLayers layers = bufferPanel.getFLayers();
147
                File outputFile = null;
148
                try {
149
                        outputFile = bufferPanel.getOutputFile();
150
                } catch (FileNotFoundException e3) {
151
                        String error = PluginServices.getText(this, "Error_entrada_datos");
152
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
153
                        bufferPanel.error(errorDescription, error);
154
                        return false;
155
                }
156
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
157
                        String error = PluginServices.getText(this, "Error_entrada_datos");
158
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
159
                        bufferPanel.error(errorDescription, error);
160
                        return false;
161
                }
162
                if (outputFile.exists()) {
163
                        if (!bufferPanel.askForOverwriteOutputFile(outputFile)) {
164
                                return false;
165
                        }
166
                }
167
                buffer = new BufferGeoprocess(inputLayer);
168
                HashMap params = new HashMap();
169
                boolean onlySelected = bufferPanel.isBufferOnlySelected();
170
                params.put("layer_selection", new Boolean(onlySelected));
171
                boolean dissolveBuffer = bufferPanel
172
                                .isDissolveBuffersSelected();
173
                params.put("dissolve_buffers", new Boolean(dissolveBuffer));
174
                byte strategy = 0;
175
                if (bufferPanel.isConstantDistanceSelected()) {
176
                        strategy = BufferGeoprocess.CONSTANT_DISTANCE_STRATEGY;
177
                        double bufferDistance = -1;
178
                        try {
179
                                bufferDistance = bufferPanel.getConstantDistance();
180
                        } catch (GeoprocessException e) {
181
                                String error = PluginServices.getText(this, "Error_entrada_datos");
182
                                String errorDescription = PluginServices.getText(this, "Error_distancia_buffer");
183
                                bufferPanel.error(errorDescription, error);
184
                                return false;
185
                        }
186
                        params.put("buffer_distance", new Double(bufferDistance));
187
                } else if (bufferPanel.isAttributeDistanceSelected()) {
188
                        strategy = BufferGeoprocess.ATTRIBUTE_DISTANCE_STRATEGY;
189
                        String attributeName = null;
190
                        try {
191
                                attributeName = bufferPanel
192
                                                .getAttributeDistanceField();
193
                        } catch (GeoprocessException e) {
194
                                String error = PluginServices.getText(this, "Error_entrada_datos");
195
                                String errorDescription = PluginServices.getText(this, "Error_atributo_no_numerico");
196
                                bufferPanel.error(errorDescription, error);
197
                                return false;
198
                        }
199
                        params.put("attr_name", attributeName);
200
                }
201
                params.put("strategy_flag", new Byte(strategy));
202

    
203
                //number of radial buffers
204
                int numberOfRadials = bufferPanel.getNumberOfRadialBuffers();
205
                params.put("numRings", new Integer(numberOfRadials));
206

    
207
                //type of polygon buffer
208
                String typePolygonBuffer = bufferPanel.getTypePolygonBuffer();
209
                byte typePolBuffer = BufferVisitor.BUFFER_OUTSIDE_POLY;
210
                if(typePolygonBuffer.equals(BufferPanelIF.BUFFER_INSIDE)){
211
                        typePolBuffer = BufferVisitor.BUFFER_INSIDE_POLY;
212
                }else if(typePolygonBuffer.equals(BufferPanelIF.BUFFER_INSIDE_OUTSIDE)){
213
                        typePolBuffer = BufferVisitor.BUFFER_INSIDE_OUTSIDE_POLY;
214
                }
215
                params.put("typePolBuffer", new Byte(typePolBuffer));
216

    
217
                //round cap or square cap
218
                byte cap = BufferVisitor.CAP_ROUND;
219
                boolean squareCap = bufferPanel.isSquareCap();
220
                if(squareCap)
221
                        cap = BufferVisitor.CAP_SQUARE;
222
                params.put("cap", new Byte(cap));
223
                try {
224
                        buffer.setParameters(params);
225
                } catch (GeoprocessException e2) {
226
                        String error = PluginServices.getText(this, "Error_ejecucion");
227
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
228
                        bufferPanel.error(errorDescription, error);
229
                        return false;
230
                }
231

    
232
                SHPLayerDefinition definition = (SHPLayerDefinition) buffer
233
                                .createLayerDefinition();
234
                definition.setFile(outputFile);
235
                ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
236
                IWriter writer = null;
237
                try {
238
                        writer = getShpWriter(definition);
239
                } catch (Exception e1) {
240
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
241
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
242
                        bufferPanel.error(errorDescription, error);
243
                        return false;
244
                }
245
                buffer.setResultLayerProperties(writer, schemaManager);
246

    
247
                try {
248
                        buffer.checkPreconditions();
249
                        IMonitorableTask task1 = buffer.createTask();
250
                        AddResultLayerTask task2 = new AddResultLayerTask(buffer);
251
                        task2.setLayers(layers);
252
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
253
                                        task2);
254
                        if (globalTask.preprocess())
255
                                PluginServices.cancelableBackgroundExecution(globalTask);
256
                        return true;
257
                } catch (GeoprocessException e) {
258
                        String error = PluginServices.getText(this, "Error_ejecucion");
259
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
260
                        bufferPanel.error(errorDescription, error);
261
                        return false;
262
                }
263

    
264
        }
265
        public int getWidth() {
266
                return 700;
267
        }
268

    
269
        public int getHeight() {
270
                return 375;
271
        }
272
}
273