Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / intersection / IntersectionGeoprocessController.java @ 29952

History | View | Annotate | Download (8.03 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: IntersectionGeoprocessController.java 29952 2009-07-14 06:00:26Z vcaballero $
47
* $Log$
48
* Revision 1.6  2006-11-29 13:11:23  jmvivo
49
* Se ha a?adido mas informaci?n al mensaje de error para los GeoprocessException: e.getMessage()
50
*
51
* Revision 1.5  2006/10/23 10:29:18  caballero
52
* ancho y alto del panel
53
*
54
* Revision 1.4  2006/08/11 16:30:38  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.3  2006/07/21 09:10:34  azabala
58
* fixed bug 608: user doesnt enter any result file to the geoprocess panel
59
*
60
* Revision 1.2  2006/06/29 07:33:57  fjp
61
* Cambios ISchemaManager y IFieldManager por terminar
62
*
63
* Revision 1.1  2006/06/20 18:20:45  azabala
64
* first version in cvs
65
*
66
* Revision 1.5  2006/06/12 19:15:38  azabala
67
* cambios para poder trabajar en geoprocessing con capas MULTI (dxf, jdbc, etc)
68
*
69
* Revision 1.4  2006/06/08 18:25:00  azabala
70
* cambios para considerar las capas DB como espacialmente indexadas (aunque la referencia de ISpatialIndex sea null)
71
*
72
* Revision 1.3  2006/06/02 18:21:28  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.2  2006/05/25 08:21:48  jmvivo
76
* A?adida peticion de confirmacion para sobreescribir el fichero de salida, si este ya existiera
77
*
78
* Revision 1.1  2006/05/24 21:10:40  azabala
79
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
80
*
81
* Revision 1.2  2006/05/08 15:35:32  azabala
82
* *** empty log message ***
83
*
84
* Revision 1.1  2006/04/11 17:55:51  azabala
85
* primera version en cvs
86
*
87
*
88
*/
89
package com.iver.cit.gvsig.geoprocess.impl.intersection;
90

    
91
import java.io.File;
92
import java.io.FileNotFoundException;
93
import java.util.HashMap;
94

    
95
import com.iver.andami.PluginServices;
96
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
97
import com.iver.cit.gvsig.fmap.edition.IWriter;
98
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
99
import com.iver.cit.gvsig.fmap.edition.writers.shp.MultiShpWriter;
100
import com.iver.cit.gvsig.fmap.layers.FLayers;
101
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
102
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
103
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
104
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
105
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
106
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessUserEntries;
107
import com.iver.cit.gvsig.geoprocess.core.gui.OverlayPanelIF;
108
import com.iver.cit.gvsig.geoprocess.impl.intersection.fmap.IntersectionGeoprocess;
109
import com.iver.utiles.swing.threads.IMonitorableTask;
110
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
111

    
112
public class IntersectionGeoprocessController extends
113
                AbstractGeoprocessController {
114

    
115

    
116
        private OverlayPanelIF geoProcessingIntersectPanel;
117
        private IntersectionGeoprocess intersection;
118

    
119
        public void setView(IGeoprocessUserEntries viewPanel) {
120
                this.geoProcessingIntersectPanel =
121
                        (OverlayPanelIF) viewPanel;
122
        }
123

    
124
        public IGeoprocess getGeoprocess() {
125
                return intersection;
126
        }
127

    
128
        public boolean launchGeoprocess() {
129
                FLyrVect inputLayer = geoProcessingIntersectPanel.getInputLayer();
130
                FLayers layers = geoProcessingIntersectPanel.getFLayers();
131
                FLyrVect overlayLayer = geoProcessingIntersectPanel.getSecondLayer();
132
                File outputFile = null;
133
                try {
134
                        outputFile = geoProcessingIntersectPanel.getOutputFile();
135
                } catch (FileNotFoundException e3) {
136
                        String error = PluginServices.getText(this, "Error_entrada_datos");
137
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
138
                        geoProcessingIntersectPanel.error(errorDescription, error);
139
                        return false;
140
                }
141
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
142
                        String error = PluginServices.getText(this, "Error_entrada_datos");
143
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
144
                        geoProcessingIntersectPanel.error(errorDescription, error);
145
                        return false;
146
                }
147

    
148

    
149
                IntersectionGeoprocess intersection = new IntersectionGeoprocess(
150
                                inputLayer);
151
                intersection.setSecondOperand(overlayLayer);
152
                HashMap params = new HashMap();
153
                boolean onlySelectedFirst = geoProcessingIntersectPanel
154
                                .onlyFirstLayerSelected();
155
                boolean onlySelectedSecond = geoProcessingIntersectPanel
156
                                .onlySecondLayerSelected();
157
                Boolean first = new Boolean(onlySelectedFirst);
158
                params.put("firstlayerselection", first);
159

    
160
                Boolean second = new Boolean(onlySelectedSecond);
161
                params.put("secondlayerselection", second);
162
                        try {
163
                                intersection.setParameters(params);
164
                        } catch (GeoprocessException e2) {
165
                                // TODO Auto-generated catch block
166
                                e2.printStackTrace();
167
                        }
168
                SHPLayerDefinition definition = (SHPLayerDefinition) intersection
169
                                .createLayerDefinition();
170
                definition.setFile(outputFile);
171
                ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile.getAbsolutePath());
172
                IWriter writer;
173
                try {
174
                        writer = getShpWriter(definition);
175
                } catch (Exception e1) {
176
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
177
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
178
                        geoProcessingIntersectPanel.error(errorDescription, error);
179
                        return false;
180
                }
181
                boolean found = false;
182
                if (writer instanceof MultiShpWriter){
183
                        MultiShpWriter mWriter = (MultiShpWriter) writer;
184

    
185
                        if (mWriter.getPointsFile().exists()){
186
                                found = true;
187
                        }
188
                        if (mWriter.getLinesFile().exists()){
189
                                found = true;
190
                        }
191
                        if (mWriter.getPolygonsFile().exists()){
192
                                found = true;
193
                        }
194

    
195
                } else{
196
                        found = outputFile.exists();
197
                }
198
                if (found){
199
                        if (!geoProcessingIntersectPanel.askForOverwriteOutputFile(outputFile)) {
200
                                return false;
201
                        }
202
                }
203

    
204
                intersection.setResultLayerProperties(writer, schemaManager);
205
                try {
206
                        intersection.checkPreconditions();
207
                        IMonitorableTask task1 = intersection.createTask();
208
                        if(task1 == null){
209
                                return false;
210
                        }
211
                        AddResultLayerTask task2 = new AddResultLayerTask(intersection);
212
                        task2.setLayers(layers);
213
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
214
                                        task2);
215
                        if(!overlayLayer.isSpatiallyIndexed()){
216
                                final IMonitorableTask sptIdxTask =
217
                                        geoProcessingIntersectPanel.askForSpatialIndexCreation(overlayLayer);
218
                                if(sptIdxTask != null){
219
                                        PluginServices.backgroundExecution(
220
                                                        new Runnable(){
221
                                                public void run() {
222
                                                        PluginServices.
223
                                                        cancelableBackgroundExecution(sptIdxTask);
224
                                                }}
225
                                        );
226
                                }
227
                        }//if
228
                        if (globalTask.preprocess())
229
                                PluginServices.cancelableBackgroundExecution(globalTask);
230
                } catch (GeoprocessException e) {
231
                        String error = PluginServices.getText(this, "Error_ejecucion");
232
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
233
                        errorDescription = "<html>" + errorDescription + ":<br>" + e.getMessage()+ "</html>";
234
                        geoProcessingIntersectPanel.error(errorDescription, error);
235
                        return false;
236
                }
237
                return true;
238
        }
239
        public int getWidth() {
240
                return 700;
241
        }
242

    
243
        public int getHeight() {
244
                return 300;
245
        }
246
}
247