Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / layer / LayerFactory.java @ 21606

History | View | Annotate | Download (9.21 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
/************************************************
42
 *                                                                                                *
43
 *   Modfied By:                                                                *
44
 *   Prodevelop Integraci?n de Tecnolog?as SL        *
45
 *   Conde Salvatierra de ?lava , 34-10                        *
46
 *   46004 Valencia                                                                *
47
 *   Spain                                                                                *
48
 *                                                                                                *
49
 *   +34 963 510 612                                                        *
50
 *   +34 963 510 968                                                        *
51
 *   gis@prodevelop.es                                                        *
52
 *   http://www.prodevelop.es                                        *
53
 *                                                                                                *
54
 *   gvSIG Mobile Team 2006                                         *
55
 *                                                                                          *         
56
 ************************************************/
57

    
58
package es.prodevelop.gvsig.mobile.fmap.layer;
59

    
60
import java.io.File;
61
import java.util.TreeMap;
62

    
63
import org.apache.log4j.Logger;
64

    
65
import com.hardcode.driverManager.Driver;
66
import com.hardcode.driverManager.DriverLoadException;
67
import com.hardcode.driverManager.DriverManager;
68
import com.hardcode.driverManager.WriterManager;
69
import com.hardcode.gdbms.engine.data.DataSourceFactory;
70

    
71
import es.prodevelop.gvsig.mobile.fmap.driver.FMapDriverException;
72
import es.prodevelop.gvsig.mobile.fmap.driver.VectorialDriver;
73
import es.prodevelop.gvsig.mobile.fmap.driver.VectorialFileDriver;
74
import es.prodevelop.gvsig.mobile.fmap.legend.LegendFactory;
75
import es.prodevelop.gvsig.mobile.fmap.proj.IProjection;
76

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

    
86
        private static String driversPath = "../FMap 03/drivers";
87

    
88
        private static String writersPath = "../FMap 03/drivers";
89

    
90
        private static DriverManager driverManager;
91

    
92
        private static WriterManager writerManager;
93

    
94
        private static DataSourceFactory dataSourceFactory;
95

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

    
106
        /*
107
         * Crea un RandomVectorialFile con el driver que se le pasa como par?metro y
108
         * guard?ndose el nombre del fichero para realizar los accesos, la capa
109
         * tendr? asociada la proyecci?n que se pasa como parametro tambi?n
110
         * 
111
         * @param layerName Nombre de la capa. @param driverName Nombre del driver.
112
         * @param f fichero. @param proj Proyecci?n.
113
         * 
114
         * @return FLayer. @throws FMapDriverException
115
         * 
116
         * @throws FMapDriverException @throws DriverIOException
117
         */
118
        public static FLayer createLayer(String layerName, String driverName,
119
                        File f, IProjection proj) throws FMapDriverException {
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
                        }
129
                } catch (DriverLoadException e) {
130
                        FMapDriverException exception = 
131
                                new FMapDriverException("While creating layer");
132
                        throw exception;
133
                }
134

    
135
                return null;
136
        }
137

    
138
        /**
139
         * It creates a FLayer (FLyrVect) which reads its data from a file driver,
140
         * projected in the specified projection.
141
         * 
142
         * @param layerName
143
         *            name of the layer
144
         * @param d
145
         *            vectorial file driver to read layer's data
146
         * @param f
147
         *            file associated to the driver
148
         * @param proj
149
         *            layer projection
150
         * 
151
         * @return FLayer new vectorial layer
152
         * 
153
         * @throws FMapDriverException
154
         */
155
        public static FLayer createLayer(String layerName, VectorialFileDriver d,
156
                        File f, IProjection proj) 
157
        
158
        /*throws FMapDriverException*/ {
159

    
160
                // TODO Comprobar si hay un adaptador ya
161
                VectorialFileAdapter adapter = new VectorialFileAdapter(f);
162
                adapter.setDriver((VectorialDriver) d);
163

    
164
                FLyrVect capa = new FLyrVect();
165
                capa.setName(layerName);
166

    
167
                // TODO Meter esto dentro de la comprobaci?n de si hay memoria
168
                if (false) {
169
                } else {
170
                        capa.setSource(adapter);
171
                        capa.setProjection(proj);
172
                }
173
                capa.setLegend(LegendFactory.createSingleSymbolLegend());
174

    
175
                return capa;
176
        }
177

    
178

    
179
        /**
180
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n.
181
         * 
182
         * @param layerName
183
         *            Nombre de la capa.
184
         * @param d
185
         *            RasterDriver.
186
         * @param f
187
         *            Fichero.
188
         * @param proj
189
         *            Proyecci?n.
190
         * 
191
         * @return Nueva capa de tipo raster.
192
         * 
193
         * @throws DriverIOException
194
         */
