Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / LayerFactory.java @ 1100

History | View | Annotate | Download (9.94 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import com.hardcode.driverManager.Driver;
44
import com.hardcode.driverManager.DriverLoadException;
45
import com.hardcode.driverManager.DriverManager;
46

    
47
import com.hardcode.gdbms.engine.data.DataSourceFactory;
48
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
49

    
50
import com.iver.cit.gvsig.fmap.DriverException;
51
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
52
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
53
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
54
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
55
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
56
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
57
import com.iver.cit.gvsig.fmap.drivers.wfs.WFSDriver;
58
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
59
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
60

    
61
import org.apache.log4j.Logger;
62

    
63
import org.cresques.cts.IProjection;
64

    
65
import java.awt.geom.Rectangle2D;
66

    
67
import java.io.File;
68

    
69
import java.net.URL;
70

    
71
import java.util.TreeMap;
72

    
73

    
74
/**
75
 * Crea un adaptador del driver que se le pasa como par?metro en los m?todos
76
 * createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
77
 * las features del driver a memoria
78
 */
79
public class LayerFactory {
80
        private static Logger logger = Logger.getLogger(LayerFactory.class.getName());
81
        private static String driversPath = "../FMap 03/drivers";
82
        private static DriverManager driverManager;
83

    
84
        /**
85
         * Map en el que se guarda para cada fuente de datos a?adida al sistema, el
86
         * adaptador que la maneja. Ha de ser un TreeMap ya que esta clase define
87
         * la igualdad entre las claves a traves del m?todo equals de las mismas.
88
         * Los objetos FileSource, DBSource y WFSSource tienen definido el m?todo
89
         * equals de forma que devuelven true cuando dos objetos apuntan a la
90
         * misma fuente de datos
91
         */
92
        private static TreeMap sourceAdapter;
93

    
94
        /**
95
         * Crea un RandomVectorialFile con el driver que se le pasa como par?metro
96
         * y guard?ndose el nombre del fichero para realizar los accesos, la capa
97
         * tendr? asociada la proyecci?n que se pasa como parametro tambi?n
98
         *
99
         * @param layerName Nombre de la capa.
100
         * @param driverName Nombre del driver.
101
         * @param f fichero.
102
         * @param proj Proyecci?n.
103
         *
104
         * @return FLayer.
105
         * @throws DriverException
106
         *
107
         * @throws DriverException
108
         * @throws DriverIOException
109
         */
110
        public static FLayer createLayer(String layerName, String driverName,
111
                File f, IProjection proj) throws DriverException  {
112
                //Se obtiene el driver que lee
113
                DriverManager dm = getDM();
114

    
115
                try {
116
                        Driver d = dm.getDriver(driverName);
117

    
118
                        if (d instanceof VectorialFileDriver) {
119
                                return createLayer(layerName, (VectorialFileDriver) d, f, proj);
120
                        } else if (d instanceof RasterDriver) {
121
                                return createLayer(layerName, (RasterDriver) d, f, proj);
122
                        }
123
                } catch (DriverLoadException e) {
124
                        throw new DriverException(e);
125
                }
126

    
127
                return null;
128
        }
129

    
130
        /**
131
         * Crea un RandomVectorialFile con el driver que se le pasa como par?metro
132
         * y guard?ndose el nombre del fichero para realizar los accesos, la capa
133
         * tendr? asociada la proyecci?n que se pasa como parametro tambi?n
134
         *
135
         * @param layerName Nombre del Layer.
136
         * @param d VectorialAdapter.
137
         * @param f Fichero.
138
         * @param proj Proyecci?n.
139
         *
140
         * @return FLayer creado.
141
         *
142
         * @throws DriverException
143
         */
144
        public static FLayer createLayer(String layerName, VectorialFileDriver d,
145
                File f, IProjection proj) throws DriverException {
146
                //TODO Comprobar si hay un adaptador ya
147
                VectorialFileAdapter adapter = new VectorialFileAdapter(f);
148
                adapter.setDriver((VectorialDriver) d);
149

    
150
                FLyrVect capa = new FLyrVect();
151
                capa.setName(layerName);
152

    
153
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
154
                if (false) {
155
                } else {
156
                        capa.setSource(adapter);
157
                        capa.setProjection(proj);
158
                }
159

    
160
                try {
161
                        // Le asignamos tambi?n una legenda por defecto acorde con
162
                        // el tipo de shape que tenga. Tampoco s? si es aqu? el
163
                        // sitio adecuado, pero en fin....
164
                        if (d instanceof WithDefaultLegend) {
165
                                WithDefaultLegend aux = (WithDefaultLegend) d;
166
                                adapter.start();
167
                                capa.setLegend((VectorialLegend) aux.getDefaultLegend());
168
                                adapter.stop();
169
                        } else {
170
                                capa.setLegend(LegendFactory.createSingleSymbolLegend(
171
                                                capa.getShapeType()));
172
                        }
173
                } catch (FieldNotFoundException e) {
174
                        //Esta no puede saltar
175
                } catch (DriverIOException e) {
176
                        throw new DriverException(e);
177
                }
178

    
179
                return capa;
180
        }
181

    
182
        /**
183
         * Crea una capa WMS con el driver que se le pasa como par?metro y
184
         * guard?ndose el nombre del fichero para realizar los accesos, la capa
185
         * tendr? asociada la proyecci?n que se pasa como parametro tambi?n
186
         *
187
         * @param layerName Nombre de la capa.
188
         * @param rect extent
189
         * @param host URL.
190
         * @param format Formato
191
         * @param query Consulta.
192
         * @param infoQuery inforamci?n de la consulta.
193
         * @param srs SRS.
194
         *
195
         * @return Capa creada.
196
         */
197
        public static FLayer createLayer(String layerName, Rectangle2D rect,
198
                URL host, String format, String query, String infoQuery, String srs) {
199
                FLyrWMS layer = new FLyrWMS();
200
                layer.setHost(host);
201
                layer.setFullExtent(rect);
202
                layer.setFormat(format);
203
                layer.setLayerQuery(query);
204
                layer.setInfoLayerQuery(infoQuery);
205
                layer.setSRS(srs);
206
                layer.setName(layerName);
207

    
208
                return layer;
209
        }
210

    
211
        /**
212
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n.
213
         *
214
         * @param layerName Nombre de la capa.
215
         * @param d RasterDriver.
216
         * @param f Fichero.
217
         * @param proj Proyecci?n.
218
         *
219
         * @return Nueva capa de tipo raster.
220
         *
221
         * @throws DriverIOException
222
         */
223
        public static FLyrRaster createLayer(String layerName, RasterDriver d,
224
                File f, IProjection proj) throws DriverException {
225
                RasterAdapter adapter = new RasterFileAdapter(f);
226
                adapter.setDriver(d);
227

    
228
                FLyrRaster capa = new FLyrRaster();
229
                capa.setName(layerName);
230

    
231
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
232
                if (false) {
233
                } else {
234
                        capa.setSource(adapter);
235
                        capa.setProjection(proj);
236
                        try {
237
                                capa.load();
238
                        } catch (DriverIOException e) {
239
                                throw new DriverException(e);
240
                        }
241
                }
242

    
243
                return capa;
244
        }
245

    
246
        /**
247
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
248
         * guard?ndose la URL del servidor que se pasa como par?metro
249
         *
250
         * @param driver Driver WFS.
251
         * @param host URL.
252
         * @param proj Proyecci?n.
253
         *
254
         * @return Capa creada.
255
         *
256
         * @throws UnsupportedOperationException
257
         */
258
        public static FLayer createLayer(WFSDriver driver, URL host,
259
                IProjection proj) {
260
                throw new UnsupportedOperationException();
261
        }
262

    
263
        /**
264
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
265
         * guard?ndose la URL del servidor que se pasa como par?metro
266
         *
267
         * @param driver
268
         * @param host
269
         * @param port
270
         * @param user
271
         * @param password
272
         * @param dbName
273
         * @param tableName
274
         * @param proj
275
         *
276
         * @return Capa creada.
277
         *
278
         * @throws UnsupportedOperationException
279
         */
280
        public static FLayer createLayer(VectorialDatabaseDriver driver,
281
                String host, int port, String user, String password, String dbName,
282
                String tableName, IProjection proj) {
283
                throw new UnsupportedOperationException();
284
        }
285

    
286
        /**
287
         * Obtiene el adaptador de un driver si ya se ha a?adido el origen de datos
288
         * o null si es un origen de datos nuevo
289
         *
290
         * @param source Adaptador.
291
         */
292
        private static void getAdapter(Source source) {
293
        }
294

    
295
        /**
296
         * Registra la asociaci?n entre la fuente de datos que se pasa como
297
         * par?metro y el VectorialAdapter que se pasa como par?metro, para
298
         * satisfacer futuras llamadas a getAdapter
299
         *
300
         * @param s Adaptador
301
         * @param a VectorialAdapter.
302
         */
303
        private static void saveSourceAdapter(Source s, VectorialAdapter a) {
304
        }
305

    
306
        /**
307
         * Crea una FLyrComplexRaster que ataca al driver que se pasa como
308
         * par?metro.
309
         *
310
         * @param driver
311
         * @param f
312
         * @param proj
313
         *
314
         * @throws IllegalArgumentException Si se pasa un driver que no implementa
315
         *                    GeorreferencedRasterDriver o NotGeorreferencedRasterDriver
316
         */
317
        public static void createLayer(RasterDriver driver, File f, IProjection proj)
318
                throws IllegalArgumentException {
319
        }
320

    
321
        /**
322
         * Devuelve el DriverManager.
323
         *
324
         * @return DriverManager.
325
         */
326
        public static DriverManager getDM() {
327
                if (driverManager == null) {
328
                        initializeDriverManager();
329
                }
330

    
331
                return driverManager;
332
        }
333

    
334
        /**
335
         * Inicializa el DriverManager.
336
         */
337
        private static void initializeDriverManager() {
338
                if (driverManager == null) {
339
                        driverManager = new DriverManager();
340
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
341

    
342
                        Throwable[] failures = driverManager.getLoadFailures();
343

    
344
                        for (int i = 0; i < failures.length; i++) {
345
                                logger.error(failures[i]);
346
                        }
347

    
348
                        DataSourceFactory.setDriverManager(driverManager);
349
                }
350
        }
351

    
352
        /**
353
         * sets drivers Directory
354
         *
355
         * @param path
356
         */
357
        public static void setDriversPath(String path) {
358
                LayerFactory.driversPath = path;
359
                initializeDriverManager();
360
        }
361
}