Statistics
| Revision:

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

History | View | Annotate | Download (19.2 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.driverManager.WriterManager;
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 String writersPath = "../FMap 03/drivers";
90
        private static DriverManager driverManager;
91
        private static WriterManager writerManager;
92

    
93
        private static DataSourceFactory dataSourceFactory;
94

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

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

    
126
                try {
127
                        Driver d = dm.getDriver(driverName);
128

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

    
138
                return null;
139
        }
140

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

    
161
                FLyrVect capa = new FLyrVect();
162
                capa.setName(layerName);
163
                try{
164
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
165
                if (false) {
166
                } else {
167
                        capa.setSource(adapter);
168
                        capa.setProjection(proj);
169
                }
170
                //try {
171

    
172
                        // Le asignamos tambi?n una legenda por defecto acorde con
173
                        // el tipo de shape que tenga. Tampoco s? si es aqu? el
174
                        // sitio adecuado, pero en fin....
175
                        if (d instanceof WithDefaultLegend) {
176
                                WithDefaultLegend aux = (WithDefaultLegend) d;
177
                                adapter.start();
178
                                capa.setLegend((VectorialLegend) aux.getDefaultLegend());
179
                                adapter.stop();
180
                        } else {
181
                                capa.setLegend(LegendFactory.createSingleSymbolLegend(
182
                                                capa.getShapeType()));
183
                        }
184
                /*} catch (FieldNotFoundException e) {
185
                        //Esta no puede saltar
186
                } catch (DriverIOException e) {
187
                        throw new DriverException(e);
188
                }*/
189
                }catch (Exception e) {
190
                /*        JOptionPane.showMessageDialog(null,"Se ha producido un error cargando la capa '"
191
                                        +layerName+"'\n El recurso asociado a la capa es : "
192
                                        +f.getAbsolutePath()+"\n Esta capa ser? eliminada del proyecto.");
193
                        */
194
                        //capa.setBroken(true);
195
                        //return capa;
196
                        //throw new DriverException(e);
197
                        capa.setAvailable(false);
198
                        return capa;
199
                }
200

    
201
                return capa;
202
        }
203

    
204
    public static FLayer createLayer(String layerName, VectorialDriver d,
205
            IProjection proj) throws DriverException {
206
                    VectorialAdapter adapter = null;
207
            if (d instanceof VectorialFileDriver)
208
            {                    
209
                    adapter = new VectorialFileAdapter(((VectorialFileDriver)d).getFile());
210
            }
211
            else if (d instanceof VectorialJDBCDriver)
212
            {
213
                    adapter = new VectorialDBAdapter();
214
            }
215
            else
216
            {
217
                    adapter = new VectorialDefaultAdapter();
218
            }
219
            adapter.setDriver((VectorialDriver) d);
220

    
221
            FLyrVect capa = new FLyrVect();
222
            capa.setName(layerName);
223

    
224
            capa.setSource(adapter);
225
            capa.setProjection(proj);
226

    
227
            try {
228

    
229
                // Le asignamos tambi?n una legenda por defecto acorde con
230
                // el tipo de shape que tenga. Tampoco s? si es aqu? el
231
                // sitio adecuado, pero en fin....
232
                if (d instanceof WithDefaultLegend) {
233
                    WithDefaultLegend aux = (WithDefaultLegend) d;
234
                    adapter.start();
235
                    capa.setLegend((VectorialLegend) aux.getDefaultLegend());
236
                    adapter.stop();
237
                } else {
238
                    capa.setLegend(LegendFactory.createSingleSymbolLegend(
239
                            capa.getShapeType()));
240
                }
241
            } catch (FieldNotFoundException e) {
242
                //Esta no puede saltar
243
            } catch (DriverIOException e) {
244
                throw new DriverException(e);
245
            }
246

    
247
            return capa;
248
        }
249

    
250
        /**
251
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n.
252
         *
253
         * @param layerName Nombre de la capa.
254
         * @param d RasterDriver.
255
         * @param f Fichero.
256
         * @param proj Proyecci?n.
257
         *
258
         * @return Nueva capa de tipo raster.
259
         *
260
         * @throws DriverIOException
261
         */
