Statistics
| Revision:

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

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

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

    
54
import com.hardcode.driverManager.Driver;
55
import com.hardcode.driverManager.DriverLoadException;
56
import com.hardcode.driverManager.DriverManager;
57
import com.hardcode.gdbms.engine.customQuery.QueryManager;
58
import com.hardcode.gdbms.engine.data.DataSource;
59
import com.hardcode.gdbms.engine.data.DataSourceFactory;
60
import com.hardcode.gdbms.engine.data.NoSuchTableException;
61
import com.hardcode.gdbms.engine.data.edition.DataWare;
62
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
63
import com.hardcode.gdbms.engine.values.Value;
64
import com.hardcode.gdbms.engine.values.ValueFactory;
65
import com.iver.cit.gvsig.fmap.DriverException;
66
import com.iver.cit.gvsig.fmap.ProgressListener;
67
import com.iver.cit.gvsig.fmap.ViewPort;
68
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
69
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
70
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
71
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
72
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
73
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
74
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
75
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
76
import com.iver.cit.gvsig.fmap.operations.arcview.ArcJoin;
77
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
78
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
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 una capa Raster a partir del nombre driver, fichero y proyecci?n y 
258
         * coordenadas de georeferenciaci?n. Esta funci?n es para georeferenciar 
259
         * capas raster. Para imagenes que no tienen georeferenciaci?n hay que asignarle
260
         * una temporal, normalmente a partir de la vista activa.
261
         *
262
         * @param layerName Nombre de la capa.
263
         * @param d RasterDriver.
264
         * @param f Fichero.
265
         * @param proj Proyecci?n.
266
         * @param extent Extent de la vista activa
267
         *
268
         * @return Nueva capa de tipo raster.
269
         *
270
         * @throws DriverIOException
271
         */
272
        public static FLyrRaster createLayer(String layerName, RasterDriver d,
273
                File f, IProjection proj, ViewPort vp) throws DriverException {
274
                RasterAdapter adapter = new RasterFileAdapter(f);
275
                adapter.setDriver(d);
276
                
277
                FLyrRaster capa = new FLyrRaster();
278
                capa.setName(layerName);
279
                
280
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
281
                if (false) {
282
                } else {
283
                        capa.setSource(adapter);
284
                        capa.setProjection(proj);
285
                        //capa.setTempExtent(vp);
286
                        try {
287
                                capa.load();
288
                        } catch (DriverIOException e) {
289
                                throw new DriverException(e);
290
                        }
291
                }
292

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

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

    
420
                return capa;
421

    
422

    
423
                    
424
                }
425

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

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

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

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

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

    
548
        /**
549
         * Devuelve el DriverManager.
550
         *
551
         * @return DriverManager.
552
         */
553
        public static DriverManager getDM() {
554
                initializeDriverManager();
555

    
556
                return driverManager;
557
        }
558

    
559
        /**
560
         * Inicializa el DriverManager.
561
         */
562
        private static void initializeDriverManager() {
563
                if (driverManager == null) {
564
                        driverManager = new DriverManager();
565
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
566

    
567
                        Throwable[] failures = driverManager.getLoadFailures();
568

    
569
                        for (int i = 0; i < failures.length; i++) {
570
                                logger.error("", failures[i]);
571
                        }
572

    
573
                        getDataSourceFactory().setDriverManager(driverManager);
574
                        getDataSourceFactory().initialize();
575
                        QueryManager.registerQuery(new ArcJoin());
576
                }
577
        }
578

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