Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / LayerFactory.java @ 3333

History | View | Annotate | Download (19 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 java.io.File;
44
import java.io.IOException;
45
import java.net.URL;
46
import java.sql.SQLException;
47
import java.sql.Types;
48
import java.util.TreeMap;
49

    
50
import org.apache.log4j.Logger;
51
import org.cresques.cts.IProjection;
52

    
53
import com.hardcode.driverManager.Driver;
54
import com.hardcode.driverManager.DriverLoadException;
55
import com.hardcode.driverManager.DriverManager;
56
import com.hardcode.gdbms.engine.customQuery.QueryManager;
57
import com.hardcode.gdbms.engine.data.DataSource;
58
import com.hardcode.gdbms.engine.data.DataSourceFactory;
59
import com.hardcode.gdbms.engine.data.NoSuchTableException;
60
import com.hardcode.gdbms.engine.data.edition.DataWare;
61
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.hardcode.gdbms.engine.values.ValueFactory;
64
import com.iver.cit.gvsig.fmap.DriverException;
65
import com.iver.cit.gvsig.fmap.ProgressListener;
66
import com.iver.cit.gvsig.fmap.ViewPort;
67
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
68
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
69
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
70
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
71
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
72
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
73
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
74
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
75
import com.iver.cit.gvsig.fmap.operations.arcview.ArcJoin;
76
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
77
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
78

    
79

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

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

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

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

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

    
135
                return null;
136
        }
137

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

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

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

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

    
188
                return capa;
189
        }
190

    
191
    public static FLayer createLayer(String layerName, VectorialDriver d,
192
            IProjection proj) throws DriverException {
193
            //TODO Comprobar si hay un adaptador ya
194
            VectorialDefaultAdapter adapter = new VectorialDefaultAdapter();
195
            adapter.setDriver((VectorialDriver) d);
196

    
197
            FLyrVect capa = new FLyrVect();
198
            capa.setName(layerName);
199

    
200
            capa.setSource(adapter);
201
            capa.setProjection(proj);
202

    
203
            try {
204
                
205
                // Le asignamos tambi?n una legenda por defecto acorde con
206
                // el tipo de shape que tenga. Tampoco s? si es aqu? el
207
                // sitio adecuado, pero en fin....
208
                if (d instanceof WithDefaultLegend) {
209
                    WithDefaultLegend aux = (WithDefaultLegend) d;
210
                    adapter.start();
211
                    capa.setLegend((VectorialLegend) aux.getDefaultLegend());
212
                    adapter.stop();
213
                } else {
214
                    capa.setLegend(LegendFactory.createSingleSymbolLegend(
215
                            capa.getShapeType()));
216
                }
217
            } catch (FieldNotFoundException e) {
218
                //Esta no puede saltar
219
            } catch (DriverIOException e) {
220
                throw new DriverException(e);
221
            }
222

    
223
            return capa;
224
        }
225

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

    
243
                FLyrRaster capa = new FLyrRaster();
244
                capa.setName(layerName);
245

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

    
258
                return capa;
259
        }
260

    
261
        /**
262
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n y 
263
         * coordenadas de georeferenciaci?n. Esta funci?n es para georeferenciar 
264
         * capas raster. Para imagenes que no tienen georeferenciaci?n hay que asignarle
265
         * una temporal, normalmente a partir de la vista activa.
266
         *
267
         * @param layerName Nombre de la capa.
268
         * @param d RasterDriver.
269
         * @param f Fichero.
270
         * @param proj Proyecci?n.
271
         * @param extent Extent de la vista activa
272
         *
273
         * @return Nueva capa de tipo raster.
274
         *
275
         * @throws DriverIOException
276
         */
