Statistics
| Revision:

root / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / LayerFactory.java @ 1920

History | View | Annotate | Download (13 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.customQuery.QueryManager;
48
import com.hardcode.gdbms.engine.data.DataSourceFactory;
49
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
50

    
51
import com.iver.cit.gvsig.fmap.DriverException;
52
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
53
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
54
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
55
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
56
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
57
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
58
import com.iver.cit.gvsig.fmap.drivers.wfs.WFSDriver;
59
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
60
import com.iver.cit.gvsig.fmap.operations.arcview.ArcJoin;
61
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
62
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
63

    
64
import org.apache.log4j.Logger;
65

    
66
import org.cresques.cts.IProjection;
67

    
68
import java.awt.geom.Rectangle2D;
69

    
70
import java.io.File;
71

    
72
import java.net.URL;
73

    
74
import java.sql.ResultSet;
75
import java.sql.SQLException;
76
import java.sql.Statement;
77
import java.util.Enumeration;
78
import java.util.TreeMap;
79

    
80

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

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

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

124
                try {
125
                        Driver d = dm.getDriver(driverName);
126

127
                        if (d instanceof VectorialFileDriver) {
128
                                return createLayer(layerName, (VectorialFileDriver) d, f, proj);
129
                        } else if (d instanceof RasterDriver) {
130
                                return createLayer(layerName, (RasterDriver) d, f, proj);
131
                        }
132
                } catch (DriverLoadException e) {
133
                        throw new DriverException(e);
134
                }
135

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

    
159
                FLyrVect capa = new FLyrVect();
160
                capa.setName(layerName);
161

    
162
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
163
                if (false) {
164
                } else {
165
                        capa.setSource(adapter);
166
                        capa.setProjection(proj);
167
                }
168

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

    
189
                return capa;
190
        }
191

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

    
218
                return layer;
219
        }
220

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

    
238
                FLyrRaster capa = new FLyrRaster();
239
                capa.setName(layerName);
240

    
241
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
242
                if (false) {
243
                } else {
244
                        capa.setSource(adapter);
245
                        capa.setProjection(proj);
246
                        try {
247
                                capa.load();
248
                        } catch (DriverIOException e) {
249
                                throw new DriverException(e);
250
                        }
251
                }
252

    
253
                return capa;
254
        }
255

    
256
        /**
257
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
258
         * guard?ndose la URL del servidor que se pasa como par?metro
259
         *
260
         * @param driver Driver WFS.
261
         * @param host URL.
262
         * @param proj Proyecci?n.
263
         *
264
         * @return Capa creada.
265
         *
266
         * @throws UnsupportedOperationException
267
         */
268
        public static FLayer createWFSLayer(String layerName,WFSDriver driver, URL host,
269
                IProjection proj) {
270
                //throw new UnsupportedOperationException();
271
                FLyrVect layer=new FLyrVect();
272
                //layer.setName(name);
273
                VectorialAdapter adapter=new WFSAdapter();
274
                adapter.setDriver(driver);
275
                //WithDefaultLegend aux=(WithDefaultLegend)driver;
276
                
277
                layer.setName(layerName);
278
                layer.setSource(adapter);
279
                layer.setProjection(proj);
280
                try {
281
                        layer.setLegend(LegendFactory.createSingleSymbolLegend(
282
                                        layer.getShapeType()));
283
                } catch (FieldNotFoundException e) {
284
                        e.printStackTrace();
285
                } catch (DriverException e) {
286
                        e.printStackTrace();
287
                }
288
                //layer.setHost(host);
289
                return layer;
290
        }
291

    
292
        /**
293
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
294
         * guard?ndose la URL del servidor que se pasa como par?metro
295
         *
296
         * @param driver
297
         * @param host
298
         * @param port
299
         * @param user
300
         * @param password
301
         * @param dbName
302
         * @param tableName
303
         * @param proj
304
         *
305
         * @return Capa creada.
306
         *
307
         * @throws UnsupportedOperationException
308
         */
309
        public static FLayer createLayer(VectorialDatabaseDriver driver,
310
                String host, int port, String user, String password, String dbName,
311
                String tableName, IProjection proj) {
312
                throw new UnsupportedOperationException();
313
        }
314
        public static FLayer createDBLayer(VectorialDatabaseDriver driver,String layerName, IProjection proj)