195
        
196

    
197
        /**
198
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n y
199
         * coordenadas de georeferenciaci?n. Esta funci?n es para georeferenciar
200
         * capas raster. Para imagenes que no tienen georeferenciaci?n hay que
201
         * asignarle una temporal, normalmente a partir de la vista activa.
202
         * 
203
         * @param layerName
204
         *            Nombre de la capa.
205
         * @param d
206
         *            RasterDriver.
207
         * @param f
208
         *            Fichero.
209
         * @param proj
210
         *            Proyecci?n.
211
         * @param extent
212
         *            Extent de la vista activa
213
         * 
214
         * @return Nueva capa de tipo raster.
215
         * 
216
         * @throws DriverIOException
217
         */
218
        
219

    
220
        
221
        /**
222
         * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
223
         * guard?ndose la URL del servidor que se pasa como par?metro
224
         * 
225
         * @param driver
226
         * @param host
227
         * @param port
228
         * @param user
229
         * @param password
230
         * @param dbName
231
         * @param tableName
232
         * @param proj
233
         * 
234
         * @return Capa creada.
235
         * 
236
         * @throws UnsupportedOperationException
237
         */
238
        
239

    
240
        
241
        /**
242
         * @param driver
243
         * @param layerName
244
         * @param object
245
         * @return
246
         * @throws SQLException
247
         * @throws DriverIOException
248
         * @throws IOException
249
         * @throws DriverLoadException
250
         * @throws com.hardcode.gdbms.engine.data.driver.DriverException
251
         * @throws NoSuchTableException
252
         * @throws ClassNotFoundException
253
         * @throws
254
         */
255
        
256
        /**
257
         * Crea una FLyrComplexRaster que ataca al driver que se pasa como
258
         * par?metro.
259
         * 
260
         * @param driver
261
         * @param f
262
         * @param proj
263
         * 
264
         * @throws IllegalArgumentException
265
         *             Si se pasa un driver que no implementa
266
         *             GeorreferencedRasterDriver o NotGeorreferencedRasterDriver
267
         */
268
        
269

    
270
        /**
271
         * Devuelve el DriverManager.
272
         * 
273
         * @return DriverManager.
274
         */
275
        public static DriverManager getDM() {
276
                initializeDriverManager();
277

    
278
                return driverManager;
279
        }
280

    
281
        /**
282
         * Devuelve el WriterManager.
283
         * 
284
         * @return WriterManager.
285
         */
286
        public static WriterManager getWM() {
287
                initializeWriterManager();
288

    
289
                return writerManager;
290
        }
291

    
292
        /**
293
         * Inicializa el DriverManager.
294
         */
295
        private static void initializeDriverManager() {
296
                if (driverManager == null) {
297
                        driverManager = new DriverManager();
298
                        driverManager.loadDrivers(new File(LayerFactory.driversPath));
299

    
300
                        Throwable[] failures = driverManager.getLoadFailures();
301

    
302
                        for (int i = 0; i < failures.length; i++) {
303
                                logger.error("", failures[i]);
304
                        }
305

    
306
                        getDataSourceFactory().setDriverManager(driverManager);
307
                        getDataSourceFactory().initialize();
308
                }
309
        }
310

    
311
        /**
312
         * Inicializa el DriverManager.
313
         */
314
        private static void initializeWriterManager() {
315
                if (writerManager == null) {
316
                        writerManager = new WriterManager();
317
                        writerManager.loadWriters(new File(LayerFactory.writersPath));
318

    
319
                        Throwable[] failures = writerManager.getLoadFailures();
320

    
321
                        for (int i = 0; i < failures.length; i++) {
322
                                logger.error("", failures[i]);
323
                        }
324

    
325
                        getDataSourceFactory().setWriterManager(writerManager);
326
                        getDataSourceFactory().initialize();
327
                        // QueryManager.registerQuery(new ArcJoin());
328
                }
329
        }
330

    
331
        /**
332
         * sets drivers Directory
333
         * 
334
         * @param path
335
         */
336
        public static void setDriversPath(String path) {
337
                LayerFactory.driversPath = path;
338
                initializeDriverManager();
339
        }
340

    
341
        /**
342
         * sets writers Directory
343
         * 
344
         * @param path
345
         */
346
        public static void setWritersPath(String path) {
347
                LayerFactory.writersPath = path;
348
                initializeWriterManager();
349
        }
350

    
351
        /**
352
         * @return Returns the dataSourceFactory.
353
         */
354
        public static DataSourceFactory getDataSourceFactory() {
355
                if (dataSourceFactory == null) {
356
                        dataSourceFactory = new DataSourceFactory();
357
                }
358
                return dataSourceFactory;
359
        }
360
}