Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / core / fmap / AbstractGeoprocess.java @ 8212

History | View | Annotate | Download (6.76 KB)

1
/*
2
 * Created on 16-feb-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: AbstractGeoprocess.java 8212 2006-10-20 14:28:30Z azabala $
47
* $Log$
48
* Revision 1.9  2006-10-20 14:28:30  azabala
49
* changed visibility of createLayerFrom method to protected
50
*
51
* Revision 1.8  2006/09/15 10:42:54  caballero
52
* extensibilidad de documentos
53
*
54
* Revision 1.7  2006/08/29 07:56:30  cesar
55
* Rename the *View* family of classes to *Window* (ie: SingletonView to SingletonWindow, ViewInfo to WindowInfo, etc)
56
*
57
* Revision 1.6  2006/08/29 07:21:09  cesar
58
* Rename com.iver.cit.gvsig.fmap.Fmap class to com.iver.cit.gvsig.fmap.MapContext
59
*
60
* Revision 1.5  2006/06/29 07:33:57  fjp
61
* Cambios ISchemaManager y IFieldManager por terminar
62
*
63
* Revision 1.4  2006/06/20 18:19:43  azabala
64
* refactorizaci?n para que todos los nuevos geoprocesos cuelguen del paquete impl
65
*
66
* Revision 1.3  2006/06/12 19:15:38  azabala
67
* cambios para poder trabajar en geoprocessing con capas MULTI (dxf, jdbc, etc)
68
*
69
* Revision 1.2  2006/05/31 09:10:12  fjp
70
* Ubicaci?n de IWriter
71
*
72
* Revision 1.1  2006/05/24 21:12:16  azabala
73
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
74
*
75
* Revision 1.7  2006/05/01 19:19:10  azabala
76
* la capa resultado tiene como nombre ahora solo el nombre del fichero (no la ruta completa)
77
*
78
* Revision 1.6  2006/03/17 19:52:19  azabala
79
* *** empty log message ***
80
*
81
* Revision 1.5  2006/03/14 19:34:18  azabala
82
* *** empty log message ***
83
*
84
* Revision 1.4  2006/03/14 18:32:46  fjp
85
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
86
*
87
* Revision 1.3  2006/03/07 21:01:33  azabala
88
* *** empty log message ***
89
*
90
* Revision 1.2  2006/03/06 19:48:39  azabala
91
* *** empty log message ***
92
*
93
* Revision 1.1  2006/02/17 16:04:28  azabala
94
* *** empty log message ***
95
*
96
*
97
*/
98
package com.iver.cit.gvsig.geoprocess.core.fmap;
99

    
100
import java.io.File;
101
import java.util.Map;
102

    
103
import org.cresques.cts.IProjection;
104

    
105
import com.iver.andami.PluginServices;
106
import com.iver.cit.gvsig.fmap.MapContext;
107
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
108
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
109
import com.iver.cit.gvsig.fmap.edition.EditionException;
110
import com.iver.cit.gvsig.fmap.edition.ISchemaManager;
111
import com.iver.cit.gvsig.fmap.edition.IWriter;
112
import com.iver.cit.gvsig.fmap.edition.writers.shp.MultiShpWriter;
113
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
114
import com.iver.cit.gvsig.fmap.layers.FLayer;
115
import com.iver.cit.gvsig.fmap.layers.FLayers;
116
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
117
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
118
import com.iver.cit.gvsig.project.documents.view.gui.View;
119
/**
120
 * Base class with all commong logic to geoprocesses
121
 * @author azabala
122
 *
123
 */
124
public abstract class AbstractGeoprocess implements IGeoprocess {
125
        /**
126
         * Writes result features in a persistent
127
         * data format.
128
         */
129
        protected IWriter writer;
130
        /**
131
         * Creates schema of result data format.
132
         */
133
        protected ISchemaManager schemaManager;
134
        /**
135
         * All geoprocesses at least work with one
136
         * vectorial layer
137
         */
138
        protected FLyrVect firstLayer;
139
                
140
        public abstract void setParameters(Map params) throws GeoprocessException;
141

    
142
        public abstract void checkPreconditions() throws GeoprocessException;
143

    
144
        public abstract void process() throws GeoprocessException;
145
        
146
        public  void cancel(){
147
                try {
148
                        schemaManager.removeSchema("");
149
                } catch (EditionException e) {
150
                        // TODO Auto-generated catch block
151
                        e.printStackTrace();
152
                }
153
        }
154
        
155
        
156
        protected FLayer createLayerFrom(IWriter writer) throws GeoprocessException{
157
                FLyrVect solution = null;
158
                //Para evitar todos estos casts, hay que meter la
159
                //interfaz FileWriter
160
                String fileName = ((ShpWriter)writer).getShpPath();
161
                String layerName = null;
162
                int fileNameStart = fileName.lastIndexOf(File.separator) + 1;
163
                if(fileNameStart == -1)
164
                        fileNameStart = 0;
165
                layerName = fileName.substring(fileNameStart, fileName.length() /*-1*/);                
166
                File file = new File(fileName);
167
                IProjection proj = firstLayer.getProjection();
168
                //TODO La proyeccion se deberia leer del WRITER
169
                try {
170
                        IndexedShpDriver driver = new IndexedShpDriver();
171
                        driver.open(file);
172
                        driver.initialize();
173
                        solution = (FLyrVect) LayerFactory.createLayer(layerName,
174
                                                                        driver,
175
                                                                        file,
176
                                                                        proj);
177
                        return solution; 
178
                } catch (Exception e) {
179
                        throw new GeoprocessException("Problemas al cargar la capa resultado", e);
180
                }         
181
        }
182
                
183
        /**
184
         * Creates a new Layer with:
185
         * a) the same projection than input layer.
186
         * b) an adapter created to work with writer's persistent store
187
         * 
188
         * @return FLyrVect with geoprocess result
189
         */
190
        public FLayer getResult() throws GeoprocessException {
191
                if(! (writer instanceof MultiShpWriter)){
192
                        return createLayerFrom(writer);
193
                }else{
194
                        IWriter[] writers = ((MultiShpWriter)writer).getWriters();
195
                        if(writers.length > 1){
196
                                MapContext map = ((View)PluginServices.
197
                                                        getMDIManager().
198
                                                        getActiveWindow()).
199
                                                        getModel().
200
                                                        getMapContext();
201
                                FLayers solution = new FLayers(map, null);
202
                                String name = ((MultiShpWriter)writer).getFileName();
203
                                int fileNameStart = name.lastIndexOf(File.separator) + 1;
204
                                if(fileNameStart == -1)
205
                                        fileNameStart = 0;
206
                                name = name.substring(fileNameStart, name.length());                
207
                                solution.setName(name);
208
                                for(int i = 0; i < writers.length; i++){
209
                                        solution.addLayer(createLayerFrom(writers[i]));
210
                                }
211
                                return solution;
212
                        }else{
213
                                return createLayerFrom(writers[0]);
214
                        }
215
                }                
216
        }
217

    
218
        public void setResultLayerProperties(IWriter writer,
219
                        ISchemaManager schemaManager) {
220
                this.writer = writer;
221
                this.schemaManager = schemaManager;
222
        }
223
        
224
        public abstract ILayerDefinition createLayerDefinition();
225
        
226
}
227