Statistics
| Revision:

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

History | View | Annotate | Download (11.5 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 createLayer(WFSDriver driver, URL host,
269
                IProjection proj) {
270
                throw new UnsupportedOperationException();
271
        }
272

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

    
323
                return capa;
324

    
325

    
326
                    
327
                }
328

    
329
        
330
        /**
331
         * Obtiene el adaptador de un driver si ya se ha a?adido el origen de datos
332
         * o null si es un origen de datos nuevo
333
         *
334
         * @param source Adaptador.
335
         */
336
        private static void getAdapter(Source source) {
337
        }
338

    
339
        /**
340
         * Registra la asociaci?n entre la fuente de datos que se pasa como
341
         * par?metro y el VectorialAdapter que se pasa como par?metro, para
342
         * satisfacer futuras llamadas a getAdapter
343
         *
344
         * @param s Adaptador
345
         * @param a VectorialAdapter.
346
         */
347
        private static void saveSourceAdapter(Source s, VectorialAdapter a) {
348
        }
349

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

    
365
        /**
366
         * Devuelve el DriverManager.
367
         *
368
         * @return DriverManager.
369
         */
370
        public static DriverManager getDM() {
371
                if (driverManager == null) {
372
                        initializeDriverManager();
373
                }
374

    
375
                return driverManager;
376
        }
377

    
378
        /**
379
         * Inicializa el DriverManager.
380
         */
381
        private static void initializeDriverManager() {
382
                if (driverManager == null) {
383
                        driverManager = new DriverManager();
384
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
385

    
386
                        Throwable[] failures = driverManager.getLoadFailures();
387

    
388
                        for (int i = 0; i < failures.length; i++) {
389
                                logger.error(failures[i]);
390
                        }
391

    
392
                        getDataSourceFactory().setDriverManager(driverManager);
393
                        QueryManager.registerQuery(new ArcJoin());
394
                }
395
        }
396

    
397
        /**
398
         * sets drivers Directory
399
         *
400
         * @param path
401
         */
402
        public static void setDriversPath(String path) {
403
                LayerFactory.driversPath = path;
404
                initializeDriverManager();
405
        }
406
        
407
        /**
408
         * @return Returns the dataSourceFactory.
409
         */
410
        public static DataSourceFactory getDataSourceFactory() {
411
                if (dataSourceFactory == null){
412
                        dataSourceFactory = new DataSourceFactory();
413
                }
414
                return dataSourceFactory;
415
        }
416
}