Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / LayerFactory.java @ 1773

History | View | Annotate | Download (11.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 com.hardcode.driverManager.Driver;
44
import com.hardcode.driverManager.DriverLoadException;
45
import com.hardcode.driverManager.DriverManager;
46

    
47
import com.hardcode.gdbms.engine.data.DataSourceFactory;
48
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
49

    
50
import com.iver.cit.gvsig.fmap.DriverException;
51
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
52
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
53
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
54
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
55
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
56
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
57
import com.iver.cit.gvsig.fmap.drivers.wfs.WFSDriver;
58
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
59
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
60

    
61
import org.apache.log4j.Logger;
62

    
63
import org.cresques.cts.IProjection;
64

    
65
import java.awt.geom.Rectangle2D;
66

    
67
import java.io.File;
68

    
69
import java.net.URL;
70

    
71
import java.sql.ResultSet;
72
import java.sql.SQLException;
73
import java.sql.Statement;
74
import java.util.Enumeration;
75
import java.util.TreeMap;
76

    
77

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

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

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

    
121
                try {
122
                        Driver d = dm.getDriver(driverName);
123

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

    
133
                return null;
134
        }
135

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

    
156
                FLyrVect capa = new FLyrVect();
157
                capa.setName(layerName);
158

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

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

    
185
                return capa;
186
        }
187

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

    
214
                return layer;
215
        }
216

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

    
234
                FLyrRaster capa = new FLyrRaster();
235
                capa.setName(layerName);
236

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

    
249
                return capa;
250
        }
251

    
252
        /**
253
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
254
         * guard?ndose la URL del servidor que se pasa como par?metro
255
         *
256
         * @param driver Driver WFS.
257
         * @param host URL.
258
         * @param proj Proyecci?n.
259
         *
260
         * @return Capa creada.
261
         *
262
         * @throws UnsupportedOperationException
263
         */
264
        public static FLayer createLayer(WFSDriver driver, URL host,
265
                IProjection proj) {
266
                throw new UnsupportedOperationException();
267
        }
268

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

    
319
                return capa;
320

    
321

    
322
                    
323
                }
324

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

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

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

    
361
        /**
362
         * Devuelve el DriverManager.
363
         *
364
         * @return DriverManager.
365
         */
366
        public static DriverManager getDM() {
367
                if (driverManager == null) {
368
                        initializeDriverManager();
369
                }
370

    
371
                return driverManager;
372
        }
373

    
374
        /**
375
         * Inicializa el DriverManager.
376
         */
377
        private static void initializeDriverManager() {
378
                if (driverManager == null) {
379
                        driverManager = new DriverManager();
380
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
381

    
382
                        Throwable[] failures = driverManager.getLoadFailures();
383

    
384
                        for (int i = 0; i < failures.length; i++) {
385
                                logger.error(failures[i]);
386
                        }
387

    
388
                        dataSourceFactory.setDriverManager(driverManager);
389
                }
390
        }
391

    
392
        /**
393
         * sets drivers Directory
394
         *
395
         * @param path
396
         */
397
        public static void setDriversPath(String path) {
398
                LayerFactory.driversPath = path;
399
                initializeDriverManager();
400
        }
401
        
402
        /**
403
         * @return Returns the dataSourceFactory.
404
         */
405
        public static DataSourceFactory getDataSourceFactory() {
406
                return dataSourceFactory;
407
        }
408
}