Index: MapContext3DImpl.java
===================================================================
--- MapContext3DImpl.java	(revision 256)
+++ MapContext3DImpl.java	(revision 257)
@@ -22,6 +22,7 @@
 package org.gvsig.gvsig3d.impl.map3d;
 
 import java.awt.Graphics2D;
+import java.awt.Point;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
@@ -33,16 +34,18 @@
 import javax.print.attribute.PrintRequestAttributeSet;
 
 import org.cresques.cts.IProjection;
-import org.gvsig.fmap.mapcontext.MapContext;
 import org.gvsig.fmap.mapcontext.ViewPort;
 import org.gvsig.fmap.mapcontext.layers.FLayer;
+import org.gvsig.fmap.mapcontext.layers.FLayers;
 import org.gvsig.fmap.mapcontext.layers.LayerEvent;
+import org.gvsig.fmap.mapcontext.layers.LayerListener;
 import org.gvsig.fmap.mapcontext.layers.SelectionEvent;
 import org.gvsig.fmap.mapcontext.layers.SingleLayerIterator;
+import org.gvsig.fmap.mapcontext.layers.operations.Classifiable;
+import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
 import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
 import org.gvsig.gvsig3d.map3d.Layer3DProps;
 import org.gvsig.gvsig3d.map3d.MapContext3D;
-import org.gvsig.gvsig3d.map3d.ViewPort3D;
 import org.gvsig.gvsig3d.map3d.layers.FLayers3D;
 import org.gvsig.osgvp.core.osg.Image;
 import org.gvsig.osgvp.core.osg.Matrix;
@@ -57,6 +60,7 @@
 import org.gvsig.osgvp.terrain.LayerManager;
 import org.gvsig.osgvp.terrain.RequestDataEvent;
 import org.gvsig.osgvp.terrain.Terrain;
+import org.gvsig.osgvp.terrain.Terrain.CoordinateSystemType;
 import org.gvsig.osgvp.terrain.TerrainViewer;
 import org.gvsig.osgvp.terrain.UpdateDataEvent;
 import org.gvsig.osgvp.viewer.Camera;
@@ -92,13 +96,18 @@
 
 	private boolean visibilityChange = false;
 
-	public MapContext3DImpl() {
-		super();
-	}
+	
+	public MapContext3DImpl(){}
 
 	public MapContext3DImpl(ViewPort vp) {
 		super(vp);
 	}
