Statistics
| Revision:

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

History | View | Annotate | Download (11 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.sql.ResultSet;
72
import java.sql.SQLException;
73
import java.sql.Statement;
74
import java.util.Enumeration;
75
import java.util.TreeMap;
76

    
77

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

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

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

    
119
                try {
120
                        Driver d = dm.getDriver(driverName);
121

    
122
                        if (d instanceof VectorialFileDriver) {
123
                                return createLayer(layerName, (VectorialFileDriver) d, f, proj);
124
                        } else if (d instanceof RasterDriver) {
125
                                return createLayer(layerName, (RasterDriver) d, f, proj);
126
                        }
127
                } catch (DriverLoadException e) {
128
                        throw new DriverException(e);
129
                }
130

    
131
                return null;
132
        }
133

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

    
154
                FLyrVect capa = new FLyrVect();
155
                capa.setName(layerName);
156

    
157
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
158
                if (false) {
159
                } else {
160
                        capa.setSource(adapter);
161
                        capa.setProjection(proj);
162
                }
163

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

    
183
                return capa;
184
        }
185

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

    
212
                return layer;
213
        }
214

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

    
232
                FLyrRaster capa = new FLyrRaster();
233
                capa.setName(layerName);
234

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

    
247
                return capa;
248
        }
249

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

    
267
        /**
268
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
269
         * guard?ndose la URL del servidor que se pasa como par?metro
270
         *
271
         * @param driver
272
         * @param host
273
         * @param port
274
         * @param user
275
         * @param password
276
         * @param dbName
277
         * @param tableName
278
         * @param proj
279
         *
280
         * @return Capa creada.
281
         *
282
         * @throws UnsupportedOperationException
283
         */
284
        public static FLayer createLayer(VectorialDatabaseDriver driver,
285
                String host, int port, String user, String password, String dbName,
286
                String tableName, IProjection proj) {
287
                throw new UnsupportedOperationException();
288
        }
289
        public static FLayer createDBLayer(VectorialDatabaseDriver driver,String layerName, IProjection proj)
290
        {
291
            
292
                
293
                FLyrVect capa = new FLyrVect();
294
                
295
                capa.setName(layerName);
296
                VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
297
                dbAdapter.setDriver(driver);                        
298
        
299
                capa.setSource(dbAdapter);
300
                capa.setProjection(proj);
301
                try {
302
                        if (driver instanceof WithDefaultLegend) {
303
                                WithDefaultLegend aux = (WithDefaultLegend) driver;
304
                                dbAdapter.start();
305
                    capa.setLegend((VectorialLegend) aux.getDefaultLegend());
306
                                dbAdapter.stop();
307
                        } else { 
308
                                capa.setLegend(LegendFactory.createSingleSymbolLegend(
309
                                                capa.getShapeType()));
310
                        }
311
        } catch (FieldNotFoundException e) {
312
            throw new UnsupportedOperationException(e.getMessage());
313
        } catch (DriverException e) {
314
            throw new UnsupportedOperationException(e.getMessage());
315
        }
316

    
317
                return capa;
318

    
319

    
320
                    
321
                }
322

    
323
        
324
        /**
325
         * Obtiene el adaptador de un driver si ya se ha a?adido el origen de datos
326
         * o null si es un origen de datos nuevo
327
         *
328
         * @param source Adaptador.
329
         */
330
        private static void getAdapter(Source source) {
331
        }
332

    
333
        /**
334
         * Registra la asociaci?n entre la fuente de datos que se pasa como
335
         * par?metro y el VectorialAdapter que se pasa como par?metro, para
336
         * satisfacer futuras llamadas a getAdapter
337
         *
338
         * @param s Adaptador
339
         * @param a VectorialAdapter.
340
         */
341
        private static void saveSourceAdapter(Source s, VectorialAdapter a) {
342
        }
343

    
344
        /**
345
         * Crea una FLyrComplexRaster que ataca al driver que se pasa como
346
         * par?metro.
347
         *
348
         * @param driver
349
         * @param f
350
         * @param proj
351
         *
352
         * @throws IllegalArgumentException Si se pasa un driver que no implementa
353
         *                    GeorreferencedRasterDriver o NotGeorreferencedRasterDriver
354
         */
355
        public static void createLayer(RasterDriver driver, File f, IProjection proj)
356
                throws IllegalArgumentException {
357
        }
358

    
359
        /**
360
         * Devuelve el DriverManager.
361
         *
362
         * @return DriverManager.
363
         */
364
        public static DriverManager getDM() {
365
                if (driverManager == null) {
366
                        initializeDriverManager();
367
                }
368

    
369
                return driverManager;
370
        }
371

    
372
        /**
373
         * Inicializa el DriverManager.
374
         */
375
        private static void initializeDriverManager() {
376
                if (driverManager == null) {
377
                        driverManager = new DriverManager();
378
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
379

    
380
                        Throwable[] failures = driverManager.getLoadFailures();
381

    
382
                        for (int i = 0; i < failures.length; i++) {
383
                                logger.error(failures[i]);
384
                        }
385

    
386
                        DataSourceFactory.setDriverManager(driverManager);
387
                }
388
        }
389

    
390
        /**
391
         * sets drivers Directory
392
         *
393
         * @param path
394
         */
395
        public static void setDriversPath(String path) {
396
                LayerFactory.driversPath = path;
397
                initializeDriverManager();
398
        }
399
}