315
        {
316
            
317
                
318
                FLyrVect capa = new FLyrVect();
319
                
320
                capa.setName(layerName);
321
                VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
322
                dbAdapter.setDriver(driver);                        
323
        
324
                capa.setSource(dbAdapter);
325
                capa.setProjection(proj);
326
                try {
327
                        if (driver instanceof WithDefaultLegend) {
328
                                WithDefaultLegend aux = (WithDefaultLegend) driver;
329
                                dbAdapter.start();
330
                    capa.setLegend((VectorialLegend) aux.getDefaultLegend());
331
                                dbAdapter.stop();
332
                        } else { 
333
                                capa.setLegend(LegendFactory.createSingleSymbolLegend(
334
                                                capa.getShapeType()));
335
                        }
336
        } catch (FieldNotFoundException e) {
337
            throw new UnsupportedOperationException(e.getMessage());
338
        } catch (DriverException e) {
339
            throw new UnsupportedOperationException(e.getMessage());
340
        }
341

    
342
                return capa;
343

    
344

    
345
                    
346
                }
347

    
348
        /**
349
         * @param driver
350
         * @param layerName
351
         * @param object
352
         * @return
353
         */
354
        public static FLayer createDisconnectedDBLayer(VectorialDatabaseDriver driver, String layerName, IProjection proj) {
355
            
356
        
357
        FLyrVect capa = new FLyrVect();
358
        
359
        capa.setName(layerName);
360
        VectorialDisconnectedDBAdapter dbAdapter = new VectorialDisconnectedDBAdapter();
361
        dbAdapter.setDriver(driver);                        
362

    
363
        capa.setSource(dbAdapter);
364
        capa.setProjection(proj);
365
        try {
366
                if (driver instanceof WithDefaultLegend) {
367
                        WithDefaultLegend aux = (WithDefaultLegend) driver;
368
                        dbAdapter.start();
369
            capa.setLegend((VectorialLegend) aux.getDefaultLegend());
370
                        dbAdapter.stop();
371
                } else { 
372
                        capa.setLegend(LegendFactory.createSingleSymbolLegend(
373
                                        capa.getShapeType()));
374
                }
375
    } catch (FieldNotFoundException e) {
376
        throw new UnsupportedOperationException(e.getMessage());
377
    } catch (DriverException e) {
378
        throw new UnsupportedOperationException(e.getMessage());
379
    }
380

    
381
        return capa;
382

    
383

    
384
            
385
        }
386
        
387
        
388
        /**
389
         * Obtiene el adaptador de un driver si ya se ha a?adido el origen de datos
390
         * o null si es un origen de datos nuevo
391
         *
392
         * @param source Adaptador.
393
         */
394
        private static void getAdapter(Source source) {
395
        }
396

    
397
        /**
398
         * Registra la asociaci?n entre la fuente de datos que se pasa como
399
         * par?metro y el VectorialAdapter que se pasa como par?metro, para
400
         * satisfacer futuras llamadas a getAdapter
401
         *
402
         * @param s Adaptador
403
         * @param a VectorialAdapter.
404
         */
405
        private static void saveSourceAdapter(Source s, VectorialAdapter a) {
406
        }
407

    
408
        /**
409
         * Crea una FLyrComplexRaster que ataca al driver que se pasa como
410
         * par?metro.
411
         *
412
         * @param driver
413
         * @param f
414
         * @param proj
415
         *
416
         * @throws IllegalArgumentException Si se pasa un driver que no implementa
417
         *                    GeorreferencedRasterDriver o NotGeorreferencedRasterDriver
418
         */
419
        public static void createLayer(RasterDriver driver, File f, IProjection proj)
420
                throws IllegalArgumentException {
421
        }
422

    
423
        /**
424
         * Devuelve el DriverManager.
425
         *
426
         * @return DriverManager.
427
         */
428
        public static DriverManager getDM() {
429
                initializeDriverManager();
430

    
431
                return driverManager;
432
        }
433

    
434
        /**
435
         * Inicializa el DriverManager.
436
         */
437
        private static void initializeDriverManager() {
438
                if (driverManager == null) {
439
                        driverManager = new DriverManager();
440
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
441

    
442
                        Throwable[] failures = driverManager.getLoadFailures();
443

    
444
                        for (int i = 0; i < failures.length; i++) {
445
                                logger.error(failures[i]);
446
                        }
447

    
448
                        getDataSourceFactory().setDriverManager(driverManager);
449
                        QueryManager.registerQuery(new ArcJoin());
450
                }
451
        }
452

    
453
        /**
454
         * sets drivers Directory
455
         *
456
         * @param path
457
         */
458
        public static void setDriversPath(String path) {
459
                LayerFactory.driversPath = path;
460
                initializeDriverManager();
461
        }
462
        
463
        
464
        /**
465
         * @return Returns the dataSourceFactory.
466
         */
467
        public static DataSourceFactory getDataSourceFactory() {
468
                if (dataSourceFactory == null){
469
                        dataSourceFactory = new DataSourceFactory();
470
                }
471
                return dataSourceFactory;
472
        }
473
}