svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayers.java @ 3481
History | View | Annotate | Download (30.9 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.awt.Graphics2D; |
44 |
import java.awt.geom.Rectangle2D; |
45 |
import java.awt.image.BufferedImage; |
46 |
import java.io.File; |
47 |
import java.net.ConnectException; |
48 |
import java.util.ArrayList; |
49 |
import java.util.Collections; |
50 |
import java.util.ConcurrentModificationException; |
51 |
import java.util.Iterator; |
52 |
import java.util.List; |
53 |
|
54 |
import org.apache.log4j.Logger; |
55 |
import org.cresques.cts.ICoordTrans; |
56 |
import org.cresques.cts.IProjection; |
57 |
import org.cresques.cts.ProjectionPool; |
58 |
import org.cresques.cts.gt2.CoordSys; |
59 |
import org.cresques.cts.gt2.CoordTrans; |
60 |
|
61 |
import com.hardcode.driverManager.Driver; |
62 |
import com.hardcode.driverManager.DriverLoadException; |
63 |
import com.iver.cit.gvsig.fmap.DriverException; |
64 |
import com.iver.cit.gvsig.fmap.FMap; |
65 |
import com.iver.cit.gvsig.fmap.ViewPort; |
66 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver; |
67 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
68 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
69 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
70 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
71 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection; |
72 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData; |
73 |
import com.iver.cit.gvsig.fmap.operations.Cancellable; |
74 |
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
75 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
76 |
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
77 |
import com.iver.utiles.IPersistance; |
78 |
import com.iver.utiles.XMLEntity; |
79 |
|
80 |
/**
|
81 |
* Representa un nodo interno en el ?rbol de capas
|
82 |
*/
|
83 |
public class FLayers extends FLyrDefault implements VectorialData, |
84 |
LayerCollection { |
85 |
//ArrayList de Listener registrados.
|
86 |
private ArrayList layerListeners = new ArrayList(); |
87 |
private List layers = Collections.synchronizedList(new ArrayList()); |
88 |
private FMap fmap;
|
89 |
private static Logger logger = Logger.getLogger(FLayers.class.getName()); |
90 |
/**
|
91 |
* Crea un nuevo FLayers.
|
92 |
*
|
93 |
* @param fmap
|
94 |
* @param parent
|
95 |
*/
|
96 |
public FLayers(FMap fmap, FLayers parent) {
|
97 |
setParentLayer(parent); |
98 |
this.fmap = fmap;
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* A?ade un listener de los eventos de la colecci?n de capas
|
103 |
*
|
104 |
* @param listener
|
105 |
*/
|
106 |
public void addLayerCollectionListener(LayerCollectionListener listener) { |
107 |
layerListeners.add(listener); |
108 |
} |
109 |
|
110 |
/**
|
111 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
|
112 |
*/
|
113 |
public void setAllVisibles(boolean visible) { |
114 |
FLayer lyr; |
115 |
|
116 |
for (int i = 0; i < layers.size(); i++) { |
117 |
lyr = ((FLayer) layers.get(i)); |
118 |
lyr.setVisible(visible); |
119 |
|
120 |
if (lyr instanceof LayerCollection) { |
121 |
((LayerCollection) lyr).setAllVisibles(visible); |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
/**
|
127 |
* Elimina un listener de los eventos de la colecci?n de capas
|
128 |
*
|
129 |
* @param listener
|
130 |
*/
|
131 |
public void removeLayerCollectionListener(LayerCollectionListener listener) { |
132 |
layerListeners.remove(listener); |
133 |
} |
134 |
|
135 |
/**
|
136 |
* A?ade una capa.
|
137 |
*
|
138 |
* @param layer
|
139 |
*/
|
140 |
private void doAddLayer(FLayer layer) { |
141 |
layers.add(layer); |
142 |
layer.setParentLayer(this);
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* A?ade una capa al final de la lista de subcapas
|
147 |
*
|
148 |
* @param layer
|
149 |
*
|
150 |
* @throws CancelationException
|
151 |
*/
|
152 |
public void addLayer(FLayer layer) throws CancelationException { |
153 |
if (layer instanceof FLayers){ |
154 |
FLayers layers=(FLayers)layer; |
155 |
fmap.addAsCollectionListener(layers); |
156 |
} |
157 |
callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer)); |
158 |
|
159 |
doAddLayer(layer); |
160 |
|
161 |
callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer)); |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Mueve una capa de la posici?n from a la posici?n to. (Se supone que
|
166 |
* est?n dentro del mismo nivel). Para mover entre niveles, usar otro
|
167 |
* m?todo (por hacer)
|
168 |
*
|
169 |
* @param from origen.
|
170 |
* @param to destino.
|
171 |
*
|
172 |
* @throws CancelationException
|
173 |
*/
|
174 |
public void moveTo(int from, int to) throws CancelationException { |
175 |
int newfrom=layers.size()-from-1; |
176 |
int newto=layers.size()-to-1; |
177 |
FLayer aux = (FLayer) layers.get(newfrom); |
178 |
callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto)); |
179 |
layers.remove(newfrom); |
180 |
layers.add(newto, aux); |
181 |
callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto)); |
182 |
} |
183 |
|
184 |
/**
|
185 |
* Borra una capa del ArrayList que contiene a las capas.
|
186 |
*
|
187 |
* @param lyr FLayer.
|
188 |
*/
|
189 |
private void doRemoveLayer(FLayer lyr) { |
190 |
layers.remove(lyr); |
191 |
} |
192 |
|
193 |
/**
|
194 |
* Borra una capa de la colecci?n de capas.
|
195 |
*
|
196 |
* @param lyr FLayer
|
197 |
*
|
198 |
* @throws CancelationException
|
199 |
*/
|
200 |
public void removeLayer(FLayer lyr) throws CancelationException { |
201 |
callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr)); |
202 |
doRemoveLayer(lyr); |
203 |
callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr)); |
204 |
} |
205 |
|
206 |
/**
|
207 |
* Borra una FLayer del vector a partir del identificador.
|
208 |
*
|
209 |
* @param idLayer
|
210 |
*/
|
211 |
public void removeLayer(int idLayer) { |
212 |
FLayer lyr = (FLayer) layers.get(idLayer); |
213 |
callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr)); |
214 |
layers.remove(idLayer); |
215 |
callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr)); |
216 |
} |
217 |
|
218 |
/**
|
219 |
* M?todo de conveniencia para borrar una capa con ese nombre
|
220 |
*
|
221 |
* @param layerName
|
222 |
*/
|
223 |
public void removeLayer(String layerName) { |
224 |
FLayer lyr; |
225 |
|
226 |
for (int i = 0; i < layers.size(); i++) { |
227 |
lyr = ((FLayer) layers.get(i)); |
228 |
|
229 |
if (lyr.getName().compareToIgnoreCase(layerName) == 0) { |
230 |
removeLayer(i); |
231 |
|
232 |
break;
|
233 |
} |
234 |
} |
235 |
} |
236 |
|
237 |
/**
|
238 |
* Obtiene el array de capas visibles que penden del arbol cuya ra?z es
|
239 |
* este nodo
|
240 |
*
|
241 |
* @return Vector de FLayer.
|
242 |
*/
|
243 |
public FLayer[] getVisibles() { |
244 |
ArrayList array = new ArrayList(); |
245 |
|
246 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
247 |
if (((FLayer) iter.next()).isVisible()) {
|
248 |
array.add((FLayer) iter.next()); |
249 |
} |
250 |
} |
251 |
|
252 |
return (FLayer[]) array.toArray(new FLayer[0]); |
253 |
} |
254 |
|
255 |
/**
|
256 |
* Obtiene el hijo i-?simo directo de esta colecci?n
|
257 |
*
|
258 |
* @param index ?ndice.
|
259 |
*
|
260 |
* @return capa seleccionada.
|
261 |
*/
|
262 |
public FLayer getLayer(int index) { |
263 |
return (FLayer) layers.get(index);
|
264 |
} |
265 |
|
266 |
/**
|
267 |
* M?todo de conveniencia para buscar una capa por su nombre Es conveniente
|
268 |
* usar getLayer(int) siempre que se pueda, es m?s directo.
|
269 |
*
|
270 |
* @param layerName nombre.
|
271 |
*
|
272 |
* @return capa seleccionada.
|
273 |
*/
|
274 |
public FLayer getLayer(String layerName) { |
275 |
FLayer lyr; |
276 |
FLayer lyr2; |
277 |
ArrayList layerList;
|
278 |
|
279 |
for (int i = 0; i < layers.size(); i++) { |
280 |
lyr = ((FLayer) layers.get(i)); |
281 |
|
282 |
layerList = new ArrayList(); |
283 |
splitLayerGroup(lyr,layerList); |
284 |
for(int j = 0; j<layerList.size(); j++ ) |
285 |
{ |
286 |
lyr2 = ((FLayer)layerList.get(j)); |
287 |
if (lyr2.getName().compareToIgnoreCase(layerName) == 0) { |
288 |
return lyr2;
|
289 |
} |
290 |
} |
291 |
} |
292 |
|
293 |
return null; |
294 |
} |
295 |
|
296 |
// added by Laura
|
297 |
// this is to to split up a layer group in order to get a layer by name
|
298 |
// when there are layergroups.
|
299 |
private void splitLayerGroup(FLayer layer, ArrayList result) |
300 |
{ |
301 |
int i;
|
302 |
FLayers layerGroup; |
303 |
if (layer instanceof FLayers) |
304 |
{ |
305 |
layerGroup = (FLayers)layer; |
306 |
for (i=0; i < layerGroup.getLayersCount(); i++ ) |
307 |
{ |
308 |
splitLayerGroup(layerGroup.getLayer(i),result); |
309 |
} |
310 |
} |
311 |
else
|
312 |
{ |
313 |
result.add(layer); |
314 |
} |
315 |
} |
316 |
|
317 |
/**
|
318 |
* Obtiene el n?mero de capas que hay en su mismo nivel Es decir, no cuenta
|
319 |
* las subcapas de un FLayers
|
320 |
*
|
321 |
* @return n?mero de capas.
|
322 |
*/
|
323 |
public int getLayersCount() { |
324 |
return layers.size();
|
325 |
} |
326 |
|
327 |
/**
|
328 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
|
329 |
* java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
|
330 |
*/
|
331 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
332 |
Cancellable cancel,double scale) throws DriverException { |
333 |
//double scale = getFMap().getScaleView();
|
334 |
Iterator iter = layers.iterator();
|
335 |
try
|
336 |
{ |
337 |
while (iter.hasNext())
|
338 |
{ |
339 |
// for (int i=0; i < layers.size(); i++) {
|
340 |
if (cancel.isCanceled())
|
341 |
break; // M?s que nada porque las capas raster no son interrumpibles por ahora. |
342 |
FLayer lyr = (FLayer) iter.next(); |
343 |
// FLayer lyr = (FLayer) layers.get(i);
|
344 |
/* if (lyr instanceof FLyrVect && ((FLyrVect)lyr).isBroken()){
|
345 |
continue;
|
346 |
}
|
347 |
*/
|
348 |
if (lyr.isVisible()) {
|
349 |
|
350 |
long t1 = System.currentTimeMillis(); |
351 |
// synchronized (this) {
|
352 |
//Sincronizaci?n del m?todo dibujar de cada Layer, esto es posible hacerlo de otra forma,
|
353 |
//pero de momento se queda as?, para solucionar el refresco de una misma vista que est? a?adida en el Layout.
|
354 |
lyr.draw(image, g, viewPort, cancel,scale); |
355 |
// }
|
356 |
long t2 = System.currentTimeMillis(); |
357 |
System.out.println("Layer " + lyr.getName() + " " |
358 |
+ (t2-t1) + " milisecs.");
|
359 |
} |
360 |
} |
361 |
if (getVirtualLayers() != null) { |
362 |
getVirtualLayers().draw(image, g, viewPort, cancel,scale); |
363 |
} |
364 |
} |
365 |
catch (ConcurrentModificationException e) |
366 |
{ |
367 |
System.err.println(e.getMessage());
|
368 |
} |
369 |
} |
370 |
|
371 |
/**
|
372 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
|
373 |
* com.iver.cit.gvsig.fmap.ViewPort,
|
374 |
* com.iver.cit.gvsig.fmap.operations.Cancellable)
|
375 |
*/
|
376 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale) |
377 |
throws DriverException {
|
378 |
|
379 |
// TODO: A la hora de imprimir, isWithinScale falla, porque est?
|
380 |
// calculando la escala en pantalla, no para el layout.
|
381 |
// Revisar esto.
|
382 |
|
383 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
384 |
FLayer lyr = (FLayer) iter.next(); |
385 |
lyr.print(g, viewPort, cancel,scale); |
386 |
} |
387 |
if (getVirtualLayers() != null) { |
388 |
getVirtualLayers().print( g, viewPort, cancel,scale); |
389 |
} |
390 |
} |
391 |
|
392 |
/**
|
393 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
|
394 |
*/
|
395 |
public Rectangle2D getFullExtent() throws DriverException { |
396 |
Rectangle2D rAux = null; |
397 |
boolean first = true; |
398 |
|
399 |
|
400 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
401 |
FLayer capa = (FLayer) iter.next(); |
402 |
try{
|
403 |
if (first) {
|
404 |
rAux = capa.getFullExtent(); |
405 |
first=false;
|
406 |
} else {
|
407 |
rAux.add(capa.getFullExtent()); |
408 |
} |
409 |
}catch (Exception e) { |
410 |
e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
|
411 |
} |
412 |
} |
413 |
|
414 |
return rAux;
|
415 |
} |
416 |
|
417 |
/**
|
418 |
* Llama al m?todo layerAdding de los listeners dados de alta.
|
419 |
*
|
420 |
* @param event
|
421 |
*
|
422 |
* @throws CancelationException
|
423 |
*/
|
424 |
private void callLayerAdding(LayerCollectionEvent event) |
425 |
throws CancelationException {
|
426 |
ArrayList aux = (ArrayList) layerListeners.clone(); |
427 |
for (Iterator iter = aux.iterator(); iter.hasNext();) { |
428 |
((LayerCollectionListener) iter.next()).layerAdding(event); |
429 |
} |
430 |
} |
431 |
|
432 |
/**
|
433 |
* Llama al m?todo layerRemoving de los listeners dados de alta.
|
434 |
*
|
435 |
* @param event
|
436 |
*
|
437 |
* @throws CancelationException
|
438 |
*/
|
439 |
private void callLayerRemoving(LayerCollectionEvent event) |
440 |
throws CancelationException {
|
441 |
ArrayList aux = (ArrayList) layerListeners.clone(); |
442 |
for (Iterator iter = aux.iterator(); iter.hasNext();) { |
443 |
((LayerCollectionListener) iter.next()).layerRemoving(event); |
444 |
} |
445 |
} |
446 |
|
447 |
/**
|
448 |
* Llama al m?todo layerMoving de los listeners dados de alta.
|
449 |
*
|
450 |
* @param event
|
451 |
*
|
452 |
* @throws CancelationException
|
453 |
*/
|
454 |
private void callLayerMoving(LayerPositionEvent event) |
455 |
throws CancelationException {
|
456 |
ArrayList aux = (ArrayList) layerListeners.clone(); |
457 |
for (Iterator iter = aux.iterator(); iter.hasNext();) { |
458 |
((LayerCollectionListener) iter.next()).layerMoving(event); |
459 |
} |
460 |
} |
461 |
|
462 |
/**
|
463 |
* Llama al m?todo layerAdded de los listeners dados de alta.
|
464 |
*
|
465 |
* @param event
|
466 |
*/
|
467 |
private void callLayerAdded(LayerCollectionEvent event) { |
468 |
ArrayList aux = (ArrayList) layerListeners.clone(); |
469 |
for (Iterator iter = aux.iterator(); iter.hasNext();) { |
470 |
((LayerCollectionListener) iter.next()).layerAdded(event); |
471 |
} |
472 |
} |
473 |
|
474 |
/**
|
475 |
* Llama al m?todo layerRemoved de los listeners dados de alta.
|
476 |
*
|
477 |
* @param event
|
478 |
*/
|
479 |
private void callLayerRemoved(LayerCollectionEvent event) { |
480 |
ArrayList aux = (ArrayList) layerListeners.clone(); |
481 |
for (Iterator iter = aux.iterator(); iter.hasNext();) { |
482 |
((LayerCollectionListener) iter.next()).layerRemoved(event); |
483 |
} |
484 |
} |
485 |
|
486 |
/**
|
487 |
* Llama al m?todo layerMoved de los listeners dados de alta.
|
488 |
*
|
489 |
* @param event
|
490 |
*/
|
491 |
private void callLayerMoved(LayerPositionEvent event) { |
492 |
ArrayList aux = (ArrayList) layerListeners.clone(); |
493 |
for (Iterator iter = aux.iterator(); iter.hasNext();) { |
494 |
((LayerCollectionListener) iter.next()).layerMoved(event); |
495 |
} |
496 |
} |
497 |
|
498 |
/**
|
499 |
* @throws XMLException
|
500 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
|
501 |
*/
|
502 |
public XMLEntity getXMLEntity() throws XMLException { |
503 |
XMLEntity xml = super.getXMLEntity();
|
504 |
xml.putProperty("numLayers", layers.size());
|
505 |
|
506 |
String[] s = new String[layers.size()]; |
507 |
|
508 |
for (int i = 0; i < layers.size(); i++) { |
509 |
s[i] = ((FLayer) layers.get(i)).getName(); |
510 |
} |
511 |
|
512 |
xml.putProperty("LayerNames", s);
|
513 |
|
514 |
for (int i = 0; i < layers.size(); i++) { |
515 |
xml.addChild(((FLayer) layers.get(i)).getXMLEntity()); |
516 |
} |
517 |
|
518 |
return xml;
|
519 |
} |
520 |
|
521 |
/**
|
522 |
* DOCUMENT ME!
|
523 |
*
|
524 |
* @param xml DOCUMENT ME!
|
525 |
*
|
526 |
* @throws XMLException
|
527 |
*
|
528 |
* @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
|
529 |
*/
|
530 |
public void setXMLEntity03(XMLEntity xml) throws XMLException{ |
531 |
super.setXMLEntity03(xml);
|
532 |
int numLayers = xml.getIntProperty("numLayers"); |
533 |
|
534 |
String[] s = xml.getStringArrayProperty("LayerNames"); |
535 |
try {
|
536 |
for (int i = 0; i < numLayers; i++) { |
537 |
FLayer layer = null;
|
538 |
|
539 |
String className = xml.getChild(i).getStringProperty("className"); |
540 |
|
541 |
if (className.equals(FLyrVect.class.getName())) {
|
542 |
if (xml.getChild(i).contains("file")) { |
543 |
|
544 |
layer = LayerFactory.createLayer(s[i], |
545 |
(VectorialFileDriver)LayerFactory.getDM().getDriver(xml.getChild(i).getStringProperty("driverName")),
|
546 |
new File(xml.getChild(i).getStringProperty("file")), |
547 |
this.getFMap().getViewPort().getProjection());
|
548 |
|
549 |
} else if (true) { |
550 |
//TODO falta por implementar
|
551 |
} else if (true) { |
552 |
//TODO falta por implementar
|
553 |
} |
554 |
|
555 |
layer.setXMLEntity03(xml.getChild(i)); |
556 |
// Comprobar que la proyecci?n es la misma que la de FMap
|
557 |
// Si no lo es, es una capa que est? reproyectada al vuelo
|
558 |
IProjection proj = layer.getProjection(); |
559 |
if (proj != null) |
560 |
if (proj != fmap.getProjection())
|
561 |
{ |
562 |
ICoordTrans ct = new CoordTrans((CoordSys) proj,
|
563 |
(CoordSys) fmap.getProjection()); |
564 |
layer.setCoordTrans(ct); |
565 |
System.err.println("coordTrans = " + |
566 |
proj.getAbrev() + " " +
|
567 |
fmap.getProjection().getAbrev()); |
568 |
} |
569 |
|
570 |
// } else if (className.equals(FLyrWMS.class.getName())) {
|
571 |
// try {
|
572 |
// layer = LayerFactory.createLayer(s[i],
|
573 |
// StringUtilities.string2Rect(
|
574 |
// xml.getChild(i).getStringProperty("fullExtent")),
|
575 |
// new URL(xml.getChild(i).getStringProperty("host")),
|
576 |
// xml.getChild(i).getStringProperty("format"),
|
577 |
// xml.getChild(i).getStringProperty("layerQuery"),
|
578 |
// xml.getChild(i).getStringProperty("infoLayerQuery"),
|
579 |
// xml.getChild(i).getStringProperty("srs"));
|
580 |
// } catch (MalformedURLException e) {
|
581 |
// throw new XMLException(e);
|
582 |
// }
|
583 |
} else if (className.equals((FLyrRaster.class.getName()))) { |
584 |
//if (xml.getChild(i).contains("file")) {
|
585 |
/*layer = LayerFactory.createLayer(s[i],
|
586 |
xml.getChild(i).getStringProperty("driverName"),
|
587 |
new File(xml.getChild(i).getStringProperty("file")),
|
588 |
this.getFMap().getViewPort().getProjection());*/
|
589 |
layer = new FLyrRaster();
|
590 |
layer.setName(s[i]); |
591 |
if (!xml.getChild(i).contains("proj")) |
592 |
((FLyrRaster)layer).setProjection(this.getFMap().getViewPort().getProjection());
|
593 |
layer.setXMLEntity03(xml.getChild(i)); |
594 |
try {
|
595 |
layer.load(); |
596 |
|
597 |
} catch (DriverIOException e) {
|
598 |
throw new XMLException(e); |
599 |
} |
600 |
|
601 |
}else if (className.equals((FLayers.class.getName()))) { |
602 |
layer = new FLayers(getFMap(),this); |
603 |
layer.setXMLEntity(xml.getChild(i)); |
604 |
} else {
|
605 |
// Capas Nuevas (externas)
|
606 |
try {
|
607 |
Class clase = Class.forName(className); |
608 |
layer = (FLayer) clase.newInstance(); |
609 |
layer.setName(s[i]); |
610 |
layer.setXMLEntity03(xml.getChild(i)); |
611 |
try {
|
612 |
layer.load(); |
613 |
} catch (DriverIOException e) {
|
614 |
throw new XMLException(e); |
615 |
} |
616 |
} catch (Exception e) { |
617 |
//e.printStackTrace();
|
618 |
throw new XMLException(e); |
619 |
} |
620 |
} |
621 |
|
622 |
this.addLayer(layer);
|
623 |
} |
624 |
} catch (DriverException e) {
|
625 |
throw new XMLException(e); |
626 |
} catch (DriverLoadException e) {
|
627 |
throw new XMLException(e); |
628 |
} |
629 |
|
630 |
} |
631 |
|
632 |
/**
|
633 |
* DOCUMENT ME!
|
634 |
*
|
635 |
* @param xml DOCUMENT ME!
|
636 |
*
|
637 |
* @throws XMLException
|
638 |
*
|
639 |
* @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
|
640 |
*/
|
641 |
public void setXMLEntity(XMLEntity xml) throws XMLException{ |
642 |
super.setXMLEntity(xml);
|
643 |
//LoadLayerException loadLayerException=new LoadLayerException();
|
644 |
int numLayers = xml.getIntProperty("numLayers"); |
645 |
|
646 |
String[] s = xml.getStringArrayProperty("LayerNames"); |
647 |
// try {
|
648 |
for (int i = 0; i < numLayers; i++) { |
649 |
FLayer layer = null;
|
650 |
try {
|
651 |
|
652 |
|
653 |
String className = xml.getChild(i).getStringProperty("className"); |
654 |
|
655 |
if (className.equals(FLyrVect.class.getName())) {
|
656 |
String type = xml.getChild(i).getStringProperty("type"); |
657 |
if ("vectorial".equals(type)){ |
658 |
//String recordsetName = xml.getChild(i).getStringProperty("recordset-name");
|
659 |
IProjection proj = null;
|
660 |
if (xml.getChild(i).contains("proj")) { |
661 |
proj = ProjectionPool.get(xml.getChild(i).getStringProperty("proj"));
|
662 |
} |
663 |
else
|
664 |
{ |
665 |
proj = this.getFMap().getViewPort().getProjection();
|
666 |
} |
667 |
if (xml.getChild(i).contains("file")) { |
668 |
Driver d;
|
669 |
try {
|
670 |
d = LayerFactory.getDM().getDriver(xml.getChild(i).getStringProperty("driverName"));
|
671 |
} catch (DriverLoadException e1) {
|
672 |
throw new XMLException(e1); |
673 |
} |
674 |
layer = LayerFactory.createLayer(s[i], (VectorialFileDriver) d, |
675 |
new File(xml.getChild(i).getStringProperty("file")), |
676 |
proj); |
677 |
|
678 |
|
679 |
} |
680 |
if (xml.getChild(i).contains("db")) { |
681 |
|
682 |
String driverName = xml.getChild(i).getStringProperty("db"); |
683 |
VectorialDatabaseDriver driver; |
684 |
try {
|
685 |
driver = (VectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName); |
686 |
//Hay que separar la carga de los datos del XMLEntity del load.
|
687 |
driver.setXMLEntity(xml.getChild(i).getChild(2));
|
688 |
|
689 |
layer = LayerFactory.createDBLayer(driver, s[i], proj); |
690 |
((DefaultDBDriver)driver).load(); |
691 |
} catch (DriverLoadException e) {
|
692 |
e.printStackTrace(); |
693 |
throw new XMLException(e); |
694 |
} |
695 |
|
696 |
} |
697 |
// Clases con algun driver gen?rico creado por otro
|
698 |
// programador
|
699 |
if (xml.getChild(i).contains("other")) { |
700 |
|
701 |
String driverName = xml.getChild(i).getStringProperty("other"); |
702 |
VectorialDriver driver = null;
|
703 |
try {
|
704 |
driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName); |
705 |
} catch (DriverLoadException e) {
|
706 |
// Si no existe ese driver, no pasa nada.
|
707 |
// Puede que el desarrollador no quiera que
|
708 |
// aparezca en el cuadro de di?logo y ha metido
|
709 |
// el jar con sus clases en nuestro directorio lib.
|
710 |
// Intentamos cargar esa clase "a pelo".
|
711 |
if (xml.getChild(i).contains("className")) |
712 |
{ |
713 |
String className2 = xml.getChild(i).getStringProperty("className"); |
714 |
try {
|
715 |
driver = (VectorialDriver) Class.forName(className2).newInstance();
|
716 |
} catch (Exception e1) { |
717 |
throw new XMLException(e1); |
718 |
} |
719 |
} |
720 |
} catch (NullPointerException npe) { |
721 |
// Si no existe ese driver, no pasa nada.
|
722 |
// Puede que el desarrollador no quiera que
|
723 |
// aparezca en el cuadro de di?logo y ha metido
|
724 |
// el jar con sus clases en nuestro directorio lib.
|
725 |
// Intentamos cargar esa clase "a pelo".
|
726 |
if (xml.getChild(i).getChild(2).contains("className")) |
727 |
{ |
728 |
String className2 = xml.getChild(i).getChild(2).getStringProperty("className"); |
729 |
try {
|
730 |
driver = (VectorialDriver) Class.forName(className2).newInstance();
|
731 |
} catch (Exception e1) { |
732 |
throw new XMLException(e1); |
733 |
} |
734 |
} |
735 |
} |
736 |
if (driver instanceof IPersistance) |
737 |
{ |
738 |
IPersistance persist = (IPersistance) driver; |
739 |
persist.setXMLEntity(xml.getChild(i).getChild(2));
|
740 |
} |
741 |
layer = LayerFactory.createLayer(s[i], driver, proj); |
742 |
} |
743 |
|
744 |
|
745 |
} else if ("raster".equals(type)) { |
746 |
/* if (xml.getChild(i).contains("file")) {
|
747 |
Driver d = LayerFactory.getDM().getDriver(xml.getChild(i).getStringProperty("driverName"));
|
748 |
layer = LayerFactory.createLayer(s[i],
|
749 |
,
|
750 |
new File(xml.getChild(i).getStringProperty("file")),
|
751 |
this.getFMap().getViewPort().getProjection());
|
752 |
|
753 |
}
|
754 |
*/ }
|
755 |
|
756 |
layer.setXMLEntity(xml.getChild(i)); |
757 |
|
758 |
// } else if (className.equals(FLyrWMS.class.getName())) {
|
759 |
// try {
|
760 |
// layer = LayerFactory.createLayer(s[i],
|
761 |
// StringUtilities.string2Rect(
|
762 |
// xml.getChild(i).getStringProperty("fullExtent")),
|
763 |
// new URL(xml.getChild(i).getStringProperty("host")),
|
764 |
// xml.getChild(i).getStringProperty("format"),
|
765 |
// xml.getChild(i).getStringProperty("layerQuery"),
|
766 |
// xml.getChild(i).getStringProperty("infoLayerQuery"),
|
767 |
// xml.getChild(i).getStringProperty("srs"));
|
768 |
// layer.setXMLEntity(xml.getChild(i));
|
769 |
// } catch (MalformedURLException e) {
|
770 |
// throw new XMLException(e);
|
771 |
// }
|
772 |
} else if (className.equals((FLyrRaster.class.getName()))) { |
773 |
//if (xml.getChild(i).contains("file")) {
|
774 |
/*layer = LayerFactory.createLayer(s[i],
|
775 |
xml.getChild(i).getStringProperty("driverName"),
|
776 |
new File(xml.getChild(i).getStringProperty("file")),
|
777 |
this.getFMap().getViewPort().getProjection());*/
|
778 |
layer = new FLyrRaster();
|
779 |
layer.setName(s[i]); |
780 |
if (!xml.getChild(i).contains("proj")) |
781 |
((FLyrRaster)layer).setProjection(this.getFMap().getViewPort().getProjection());
|
782 |
layer.setXMLEntity(xml.getChild(i)); |
783 |
try {
|
784 |
layer.load(); |
785 |
} catch (DriverIOException e) {
|
786 |
throw new XMLException(e); |
787 |
} |
788 |
|
789 |
}else if (className.equals((FLayers.class.getName()))) { |
790 |
layer = new FLayers(getFMap(),this); |
791 |
layer.setXMLEntity(xml.getChild(i)); |
792 |
} else {
|
793 |
// Capas Nuevas (externas)
|
794 |
Class clase = Class.forName(className); |
795 |
layer = (FLayer) clase.newInstance(); |
796 |
layer.setName(s[i]); |
797 |
layer.setXMLEntity(xml.getChild(i)); |
798 |
try {
|
799 |
layer.load(); |
800 |
}catch (DriverIOException e) {
|
801 |
throw new XMLException(e); |
802 |
} |
803 |
|
804 |
} |
805 |
|
806 |
this.addLayer(layer);
|
807 |
logger.debug("capa :"+ layer.getName() +"cargada"); |
808 |
// Comprobar que la proyecci?n es la misma que la de FMap
|
809 |
// Si no lo es, es una capa que est? reproyectada al vuelo
|
810 |
IProjection proj = layer.getProjection(); |
811 |
if ((proj != null)) |
812 |
if (proj != getFMap().getProjection())
|
813 |
{ |
814 |
ICoordTrans ct = new CoordTrans((CoordSys) proj,
|
815 |
(CoordSys) getFMap().getProjection()); |
816 |
// TODO: REVISAR CON LUIS
|
817 |
// Se lo fijamos a todas, luego cada una que se reproyecte
|
818 |
// si puede, o que no haga nada
|
819 |
layer.setCoordTrans(ct); |
820 |
// NOTA: DESPU?S DE ESTO DEBER?AMOS CREAR LAS ETIQUETAS
|
821 |
// SI ES NECESARIO.
|
822 |
// TODO: REVISAR A FONDO ESTO, PORQUE LAS ETIQUETAS SE CREAN
|
823 |
// 2 VECES SI HAY REPROYECC?N
|
824 |
if (layer instanceof FLyrVect) |
825 |
{ |
826 |
FLyrVect lyrVect = (FLyrVect)layer; |
827 |
VectorialLegend legend = (VectorialLegend) lyrVect.getLegend(); |
828 |
if (legend.getLabelField() != null) { |
829 |
lyrVect.createLabelLayer(lyrVect.getRecordset()); |
830 |
} |
831 |
|
832 |
} |
833 |
} |
834 |
}catch (Exception e) { |
835 |
fmap.addLayerError(xml.getChild(i).getStringProperty("name"));
|
836 |
//loadLayerException.addException(e);
|
837 |
/*if (layer!=null){
|
838 |
layer.setActive(false);
|
839 |
layer.setVisible(false);
|
840 |
this.addLayer(layer);
|
841 |
}*/
|
842 |
//throw new XMLException(e);
|
843 |
System.err.println("No se ha podido cargar la capa : "+xml.getChild(i).getStringProperty("name")); |
844 |
logger.debug("No se ha podido cargar la capa : "+xml.getChild(i).getStringProperty("name")); |
845 |
} |
846 |
|
847 |
} |
848 |
/* } catch (DriverException e) {
|
849 |
throw new XMLException(e);
|
850 |
} */
|
851 |
/*if (loadLayerException.getExceptions().size()>0){
|
852 |
throw loadLayerException;
|
853 |
}*/
|
854 |
} |
855 |
|
856 |
/**
|
857 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
|
858 |
* FBitSet)
|
859 |
*/
|
860 |
public void process(FeatureVisitor visitor, FBitSet subset) |
861 |
throws DriverException, VisitException {
|
862 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
863 |
FLayer layer = (FLayer) iter.next(); |
864 |
|
865 |
if (layer instanceof VectorialData) { |
866 |
((VectorialData) layer).process(visitor, subset); |
867 |
} |
868 |
} |
869 |
} |
870 |
|
871 |
/**
|
872 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
|
873 |
*/
|
874 |
public void process(FeatureVisitor visitor) |
875 |
throws DriverException, VisitException {
|
876 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
877 |
FLayer layer = (FLayer) iter.next(); |
878 |
|
879 |
if (layer.isActive()) {
|
880 |
if (layer instanceof VectorialData) { |
881 |
((VectorialData) layer).process(visitor); |
882 |
} |
883 |
} |
884 |
} |
885 |
} |
886 |
|
887 |
/**
|
888 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getFMap()
|
889 |
*/
|
890 |
public FMap getFMap() {
|
891 |
return fmap;
|
892 |
} |
893 |
|
894 |
/* (non-Javadoc)
|
895 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#setCoordTrans(org.cresques.cts.ICoordTrans)
|
896 |
*/
|
897 |
public void setCoordTrans(ICoordTrans ct) { |
898 |
super.setCoordTrans(ct);
|
899 |
|
900 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
901 |
FLayer layer = (FLayer) iter.next(); |
902 |
layer.setCoordTrans(ct); |
903 |
} |
904 |
} |
905 |
|
906 |
/**
|
907 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
|
908 |
*/
|
909 |
public void setAllActives(boolean active) { |
910 |
FLayer lyr; |
911 |
|
912 |
for (int i = 0; i < layers.size(); i++) { |
913 |
lyr = ((FLayer) layers.get(i)); |
914 |
lyr.setActive(active); |
915 |
|
916 |
if (lyr instanceof LayerCollection) { |
917 |
((LayerCollection) lyr).setAllActives(active); |
918 |
} |
919 |
} |
920 |
} |
921 |
|
922 |
/**
|
923 |
* @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
|
924 |
*/
|
925 |
public FLayer[] getActives() { |
926 |
ArrayList ret = new ArrayList(); |
927 |
|
928 |
for (int i = 0; i < layers.size(); i++) { |
929 |
if (((FLayer) layers.get(i)).isActive()) {
|
930 |
ret.add(layers.get(i)); |
931 |
}else{
|
932 |
if (layers.get(i) instanceof FLayers){ |
933 |
FLayer[] lyrs=((FLayers) layers.get(i)).getActives();
|
934 |
for (int j=0;j<lyrs.length;j++){ |
935 |
ret.add(lyrs[j]); |
936 |
} |
937 |
} |
938 |
} |
939 |
} |
940 |
|
941 |
return (FLayer[]) ret.toArray(new FLayer[0]); |
942 |
} |
943 |
|
944 |
/* (non-Javadoc)
|
945 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getMinScale()
|
946 |
*/
|
947 |
public double getMinScale() { |
948 |
return -1; // La visibilidad o no la controla cada capa |
949 |
// dentro de una colecci?n
|
950 |
} |
951 |
|
952 |
/* (non-Javadoc)
|
953 |
* @see com.iver.cit.gvsig.fmap.layers.FLayer#getMaxScale()
|
954 |
*/
|
955 |
public double getMaxScale() { |
956 |
return -1; |
957 |
} |
958 |
public void setMinScale(double minScale) |
959 |
{ |
960 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
961 |
FLayer lyr = (FLayer) iter.next(); |
962 |
lyr.setMinScale(minScale); |
963 |
} |
964 |
} |
965 |
public void setMaxScale(double maxScale) |
966 |
{ |
967 |
for (Iterator iter = layers.iterator(); iter.hasNext();) { |
968 |
FLayer lyr = (FLayer) iter.next(); |
969 |
lyr.setMinScale(maxScale); |
970 |
} |
971 |
} |
972 |
public void setActive(boolean b){ |
973 |
super.setActive(b);
|
974 |
for (int i=0;i<layers.size();i++){ |
975 |
((FLayer)layers.get(i)).setActive(b); |
976 |
} |
977 |
} |
978 |
|
979 |
} |