Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / buffer / BufferGeoprocessController.java @ 5627

History | View | Annotate | Download (8.13 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 5627 2006-06-02 18:20:58Z azabala $
47
* $Log$
48
* Revision 1.3  2006-06-02 18:20:04  azabala
49
* cuando el buffer es con dissolve se crea indice espacial para optimizar
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:15:07  azabala
55
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
56
*
57
* Revision 1.3  2006/05/08 15:34:59  azabala
58
* refactoring of ITask api
59
*
60
* Revision 1.2  2006/05/01 19:20:14  azabala
61
* revisi?n general del buffer (a?adidos anillos concentricos, buffers interiores y exteriores, etc)
62
*
63
* Revision 1.1  2006/04/11 17:55:51  azabala
64
* primera version en cvs
65
*
66
*
67
*/
68
package com.iver.cit.gvsig.geoprocess.buffer;
69

    
70
import java.io.File;
71
import java.util.HashMap;
72

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

    
122
        public IGeoprocess getGeoprocess() {
123
                return buffer;
124
        }
125

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

    
221
                try {
222
                        buffer.checkPreconditions();
223
                        IMonitorableTask task1 = buffer.createTask();
224
                        AddResultLayerTask task2 = new AddResultLayerTask(buffer);
225
                        task2.setLayers(layers);
226
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
227
                                        task2);
228
                        if (globalTask.preprocess())
229
                                PluginServices.cancelableBackgroundExecution(globalTask);
230
                        return true;
231
                } catch (GeoprocessException e) {
232
                        String error = PluginServices.getText(this, "Error_ejecucion");
233
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
234
                        bufferPanel.error(errorDescription, error);
235
                        return false;
236
                }
237
                
238
        }
239

    
240
}
241