Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / impl / merge / MergeGeoprocess.java @ 4447

History | View | Annotate | Download (5.54 KB)

1
/*
2
 * Created on 02-mar-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: MergeGeoprocess.java 4447 2006-03-15 18:35:56Z azabala $
47
* $Log$
48
* Revision 1.3  2006-03-15 18:34:16  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.2  2006/03/14 18:32:46  fjp
52
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
53
*
54
* Revision 1.1  2006/03/05 19:59:16  azabala
55
* *** empty log message ***
56
*
57
*
58
*/
59
package com.iver.gvsig.geoprocessing.impl.merge;
60

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

    
64
import org.cresques.cts.IProjection;
65

    
66
import com.hardcode.gdbms.engine.data.driver.DriverException;
67
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
68
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
69
import com.iver.cit.gvsig.fmap.edition.ISchemaManager;
70
import com.iver.cit.gvsig.fmap.edition.IWriter;
71
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
72
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
73
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
74
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
75
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
76
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
77
import com.iver.gvsig.geoprocessing.impl.jtsprocessors.FeaturePersisterProcessor2;
78
import com.iver.gvsig.geoprocessing.model.GeoprocessException;
79
import com.iver.gvsig.geoprocessing.model.IGeoprocess;
80
import com.iver.gvsig.geoprocessing.schemabuilder.DefinitionUtils;
81
import com.iver.utiles.swing.threads.IMonitorableTask;
82
/**
83
 * This geoprocess is "equivalent" to UNION relational operator.
84
 * Output layer will have a geometry for each geometry of input layer.
85
 * Alse, output layer will preserve a schema of one of input layers.
86
 * @author azabala
87
 *
88
 */
89
public class MergeGeoprocess implements IGeoprocess {
90

    
91
        private FLyrVect[] inputLayers;
92
        
93
        private FLyrVect outputSchemaLayer;
94
        
95
        private IWriter writer;
96

    
97
        private ISchemaManager schemaManager;
98
        
99
        public MergeGeoprocess(){
100
                
101
        }
102
        
103
        public void setParameters(Map params) throws GeoprocessException {
104
        }
105

    
106
        public void checkPreconditions() throws GeoprocessException {
107
                if(inputLayers == null)
108
                        throw new GeoprocessException("No se han especificado las capas a juntar en un merge");
109
                if(outputSchemaLayer == null)
110
                        throw new GeoprocessException("Merge: no se ha especificado que capa determinara los atributos");
111
                for(int i = 0; i < inputLayers.length; i++){
112
                        if(inputLayers[i] == null)
113
                                throw new GeoprocessException("Merge: la capa "+i+" no est? definida -null");
114
                }
115

    
116
        }
117

    
118
        public void process() throws GeoprocessException {
119
                FeaturePersisterProcessor2 processor =
120
                        new FeaturePersisterProcessor2(writer);
121
                MergeVisitor merge = new MergeVisitor(
122
                                                        createLayerDefinition(), 
123
                                                        processor);
124
                for(int i = 0; i < inputLayers.length; i++){
125
                        Strategy strategy = StrategyManager.
126
                                        getStrategy(inputLayers[i]);
127
                        try {
128
                                strategy.process(merge);
129
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
130
                                throw new GeoprocessException("Merge: error al leer la capa "+inputLayers[i].getName());
131
                        } catch (VisitException e) {
132
                                throw new GeoprocessException("Merge: error al procesar la capa "+inputLayers[i].getName());
133
                        }
134
                }
135
        }
136

    
137
        public void cancel() {
138
                // TODO Auto-generated method stub
139

    
140
        }
141

    
142
        public FLyrVect getResult() throws GeoprocessException {
143
                FLyrVect solution = null;
144
                String fileName = ((ShpWriter)writer).getShpPath();
145
                File file = new File(fileName);
146
                IProjection proj = this.outputSchemaLayer.getProjection();
147
                
148
                try {
149
                        IndexedShpDriver driver = new IndexedShpDriver();
150
                        driver.open(file);
151
                        driver.initialize();
152
                        solution = (FLyrVect) LayerFactory.createLayer(fileName,
153
                                                                        driver,
154
                                                                        file,
155
                                                                        proj);
156
                        return solution; 
157
                } catch (Exception e) {
158
                        throw new GeoprocessException("Problemas al cargar la capa resultado");
159
                }         
160
        }
161

    
162
        public void setResultLayerProperties(IWriter writer,
163
                        ISchemaManager schemaManager) {
164
                this.writer = writer;
165
                this.schemaManager = schemaManager;
166
        }
167

    
168
        public ILayerDefinition createLayerDefinition() {
169
                try {
170
                        return DefinitionUtils.createLayerDefinition(this.outputSchemaLayer);
171
                } catch (Exception e) {
172
                        return null;
173
                }
174
        }
175

    
176
        public void setInputLayers(FLyrVect[] inputLayers) {
177
                this.inputLayers = inputLayers;
178
        }
179

    
180
        public void setOutputSchemaLayer(FLyrVect outputSchemaLayer) {
181
                this.outputSchemaLayer = outputSchemaLayer;
182
        }
183

    
184
        public IMonitorableTask createTask() {
185
                // TODO Auto-generated method stub
186
                return null;
187
        }
188

    
189
}
190