Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / DissolveGeoprocessController.java @ 4820

History | View | Annotate | Download (4.92 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: DissolveGeoprocessController.java 4818 2006-04-11 17:55:51Z azabala $
47
* $Log$
48
* Revision 1.1  2006-04-11 17:55:51  azabala
49
* primera version en cvs
50
*
51
*
52
*/
53
package com.iver.gvsig.geoprocessing;
54

    
55
import java.io.File;
56
import java.util.HashMap;
57
import java.util.Map;
58

    
59
import com.iver.andami.PluginServices;
60
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
61
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
62
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
63
import com.iver.cit.gvsig.fmap.layers.FLayers;
64
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
65
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingDissolvePanel;
66
import com.iver.gvsig.geoprocessing.impl.dissolve.DissolveGeoprocess;
67
import com.iver.gvsig.geoprocessing.model.GeoprocessException;
68
import com.iver.gvsig.geoprocessing.model.IGeoprocess;
69
import com.iver.utiles.swing.threads.IMonitorableTask;
70
import com.iver.utiles.swing.threads.MonitorableDecorator;
71

    
72
public class DissolveGeoprocessController extends AbstractGeoprocessController {
73
        private GeoProcessingDissolvePanel geoProcessingDissolvePanel;
74
        private DissolveGeoprocess dissolve;
75
        
76
        public void setView(IGeoprocessPanel viewPanel) {
77
                this.geoProcessingDissolvePanel =
78
                        (GeoProcessingDissolvePanel) viewPanel;
79
        }
80

    
81
        public IGeoprocess getGeoprocess() {
82
                return dissolve;
83
        }
84

    
85
        public boolean launchGeoprocess() {
86
                FLyrVect inputLayer = geoProcessingDissolvePanel.getInputLayer();
87
                FLayers layers = geoProcessingDissolvePanel.getFLayers();
88
                File outputFile = geoProcessingDissolvePanel.getOutputFile();
89
                if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
90
                        String error = PluginServices.getText(this, "Error_entrada_datos");
91
                        String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
92
                        geoProcessingDissolvePanel.error(errorDescription, error);
93
                        return false;
94
                }
95
                String dissolveField = geoProcessingDissolvePanel
96
                                .getDissolveFieldName();
97
                boolean onlySelection = geoProcessingDissolvePanel
98
                                .isDissolveOnlySelected();
99
                DissolveGeoprocess dissolve = new DissolveGeoprocess(inputLayer,
100
                                dissolveField);
101

    
102
                // Antes de crear el esquema vemos la seleccion del usuario
103
                Map fields_functions = geoProcessingDissolvePanel.getFieldFunctionMap();
104
                dissolve.setFieldsFunctions(fields_functions);
105

    
106
                SHPLayerDefinition definition = (SHPLayerDefinition) dissolve
107
                                .createLayerDefinition();
108
                definition.setFile(outputFile);
109
                ShpSchemaManager schemaManager = new ShpSchemaManager();
110
                
111
                ShpWriter writer = null;
112
                try {
113
                        schemaManager.createOrAlterSchema(definition);
114
                        writer = new ShpWriter();
115
                        writer.setFile(outputFile);
116
                        writer.initialize(definition);
117
                } catch (Exception e) {
118
                        String error = PluginServices.getText(this, "Error_escritura_resultados");
119
                        String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
120
                        geoProcessingDissolvePanel.error(errorDescription, error);
121
                        return false;
122
                } 
123
                dissolve.setResultLayerProperties(writer, schemaManager);
124
                HashMap params = new HashMap();
125
                params.put("layer_selection", new Boolean(onlySelection));
126
                try {
127
                        dissolve.setParameters(params);
128
                        dissolve.checkPreconditions();
129
                        IMonitorableTask task1 = dissolve.createTask();
130
                        AddResultLayerTask task2 = new AddResultLayerTask(dissolve);
131
                        task2.setLayers(layers);
132
                        MonitorableDecorator globalTask = new MonitorableDecorator(task1,
133
                                        task2);
134
                        if (globalTask.preprocess())
135
                                PluginServices.cancelableBackgroundExecution(globalTask);
136
                } catch (GeoprocessException e) {
137
                        String error = PluginServices.getText(this, "Error_ejecucion");
138
                        String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
139
                        geoProcessingDissolvePanel.error(errorDescription, error);
140
                        return false;
141
                }
142
                return true;
143
        }
144

    
145
}
146