Statistics
| Revision:

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

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

    
82
import java.io.File;
83
import java.io.FileNotFoundException;
84
import java.util.HashMap;
85

    
86
import com.iver.andami.PluginServices;
87
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
88
import com.iver.cit.gvsig.fmap.edition.IWriter;
89
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
90
import com.iver.cit.gvsig.fmap.layers.FLayers;
91
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
92
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
93
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
94
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
95
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
96
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
97
import com.iver.cit.gvsig.geoprocess.impl.buffer.fmap.BufferGeoprocess;
98
import com.iver.cit.gvsig.geoprocess.impl.buffer.fmap.BufferVisitor;
99
import com.iver.cit.gvsig.geoprocess.impl.buffer.gui.GeoProcessingBufferPanel;
100
import com.iver.utiles.swing.threads.IMonitorableTask;
101
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
102
/**
103
 * Controller class for a Buffer Geoprocess
104
 * @author azabala
105
 *
106
 */
107
public class BufferGeoprocessController extends 
108
                                        AbstractGeoprocessController {
109
        /**
110
         * GUI class to read user entries.
111
         * TODO Put an interface, so this geoprocesscontroller
112
         * could be reused for command line environments, without
113
         * GUI
114
         */
115
        private GeoProcessingBufferPanel bufferPanel;
116
        /**
117
         * Geoprocess we are going to launch with
118
         * launchGeoprocess() method
119
         */
120
        private BufferGeoprocess buffer;
121
        
122
        /**
123
         * Default constructor
124
         *
125
         */
126
        public BufferGeoprocessController(){}
127
        
128
        /**
129
         * Sets GUI panel to read user entries
130
         */
131
        public void setView(IGeoprocessPanel viewPanel) {
132
                this.bufferPanel = (GeoProcessingBufferPanel) viewPanel;
133
        }
134

    
135
        public IGeoprocess getGeoprocess() {
136
                return buffer;
137
        }
138

    
139
        public boolean launchGeoprocess() {
140
                FLyrVect inputLayer = bufferPanel.getInputLayer();
141
                FLayers layers = bufferPanel.getFLayers();
142
                File outputFile = null;
143
                try {
144
                        outputFile = bufferPanel.getOutputFile();
145
                } catch (FileNotFoundException e3) {
146
                        String error = PluginServices.getText(this, "Error_entrada_datos");
147
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
148
                        bufferPanel.error(errorDescription, error);
149
                        return false;
150
                }
151
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
152
                        String error = PluginServices.getText(this, "Error_entrada_datos");
153
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
154
                        bufferPanel.error(errorDescription, error);
155
                        return false;
156
                }
157
                if (outputFile.exists()) {
158
                        if (!bufferPanel.askForOverwriteOutputFile(outputFile)) {
159
                                return false;
160
                        }
161
                }
162
                buffer = new BufferGeoprocess(inputLayer);
163
                HashMap params = new HashMap();
164
                boolean onlySelected = bufferPanel.isBufferOnlySelected();
165
                params.put("layer_selection", new Boolean(onlySelected));
166
                boolean dissolveBuffer = bufferPanel
167
                                .isDissolveBuffersSelected();
168
                params.put("dissolve_buffers", new Boolean(dissolveBuffer));
169
                byte strategy = 0;
170
                if (bufferPanel.isConstantDistanceSelected()) {
171
                        strategy = BufferGeoprocess.CONSTANT_DISTANCE_STRATEGY;
172
                        double bufferDistance = -1;
173
                        try {
174
                                bufferDistance = bufferPanel.getConstantDistance();
175
                        } catch (GeoprocessException e) {
176
                                String error = PluginServices.getText(this, "Error_entrada_datos");
177
                                String errorDescription = PluginServices.getText(this, "Error_distancia_buffer");
178
                                bufferPanel.error(errorDescription, error);
179
                                return false;
180
                        }
181
                        params.put("buffer_distance", new Double(bufferDistance));
182
                } else if (bufferPanel.isAttributeDistanceSelected()) {
183
                        strategy = BufferGeoprocess.ATTRIBUTE_DISTANCE_STRATEGY;
184
                        String attributeName = null;
185
                        try {
186
                                attributeName = bufferPanel
187
                                                .getAttributeDistanceField();
188
                        } catch (GeoprocessException e) {
189
                                String error = PluginServices.getText(this, "Error_entrada_datos");
190
                                String errorDescription = PluginServices.getText(this, "Error_atributo_no_numerico");
191
                                bufferPanel.error(errorDescription, error);
192
                                return false;
193
                        }
194
                        params.put("attr_name", attributeName);
195
                }
196
                params.put("strategy_flag", new Byte(strategy));
197
                
198
                //number of radial buffers
199
                int numberOfRadials = bufferPanel.getNumberOfRadialBuffers();
200
                params.put("numRings", new Integer(numberOfRadials));
201
                
202
                //type of polygon buffer
203
                String typePolygonBuffer = bufferPanel.getTypePolygonBuffer();
204
                byte typePolBuffer = BufferVisitor.BUFFER_OUTSIDE_POLY;
205
                if(typePolygonBuffer.equals(bufferPanel.BUFFER_INSIDE)){
206
                        typePolBuffer = BufferVisitor.BUFFER_INSIDE_POLY;
207
                }else if(typePolygonBuffer.equals(bufferPanel.BUFFER_INSIDE_OUTSIDE)){
208
                        typePolBuffer = BufferVisitor.BUFFER_INSIDE_OUTSIDE_POLY;
209
                }
210
                params.put("typePolBuffer", new Byte(typePolBuffer));
211
                
212
                //round cap or square cap
213
                byte cap = BufferVisitor.CAP_ROUND;
214
                boolean squareCap = bufferPanel.isSquareCap();
215
                if(squareCap)
216
                        cap = BufferVisitor.CAP_SQUARE;
217
                params.put("cap", new Byte(cap));
218
                try {
219
                        buffer.setParameters(params);
220
                } catch (GeoprocessException e2) {
221
                        String error = PluginServices.getText(this, "Error_ejecucion");
222
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
223
                        bufferPanel.error(errorDescription, error);
224
                        return false;
225
                }
226
                
227
                SHPLayerDefinition definition = (SHPLayerDefinition) buffer
228
                                .createLayerDefinition();
229
                definition.setFile(outputFile);
230
                ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
231
                IWriter writer = null;
232
                try {
233
                        writer = getShpWriter(definition);
234
                } catch (Exception e1) {
235
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
236
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
237
                        bufferPanel.error(errorDescription, error);
238
                        return false;
239
                } 
240
                buffer.setResultLayerProperties(writer, schemaManager);
241

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

    
261
}
262