277
        public static FLyrRaster createLayer(String layerName, RasterDriver d,
278
                File f, IProjection proj, ViewPort vp) throws DriverException {
279
                RasterAdapter adapter = new RasterFileAdapter(f);
280
                adapter.setDriver(d);
281
                
282
                FLyrRaster capa = new FLyrRaster();
283
                capa.setName(layerName);
284
                
285
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
286
                if (false) {
287
                } else {
288
                        capa.setSource(adapter);
289
                        capa.setProjection(proj);
290
                        //capa.setTempExtent(vp);
291
                        try {
292
                                capa.load();
293
                        } catch (DriverIOException e) {
294
                                throw new DriverException(e);
295
                        }
296
                }
297

    
298
                return capa;
299
        }
300
        
301
        /**
302
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
303
         * guard?ndose la URL del servidor que se pasa como par?metro
304
         *
305
         * @param driver Driver WFS.
306
         * @param host URL.
307
         * @param proj Proyecci?n.
308
         *
309
         * @return Capa creada.
310
         *
311
         * @throws UnsupportedOperationException
312
         */
313
        public static FLayer createWFSLayer(String layerName,VectorialDriver driver, URL host,
314
                IProjection proj) {
315
                //throw new UnsupportedOperationException();
316
                FLyrVect layer=new FLyrVect();
317
                //layer.setName(name);
318
                VectorialAdapter adapter=new WFSAdapter();
319
                adapter.setDriver(driver);
320
                //WithDefaultLegend aux=(WithDefaultLegend)driver;
321
                
322
                layer.setName(layerName);
323
                layer.setSource(adapter);
324
                layer.setProjection(proj);
325
                try {
326
                        layer.setLegend(LegendFactory.createSingleSymbolLegend(
327
                                        layer.getShapeType()));
328
                } catch (FieldNotFoundException e) {
329
                        e.printStackTrace();
330
                } catch (DriverException e) {
331
                        e.printStackTrace();
332
                }
333
                //layer.setHost(host);
334
                return layer;
335
        }
336
    public static FLayer createArcSDELayer(String layerName,VectorialDatabaseDriver driver, IProjection proj) {
337
            //throw new UnsupportedOperationException();
338
            FLyrVect layer=new FLyrVect();
339
            VectorialAdapter adapter=new VectorialDBAdapter();
340
            adapter.setDriver(driver);
341
            
342
            layer.setName(layerName);
343
            layer.setSource(adapter);
344
            layer.setProjection(proj);
345
            try {
346
                if (driver instanceof WithDefaultLegend) {
347
                    WithDefaultLegend aux = (WithDefaultLegend) driver;
348
                    adapter.start();
349
                    layer.setLegend((VectorialLegend) aux.getDefaultLegend());
350
                    adapter.stop();
351
                } else { 
352
                    layer.setLegend(LegendFactory.createSingleSymbolLegend(
353
                            layer.getShapeType()));
354
                }
355
            } catch (FieldNotFoundException e) {
356
                throw new UnsupportedOperationException(e.getMessage());
357
            } catch (DriverIOException e) {
358
                throw new UnsupportedOperationException(e.getMessage());
359
            } catch (DriverException e) {
360
                // TODO Auto-generated catch block
361
                e.printStackTrace();
362
            }
363
            
364
            try {
365
                layer.setLegend(LegendFactory.createSingleSymbolLegend(
366
                        layer.getShapeType()));
367
            } catch (FieldNotFoundException e) {
368
                e.printStackTrace();
369
            } catch (DriverException e) {
370
                e.printStackTrace();
371
            }
372
            return layer;
373
        }
374

    
375
        /**
376
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
377
         * guard?ndose la URL del servidor que se pasa como par?metro
378
         *
379
         * @param driver
380
         * @param host
381
         * @param port
382
         * @param user
383
         * @param password
384
         * @param dbName
385
         * @param tableName
386
         * @param proj
387
         *
388
         * @return Capa creada.
389
         *
390
         * @throws UnsupportedOperationException
391
         */
392
        public static FLayer createLayer(VectorialDatabaseDriver driver,
393
                String host, int port, String user, String password, String dbName,
394
                String tableName, IProjection proj) {
395
                throw new UnsupportedOperationException();
396
        }
397
        public static FLayer createDBLayer(VectorialDatabaseDriver driver,String layerName, IProjection proj)