262
        public static FLyrRaster createLayer(String layerName, RasterDriver d,
263
                File f, IProjection proj) throws DriverException {
264
                RasterAdapter adapter = new RasterFileAdapter(f);
265
                adapter.setDriver(d);
266

    
267
                FLyrRaster capa = new FLyrRaster();
268
                capa.setName(layerName);
269

    
270
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
271
                if (false) {
272
                } else {
273
                        capa.setSource(adapter);
274
                        capa.setProjection(proj);
275
                        try {
276
                                capa.load();
277
                        } catch (DriverIOException e) {
278
                                throw new DriverException(e);
279
                        }
280
                }
281

    
282
                return capa;
283
        }
284

    
285
        /**
286
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n y
287
         * coordenadas de georeferenciaci?n. Esta funci?n es para georeferenciar
288
         * capas raster. Para imagenes que no tienen georeferenciaci?n hay que asignarle
289
         * una temporal, normalmente a partir de la vista activa.
290
         *
291
         * @param layerName Nombre de la capa.
292
         * @param d RasterDriver.
293
         * @param f Fichero.
294
         * @param proj Proyecci?n.
295
         * @param extent Extent de la vista activa
296
         *
297
         * @return Nueva capa de tipo raster.
298
         *
299
         * @throws DriverIOException
300
         */
301
        public static FLyrRaster createLayer(String layerName, RasterDriver d,
302
                File f, IProjection proj, ViewPort vp) throws DriverException {
303
                RasterAdapter adapter = new RasterFileAdapter(f);
304
                adapter.setDriver(d);
305

    
306
                FLyrRaster capa = new FLyrRaster();
307
                capa.setName(layerName);
308

    
309
                //TODO Meter esto dentro de la comprobaci?n de si hay memoria
310
                if (false) {
311
                } else {
312
                        capa.setSource(adapter);
313
                        capa.setProjection(proj);
314
                        //capa.setTempExtent(vp);
315
                        try {
316
                                capa.load();
317
                        } catch (DriverIOException e) {
318
                                throw new DriverException(e);
319
                        }
320
                }
321

    
322
                return capa;
323
        }
324

    
325

    
326
    public static FLayer createArcSDELayer(String layerName,VectorialDatabaseDriver driver, IProjection proj) {
327
            //throw new UnsupportedOperationException();
328
            FLyrVect layer=new FLyrVect();
329
            VectorialAdapter adapter=new VectorialDBAdapter();
330
            adapter.setDriver(driver);
331

    
332
            layer.setName(layerName);
333
            layer.setSource(adapter);
334
            layer.setProjection(proj);
335
            try {
336
                if (driver instanceof WithDefaultLegend) {
337
                    WithDefaultLegend aux = (WithDefaultLegend) driver;
338
                    adapter.start();
339
                    layer.setLegend((VectorialLegend) aux.getDefaultLegend());
340
                    adapter.stop();
341
                } else {
342
                    layer.setLegend(LegendFactory.createSingleSymbolLegend(
343
                            layer.getShapeType()));
344
                }
345
            } catch (FieldNotFoundException e) {
346
                throw new UnsupportedOperationException(e.getMessage());
347
            } catch (DriverIOException e) {
348
                throw new UnsupportedOperationException(e.getMessage());
349
            } catch (DriverException e) {
350
                // TODO Auto-generated catch block
351
                e.printStackTrace();
352
            }
353

    
354
            try {
355
                layer.setLegend(LegendFactory.createSingleSymbolLegend(
356
                        layer.getShapeType()));
357
            } catch (FieldNotFoundException e) {
358
                e.printStackTrace();
359
            } catch (DriverException e) {
360
                e.printStackTrace();
361
            }
362
            return layer;
363
        }
364

    
365
        /**
366
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
367
         * guard?ndose la URL del servidor que se pasa como par?metro
368
         *
369
         * @param driver
370
         * @param host
371
         * @param port
372
         * @param user
373
         * @param password
374
         * @param dbName
375
         * @param tableName
376
         * @param proj
377
         *
378
         * @return Capa creada.
379
         *
380
         * @throws UnsupportedOperationException
381
         */
382
        public static FLayer createLayer(VectorialDatabaseDriver driver,
383
                String host, int port, String user, String password, String dbName,
384
                String tableName, IProjection proj) {
385
                throw new UnsupportedOperationException();
386
        }
