root / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / LayerFactory.java @ 1920
History | View | Annotate | Download (13 KB)
1 | 1100 | fjp | /* 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 | 214 | fernando | package com.iver.cit.gvsig.fmap.layers; |
42 | |||
43 | 408 | fernando | import com.hardcode.driverManager.Driver; |
44 | import com.hardcode.driverManager.DriverLoadException; |
||
45 | import com.hardcode.driverManager.DriverManager; |
||
46 | 732 | fernando | |
47 | 1848 | fernando | import com.hardcode.gdbms.engine.customQuery.QueryManager; |
48 | 408 | fernando | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
49 | 470 | fjp | import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
50 | 732 | fernando | |
51 | 651 | fernando | import com.iver.cit.gvsig.fmap.DriverException; |
52 | 422 | fjp | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
53 | 303 | fernando | import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
54 | import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
||
55 | 408 | fernando | import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
56 | 438 | fernando | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
57 | 714 | fjp | import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend; |
58 | 303 | fernando | import com.iver.cit.gvsig.fmap.drivers.wfs.WFSDriver; |
59 | 1848 | fernando | import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
60 | import com.iver.cit.gvsig.fmap.operations.arcview.ArcJoin; |
||
61 | 458 | fjp | import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
62 | 714 | fjp | import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
63 | 303 | fernando | |
64 | 732 | fernando | import org.apache.log4j.Logger; |
65 | 303 | fernando | |
66 | 732 | fernando | import org.cresques.cts.IProjection; |
67 | |||
68 | 851 | fernando | import java.awt.geom.Rectangle2D; |
69 | 1034 | vcaballero | |
70 | 732 | fernando | import java.io.File; |
71 | |||
72 | import java.net.URL; |
||
73 | |||
74 | 1691 | fjp | import java.sql.ResultSet; |
75 | import java.sql.SQLException; |
||
76 | import java.sql.Statement; |
||
77 | import java.util.Enumeration; |
||
78 | 732 | fernando | import java.util.TreeMap; |
79 | |||
80 | |||
81 | 214 | fernando | /**
|
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 | 732 | fernando | private static Logger logger = Logger.getLogger(LayerFactory.class.getName()); |
88 | 439 | luisw | private static String driversPath = "../FMap 03/drivers"; |
89 | 732 | fernando | private static DriverManager driverManager; |
90 | 408 | fernando | |
91 | 1848 | fernando | private static DataSourceFactory dataSourceFactory; |
92 | |||
93 | 732 | fernando | /**
|
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 | 214 | fernando | |
103 | 1848 | fernando | /*
|
104 | 732 | fernando | * 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 | 1034 | vcaballero | * @param layerName Nombre de la capa.
|
109 | * @param driverName Nombre del driver.
|
||
110 | * @param f fichero.
|
||
111 | * @param proj Proyecci?n.
|
||
112 | 732 | fernando | *
|
113 | 1034 | vcaballero | * @return FLayer.
|
114 | 1060 | fjp | * @throws DriverException
|
115 | 732 | fernando | *
|
116 | * @throws DriverException
|
||
117 | 1034 | vcaballero | * @throws DriverIOException
|
118 | 1848 | fernando | *
|
119 | 732 | fernando | public static FLayer createLayer(String layerName, String driverName,
|
120 | 1060 | fjp | File f, IProjection proj) throws DriverException {
|
121 | 732 | fernando | //Se obtiene el driver que lee
|
122 | DriverManager dm = getDM();
|
||
123 | 438 | fernando | |
124 | 732 | fernando | try {
|
125 | Driver d = dm.getDriver(driverName);
|
||
126 | 438 | fernando | |
127 | 732 | fernando | 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 | 1060 | fjp | throw new DriverException(e);
|
134 | 732 | fernando | }
|
135 | 438 | fernando | |
136 | 732 | fernando | return null;
|
137 | }
|
||
138 | 1848 | fernando | */
|
139 | 1034 | vcaballero | /**
|
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 | 214 | fernando | |
159 | 1034 | vcaballero | FLyrVect capa = new FLyrVect();
|
160 | capa.setName(layerName); |
||
161 | 214 | fernando | |
162 | 1034 | vcaballero | //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 | 1848 | fernando | |
171 | 470 | fjp | // 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 | 1034 | vcaballero | 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 | 470 | fjp | } catch (FieldNotFoundException e) {
|
184 | 651 | fernando | //Esta no puede saltar
|
185 | 714 | fjp | } catch (DriverIOException e) {
|
186 | throw new DriverException(e); |
||
187 | 1034 | vcaballero | } |
188 | 408 | fernando | |
189 | 1034 | vcaballero | return capa;
|
190 | } |
||
191 | 567 | luisw | |
192 | 732 | fernando | /**
|
193 | 1034 | vcaballero | * 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 | 732 | fernando | *
|
197 | 1034 | vcaballero | * @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 | 732 | fernando | *
|
205 | 1034 | vcaballero | * @return Capa creada.
|
206 | 732 | fernando | */
|
207 | 1034 | vcaballero | 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 | 732 | fernando | public static FLyrRaster createLayer(String layerName, RasterDriver d, |
234 | 1046 | fernando | File f, IProjection proj) throws DriverException { |
235 | 732 | fernando | 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 | 1046 | fernando | try {
|
247 | capa.load(); |
||
248 | } catch (DriverIOException e) {
|
||
249 | throw new DriverException(e); |
||
250 | } |
||
251 | 732 | fernando | } |
252 | |||
253 | 567 | luisw | return capa;
|
254 | 732 | fernando | } |
255 | 567 | luisw | |
256 | 732 | fernando | /**
|
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 | 1034 | vcaballero | * @param driver Driver WFS.
|
261 | * @param host URL.
|
||
262 | * @param proj Proyecci?n.
|
||
263 | 732 | fernando | *
|
264 | 1034 | vcaballero | * @return Capa creada.
|
265 | 732 | fernando | *
|
266 | 1034 | vcaballero | * @throws UnsupportedOperationException
|
267 | 732 | fernando | */
|
268 | 1920 | vcaballero | public static FLayer createWFSLayer(String layerName,WFSDriver driver, URL host, |
269 | 732 | fernando | IProjection proj) { |
270 | 1920 | vcaballero | //throw new UnsupportedOperationException();
|
271 | FLyrVect layer=new FLyrVect();
|
||
272 | //layer.setName(name);
|
||
273 | VectorialAdapter adapter=new WFSAdapter();
|
||
274 | adapter.setDriver(driver); |
||
275 | //WithDefaultLegend aux=(WithDefaultLegend)driver;
|
||
276 | |||
277 | layer.setName(layerName); |
||
278 | layer.setSource(adapter); |
||
279 | layer.setProjection(proj); |
||
280 | try {
|
||
281 | layer.setLegend(LegendFactory.createSingleSymbolLegend( |
||
282 | layer.getShapeType())); |
||
283 | } catch (FieldNotFoundException e) {
|
||
284 | e.printStackTrace(); |
||
285 | } catch (DriverException e) {
|
||
286 | e.printStackTrace(); |
||
287 | } |
||
288 | //layer.setHost(host);
|
||
289 | return layer;
|
||
290 | 732 | fernando | } |
291 | 214 | fernando | |
292 | 732 | fernando | /**
|
293 | * Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
|
||
294 | * guard?ndose la URL del servidor que se pasa como par?metro
|
||
295 | *
|
||
296 | 1034 | vcaballero | * @param driver
|
297 | * @param host
|
||
298 | * @param port
|
||
299 | * @param user
|
||
300 | * @param password
|
||
301 | * @param dbName
|
||
302 | * @param tableName
|
||
303 | * @param proj
|
||
304 | 732 | fernando | *
|
305 | 1034 | vcaballero | * @return Capa creada.
|
306 | 732 | fernando | *
|
307 | 1034 | vcaballero | * @throws UnsupportedOperationException
|
308 | 732 | fernando | */
|
309 | public static FLayer createLayer(VectorialDatabaseDriver driver, |
||
310 | String host, int port, String user, String password, String dbName, |
||
311 | String tableName, IProjection proj) {
|
||
312 | throw new UnsupportedOperationException(); |
||
313 | } |
||
314 | 1691 | fjp | public static FLayer createDBLayer(VectorialDatabaseDriver driver,String layerName, IProjection proj) |
315 | { |
||
316 | |||
317 | |||
318 | FLyrVect capa = new FLyrVect();
|
||
319 | |||
320 | capa.setName(layerName); |
||
321 | VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
|
||
322 | dbAdapter.setDriver(driver); |
||
323 | |||
324 | capa.setSource(dbAdapter); |
||
325 | capa.setProjection(proj); |
||
326 | try {
|
||
327 | if (driver instanceof WithDefaultLegend) { |
||
328 | WithDefaultLegend aux = (WithDefaultLegend) driver; |
||
329 | dbAdapter.start(); |
||
330 | capa.setLegend((VectorialLegend) aux.getDefaultLegend()); |
||
331 | dbAdapter.stop(); |
||
332 | } else {
|
||
333 | capa.setLegend(LegendFactory.createSingleSymbolLegend( |
||
334 | capa.getShapeType())); |
||
335 | } |
||
336 | } catch (FieldNotFoundException e) {
|
||
337 | throw new UnsupportedOperationException(e.getMessage()); |
||
338 | } catch (DriverException e) {
|
||
339 | throw new UnsupportedOperationException(e.getMessage()); |
||
340 | } |
||
341 | 303 | fernando | |
342 | 1691 | fjp | return capa;
|
343 | |||
344 | |||
345 | |||
346 | } |
||
347 | |||
348 | 1848 | fernando | /**
|
349 | * @param driver
|
||
350 | * @param layerName
|
||
351 | * @param object
|
||
352 | * @return
|
||
353 | */
|
||
354 | public static FLayer createDisconnectedDBLayer(VectorialDatabaseDriver driver, String layerName, IProjection proj) { |
||
355 | |||
356 | |||
357 | FLyrVect capa = new FLyrVect();
|
||
358 | 1691 | fjp | |
359 | 1848 | fernando | capa.setName(layerName); |
360 | VectorialDisconnectedDBAdapter dbAdapter = new VectorialDisconnectedDBAdapter();
|
||
361 | dbAdapter.setDriver(driver); |
||
362 | |||
363 | capa.setSource(dbAdapter); |
||
364 | capa.setProjection(proj); |
||
365 | try {
|
||
366 | if (driver instanceof WithDefaultLegend) { |
||
367 | WithDefaultLegend aux = (WithDefaultLegend) driver; |
||
368 | dbAdapter.start(); |
||
369 | capa.setLegend((VectorialLegend) aux.getDefaultLegend()); |
||
370 | dbAdapter.stop(); |
||
371 | } else {
|
||
372 | capa.setLegend(LegendFactory.createSingleSymbolLegend( |
||
373 | capa.getShapeType())); |
||
374 | } |
||
375 | } catch (FieldNotFoundException e) {
|
||
376 | throw new UnsupportedOperationException(e.getMessage()); |
||
377 | } catch (DriverException e) {
|
||
378 | throw new UnsupportedOperationException(e.getMessage()); |
||
379 | } |
||
380 | |||
381 | return capa;
|
||
382 | |||
383 | |||
384 | |||
385 | } |
||
386 | |||
387 | 1920 | vcaballero | |
388 | 732 | fernando | /**
|
389 | * Obtiene el adaptador de un driver si ya se ha a?adido el origen de datos
|
||
390 | * o null si es un origen de datos nuevo
|
||
391 | *
|
||
392 | 1034 | vcaballero | * @param source Adaptador.
|
393 | 732 | fernando | */
|
394 | private static void getAdapter(Source source) { |
||
395 | } |
||
396 | 303 | fernando | |
397 | 732 | fernando | /**
|
398 | * Registra la asociaci?n entre la fuente de datos que se pasa como
|
||
399 | * par?metro y el VectorialAdapter que se pasa como par?metro, para
|
||
400 | * satisfacer futuras llamadas a getAdapter
|
||
401 | *
|
||
402 | 1034 | vcaballero | * @param s Adaptador
|
403 | * @param a VectorialAdapter.
|
||
404 | 732 | fernando | */
|
405 | private static void saveSourceAdapter(Source s, VectorialAdapter a) { |
||
406 | } |
||
407 | 303 | fernando | |
408 | 732 | fernando | /**
|
409 | * Crea una FLyrComplexRaster que ataca al driver que se pasa como
|
||
410 | * par?metro.
|
||
411 | *
|
||
412 | 1034 | vcaballero | * @param driver
|
413 | * @param f
|
||
414 | * @param proj
|
||
415 | 732 | fernando | *
|
416 | * @throws IllegalArgumentException Si se pasa un driver que no implementa
|
||
417 | * GeorreferencedRasterDriver o NotGeorreferencedRasterDriver
|
||
418 | */
|
||
419 | public static void createLayer(RasterDriver driver, File f, IProjection proj) |
||
420 | throws IllegalArgumentException { |
||
421 | } |
||
422 | 408 | fernando | |
423 | 732 | fernando | /**
|
424 | 1034 | vcaballero | * Devuelve el DriverManager.
|
425 | 732 | fernando | *
|
426 | 1034 | vcaballero | * @return DriverManager.
|
427 | 732 | fernando | */
|
428 | public static DriverManager getDM() { |
||
429 | 1848 | fernando | initializeDriverManager(); |
430 | 732 | fernando | |
431 | return driverManager;
|
||
432 | } |
||
433 | |||
434 | /**
|
||
435 | 1034 | vcaballero | * Inicializa el DriverManager.
|
436 | 732 | fernando | */
|
437 | private static void initializeDriverManager() { |
||
438 | if (driverManager == null) { |
||
439 | driverManager = new DriverManager(); |
||
440 | driverManager.loadDrivers(new File(LayerFactory.driversPath)); |
||
441 | |||
442 | Throwable[] failures = driverManager.getLoadFailures(); |
||
443 | |||
444 | for (int i = 0; i < failures.length; i++) { |
||
445 | 626 | fernando | logger.error(failures[i]); |
446 | } |
||
447 | |||
448 | 1848 | fernando | getDataSourceFactory().setDriverManager(driverManager); |
449 | QueryManager.registerQuery(new ArcJoin());
|
||
450 | 732 | fernando | } |
451 | } |
||
452 | 408 | fernando | |
453 | 732 | fernando | /**
|
454 | * sets drivers Directory
|
||
455 | *
|
||
456 | * @param path
|
||
457 | */
|
||
458 | public static void setDriversPath(String path) { |
||
459 | LayerFactory.driversPath = path; |
||
460 | initializeDriverManager(); |
||
461 | } |
||
462 | 1848 | fernando | |
463 | 1920 | vcaballero | |
464 | 1848 | fernando | /**
|
465 | * @return Returns the dataSourceFactory.
|
||
466 | */
|
||
467 | public static DataSourceFactory getDataSourceFactory() { |
||
468 | if (dataSourceFactory == null){ |
||
469 | dataSourceFactory = new DataSourceFactory();
|
||
470 | } |
||
471 | return dataSourceFactory;
|
||
472 | } |
||
473 | 214 | fernando | } |