398
        {
399
            
400
                
401
                FLyrVect capa = new FLyrVect();
402
                
403
                capa.setName(layerName);
404
                VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
405
                dbAdapter.setDriver(driver);                        
406
        
407
                capa.setSource(dbAdapter);
408
                capa.setProjection(proj);
409
                try {
410
                        if (driver instanceof WithDefaultLegend) {
411
                                WithDefaultLegend aux = (WithDefaultLegend) driver;
412
                                dbAdapter.start();
413
                    capa.setLegend((VectorialLegend) aux.getDefaultLegend());
414
                                dbAdapter.stop();
415
                        } else { 
416
                                capa.setLegend(LegendFactory.createSingleSymbolLegend(
417
                                                capa.getShapeType()));
418
                        }
419
        } catch (FieldNotFoundException e) {
420
            throw new UnsupportedOperationException(e.getMessage());
421
        } catch (DriverException e) {
422
            throw new UnsupportedOperationException(e.getMessage());
423
        }
424

    
425
                return capa;
426

    
427

    
428
                    
429
                }
430

    
431
        /**
432
         * @param driver
433
         * @param layerName
434
         * @param object
435
         * @return
436
         * @throws SQLException
437
         * @throws DriverIOException
438
         * @throws IOException
439
         * @throws DriverLoadException
440
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException
441
         * @throws NoSuchTableException
442
         * @throws ClassNotFoundException
443
         * @throws 
444
         */
445
        public static FLayer createDisconnectedDBLayer(VectorialJDBCDriver driver, String layerName, IProjection proj, ProgressListener listener) throws SQLException, IOException, DriverIOException, DriverLoadException, NoSuchTableException, com.hardcode.gdbms.engine.data.driver.DriverException, ClassNotFoundException {
446
            VectorialDisconnectedDBAdapter dbAdapter = new VectorialDisconnectedDBAdapter();
447
            dbAdapter.setDriver(driver);
448
            DataSource ds = dbAdapter.getRecordset();
449
            ds.start();
450
            String database = dataSourceFactory.getTempFile();
451
            String[] fieldNames = new String[ds.getFieldCount() + 1];
452
            System.arraycopy(ds.getFieldNames(), 0, fieldNames, 1, ds.getFieldCount());
453
            fieldNames[0] = "the_geom";
454
            int[] types = new int[fieldNames.length];
455
            types[0] = Types.BINARY;
456
            for (int i = 1; i < types.length; i++) {
457
            types[i] = ds.getFieldType(i-1);
458
        }
459
            String dsName = dataSourceFactory.createTable(database, ds.getPKNames(), fieldNames, types);
460
            
461
        DBLayerDefinition lyrDef = new DBLayerDefinition();
462
        lyrDef.setTableName(dsName);
463
        lyrDef.setLayerName(layerName);
464
        lyrDef.setFieldNames(ds.getFieldNames());
465
        lyrDef.setFieldGeometry("the_geom");
466
        lyrDef.setFieldID(ds.getPKNames()[0]);
467
        lyrDef.setClassToInstantiate("org.hsqldb.jdbcDriver");
468
        
469
            dataSourceFactory.addDBDataSourceByTable(dsName, null, 0, "sa", "", database, dsName, "GDBMS HSQLDB Transactional driver");
470
            DataSource local = dataSourceFactory.createRandomDataSource(dsName, DataSourceFactory.MANUAL_OPENING);
471
            local.start();
472
            DataWare dw = local.getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
473
            dw.start();
474
            long t1 = System.currentTimeMillis();
475
            dw.beginTrans();
476
            
477
            if (listener == null){
478
                listener = new ProgressListener() {
479
                /**
480
                 * @see com.iver.cit.gvsig.fmap.ProgressListener#progress(int)
481
                 */
482
                public void progress(int n) {
483
                    //do nothing
484
                }
485
            };
486
            }
487
            
488
            for (int i = 0; i < dbAdapter.getShapeCount(); i++) {
489
                Value[] row = new Value[ds.getFieldCount() + 1];
490
                
491
                byte[] bytes = dbAdapter.getShape(i).toWKB();
492
                row[0] = ValueFactory.createValue(bytes);
493
                                
494
                for (int j = 0; j < ds.getFieldCount(); j++) {
495
                    row[j+1] = ds.getFieldValue(i, j);                
496
            }
497

    
498
                dw.insertFilledRow(row);
499
                listener.progress(100 * i / dbAdapter.getShapeCount());
500
        }
501
            
502
            long t2 = System.currentTimeMillis();
503
            dw.commitTrans();
504
            long t3 = System.currentTimeMillis();
505
            System.out.println((t2 - t1) + " - " + (t3 - t2));
506
            dw.stop();
507
            local.stop();
508
            ds.stop();
509
            VectorialJDBCDriver cacheDriver = (VectorialJDBCDriver) LayerFactory.getDM().getDriver("HSQLDB Driver");
510
        Class.forName("org.hsqldb.jdbcDriver");
511

    
512
            cacheDriver.setData(java.sql.DriverManager.getConnection("jdbc:hsqldb:file:" + database, "sa", ""), lyrDef);
513
            cacheDriver.setWorkingArea(driver.getWorkingArea());
514
        return createDBLayer(cacheDriver, layerName, proj);
515
        }