387
        public static FLayer createDBLayer(VectorialDatabaseDriver driver,String layerName, IProjection proj)
388
        {
389

    
390

    
391
                FLyrVect capa = new FLyrVect();
392

    
393
                capa.setName(layerName);
394
                VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
395
                dbAdapter.setDriver(driver);
396

    
397
                capa.setSource(dbAdapter);
398
                capa.setProjection(proj);
399
                try {
400
                        if (driver instanceof WithDefaultLegend) {
401
                                WithDefaultLegend aux = (WithDefaultLegend) driver;
402
                                dbAdapter.start();
403
                    capa.setLegend((VectorialLegend) aux.getDefaultLegend());
404
                                dbAdapter.stop();
405
                        } else {
406
                                capa.setLegend(LegendFactory.createSingleSymbolLegend(
407
                                                capa.getShapeType()));
408
                        }
409
        } catch (FieldNotFoundException e) {
410
                        capa.setAvailable(false);
411
                        return capa;
412
            //throw new UnsupportedOperationException(e.getMessage());
413
        } catch (DriverException e) {
414
                capa.setAvailable(false);
415
                        return capa;
416
            //throw new UnsupportedOperationException(e.getMessage());
417
        } catch (Exception e) {
418
                capa.setAvailable(false);
419
                        return capa;                
420
        }
421

    
422
                return capa;
423

    
424

    
425

    
426
                }
427

    
428
        /**
429
         * @param driver
430
         * @param layerName
431
         * @param object
432
         * @return
433
         * @throws SQLException
434
         * @throws DriverIOException
435
         * @throws IOException
436
         * @throws DriverLoadException
437
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException
438
         * @throws NoSuchTableException
439
         * @throws ClassNotFoundException
440
         * @throws
441
         */
442
        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 {
443
            VectorialDisconnectedDBAdapter dbAdapter = new VectorialDisconnectedDBAdapter();
444
            dbAdapter.setDriver(driver);
445
            DataSource ds = dbAdapter.getRecordset();
446
            ds.start();
447
            String database = dataSourceFactory.getTempFile();
448
            String[] fieldNames = new String[ds.getFieldCount() + 1];
449
            System.arraycopy(ds.getFieldNames(), 0, fieldNames, 1, ds.getFieldCount());
450
            fieldNames[0] = "the_geom";
451
            int[] types = new int[fieldNames.length];
452
            types[0] = Types.BINARY;
453
            for (int i = 1; i < types.length; i++) {
454
            types[i] = ds.getFieldType(i-1);
455
        }
456
            String dsName = dataSourceFactory.createTable(database, ds.getPKNames(), fieldNames, types);
457

    
458
        DBLayerDefinition lyrDef = new DBLayerDefinition();
459
        lyrDef.setTableName(dsName);
460
        lyrDef.setName(layerName);
461
        lyrDef.setFieldNames(ds.getFieldNames());
462
        lyrDef.setFieldGeometry("the_geom");
463
        lyrDef.setFieldID(ds.getPKNames()[0]);
464
        lyrDef.setClassToInstantiate("org.hsqldb.jdbcDriver");
465

    
466
            dataSourceFactory.addDBDataSourceByTable(dsName, null, 0, "sa", "", database, dsName, "GDBMS HSQLDB Transactional driver");
467
            DataSource local = dataSourceFactory.createRandomDataSource(dsName, DataSourceFactory.MANUAL_OPENING);
468
            local.start();
469
            DataWare dw = local.getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER);
470
            dw.start();
471
            long t1 = System.currentTimeMillis();
472
            dw.beginTrans();
473

    
474
            if (listener == null){
475
                listener = new ProgressListener() {
476
                /**
477
                 * @see com.iver.cit.gvsig.fmap.ProgressListener#progress(int)
478
                 */
479
                public void progress(int n) {
480
                    //do nothing
481
                }
482
            };
483
            }
484

    
485
            for (int i = 0; i < dbAdapter.getShapeCount(); i++) {
486
                Value[] row = new Value[ds.getFieldCount() + 1];
487

    
488
                byte[] bytes = dbAdapter.getShape(i).toWKB();
489
                row[0] = ValueFactory.createValue(bytes);
490

    
491
                for (int j = 0; j < ds.getFieldCount(); j++) {
492
                    row[j+1] = ds.getFieldValue(i, j);
493
            }
494

    
495
                dw.insertFilledRow(row);
496
                listener.progress(100 * i / dbAdapter.getShapeCount());
497
        }