+	
+	public MapContext3DImpl(FLayers fLayers, ViewPort vp){
+		
+		super(fLayers,vp);
+		
+	}
 
 	public void draw(BufferedImage image, Graphics2D g, Cancellable cancel,
 			double scale) {
@@ -246,7 +255,6 @@
 		while (lyrIterator.hasNext()) {
 			FLayer layer = lyrIterator.next();
 			Layer3DProps props = getLayer3DProps(layer);
-			// props.setTerrainOrder(props.getTocOrder());
 			addLayerToTerrain(layer, false);
 		}
 
@@ -561,58 +569,58 @@
 	public void layerAdded(FLayers3D parent, FLayer layer) {
 		
 		System.out.println("Añadida capa. Evento Capturado");
-		// // to add group layers to 3D, just add recursively child data layers
-		// if (layer instanceof FLayers) {
-		// FLayers group = (FLayers) layer;
-		// for (int iChild = 0; iChild < group.getLayersCount(); iChild++) {
-		// layerAdded((FLayers3D) group, group.getLayer(iChild));
-		// }
-		// getLayer3DProps(layer).setHooked(true);
-		// return;
-		// }
-		//
-		// if (layer instanceof Classifiable) {
-		// Classifiable legendLyr = (Classifiable) layer;
-		// // legendLyr.addLegendListener((LegendListener) this);
-		// this.addLayerListener(this);
-		// }
-		// layer.addLayerListener((LayerListener) this);
-		//
-		// // listener to manage the selection for the layers
-		// if (layer.getClass().equals(FLyrVect.class)) {
-		// FLyrVect lyr = (FLyrVect) layer;
-		// refreshLayerVectorsVisibility(lyr);
-		// try {
-		// SelectableDataSource recordSet = lyr.getRecordset();
-		// if (recordSet != null) {
-		// SelectionSupport selectionSupport = recordSet
-		// .getSelectionSupport();
-		// selectionSupport.addSelectionListener(this);
-		// }
-		// } catch (ReadDriverException e) {
-		// // TODO Auto-generated catch block
-		// e.printStackTrace();
-		// }
-		// }
-		//
-		// if (!_bLoading)
-		// addLayerToTerrain(layer, true);
-		//
-		// // Only do this the first time to add layer
-		// if (_bEmptyView && !_bLoading) {
-		// if (layers.getLayersCount() > 0) {
-		// try {
-		// zoomToExtent(layer.getFullExtent());
-		// } catch (ExpansionFileReadException e) {
-		// e.printStackTrace();
-		// } catch (ReadDriverException e) {
-		// e.printStackTrace();
-		// }
-		// _bEmptyView = false;
-		// }
-		// }
-		//
-		// //PrintDebugLayers();
+//		// to add group layers to 3D, just add recursively child data layers
+//		if (layer instanceof FLayers) {
+//			FLayers group = (FLayers) layer;
+//			for (int iChild = 0; iChild < group.getLayersCount(); iChild++) {
+//				layerAdded((FLayers3D) group, group.getLayer(iChild));
+//			}
+//			getLayer3DProps(layer).setHooked(true);
+//			return;
+//		}
+//
+//		if (layer instanceof Classifiable) {
+//			Classifiable legendLyr = (Classifiable) layer;
+//			// legendLyr.addLegendListener((LegendListener) this);
+//			this.addLayerListener(this);
+//		}
+//		layer.addLayerListener((LayerListener) this);
+//
+//		// listener to manage the selection for the layers
+//		if (layer.getClass().equals(FLyrVect.class)) {
+//			FLyrVect lyr = (FLyrVect) layer;
+//			refreshLayerVectorsVisibility(lyr);
+//			try {
+//				SelectableDataSource recordSet = lyr.getRecordset();
+//				if (recordSet != null) {
+//					SelectionSupport selectionSupport = recordSet
+//							.getSelectionSupport();
+//					selectionSupport.addSelectionListener(this);
+//				}
+//			} catch (ReadDriverException e) {
+//				// TODO Auto-generated catch block
+//				e.printStackTrace();
+//			}
+//		}
+//
+//		if (!_bLoading)
+//			addLayerToTerrain(layer, true);
+//
+//		// Only do this the first time to add layer
+//		if (_bEmptyView && !_bLoading) {
+//			if (layers.getLayersCount() > 0) {
+//				try {
+//					zoomToExtent(layer.getFullExtent());
+//				} catch (ExpansionFileReadException e) {
+//					e.printStackTrace();
+//				} catch (ReadDriverException e) {
+//					e.printStackTrace();
+//				}
+//				_bEmptyView = false;
+//			}
+//		}
+//
+//		// PrintDebugLayers();
 
 	}
 
@@ -756,102 +764,97 @@
 	public UpdateDataEvent loadData(RequestDataEvent rde) {
 		
 		System.out.println("Load data");
-		// if (_terrainLayerMap.size() == 0)
-		// return null;
-		// if (rde == null)
-		// return null;
-		// if (rde.getExtent() == null)
-		// return null;
-		// if (rde.getLayer() == null)
-		// return null;
-		// if (rde.getLayerManager() == null)
-		// return null;
-		//
-		// UpdateDataEvent ude = new UpdateDataEvent();
-		// ude.copyDataFromRequest(rde);
-		//
-		//
-		// String layerID = rde.getLayer().getLayerID();
-		// FLayer layer = (FLayer) _terrainLayerMap.get(layerID);
-		// if (layer == null)
-		// return null;
-		// if (!layer.isVisible())
-		// return null;
-		//
-		// Layer3DProps props3D = getLayer3DProps(layer);
-		//
-		// // get/create cache service
-		// FLayerCacheService cacheService = (FLayerCacheService) props3D
-		// .getCacheService();
-		// if (cacheService == null) {
-		//
-		// cacheService = new FLayerCacheService(_terrain, props3D
-		// .getCacheName(), layer, _viewProjection);
-		// cacheService.setCacheRootDir(Layer3DProps.m_cacheDir);
-		// props3D.setCacheService(cacheService);
-		//
-		// }
-		//
-		//
-		//
-		// int dataType = props3D.getType();
-		// Point tileIndices = new Point(rde.getTileX(), rde.getTileY());
-		// TileNum tileNum = new TileNum(rde.getTileLevel(), tileIndices);
-		//
-		// String tileFileName = "";
-		//
-		// double minX, minY, width, height;
-		//
-		// if (_terrain.getCoordinateSystemType() ==
-		// CoordinateSystemType.PROJECTED) {
-		// minX = rde.getExtent().xMin();
-		// minY = rde.getExtent().yMin();
-		// width = rde.getExtent().xMax() - rde.getExtent().xMin();
-		// height = rde.getExtent().yMax() - rde.getExtent().yMin();
-		// } else {
-		// minX = Math.toDegrees(rde.getExtent().xMin());
-		// minY = Math.toDegrees(rde.getExtent().yMin());
-		// width = Math.toDegrees(rde.getExtent().xMax()
-		// - rde.getExtent().xMin());
-		// height = Math.toDegrees(rde.getExtent().yMax()
-		// - rde.getExtent().yMin());
-		// }
-		// boolean failData = false;
-		//
-		// Rectangle2D extent = new Rectangle2D.Double(minX, minY, width,
-		// height);
-		// if (cacheService.intersectsLayer(extent)) { // extent test
-		// try {
-		// tileFileName = cacheService.getTileAsFName(tileNum, extent);
-		// if (tileFileName == null)
-		// failData = true;
-		// } catch (Exception e) {
-		// failData = true;
-		// NotificationManager.addInfo("Problem reading tile:" + tileNum,
-		// e);
-		// e.printStackTrace();
-		// }
-		//
-		// if (failData) {
-		// return null;
-		// // ude.setFailData();
-		// } else {
-		// switch (dataType) {
-		// case Layer3DProps.layer3DImage:
-		// ude.setRasterData(tileFileName, "gdal");
-		// break;
-		// case Layer3DProps.layer3DElevation:
-		// ude.setHeightfieldData(tileFileName, "gdal");
-		// break;
-		// case Layer3DProps.layer3DVectorMR:
-		// ude.setVectorData(tileFileName, "osg");
-		// break;
-		// }
-		// }
-		// }
-		//
-		// return ude;
-		return null;
+		if (_terrainLayerMap.size() == 0)
+			return null;
+		if (rde == null)
+			return null;
+		if (rde.getExtent() == null)
+			return null;
+		if (rde.getLayer() == null)
+			return null;
+		if (rde.getLayerManager() == null)
+			return null;
+
+		UpdateDataEvent ude = new UpdateDataEvent();
+		ude.copyDataFromRequest(rde);
+
+		String layerID = rde.getLayer().getLayerID();
+		FLayer layer = (FLayer) _terrainLayerMap.get(layerID);
+		if (layer == null)
+			return null;
+		if (!layer.isVisible())
+			return null;
+
+		Layer3DProps props3D = getLayer3DProps(layer);
+
+		// get/create cache service
+//		FLayerCacheService cacheService = (FLayerCacheService) props3D
+//				.getCacheService();
+//		if (cacheService == null) {
+//
+//			cacheService = new FLayerCacheService(_terrain,
+//					props3D.getCacheName(), layer, _viewProjection);
+//			cacheService.setCacheRootDir(Layer3DProps.m_cacheDir);
+//			props3D.setCacheService(cacheService);
+//
+//		}
+
+		int dataType = props3D.getType();
+		Point tileIndices = new Point(rde.getTileX(), rde.getTileY());
+		//TileNum tileNum = new TileNum(rde.getTileLevel(), tileIndices);
+
+		String tileFileName = "";
+
+		double minX, minY, width, height;
+
+		if (_terrain.getCoordinateSystemType() == CoordinateSystemType.PROJECTED) {
+			minX = rde.getExtent().xMin();
+			minY = rde.getExtent().yMin();
+			width = rde.getExtent().xMax() - rde.getExtent().xMin();
+			height = rde.getExtent().yMax() - rde.getExtent().yMin();
+		} else {
+			minX = Math.toDegrees(rde.getExtent().xMin());
+			minY = Math.toDegrees(rde.getExtent().yMin());
+			width = Math.toDegrees(rde.getExtent().xMax()
+					- rde.getExtent().xMin());
+			height = Math.toDegrees(rde.getExtent().yMax()
+					- rde.getExtent().yMin());
+		}
+		boolean failData = false;
+
+		Rectangle2D extent = new Rectangle2D.Double(minX, minY, width, height);
+//		if (cacheService.intersectsLayer(extent)) { // extent test
+//			try {
+//				tileFileName = cacheService.getTileAsFName(tileNum, extent);
+//				if (tileFileName == null)
+//					failData = true;
+//			} catch (Exception e) {
+//				failData = true;
+//				NotificationManager.addInfo("Problem reading tile:" + tileNum,
+//						e);
+//				e.printStackTrace();
+//			}
+//
+//			if (failData) {
+//				return null;
+//				// ude.setFailData();
+//			} else {
+//				switch (dataType) {
+//				case Layer3DProps.layer3DImage:
+//					ude.setRasterData(tileFileName, "gdal");
+//					break;
+//				case Layer3DProps.layer3DElevation:
+//					ude.setHeightfieldData(tileFileName, "gdal");
+//					break;
+//				case Layer3DProps.layer3DVectorMR:
+//					ude.setVectorData(tileFileName, "osg");
+//					break;
+//				}
+//			}
+//		}
+
+		return ude;
+		
 	}
 
 	public void nameChanged(LayerEvent e) {