516
        
517
        
518
        /**
519
         * Obtiene el adaptador de un driver si ya se ha a?adido el origen de datos
520
         * o null si es un origen de datos nuevo
521
         *
522
         * @param source Adaptador.
523
         */
524
        private static void getAdapter(Source source) {
525
        }
526

    
527
        /**
528
         * Registra la asociaci?n entre la fuente de datos que se pasa como
529
         * par?metro y el VectorialAdapter que se pasa como par?metro, para
530
         * satisfacer futuras llamadas a getAdapter
531
         *
532
         * @param s Adaptador
533
         * @param a VectorialAdapter.
534
         */
535
        private static void saveSourceAdapter(Source s, VectorialAdapter a) {
536
        }
537

    
538
        /**
539
         * Crea una FLyrComplexRaster que ataca al driver que se pasa como
540
         * par?metro.
541
         *
542
         * @param driver
543
         * @param f
544
         * @param proj
545
         *
546
         * @throws IllegalArgumentException Si se pasa un driver que no implementa
547
         *                    GeorreferencedRasterDriver o NotGeorreferencedRasterDriver
548
         */
549
        public static void createLayer(RasterDriver driver, File f, IProjection proj)
550
                throws IllegalArgumentException {
551
        }
552

    
553
        /**
554
         * Devuelve el DriverManager.
555
         *
556
         * @return DriverManager.
557
         */
558
        public static DriverManager getDM() {
559
                initializeDriverManager();
560

    
561
                return driverManager;
562
        }
563

    
564
        /**
565
         * Inicializa el DriverManager.
566
         */
567
        private static void initializeDriverManager() {
568
                if (driverManager == null) {
569
                        driverManager = new DriverManager();
570
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
571

    
572
                        Throwable[] failures = driverManager.getLoadFailures();
573

    
574
                        for (int i = 0; i < failures.length; i++) {
575
                                logger.error("", failures[i]);
576
                        }
577

    
578
                        getDataSourceFactory().setDriverManager(driverManager);
579
                        getDataSourceFactory().initialize();
580
                        QueryManager.registerQuery(new ArcJoin());
581
                }
582
        }
583

    
584
        /**
585
         * sets drivers Directory
586
         *
587
         * @param path
588
         */
589
        public static void setDriversPath(String path) {
590
                LayerFactory.driversPath = path;
591
                initializeDriverManager();
592
        }
593
        
594
        
595
        /**
596
         * @return Returns the dataSourceFactory.
597
         */
598
        public static DataSourceFactory getDataSourceFactory() {
599
                if (dataSourceFactory == null){
600
                        dataSourceFactory = new DataSourceFactory();
601
                }
602
                return dataSourceFactory;
603
        }
604
}