Statistics
| Revision:

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

History | View | Annotate | Download (6.16 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 5628 2006-06-02 18:21:28Z azabala $
47
* $Log$
48
* Revision 1.3  2006-06-02 18:21:28  azabala
49
* *** empty log message ***
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:10:40  azabala
55
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
56
*
57
* Revision 1.2  2006/05/08 15:35:32  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.1  2006/04/11 17:55:51  azabala
61
* primera version en cvs
62
*
63
*
64
*/
65
package com.iver.cit.gvsig.geoprocess.intersection;
66

    
67
import java.io.File;
68
import java.util.HashMap;
69

    
70
import com.iver.andami.PluginServices;
71
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
72
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
73
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
74
import com.iver.cit.gvsig.fmap.layers.FLayers;
75
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
76
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
77
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
78
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
79
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
80
import com.iver.cit.gvsig.geoprocess.core.gui.GeoProcessingOverlayPanel;
81
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
82
import com.iver.cit.gvsig.geoprocess.intersection.fmap.IntersectionGeoprocess;
83
import com.iver.utiles.swing.threads.IMonitorableTask;
84
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
85

    
86
public class IntersectionGeoprocessController extends
87
                AbstractGeoprocessController {
88

    
89
        
90
        private GeoProcessingOverlayPanel geoProcessingIntersectPanel;
91
        private IntersectionGeoprocess intersection;
92
        
93
        public void setView(IGeoprocessPanel viewPanel) {
94
                this.geoProcessingIntersectPanel = 
95
                        (GeoProcessingOverlayPanel) viewPanel;
96
        }
97

    
98
        public IGeoprocess getGeoprocess() {
99
                return intersection;
100
        }
101

    
102
        public boolean launchGeoprocess() {
103
                FLyrVect inputLayer = geoProcessingIntersectPanel.getInputLayer();
104
                FLayers layers = geoProcessingIntersectPanel.getFLayers();
105
                FLyrVect overlayLayer = geoProcessingIntersectPanel.getSecondLayer();
106
                File outputFile = geoProcessingIntersectPanel.getOutputFile();
107
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
108
                        String error = PluginServices.getText(this, "Error_entrada_datos");
109
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
110
                        geoProcessingIntersectPanel.error(errorDescription, error);
111
                        return false;
112
                }
113
                if (outputFile.exists()) {
114
                        if (!geoProcessingIntersectPanel.askForOverwriteOutputFile(outputFile)) {
115
                                return false;
116
                        }
117
                }
118

    
119
                IntersectionGeoprocess intersection = new IntersectionGeoprocess(
120
                                inputLayer);
121
                intersection.setSecondOperand(overlayLayer);
122
                SHPLayerDefinition definition = (SHPLayerDefinition) intersection
123
                                .createLayerDefinition();
124
                definition.setFile(outputFile);
125
                ShpSchemaManager schemaManager = new ShpSchemaManager();
126
                ShpWriter writer;
127
                try {
128
                        writer = getShpWriter(definition);
129
                } catch (Exception e1) {
130
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
131
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
132
                        geoProcessingIntersectPanel.error(errorDescription, error);
133
                        return false;
134
                } 
135
        
136
                intersection.setResultLayerProperties(writer, schemaManager);
137
                HashMap params = new HashMap();
138
                boolean onlySelectedFirst = geoProcessingIntersectPanel
139
                                .onlyFirstLayerSelected();
140
                boolean onlySelectedSecond = geoProcessingIntersectPanel
141
                                .onlySecondLayerSelected();
142
                Boolean first = new Boolean(onlySelectedFirst);
143
                params.put("firstlayerselection", first);
144

    
145
                Boolean second = new Boolean(onlySelectedSecond);
146
                params.put("secondlayerselection", second);
147

    
148
                try {
149
                        intersection.setParameters(params);
150
                        intersection.checkPreconditions();
151
                        IMonitorableTask task1 = intersection.createTask();
152
                        if(task1 == null){
153
                                return false;
154
                        }
155
                        AddResultLayerTask task2 = new AddResultLayerTask(intersection);
156
                        task2.setLayers(layers);
157
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
158
                                        task2);
159
                        if(overlayLayer.getISpatialIndex() == null){
160
                                final IMonitorableTask sptIdxTask = 
161
                                        geoProcessingIntersectPanel.askForSpatialIndexCreation(overlayLayer);
162
                                if(sptIdxTask != null){
163
                                        PluginServices.backgroundExecution(
164
                                                        new Runnable(){
165
                                                public void run() {
166
                                                        PluginServices.
167
                                                        cancelableBackgroundExecution(sptIdxTask);        
168
                                                }}
169
                                        );
170
                                }        
171
                        }//if
172
                        if (globalTask.preprocess())
173
                                PluginServices.cancelableBackgroundExecution(globalTask);
174
                } catch (GeoprocessException e) {
175
                        String error = PluginServices.getText(this, "Error_ejecucion");
176
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
177
                        geoProcessingIntersectPanel.error(errorDescription, error);
178
                        return false;
179
                }
180
                return true;
181
        }
182

    
183
}
184