Statistics
| Revision:

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

History | View | Annotate | Download (5.77 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 5412 2006-05-24 21:15:07Z azabala $
47
* $Log$
48
* Revision 1.1  2006-05-24 21:10:40  azabala
49
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
50
*
51
* Revision 1.2  2006/05/08 15:35:32  azabala
52
* *** empty log message ***
53
*
54
* Revision 1.1  2006/04/11 17:55:51  azabala
55
* primera version en cvs
56
*
57
*
58
*/
59
package com.iver.cit.gvsig.geoprocess.intersection;
60

    
61
import java.io.File;
62
import java.util.HashMap;
63

    
64
import com.iver.andami.PluginServices;
65
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
66
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
67
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
68
import com.iver.cit.gvsig.fmap.layers.FLayers;
69
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
70
import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocessController;
71
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException;
72
import com.iver.cit.gvsig.geoprocess.core.fmap.IGeoprocess;
73
import com.iver.cit.gvsig.geoprocess.core.gui.AddResultLayerTask;
74
import com.iver.cit.gvsig.geoprocess.core.gui.GeoProcessingOverlayPanel;
75
import com.iver.cit.gvsig.geoprocess.core.gui.IGeoprocessPanel;
76
import com.iver.cit.gvsig.geoprocess.intersection.fmap.IntersectionGeoprocess;
77
import com.iver.utiles.swing.threads.IMonitorableTask;
78
import com.iver.utiles.swing.threads.MonitorableDecoratorMainFirst;
79

    
80
public class IntersectionGeoprocessController extends
81
                AbstractGeoprocessController {
82

    
83
        
84
        private GeoProcessingOverlayPanel geoProcessingIntersectPanel;
85
        private IntersectionGeoprocess intersection;
86
        
87
        public void setView(IGeoprocessPanel viewPanel) {
88
                this.geoProcessingIntersectPanel = 
89
                        (GeoProcessingOverlayPanel) viewPanel;
90
        }
91

    
92
        public IGeoprocess getGeoprocess() {
93
                return intersection;
94
        }
95

    
96
        public boolean launchGeoprocess() {
97
                FLyrVect inputLayer = geoProcessingIntersectPanel.getInputLayer();
98
                FLayers layers = geoProcessingIntersectPanel.getFLayers();
99
                FLyrVect overlayLayer = geoProcessingIntersectPanel.getSecondLayer();
100
                File outputFile = geoProcessingIntersectPanel.getOutputFile();
101
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
102
                        String error = PluginServices.getText(this, "Error_entrada_datos");
103
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
104
                        geoProcessingIntersectPanel.error(errorDescription, error);
105
                        return false;
106
                }
107

    
108
                IntersectionGeoprocess intersection = new IntersectionGeoprocess(
109
                                inputLayer);
110
                intersection.setSecondOperand(overlayLayer);
111
                SHPLayerDefinition definition = (SHPLayerDefinition) intersection
112
                                .createLayerDefinition();
113
                definition.setFile(outputFile);
114
                ShpSchemaManager schemaManager = new ShpSchemaManager();
115
                ShpWriter writer;
116
                try {
117
                        writer = getShpWriter(definition);
118
                } catch (Exception e1) {
119
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
120
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
121
                        geoProcessingIntersectPanel.error(errorDescription, error);
122
                        return false;
123
                } 
124
        
125
                intersection.setResultLayerProperties(writer, schemaManager);
126
                HashMap params = new HashMap();
127
                boolean onlySelectedFirst = geoProcessingIntersectPanel
128
                                .onlyFirstLayerSelected();
129
                boolean onlySelectedSecond = geoProcessingIntersectPanel
130
                                .onlySecondLayerSelected();
131
                Boolean first = new Boolean(onlySelectedFirst);
132
                params.put("firstlayerselection", first);
133

    
134
                Boolean second = new Boolean(onlySelectedSecond);
135
                params.put("secondlayerselection", second);
136

    
137
                try {
138
                        intersection.setParameters(params);
139
                        intersection.checkPreconditions();
140
                        IMonitorableTask task1 = intersection.createTask();
141
                        if(task1 == null){
142
                                return false;
143
                        }
144
                        AddResultLayerTask task2 = new AddResultLayerTask(intersection);
145
                        task2.setLayers(layers);
146
                        MonitorableDecoratorMainFirst globalTask = new MonitorableDecoratorMainFirst(task1,
147
                                        task2);
148
                        if(overlayLayer.getISpatialIndex() == null){
149
                                final IMonitorableTask sptIdxTask = 
150
                                        geoProcessingIntersectPanel.askForSpatialIndexCreation(overlayLayer);
151
                                PluginServices.backgroundExecution(
152
                                                new Runnable(){
153
                                        public void run() {
154
                                                PluginServices.
155
                                                cancelableBackgroundExecution(sptIdxTask);        
156
                                        }}
157
                                );
158
                        }//if
159
                        if (globalTask.preprocess())
160
                                PluginServices.cancelableBackgroundExecution(globalTask);
161
                } catch (GeoprocessException e) {
162
                        String error = PluginServices.getText(this, "Error_ejecucion");
163
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
164
                        geoProcessingIntersectPanel.error(errorDescription, error);
165
                        return false;
166
                }
167
                return true;
168
        }
169

    
170
}
171