498

    
499
            long t2 = System.currentTimeMillis();
500
            dw.commitTrans();
501
            long t3 = System.currentTimeMillis();
502
            System.out.println((t2 - t1) + " - " + (t3 - t2));
503
            dw.stop();
504
            local.stop();
505
            ds.stop();
506
            VectorialJDBCDriver cacheDriver = (VectorialJDBCDriver) LayerFactory.getDM().getDriver("HSQLDB Driver");
507
        Class.forName("org.hsqldb.jdbcDriver");
508

    
509
            cacheDriver.setData(java.sql.DriverManager.getConnection("jdbc:hsqldb:file:" + database, "sa", ""), lyrDef);
510
            cacheDriver.setWorkingArea(driver.getWorkingArea());
511
        return createDBLayer(cacheDriver, layerName, proj);
512
        }
513

    
514

    
515
        /**
516
         * Crea una FLyrComplexRaster que ataca al driver que se pasa como
517
         * par?metro.
518
         *
519
         * @param driver
520
         * @param f
521
         * @param proj
522
         *
523
         * @throws IllegalArgumentException Si se pasa un driver que no implementa
524
         *                    GeorreferencedRasterDriver o NotGeorreferencedRasterDriver
525
         */
526
        public static void createLayer(RasterDriver driver, File f, IProjection proj)
527
                throws IllegalArgumentException {
528
        }
529

    
530
        /**
531
         * Devuelve el DriverManager.
532
         *
533
         * @return DriverManager.
534
         */
535
        public static DriverManager getDM() {
536
                initializeDriverManager();
537

    
538
                return driverManager;
539
        }
540
        /**
541
         * Devuelve el WriterManager.
542
         *
543
         * @return WriterManager.
544
         */
545
        public static WriterManager getWM() {
546
                initializeWriterManager();
547

    
548
                return writerManager;
549
        }
550
        /**
551
         * Inicializa el DriverManager.
552
         */
553
        private static void initializeDriverManager() {
554
                if (driverManager == null) {
555
                        driverManager = new DriverManager();
556
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
557

    
558
                        Throwable[] failures = driverManager.getLoadFailures();
559

    
560
                        for (int i = 0; i < failures.length; i++) {
561
                                logger.error("", failures[i]);
562
                        }
563

    
564
                        getDataSourceFactory().setDriverManager(driverManager);
565
                        getDataSourceFactory().initialize();
566
                        QueryManager.registerQuery(new ArcJoin());
567
                }
568
        }
569
        /**
570
         * Inicializa el DriverManager.
571
         */
572
        private static void initializeWriterManager() {
573
                if (writerManager == null) {
574
                        writerManager = new WriterManager();
575
                        writerManager.loadWriters(new File(LayerFactory.writersPath));
576

    
577
                        Throwable[] failures = writerManager.getLoadFailures();
578

    
579
                        for (int i = 0; i < failures.length; i++) {
580
                                logger.error("", failures[i]);
581
                        }
582

    
583
                        getDataSourceFactory().setWriterManager(writerManager);
584
                        getDataSourceFactory().initialize();
585
                        //QueryManager.registerQuery(new ArcJoin());
586
                }
587
        }
588
        /**
589
         * sets drivers Directory
590
         *
591
         * @param path
592
         */
593
        public static void setDriversPath(String path) {
594
                LayerFactory.driversPath = path;
595
                initializeDriverManager();
596
        }
597
        /**
598
         * sets writers Directory
599
         *
600
         * @param path
601
         */
602
        public static void setWritersPath(String path) {
603
                LayerFactory.writersPath = path;
604
                initializeWriterManager();
605
        }
606

    
607
        /**
608
         * @return Returns the dataSourceFactory.
609
         */
610
        public static DataSourceFactory getDataSourceFactory() {
611
                if (dataSourceFactory == null){
612
                        dataSourceFactory = new DataSourceFactory();
613
                }
614
                return dataSourceFactory;
615
        }
616
}