gvsig-3d / 1.10 / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / map3d / MapContext3D.java @ 174
History | View | Annotate | Download (42.9 KB)
1 |
package org.gvsig.gvsig3d.map3d; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.Graphics2D; |
5 |
import java.awt.Point; |
6 |
import java.awt.geom.AffineTransform; |
7 |
import java.awt.geom.Rectangle2D; |
8 |
import java.awt.image.BufferedImage; |
9 |
import java.awt.image.ImagingOpException; |
10 |
import java.awt.image.RenderedImage; |
11 |
import java.io.File; |
12 |
import java.io.IOException; |
13 |
import java.util.ArrayList; |
14 |
import java.util.Hashtable; |
15 |
import java.util.Vector; |
16 |
|
17 |
import javax.print.attribute.PrintRequestAttributeSet; |
18 |
|
19 |
import org.apache.log4j.Logger; |
20 |
import org.cresques.cts.IProjection; |
21 |
import org.gvsig.cacheservice.TileNum; |
22 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
23 |
import org.gvsig.gvsig3d.cacheservices.FLayerCacheService; |
24 |
import org.gvsig.gvsig3d.cacheservices.OSGCacheService; |
25 |
import org.gvsig.gvsig3d.cacheservices.VectorCacheService; |
26 |
import org.gvsig.gvsig3d.drivers.GvsigDriverOSG; |
27 |
import org.gvsig.gvsig3d.map3d.layers.FLayers3D; |
28 |
import org.gvsig.osgvp.core.osg.Image; |
29 |
import org.gvsig.osgvp.core.osg.Matrix; |
30 |
import org.gvsig.osgvp.core.osg.Node; |
31 |
import org.gvsig.osgvp.core.osg.Vec3; |
32 |
import org.gvsig.osgvp.core.osg.Vec4; |
33 |
import org.gvsig.osgvp.exceptions.image.ImageConversionException; |
34 |
import org.gvsig.osgvp.exceptions.node.ChildIndexOutOfBoundsExceptions; |
35 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
36 |
import org.gvsig.osgvp.terrain.DataLoader; |
37 |
import org.gvsig.osgvp.terrain.Extent; |
38 |
import org.gvsig.osgvp.terrain.HeightfieldLayer; |
39 |
import org.gvsig.osgvp.terrain.JavaDataDriver; |
40 |
import org.gvsig.osgvp.terrain.Layer; |
41 |
import org.gvsig.osgvp.terrain.LayerManager; |
42 |
import org.gvsig.osgvp.terrain.RasterLayer; |
43 |
import org.gvsig.osgvp.terrain.RequestDataEvent; |
44 |
import org.gvsig.osgvp.terrain.Terrain; |
45 |
import org.gvsig.osgvp.terrain.Terrain.CoordinateSystemType; |
46 |
import org.gvsig.osgvp.terrain.TerrainViewer; |
47 |
import org.gvsig.osgvp.terrain.UpdateDataEvent; |
48 |
import org.gvsig.osgvp.terrain.VectorLayer; |
49 |
import org.gvsig.osgvp.terrain.exceptions.LayerManagementException; |
50 |
import org.gvsig.osgvp.viewer.Camera; |
51 |
import org.gvsig.osgvp.viewer.IViewerContainer; |
52 |
import org.gvsig.osgvp.viewer.PrintUtilities; |
53 |
import org.gvsig.raster.grid.render.VisualPropertyEvent; |
54 |
import org.gvsig.raster.grid.render.VisualPropertyListener; |
55 |
import org.gvsig.rastertools.colortable.ui.library.ColorTableLibraryEvent; |
56 |
import org.gvsig.rastertools.colortable.ui.library.ColorTableLibraryListener; |
57 |
|
58 |
import com.hardcode.driverManager.Driver; |
59 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
60 |
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps; |
61 |
import com.iver.andami.PluginServices; |
62 |
import com.iver.andami.messages.NotificationManager; |
63 |
import com.iver.andami.ui.mdiManager.IWindow; |
64 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
65 |
import com.iver.cit.gvsig.fmap.MapContext; |
66 |
import com.iver.cit.gvsig.fmap.ViewPort; |
67 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
68 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
69 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
70 |
import com.iver.cit.gvsig.fmap.layers.FLyrDefault; |
71 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
72 |
import com.iver.cit.gvsig.fmap.layers.FLyrWCS; |
73 |
import com.iver.cit.gvsig.fmap.layers.GraphicLayer; |
74 |
import com.iver.cit.gvsig.fmap.layers.LayerEvent; |
75 |
import com.iver.cit.gvsig.fmap.layers.LayerListener; |
76 |
import com.iver.cit.gvsig.fmap.layers.LegendChangedEvent; |
77 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
78 |
import com.iver.cit.gvsig.fmap.layers.SelectionEvent; |
79 |
import com.iver.cit.gvsig.fmap.layers.SelectionListener; |
80 |
import com.iver.cit.gvsig.fmap.layers.SelectionSupport; |
81 |
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
82 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
83 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable; |
84 |
import com.iver.cit.gvsig.fmap.rendering.LegendListener; |
85 |
import com.iver.cit.gvsig.project.documents.view.gui.BaseView; |
86 |
import com.iver.utiles.XMLEntity; |
87 |
import com.iver.utiles.swing.threads.Cancellable; |
88 |
|
89 |
public class MapContext3D extends MapContext implements DataLoader, |
90 |
LayerListener, LegendListener, SelectionListener { |
91 |
|
92 |
// JOSG library objects
|
93 |
private Terrain _terrain;
|
94 |
private IViewerContainer _canvas3d;
|
95 |
private TerrainViewer _terrainViewer;
|
96 |
private LayerManager _terrainLayerManager;
|
97 |
private JavaDataDriver _terrainDataManager = null; |
98 |
|
99 |
private Hashtable<FLayer, Layer> _terrainFLayerMap = new Hashtable<FLayer, Layer>(); |
100 |
private Hashtable<String, FLayer> _terrainLayerMap = new Hashtable<String, FLayer>(); |
101 |
|
102 |
private IProjection _viewProjection;
|
103 |
private float _verticalExaggeration; |
104 |
private boolean _bEmptyView = true; |
105 |
private boolean _bListenToLegend = true; |
106 |
private boolean _bLoading = false; |
107 |
private TerrainViewer _canvasoff = null; |
108 |
private boolean _renewCanvasOff = false; |
109 |
private static Logger _logger = Logger.getLogger(MapContext3D.class |
110 |
.getName()); |
111 |
TerrainViewer _printViewer = null;
|
112 |
Layer _testRasterLayer = null;
|
113 |
|
114 |
private boolean visibilityChange = false; |
115 |
|
116 |
@Override
|
117 |
public void draw(BufferedImage image, Graphics2D g, Cancellable cancel, |
118 |
double scale) throws ReadDriverException { |
119 |
|
120 |
if(_canvas3d == null || _canvas3d.getOSGViewer() == null) return; |
121 |
double x = getViewPort().getOffset().getX();
|
122 |
double y = getViewPort().getOffset().getY();
|
123 |
double w = image.getWidth();
|
124 |
double h = image.getHeight();
|
125 |
|
126 |
if (_canvasoff == null) { |
127 |
try {
|
128 |
_canvasoff = new TerrainViewer();
|
129 |
_canvasoff.setUpViewerInBackground(0, 0, 512, 512); |
130 |
|
131 |
Terrain planetView3D = ((TerrainViewer) _canvas3d |
132 |
.getOSGViewer()).getTerrain(0);
|
133 |
Terrain terrain = new Terrain();
|
134 |
terrain.setCoordinateSystemType(planetView3D |
135 |
.getCoordinateSystemType()); |
136 |
terrain.setCoordinateSystemName(planetView3D |
137 |
.getCoordinateSystemName()); |
138 |
terrain.setCoordinateSystemFormat(planetView3D |
139 |
.getCoordinateSystemFormat()); |
140 |
terrain.setExtent(planetView3D.getExtent()); |
141 |
terrain.setTerrainName(planetView3D.getTerrainName()); |
142 |
|
143 |
terrain.init(); |
144 |
terrain.setLayerManager(_terrainLayerManager); |
145 |
|
146 |
_canvasoff.addTerrain(terrain); |
147 |
_canvasoff.addFeature((((TerrainViewer) _canvas3d |
148 |
.getOSGViewer()).getFeatures())); |
149 |
Vec4 color = ((TerrainViewer) _canvas3d.getOSGViewer()) |
150 |
.getClearColor(); |
151 |
_canvasoff.setClearColor(color.x(), color.y(), color.z(), color |
152 |
.w()); |
153 |
this.setRenewCanvasOff(false); |
154 |
} catch (NodeException e) {
|
155 |
// TODO Auto-generated catch block
|
156 |
e.printStackTrace(); |
157 |
} |
158 |
|
159 |
} |
160 |
|
161 |
Camera refCam = _canvas3d.getOSGViewer().getCamera(); |
162 |
Matrix refView = refCam.getViewMatrix(); |
163 |
Matrix refProy = refCam.getProjectionMatrix(); |
164 |
Matrix.Perspective pers = refProy.getPerspective(); |
165 |
Camera viewCam = new Camera();
|
166 |
viewCam.setProjectionMatrixAsPerspective(pers.fovy, w / h, pers.zNear, |
167 |
pers.zFar); |
168 |
viewCam.setViewMatrix(refView); |
169 |
_canvasoff.setViewport(0, 0, (int) w, (int) h); |
170 |
_canvasoff.setCamera(viewCam); |
171 |
_canvasoff.takeScreenshotToMemory(); |
172 |
_canvasoff.frame(); |
173 |
|
174 |
Image OSGimage = _canvasoff.getScreenshotImage();
|
175 |
|
176 |
BufferedImage img = null; |
177 |
try {
|
178 |
img = OSGimage.getBufferedImage(); |
179 |
} catch (ImageConversionException e1) {
|
180 |
// TODO Auto-generated catch block
|
181 |
e1.printStackTrace(); |
182 |
} catch (IOException e1) { |
183 |
// TODO Auto-generated catch block
|
184 |
e1.printStackTrace(); |
185 |
} |
186 |
|
187 |
double scalex = w / img.getWidth(null); |
188 |
double scaley = h / img.getHeight(null); |
189 |
try {
|
190 |
AffineTransform xform = AffineTransform.getScaleInstance(scalex, |
191 |
scaley); |
192 |
AffineTransform xpos = AffineTransform.getTranslateInstance(x, y); |
193 |
xpos.concatenate(xform); |
194 |
g.drawRenderedImage(img, xpos); |
195 |
} catch (ImagingOpException e) { |
196 |
NotificationManager.addError("Dibujando FFramePicture", e);
|
197 |
} |
198 |
|
199 |
} |
200 |
|
201 |
public void print(Graphics2D g, double scale, PrintRequestAttributeSet arg2) |
202 |
throws ReadDriverException {
|
203 |
|
204 |
int x = (int) getViewPort().getOffset().getX(); |
205 |
int y = (int) getViewPort().getOffset().getY(); |
206 |
int w = (int) g.getClipBounds().getWidth(); |
207 |
int h = (int) g.getClipBounds().getHeight(); |
208 |
System.err.println("x " + x + "; y " + y + "; w " + w + "; h" + h); |
209 |
|
210 |
Camera viewerCam = _canvas3d.getOSGViewer().getCamera(); |
211 |
BufferedImage s = null; |
212 |
|
213 |
try {
|
214 |
int minw = (int) w / 10; |
215 |
int minh = (int) h / 10; |
216 |
System.out.println("minw" + minw + " minh" + minh); |
217 |
|
218 |
_printViewer = new TerrainViewer();
|
219 |
// Terrain planetView3D = ((TerrainViewer)
|
220 |
// _canvas3d.getOSGViewer()).getTerrain(0);
|
221 |
// Terrain terrain = new Terrain();
|
222 |
// terrain.setCoordinateSystemType(planetView3D
|
223 |
// .getCoordinateSystemType());
|
224 |
// terrain.setCoordinateSystemName(planetView3D
|
225 |
// .getCoordinateSystemName());
|
226 |
// terrain.setCoordinateSystemFormat(planetView3D
|
227 |
// .getCoordinateSystemFormat());
|
228 |
// terrain.setExtent(planetView3D.getExtent());
|
229 |
// terrain.setTerrainName(planetView3D.getTerrainName());
|
230 |
//
|
231 |
//
|
232 |
// terrain.init();
|
233 |
// terrain.setLayerManager(_terrainLayerManager);
|
234 |
|
235 |
_printViewer.addTerrain((((TerrainViewer) _canvas3d.getOSGViewer()) |
236 |
.getTerrain(0)));
|
237 |
_printViewer.addFeature((((TerrainViewer) _canvas3d.getOSGViewer()) |
238 |
.getFeatures())); |
239 |
Vec4 color = ((TerrainViewer) _canvas3d.getOSGViewer()) |
240 |
.getClearColor(); |
241 |
_printViewer.setClearColor(color.x(), color.y(), color.z(), color |
242 |
.w()); |
243 |
_printViewer.setUpViewerInBackground(0, 0, minw, minh); |
244 |
|
245 |
PrintUtilities util = new PrintUtilities();
|
246 |
util.setViewer(_printViewer); |
247 |
s = util.getHighResolutionImage(viewerCam, w, h, |
248 |
new PrintUtilities.FrameFinishedListener() {
|
249 |
|
250 |
public boolean isframeFinished() { |
251 |
/*if (getDataManager().getNumPendingRequests() > 0) {
|
252 |
System.out.println("getNumPending:" + getDataManager().getNumPendingRequests());
|
253 |
return false;
|
254 |
}
|
255 |
System.out.println("------------------ OLE! -------------");*/
|
256 |
return true; |
257 |
} |
258 |
}); |
259 |
|
260 |
RenderedImage render = s;
|
261 |
} catch (NodeException e1) {
|
262 |
// TODO Auto-generated catch block
|
263 |
e1.printStackTrace(); |
264 |
} |
265 |
|
266 |
double scalex = w / s.getWidth(null); |
267 |
double scaley = h / s.getHeight(null); |
268 |
try {
|
269 |
AffineTransform xform = AffineTransform.getScaleInstance(scalex, |
270 |
scaley); |
271 |
AffineTransform xpos = AffineTransform.getTranslateInstance(x, y); |
272 |
xpos.concatenate(xform); |
273 |
g.drawRenderedImage(s, xpos); |
274 |
// g.drawRenderedImage(img, new AffineTransform());
|
275 |
} catch (ImagingOpException e) { |
276 |
NotificationManager.addError("Dibujando FFramePicture", e);
|
277 |
} |
278 |
_printViewer.releaseGLContext(); |
279 |
_printViewer.dispose(); |
280 |
System.gc();
|
281 |
} |
282 |
|
283 |
public MapContext3D(ViewPort vp) {
|
284 |
super(vp);
|
285 |
} |
286 |
|
287 |
public MapContext3D(FLayers fLayers, ViewPort vp) {
|
288 |
super(fLayers, vp);
|
289 |
} |
290 |
|
291 |
public void setTerrain(Terrain terrain) { |
292 |
if (_terrain == terrain)
|
293 |
return;
|
294 |
|
295 |
_terrain = terrain; |
296 |
|
297 |
// add layers to terrain necessary when we are loading
|
298 |
addCurrentLayers(); |
299 |
} |
300 |
|
301 |
public Terrain getTerrain() {
|
302 |
return _terrain;
|
303 |
} |
304 |
|
305 |
public void setDataManager(JavaDataDriver manager) { |
306 |
_terrainDataManager = manager; |
307 |
_terrainDataManager.setDataLoader(this);
|
308 |
} |
309 |
|
310 |
public JavaDataDriver getDataManager() {
|
311 |
return _terrainDataManager;
|
312 |
} |
313 |
|
314 |
public void setLayerManager(LayerManager manager) { |
315 |
_terrainLayerManager = manager; |
316 |
} |
317 |
|
318 |
public LayerManager getLayerManager() {
|
319 |
return _terrainLayerManager;
|
320 |
} |
321 |
|
322 |
public void setViewer(IViewerContainer canvas) { |
323 |
_canvas3d = canvas; |
324 |
_terrainViewer = (TerrainViewer) _canvas3d.getOSGViewer(); |
325 |
} |
326 |
|
327 |
public IProjection getViewProjection() {
|
328 |
return _viewProjection;
|
329 |
} |
330 |
|
331 |
public void setViewProjection(IProjection projection) { |
332 |
_viewProjection = projection; |
333 |
} |
334 |
|
335 |
public IViewerContainer getCanvas3d() {
|
336 |
return _canvas3d;
|
337 |
} |
338 |
|
339 |
public float getVerticalExaggeration() { |
340 |
return _verticalExaggeration;
|
341 |
} |
342 |
|
343 |
public void setVerticalExaggeration(float verticalExaggeration) { |
344 |
_verticalExaggeration = verticalExaggeration; |
345 |
} |
346 |
|
347 |
public void setLoading(boolean bLoading) { |
348 |
_bLoading = bLoading; |
349 |
} |
350 |
|
351 |
public FLayers getNewGroupLayer(FLayers parent) {
|
352 |
return new FLayers3D(this, parent, getViewPort()); |
353 |
} |
354 |
|
355 |
/** * LAYER CHANGES called by FLayers3D ** */
|
356 |
|
357 |
public void layerMoved(FLayers3D parent, FLayer layer, |
358 |
int oldPos, int newPos) { |
359 |
|
360 |
if (layer instanceof FLayers) { |
361 |
FLayers group = (FLayers) layer; |
362 |
if (newPos > oldPos) {
|
363 |
for (int iChild = group.getLayersCount() - 1; iChild >= 0; iChild--) |
364 |
layerMoved((FLayers3D) group, group.getLayer(iChild), |
365 |
oldPos, newPos); |
366 |
} else {
|
367 |
for (int iChild = 0; iChild < group.getLayersCount(); iChild++) |
368 |
layerMoved((FLayers3D) group, group.getLayer(iChild), |
369 |
oldPos, newPos); |
370 |
} |
371 |
return;
|
372 |
} |
373 |
|
374 |
Layer3DProps props3D = getLayer3DProps(layer); |
375 |
int type = props3D.getType();
|
376 |
|
377 |
// Now reorder in layer manager only for terrain layer types
|
378 |
if ((type == Layer3DProps.layer3DOSG)
|
379 |
|| ((type == Layer3DProps.layer3DVector))) |
380 |
return;
|
381 |
|
382 |
// Obtain the old position in the layer
|
383 |
Layer terrainLayer = _terrainFLayerMap.get(layer); |
384 |
Vector<Integer> terrainOldPos=null; |
385 |
try {
|
386 |
terrainOldPos = _terrainLayerManager |
387 |
.getOrder(terrainLayer); |
388 |
} catch (LayerManagementException e) {
|
389 |
// TODO Auto-generated catch block
|
390 |
e.printStackTrace(); |
391 |
} |
392 |
|
393 |
int terrainNewPos = 0; |
394 |
// Compute the new position.
|
395 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(layers);
|
396 |
while (lyrIterator.hasNext()) {
|
397 |
FLayer auxLayer = lyrIterator.next(); |
398 |
if (auxLayer == layer)
|
399 |
break;
|
400 |
Layer3DProps auxProps3D = getLayer3DProps(auxLayer); |
401 |
if ((auxProps3D.getType() != Layer3DProps.layer3DOSG)
|
402 |
&& (auxProps3D.getType() != Layer3DProps.layer3DVector)) { |
403 |
terrainNewPos++; |
404 |
} |
405 |
|
406 |
} |
407 |
|
408 |
if ((terrainOldPos == null) || (terrainOldPos.size() == 0)) |
409 |
return;
|
410 |
if (terrainOldPos.get(0) != terrainNewPos) |
411 |
try {
|
412 |
_terrainLayerManager.moveLayer(terrainOldPos.get(0), terrainNewPos);
|
413 |
} catch (LayerManagementException e) {
|
414 |
// TODO Auto-generated catch block
|
415 |
e.printStackTrace(); |
416 |
} |
417 |
|
418 |
System.out.println("En el toc antes era el " + oldPos + " y ahora el " |
419 |
+ newPos); |
420 |
System.out.println("En el terrain antes era el " + terrainOldPos |
421 |
+ " y ahora el " + terrainNewPos);
|
422 |
|
423 |
PrintDebugLayers(); |
424 |
} |
425 |
|
426 |
public void layerAdded(FLayers3D parent, FLayer layer) { |
427 |
|
428 |
// to add group layers to 3D, just add recursively child data layers
|
429 |
if (layer instanceof FLayers) { |
430 |
FLayers group = (FLayers) layer; |
431 |
for (int iChild = 0; iChild < group.getLayersCount(); iChild++) { |
432 |
layerAdded((FLayers3D) group, group.getLayer(iChild)); |
433 |
} |
434 |
getLayer3DProps(layer).setHooked(true);
|
435 |
return;
|
436 |
} |
437 |
|
438 |
if (layer instanceof Classifiable) { |
439 |
Classifiable legendLyr = (Classifiable) layer; |
440 |
// legendLyr.addLegendListener((LegendListener) this);
|
441 |
this.addLayerListener(this); |
442 |
} |
443 |
layer.addLayerListener((LayerListener) this);
|
444 |
|
445 |
// listener to manage the selection for the layers
|
446 |
if (layer.getClass().equals(FLyrVect.class)) {
|
447 |
FLyrVect lyr = (FLyrVect) layer; |
448 |
refreshLayerVectorsVisibility(lyr); |
449 |
try {
|
450 |
SelectableDataSource recordSet = lyr.getRecordset(); |
451 |
if (recordSet != null) { |
452 |
SelectionSupport selectionSupport = recordSet |
453 |
.getSelectionSupport(); |
454 |
selectionSupport.addSelectionListener(this);
|
455 |
} |
456 |
} catch (ReadDriverException e) {
|
457 |
// TODO Auto-generated catch block
|
458 |
e.printStackTrace(); |
459 |
} |
460 |
} |
461 |
|
462 |
if (!_bLoading)
|
463 |
addLayerToTerrain(layer, true);
|
464 |
|
465 |
// Only do this the first time to add layer
|
466 |
if (_bEmptyView && !_bLoading) {
|
467 |
if (layers.getLayersCount() > 0) { |
468 |
try {
|
469 |
zoomToExtent(layer.getFullExtent()); |
470 |
} catch (ExpansionFileReadException e) {
|
471 |
e.printStackTrace(); |
472 |
} catch (ReadDriverException e) {
|
473 |
e.printStackTrace(); |
474 |
} |
475 |
_bEmptyView = false;
|
476 |
} |
477 |
} |
478 |
|
479 |
//PrintDebugLayers();
|
480 |
} |
481 |
|
482 |
public void layerRemoved(FLayers3D parent, FLayer layer) { |
483 |
|
484 |
// to remove group layers to 3D, just remove recursively child data
|
485 |
// layers
|
486 |
if (layer instanceof FLayers) { |
487 |
FLayers group = (FLayers) layer; |
488 |
for (int iChild = 0; iChild < group.getLayersCount(); iChild++) { |
489 |
layerRemoved((FLayers3D) group, group.getLayer(iChild)); |
490 |
} |
491 |
getLayer3DProps(layer).setHooked(false);
|
492 |
return;
|
493 |
} |
494 |
|
495 |
if (layer instanceof Classifiable) { |
496 |
Classifiable legendLyr = (Classifiable) layer; |
497 |
legendLyr.removeLegendListener((LegendListener) this);
|
498 |
} |
499 |
|
500 |
layer.removeLayerListener((LayerListener) this);
|
501 |
|
502 |
removeLayerToTerrain(layer); |
503 |
|
504 |
// All layers are removed
|
505 |
if (layers.getLayersCount() == 0) { |
506 |
_bEmptyView = true;
|
507 |
} |
508 |
|
509 |
PrintDebugLayers(); |
510 |
} |
511 |
|
512 |
/**
|
513 |
* DOCUMENT ME!
|
514 |
*
|
515 |
* @param e
|
516 |
* DOCUMENT ME!
|
517 |
*/
|
518 |
public void legendChanged(LegendChangedEvent e) { |
519 |
if (!_bListenToLegend)
|
520 |
return;
|
521 |
if ((e == null) && (!visibilityChange)) { |
522 |
// find layer whose legend changed
|
523 |
FLayer found = null;
|
524 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(layers);
|
525 |
while (lyrIterator.hasNext()) {
|
526 |
FLayer lyr = lyrIterator.next(); |
527 |
if (lyr instanceof FLyrVect) { |
528 |
FLyrVect lyrVect = (FLyrVect) lyr; |
529 |
long newDrawVersion = lyrVect.getDrawVersion();
|
530 |
Layer3DProps props3D = getLayer3DProps(lyrVect); |
531 |
if ((newDrawVersion - props3D.drawVersion) >= 1) { |
532 |
props3D.drawVersion = lyrVect.getDrawVersion(); |
533 |
refreshLayerInTerrain(props3D, true);
|
534 |
refreshLayerVectorsVisibility(lyrVect); |
535 |
_bListenToLegend = false;
|
536 |
//props3D.VerifyLegend(_terrain.getTerrainName());
|
537 |
_bListenToLegend = true;
|
538 |
|
539 |
} |
540 |
} |
541 |
} |
542 |
|
543 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
544 |
if (f instanceof BaseView) { |
545 |
BaseView view3D = (BaseView) f; |
546 |
view3D.getTOC().refresh(); |
547 |
} |
548 |
} |
549 |
visibilityChange = false;
|
550 |
} |
551 |
|
552 |
public void visibilityChanged(LayerEvent e) { |
553 |
FLayer lyr = e.getSource(); |
554 |
|
555 |
Layer3DProps props3D = getLayer3DProps(lyr); |
556 |
|
557 |
if (props3D.getType() == Layer3DProps.layer3DVector) {
|
558 |
refreshLayerVectorsVisibility(lyr); |
559 |
} else if (props3D.getType() == Layer3DProps.layer3DOSG) { |
560 |
refreshLayer3DOSGVisibility(lyr); |
561 |
} else {
|
562 |
refreshLayerVisibility(lyr); |
563 |
} |
564 |
visibilityChange = true;
|
565 |
} |
566 |
|
567 |
private void refreshLayer3DOSGVisibility(FLayer lyr) { |
568 |
if (_terrain == null || _viewProjection == null) |
569 |
return;
|
570 |
|
571 |
Layer3DProps props3D = getLayer3DProps(lyr); |
572 |
|
573 |
OSGCacheService cacheService = (OSGCacheService) props3D |
574 |
.getCacheService(); |
575 |
if (cacheService != null) { |
576 |
// use VectorCacheService to add features to terrain
|
577 |
cacheService.refreshFeaturesToTerrain(lyr.isVisible()); |
578 |
} |
579 |
|
580 |
} |
581 |
|
582 |
private void refreshLayerVisibility(FLayer layer) { |
583 |
|
584 |
Layer terrainLayer = _terrainFLayerMap.get(layer); |
585 |
terrainLayer.setEnabled(layer.isVisible()); |
586 |
_terrainLayerManager.updateLayers(); |
587 |
|
588 |
} |
589 |
|
590 |
private void refreshLayerVectorsVisibility(FLayer lyr) { |
591 |
if (_terrain == null || _viewProjection == null) |
592 |
return;
|
593 |
|
594 |
Layer3DProps props3D = getLayer3DProps(lyr); |
595 |
|
596 |
VectorCacheService cacheService = (VectorCacheService) props3D |
597 |
.getCacheService(); |
598 |
if (cacheService != null) { |
599 |
// use VectorCacheService to add features to terrain
|
600 |
cacheService.refreshFeaturesToTerrain(lyr.isVisible()); |
601 |
props3D.drawVersion = lyr.getDrawVersion(); |
602 |
} |
603 |
|
604 |
} |
605 |
|
606 |
public void activationChanged(LayerEvent e) { |
607 |
|
608 |
int a = 0; |
609 |
} |
610 |
|
611 |
public void nameChanged(LayerEvent e) { |
612 |
// TODO Implement this method
|
613 |
} |
614 |
|
615 |
public void editionChanged(LayerEvent e) { |
616 |
// TODO Implement this method
|
617 |
|
618 |
} |
619 |
|
620 |
public void refreshLayerInTerrain(Layer3DProps props, boolean bRemoveCache) { |
621 |
if (props == null) |
622 |
return;
|
623 |
|
624 |
if (_terrain == null) |
625 |
return; // view not opened yet |
626 |
|
627 |
// clear cache
|
628 |
|
629 |
|
630 |
|
631 |
// refresh layer in terrain
|
632 |
int type = props.getType();
|
633 |
if ((type == Layer3DProps.layer3DImage)
|
634 |
|| (type == Layer3DProps.layer3DElevation) |
635 |
|| (type == Layer3DProps.layer3DVectorMR)) { |
636 |
|
637 |
if (bRemoveCache) {
|
638 |
|
639 |
String layerCacheDir = Layer3DProps.m_cacheDir + File.separator |
640 |
+ _terrain.getTerrainName() + File.separator + props.getCacheName();
|
641 |
|
642 |
FLayerCacheService cacheServices = (FLayerCacheService) props.getCacheService(); |
643 |
if(cacheServices != null) |
644 |
{ |
645 |
try {
|
646 |
cacheServices.free(); |
647 |
props.setCacheService(null);
|
648 |
} catch (Throwable e) { |
649 |
// TODO Auto-generated catch block
|
650 |
e.printStackTrace(); |
651 |
} |
652 |
} |
653 |
|
654 |
removeCache(new File(layerCacheDir)); |
655 |
} |
656 |
try {
|
657 |
_terrainLayerManager.invalidateLayer(props.getTerrainLayer()); |
658 |
} catch (LayerManagementException e) {
|
659 |
// TODO Auto-generated catch block
|
660 |
e.printStackTrace(); |
661 |
} |
662 |
} else if (type == Layer3DProps.layer3DVector) { |
663 |
invalidateVectorLayer(props); |
664 |
} |
665 |
} |
666 |
|
667 |
private void invalidateVectorLayer(Layer3DProps props) { |
668 |
// TODO Auto-generated method stub
|
669 |
if (_terrain == null || _viewProjection == null) |
670 |
return;
|
671 |
|
672 |
VectorCacheService cacheService = (VectorCacheService) props |
673 |
.getCacheService(); |
674 |
if (cacheService != null) { |
675 |
// use VectorCacheService to add features to terrain
|
676 |
cacheService.refreshFeaturesToTerrain(); |
677 |
} |
678 |
} |
679 |
|
680 |
private boolean removeCache(File path) { |
681 |
if( path.exists() ) {
|
682 |
File[] files = path.listFiles(); |
683 |
for(int i=0; i<files.length; i++) { |
684 |
if(files[i].isDirectory()) {
|
685 |
removeCache(files[i]); |
686 |
} |
687 |
else {
|
688 |
files[i].delete(); |
689 |
} |
690 |
} |
691 |
} |
692 |
return( path.delete() );
|
693 |
} |
694 |
|
695 |
protected Layer3DProps getLayer3DProps(FLayer layer) {
|
696 |
|
697 |
Layer3DProps props3D = Layer3DProps.getLayer3DProps(layer); |
698 |
|
699 |
// Create instance of props for osg layers.
|
700 |
if (props3D == null) { |
701 |
props3D = new Layer3DProps();
|
702 |
if (layer instanceof FLyrVect) { |
703 |
FLyrVect nLayer = (FLyrVect) layer; |
704 |
Driver driver;
|
705 |
try {
|
706 |
driver = nLayer.getRecordset().getDriver(); |
707 |
if (driver instanceof GvsigDriverOSG) { |
708 |
props3D.setChooseType(false);
|
709 |
props3D.setType(Layer3DProps.layer3DOSG); |
710 |
} |
711 |
} catch (ReadDriverException e) {
|
712 |
// TODO Auto-generated catch block
|
713 |
e.printStackTrace(); |
714 |
} |
715 |
|
716 |
} |
717 |
// Set the properties
|
718 |
props3D.setLayer(layer); |
719 |
|
720 |
props3D.initCacheName(_terrain.getCoordinateSystemType(), |
721 |
_viewProjection, Terrain.CoordinateSystemType.GEOCENTRIC); |
722 |
FLyrDefault baseLayer = (FLyrDefault) layer; |
723 |
baseLayer.setProperty("3DLayerExtension", props3D);
|
724 |
props3D.setVerticalEx(getVerticalExaggeration()); |
725 |
} else {
|
726 |
if (_bLoading)
|
727 |
props3D.setChooseType(false);
|
728 |
props3D.setLayer(layer); |
729 |
} |
730 |
|
731 |
return props3D;
|
732 |
} |
733 |
|
734 |
public void addCurrentLayers() { |
735 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(layers);
|
736 |
while (lyrIterator.hasNext()) {
|
737 |
FLayer layer = lyrIterator.next(); |
738 |
Layer3DProps props = getLayer3DProps(layer); |
739 |
// props.setTerrainOrder(props.getTocOrder());
|
740 |
addLayerToTerrain(layer, false);
|
741 |
} |
742 |
} |
743 |
|
744 |
public void addLayerToTerrain(FLayer layer, |
745 |
boolean bVerifyLegend) {
|
746 |
Layer3DProps props3D = getLayer3DProps(layer); |
747 |
|
748 |
if (props3D.getType() == Layer3DProps.layer3DVector) {
|
749 |
CreateVectors(layer, props3D); // special case for now without
|
750 |
// disk cache
|
751 |
} else if (props3D.getType() == Layer3DProps.layer3DOSG) { |
752 |
CreateOSGLayer(layer, props3D); |
753 |
} else {
|
754 |
Rectangle2D layerExtent = null; |
755 |
try {
|
756 |
layerExtent = layer.getFullExtent(); |
757 |
} catch (ExpansionFileReadException e) {
|
758 |
e.printStackTrace(); |
759 |
} catch (ReadDriverException e) {
|
760 |
e.printStackTrace(); |
761 |
} |
762 |
if (layerExtent == null) { // hack for missing extents |
763 |
if (_terrain.getCoordinateSystemType() == Terrain.CoordinateSystemType.GEOCENTRIC)
|
764 |
layerExtent = new Rectangle2D.Double(-180.0, -90.0, 360.0, |
765 |
180.0);
|
766 |
else
|
767 |
layerExtent = new Rectangle2D.Double(-20000000.0, |
768 |
-10000000.0, 40000000.0, 20000000.0); |
769 |
// TODO toggle comment because this code use WCS extension. When
|
770 |
// WCS extension runs correctly uncoment!!!
|
771 |
|
772 |
if (layer instanceof FLyrWCS) |
773 |
((FLyrWCS) layer).setFullExtent(layerExtent); |
774 |
} |
775 |
|
776 |
Extent extent = null;
|
777 |
if (_terrain.getCoordinateSystemType() == CoordinateSystemType.PROJECTED) {
|
778 |
extent = new Extent(layerExtent.getMinX(), layerExtent
|
779 |
.getMinY(), layerExtent.getMaxX(), layerExtent |
780 |
.getMaxY()); |
781 |
} else {
|
782 |
extent = new Extent(Math.toRadians(layerExtent.getMinX()), Math |
783 |
.toRadians(layerExtent.getMinY()), Math
|
784 |
.toRadians(layerExtent.getMaxX()), Math
|
785 |
.toRadians(layerExtent.getMaxY())); |
786 |
} |
787 |
|
788 |
if (props3D.getType() == Layer3DProps.layer3DImage) {
|
789 |
RasterLayer rlayer = new RasterLayer();
|
790 |
rlayer.setEnabled(layer.isVisible()); |
791 |
rlayer.setExtent(extent); |
792 |
rlayer.setOpacity(props3D.getOpacity()); |
793 |
rlayer.setDataDriver(_terrainDataManager); |
794 |
_logger.warn("computed optimum level = "+computeOptimumLevel(layer,20,256)); |
795 |
rlayer.setMaxResolution(computeOptimumLevel(layer,20,256)); |
796 |
try {
|
797 |
_terrainLayerManager.addLayer(rlayer); |
798 |
} catch (LayerManagementException e) {
|
799 |
// TODO Auto-generated catch block
|
800 |
e.printStackTrace(); |
801 |
} |
802 |
_terrainFLayerMap.put(layer, rlayer); |
803 |
_terrainLayerMap.put(rlayer.getLayerID(), layer); |
804 |
props3D.setTerrainLayer(rlayer); |
805 |
// Refresh cache for vector rasterized layers
|
806 |
if (layer.getClass().equals(FLyrVect.class)) {
|
807 |
refreshLayerInTerrain(props3D, true);
|
808 |
} |
809 |
} else if (props3D.getType() == Layer3DProps.layer3DElevation) { |
810 |
HeightfieldLayer hlayer = new HeightfieldLayer();
|
811 |
hlayer.setEnabled(layer.isVisible()); |
812 |
hlayer.setExtent(extent); |
813 |
hlayer.setVerticalExaggeration(props3D.getVerticalEx()); |
814 |
_logger.warn("computed optimum level = "+computeOptimumLevel(layer,20,32)); |
815 |
hlayer.setMaxResolution(computeOptimumLevel(layer,20,32)); |
816 |
hlayer.setDataDriver(_terrainDataManager); |
817 |
try {
|
818 |
_terrainLayerManager.addLayer(hlayer); |
819 |
} catch (LayerManagementException e) {
|
820 |
// TODO Auto-generated catch block
|
821 |
e.printStackTrace(); |
822 |
} |
823 |
_terrainFLayerMap.put(layer, hlayer); |
824 |
_terrainLayerMap.put(hlayer.getLayerID(), layer); |
825 |
props3D.setTerrainLayer(hlayer); |
826 |
} else if (props3D.getType() == Layer3DProps.layer3DVectorMR) { |
827 |
VectorLayer vlayer = new VectorLayer();
|
828 |
vlayer.setEnabled(true);
|
829 |
vlayer.setExtent(extent); |
830 |
vlayer.setDensity(1.0f);
|
831 |
vlayer.setDataDriver(_terrainDataManager); |
832 |
try {
|
833 |
_terrainLayerManager.addLayer(vlayer); |
834 |
} catch (LayerManagementException e) {
|
835 |
// TODO Auto-generated catch block
|
836 |
e.printStackTrace(); |
837 |
} |
838 |
_terrainFLayerMap.put(layer, vlayer); |
839 |
_terrainLayerMap.put(vlayer.getLayerID(), layer); |
840 |
props3D.setTerrainLayer(vlayer); |
841 |
} |
842 |
|
843 |
if (bVerifyLegend) {
|
844 |
_bListenToLegend = false;
|
845 |
props3D.VerifyLegend(_terrain.getTerrainName()); |
846 |
_bListenToLegend = true;
|
847 |
} |
848 |
} |
849 |
} |
850 |
|
851 |
public int computeOptimumLevel(FLayer layer, int maxLevel, double tileSize) { |
852 |
int level = maxLevel;
|
853 |
if(!(layer instanceof FLyrRasterSE)) |
854 |
return level;
|
855 |
|
856 |
Rectangle2D ext = null; |
857 |
try {
|
858 |
ext = layer.getFullExtent(); |
859 |
} catch (ReadDriverException e1) {
|
860 |
NotificationManager.addError("Error al obtener el extent", e1);
|
861 |
} |
862 |
|
863 |
ArrayList attr = ((FLyrRasterSE)layer).getAttributes();
|
864 |
if(attr == null) |
865 |
{ |
866 |
return level;
|
867 |
} |
868 |
int width = 0, height = 0; |
869 |
for (int i=0; i<attr.size(); i++) { |
870 |
Object[] a = (Object []) attr.get(i); |
871 |
if (a[0].toString().equals("Width")) |
872 |
width = ((Integer)a[1]).intValue(); |
873 |
if (a[0].toString().equals("Height")) |
874 |
height = ((Integer)a[1]).intValue(); |
875 |
} |
876 |
|
877 |
if( ext != null && |
878 |
width != 0 &&
|
879 |
height != 0){
|
880 |
|
881 |
Extent destinationExtents = _terrain.getExtent(); |
882 |
double xMax = destinationExtents.xMax();
|
883 |
double xMin = destinationExtents.xMin();
|
884 |
double yMax = destinationExtents.yMax();
|
885 |
double yMin = destinationExtents.yMin();
|
886 |
int terrainType = _terrain.getCoordinateSystemType();
|
887 |
if(terrainType == Terrain.CoordinateSystemType.GEOCENTRIC)
|
888 |
{ |
889 |
xMax = Math.toDegrees(xMax);
|
890 |
xMin = Math.toDegrees(xMin);
|
891 |
yMax = Math.toDegrees(yMax);
|
892 |
yMin = Math.toDegrees(yMin);
|
893 |
} |
894 |
|
895 |
double destination_xRange = xMax-xMin;
|
896 |
double destination_yRange = yMax-yMin;
|
897 |
double AR = destination_xRange / destination_yRange;
|
898 |
int C1 = 1; |
899 |
int R1 = 1; |
900 |
|
901 |
boolean swapAxis = AR<1.0; |
902 |
if (swapAxis) AR = 1.0/AR; |
903 |
|
904 |
double lower_AR = Math.floor(AR); |
905 |
double upper_AR = Math.ceil(AR); |
906 |
|
907 |
if (AR<Math.sqrt(lower_AR*upper_AR)) |
908 |
{ |
909 |
C1 = (int)(lower_AR);
|
910 |
R1 = 1;
|
911 |
} |
912 |
else
|
913 |
{ |
914 |
C1 = (int)(upper_AR);
|
915 |
R1 = 1;
|
916 |
} |
917 |
|
918 |
if (swapAxis)
|
919 |
{ |
920 |
//std::swap(C1,R1);
|
921 |
int t = C1;
|
922 |
C1 = R1; |
923 |
R1 = t; |
924 |
} |
925 |
|
926 |
|
927 |
double source_xRange = ext.getMaxX() - ext.getMinX();
|
928 |
double source_yRange = ext.getMaxY() - ext.getMinY();
|
929 |
|
930 |
float sourceResolutionX = ((float)source_xRange)/(float)width; |
931 |
float sourceResolutionY = ((float)source_yRange)/(float)height; |
932 |
|
933 |
// log(osg::NOTICE,"Source %s resX %f resY %f",source->getFileName().c_str(), sourceResolutionX, sourceResolutionX);
|
934 |
|
935 |
|
936 |
int k_cols = (int)( Math.ceil( 1.0 + Math.log( destination_xRange / (C1 * sourceResolutionX * tileSize ) ) / Math.log(2.0) ) ); |
937 |
int k_rows = (int)( Math.ceil( 1.0 + Math.log( destination_yRange / (R1 * sourceResolutionY * tileSize ) ) / Math.log(2.0) ) ); |
938 |
level = Math.max(k_cols, k_rows);
|
939 |
level = Math.min(level, maxLevel);
|
940 |
level = Math.max(level, 0); |
941 |
} |
942 |
return level;
|
943 |
} |
944 |
|
945 |
private void CreateOSGLayer(FLayer layer, Layer3DProps props3D) { |
946 |
if (_terrain == null || _viewProjection == null) |
947 |
return;
|
948 |
|
949 |
OSGCacheService cacheService = (OSGCacheService) props3D |
950 |
.getCacheService(); |
951 |
if (cacheService == null) { |
952 |
cacheService = new OSGCacheService(_canvas3d, _terrain, props3D
|
953 |
.getCacheName(), layer, _viewProjection); |
954 |
cacheService.setCacheRootDir(Layer3DProps.m_cacheDir); |
955 |
props3D.setCacheService(cacheService); |
956 |
} |
957 |
|
958 |
// use VectorCacheService to add features to planet
|
959 |
cacheService.AddFeaturesToTerrain(); |
960 |
|
961 |
} |
962 |
|
963 |
@Override
|
964 |
public XMLEntity getXMLEntity() throws XMLException { |
965 |
// TODO Auto-generated method stub
|
966 |
XMLEntity xml = super.getXMLEntity();
|
967 |
|
968 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(layers);
|
969 |
while (lyrIterator.hasNext()) {
|
970 |
FLayer lyr = lyrIterator.next(); |
971 |
Layer3DProps props3D = getLayer3DProps(lyr); |
972 |
int type = props3D.getType();
|
973 |
if (type == Layer3DProps.layer3DOSG) {
|
974 |
|
975 |
OSGCacheService cacheService = (OSGCacheService) props3D |
976 |
.getCacheService(); |
977 |
if (cacheService != null) { |
978 |
// use VectorCacheService to add features to planet
|
979 |
if (props3D.isEditing()) {
|
980 |
// TODO: PONER AKI EL CODIGO DE SALVAR
|
981 |
} |
982 |
} |
983 |
} |
984 |
} |
985 |
|
986 |
return xml;
|
987 |
|
988 |
} |
989 |
|
990 |
private void CreateVectors(FLayer layer, Layer3DProps props3D) { |
991 |
if (_terrain == null || _viewProjection == null) |
992 |
return;
|
993 |
|
994 |
VectorCacheService cacheService = (VectorCacheService) props3D |
995 |
.getCacheService(); |
996 |
if (cacheService == null) { |
997 |
cacheService = new VectorCacheService(_canvas3d, _terrain, props3D
|
998 |
.getCacheName(), layer, _viewProjection); |
999 |
cacheService.setCacheRootDir(Layer3DProps.m_cacheDir); |
1000 |
props3D.setCacheService(cacheService); |
1001 |
} |
1002 |
|
1003 |
// use VectorCacheService to add features to terrain
|
1004 |
cacheService.addFeaturesToTerrain(); |
1005 |
} |
1006 |
|
1007 |
private void DeleteVectors(FLayer layer, Layer3DProps props3D) { |
1008 |
if (_terrain == null || _viewProjection == null) |
1009 |
return;
|
1010 |
|
1011 |
VectorCacheService cacheService = (VectorCacheService) props3D |
1012 |
.getCacheService(); |
1013 |
if (cacheService != null) { |
1014 |
// use VectorCacheService to delete features to terrain
|
1015 |
cacheService.deleteFeaturesToTerrain(); |
1016 |
} |
1017 |
} |
1018 |
|
1019 |
/**
|
1020 |
* Sets the given zoom extent to the view
|
1021 |
*
|
1022 |
* @param extent
|
1023 |
* The extent to zoom to.
|
1024 |
*/
|
1025 |
public void zoomToExtent(Rectangle2D geoExtent) { |
1026 |
|
1027 |
if(geoExtent == null) return; |
1028 |
if((geoExtent.getWidth() == 0) || (geoExtent.getHeight() ==0)) return; |
1029 |
|
1030 |
double maxHeight = 0.0; |
1031 |
|
1032 |
// Getting extent positions
|
1033 |
double minLat = geoExtent.getMinY();
|
1034 |
double maxLat = geoExtent.getMaxY();
|
1035 |
double cenLon = geoExtent.getCenterX();
|
1036 |
double cenLat = geoExtent.getCenterY();
|
1037 |
|
1038 |
double elevation = 0; |
1039 |
|
1040 |
// calculate altitude
|
1041 |
double avLat = 0; |
1042 |
if (minLat > 0.0 || maxLat < 0.0) |
1043 |
avLat = Math.min(Math.abs(minLat), Math.abs(maxLat)); |
1044 |
double avLon = Math.min(180.0, geoExtent.getWidth()); |
1045 |
|
1046 |
double terrainRadius = _terrain.getRadiusEquatorial();
|
1047 |
double radiusLat = terrainRadius * Math.cos(Math.toRadians(avLat)); |
1048 |
double deltaLon = Math.sqrt(2 * radiusLat * radiusLat |
1049 |
* (1 - Math.cos(Math.toRadians(avLon)))); |
1050 |
double deltaLat = Math.sqrt(2 * terrainRadius * terrainRadius |
1051 |
* (1 - Math.cos(Math.toRadians(geoExtent.getHeight())))); |
1052 |
|
1053 |
double zoomFactor = 1.5; |
1054 |
elevation = (Math.max(deltaLon, deltaLat) * zoomFactor) + maxHeight;
|
1055 |
|
1056 |
// Calculate XYZ positions for camera.
|
1057 |
|
1058 |
int terrainType = _terrain.getCoordinateSystemType();
|
1059 |
|
1060 |
Vec3 eye = new Vec3();
|
1061 |
Vec3 center = new Vec3();
|
1062 |
Vec3 up = new Vec3();
|
1063 |
// Calculate positions for PLAIN MODE.
|
1064 |
if (terrainType == Terrain.CoordinateSystemType.PROJECTED) {
|
1065 |
|
1066 |
double difx = (geoExtent.getMaxX() - geoExtent.getX()) / 1.2d; |
1067 |
double dify = (geoExtent.getMaxY() - geoExtent.getY()) / 1.2d; |
1068 |
double height;
|
1069 |
|
1070 |
height = Math.sqrt(difx * difx + dify * dify);
|
1071 |
double fullWindowFactor = 1.7; |
1072 |
// EYE
|
1073 |
eye.setX(cenLon); |
1074 |
eye.setY(cenLat); |
1075 |
eye.setZ(height * fullWindowFactor); |
1076 |
// CENTER
|
1077 |
center.setX(cenLon); |
1078 |
center.setY(cenLat); |
1079 |
center.setZ(0.0);
|
1080 |
// UP
|
1081 |
up.setX(0.0);
|
1082 |
up.setY(1.0);
|
1083 |
up.setZ(0.0);
|
1084 |
} else
|
1085 |
// Calculate positions for SPHERICAL MODE.
|
1086 |
if (terrainType == Terrain.CoordinateSystemType.GEOCENTRIC) {
|
1087 |
// EYE
|
1088 |
Vec3 aux = new Vec3(cenLat, cenLon, elevation);
|
1089 |
eye = _terrain.convertLatLongHeightToXYZ(aux); |
1090 |
// CENTER
|
1091 |
center.setX(0.0);
|
1092 |
center.setY(0.0);
|
1093 |
center.setZ(0.0);
|
1094 |
// UP
|
1095 |
up.setX(0.0);
|
1096 |
up.setY(0.0);
|
1097 |
up.setZ(1.0);
|
1098 |
} |
1099 |
Camera cam = new Camera();
|
1100 |
cam.setViewByLookAt(eye, center, up); |
1101 |
|
1102 |
_terrainViewer.setCamera(cam); |
1103 |
|
1104 |
} |
1105 |
|
1106 |
private void PrintDebugLayers() { |
1107 |
if (_terrainLayerManager != null) { |
1108 |
System.out.println("==========================="); |
1109 |
System.out.println("Total terrain layers: " |
1110 |
+ _terrainLayerManager.getNumLayers()); |
1111 |
System.out.println("==========================="); |
1112 |
} |
1113 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(layers);
|
1114 |
int pos = 0; |
1115 |
while (lyrIterator.hasNext()) {
|
1116 |
FLayer layer = lyrIterator.next(); |
1117 |
System.out.println(" Layer " + layer.getName()); |
1118 |
Layer3DProps props3D = getLayer3DProps(layer); |
1119 |
System.out.println(" Type " + props3D.getType()); |
1120 |
System.out.println(" Order " + pos); |
1121 |
pos++; |
1122 |
} |
1123 |
} |
1124 |
|
1125 |
/**
|
1126 |
* Crea un nuevo MapContext(3D) a partir del XMLEntity.
|
1127 |
*
|
1128 |
* @param xml
|
1129 |
* XMLEntity
|
1130 |
*
|
1131 |
* @return Nuevo MapContext3D.
|
1132 |
*
|
1133 |
* @throws XMLException
|
1134 |
*/
|
1135 |
public static MapContext createFromXML(XMLEntity xml) throws XMLException { |
1136 |
ViewPort3D vp = (ViewPort3D) ViewPort3D.createFromXML(xml.getChild(0));
|
1137 |
|
1138 |
FLayers layers = new FLayers3D(null, null, vp); |
1139 |
layers.setXMLEntity(xml.getChild(1));
|
1140 |
|
1141 |
MapContext fmap = layers.getMapContext(); |
1142 |
|
1143 |
return fmap;
|
1144 |
} |
1145 |
|
1146 |
public static MapContext createFromXML03(XMLEntity xml) throws XMLException { |
1147 |
ViewPort vp = ViewPort.createFromXML03(xml.getChild(0));
|
1148 |
|
1149 |
FLayers layers = new FLayers3D(null, null, vp); |
1150 |
layers.setXMLEntity03(xml.getChild(1));
|
1151 |
|
1152 |
MapContext fmap = layers.getMapContext(); |
1153 |
|
1154 |
return fmap;
|
1155 |
} |
1156 |
|
1157 |
public GraphicLayer getGraphicsLayer() {
|
1158 |
GraphicLayer3D gra3D = new GraphicLayer3D(_terrainViewer, getTerrain());
|
1159 |
return gra3D;
|
1160 |
} |
1161 |
|
1162 |
public Node getSpecialNode() {
|
1163 |
Node specialNode = null;
|
1164 |
try {
|
1165 |
specialNode = _terrainViewer.getFeature(0);
|
1166 |
} catch (ChildIndexOutOfBoundsExceptions e) {
|
1167 |
_logger.error("Command: " + "Error child index out of bound.", e); |
1168 |
} |
1169 |
return specialNode;
|
1170 |
} |
1171 |
|
1172 |
public ViewPort getViewPort() {
|
1173 |
|
1174 |
ViewPort3D vp = (ViewPort3D) super.getViewPort();
|
1175 |
|
1176 |
if (_canvas3d != null) |
1177 |
vp.setImageSize(new Dimension(_terrainViewer.getWidth(), |
1178 |
_terrainViewer.getHeight())); |
1179 |
vp.setTerrain(this.getTerrain());
|
1180 |
vp.setViewer(this._canvas3d);
|
1181 |
|
1182 |
return vp;
|
1183 |
} |
1184 |
|
1185 |
/**
|
1186 |
* M�todo de conveniencia que se usa provisionalmente para solicitar un
|
1187 |
* refresco de todo lo que dependa del FMap (MapContext). Esto provocar� un
|
1188 |
* evento de cambio de orden de capas que obligar� a redibujar todo lo que
|
1189 |
* depende de FMap (TOC, MapControl, FFrameView, etc).
|
1190 |
*/
|
1191 |
|
1192 |
/*
|
1193 |
* public void invalidate() { ViewPort3D vp = (ViewPort3D)
|
1194 |
* super.getViewPort(); if (vp.getDirty()) { vp.setDirty(false); } FLayer[]
|
1195 |
* selectedExtent = getLayers().getActives();
|
1196 |
*
|
1197 |
* if (selectedExtent.length > 0) { for (int i = 0; i <
|
1198 |
* selectedExtent.length; i++) { if (selectedExtent[0].isAvailable()) {
|
1199 |
*
|
1200 |
* FLayer lyr3D = selectedExtent[i]; Layer3DProps layer3DProps =
|
1201 |
* Layer3DProps .getLayer3DProps(lyr3D);
|
1202 |
*
|
1203 |
* Layer terrainLayer = _terrainFLayerMap.get(lyr3D); float opacity =
|
1204 |
* (((FLyrDefault) lyr3D).getTransparency()) / (float) 255; if()
|
1205 |
* //_terrain.setTextureOpacityLayer(order, opacity); } }
|
1206 |
*
|
1207 |
* } }
|
1208 |
*/
|
1209 |
|
1210 |
public void selectionChanged(SelectionEvent e) { |
1211 |
// TODO Auto-generated method stub
|
1212 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(layers);
|
1213 |
while (lyrIterator.hasNext()) {
|
1214 |
FLayer layer = lyrIterator.next(); |
1215 |
Layer3DProps props3D = getLayer3DProps(layer); |
1216 |
if (props3D.getType() != Layer3DProps.layer3DOSG) {
|
1217 |
if (layer.getClass().equals(FLyrVect.class)) {
|
1218 |
FLyrVect lyr = (FLyrVect) layer; |
1219 |
|
1220 |
FBitSet selection = null;
|
1221 |
try {
|
1222 |
selection = lyr.getRecordset().getSelectionSupport() |
1223 |
.getSelection(); |
1224 |
} catch (ReadDriverException e1) {
|
1225 |
// TODO Auto-generated catch block
|
1226 |
e1.printStackTrace(); |
1227 |
} |
1228 |
|
1229 |
if ((selection.cardinality() == 0) |
1230 |
|| (!(selection.isEmpty()))) { |
1231 |
Layer3DProps props = Layer3DProps |
1232 |
.getLayer3DProps(layer); |
1233 |
refreshLayerInTerrain(props, true);
|
1234 |
if (layer instanceof FLyrVect) { |
1235 |
FLyrVect fvect = (FLyrVect) layer; |
1236 |
props.drawVersion = fvect.getDrawVersion(); |
1237 |
} |
1238 |
} |
1239 |
} |
1240 |
} |
1241 |
} |
1242 |
} |
1243 |
|
1244 |
public void removeLayerToTerrain(FLayer layer) { |
1245 |
// TODO Auto-generated method stub
|
1246 |
Layer3DProps props3D = getLayer3DProps(layer); |
1247 |
|
1248 |
if (props3D.getType() == Layer3DProps.layer3DVector) {
|
1249 |
DeleteVectors(layer, props3D); |
1250 |
} else if (props3D.getType() == Layer3DProps.layer3DOSG) { |
1251 |
DeleteOSGLayer(layer, props3D); |
1252 |
} else {
|
1253 |
Layer terrainLayer = _terrainFLayerMap.get(layer); |
1254 |
if (terrainLayer != null) { |
1255 |
FLayerCacheService cacheService = (FLayerCacheService) props3D.getCacheService(); |
1256 |
if(cacheService != null) |
1257 |
try {
|
1258 |
cacheService.free(); |
1259 |
} catch (Throwable e) { |
1260 |
// TODO Auto-generated catch block
|
1261 |
e.printStackTrace(); |
1262 |
} |
1263 |
_terrainLayerMap.remove(terrainLayer.getLayerID()); |
1264 |
_terrainFLayerMap.remove(layer); |
1265 |
|
1266 |
try {
|
1267 |
_terrainLayerManager.removeLayer(terrainLayer); |
1268 |
} catch (LayerManagementException e) {
|
1269 |
// TODO Auto-generated catch block
|
1270 |
e.printStackTrace(); |
1271 |
} |
1272 |
} |
1273 |
} |
1274 |
|
1275 |
} |
1276 |
|
1277 |
private void DeleteOSGLayer(FLayer layer, Layer3DProps props3D) { |
1278 |
if (_terrain == null || _viewProjection == null) |
1279 |
return;
|
1280 |
|
1281 |
OSGCacheService cacheService = (OSGCacheService) props3D |
1282 |
.getCacheService(); |
1283 |
if (cacheService != null) { |
1284 |
// use VectorCacheService to delete features to terrain
|
1285 |
cacheService.DeleteFeaturesToTerrain(); |
1286 |
} |
1287 |
} |
1288 |
|
1289 |
public boolean isRenewCanvasOff() { |
1290 |
return _renewCanvasOff;
|
1291 |
} |
1292 |
|
1293 |
public void setRenewCanvasOff(boolean renewCanvasOff) { |
1294 |
_renewCanvasOff = renewCanvasOff; |
1295 |
} |
1296 |
|
1297 |
public void drawValueChanged(LayerEvent e) { |
1298 |
FLayer layer = e.getSource(); |
1299 |
|
1300 |
if(layer == null) return; |
1301 |
|
1302 |
if(layer instanceof FLyrRasterSE) |
1303 |
{ |
1304 |
Layer3DProps props3D = getLayer3DProps(layer); |
1305 |
if(props3D != null) |
1306 |
{ |
1307 |
if(props3D.drawVersion < layer.getDrawVersion())
|
1308 |
{ |
1309 |
props3D.drawVersion = layer.getDrawVersion(); |
1310 |
refreshLayerInTerrain(props3D, true);
|
1311 |
} |
1312 |
} |
1313 |
} |
1314 |
} |
1315 |
|
1316 |
|
1317 |
|
1318 |
public UpdateDataEvent loadData(RequestDataEvent rde) {
|
1319 |
|
1320 |
if (_terrainLayerMap.size() == 0) |
1321 |
return null; |
1322 |
if (rde == null) |
1323 |
return null; |
1324 |
if (rde.getExtent() == null) |
1325 |
return null; |
1326 |
if (rde.getLayer() == null) |
1327 |
return null; |
1328 |
if (rde.getLayerManager() == null) |
1329 |
return null; |
1330 |
|
1331 |
UpdateDataEvent ude = new UpdateDataEvent();
|
1332 |
ude.copyDataFromRequest(rde); |
1333 |
|
1334 |
|
1335 |
String layerID = rde.getLayer().getLayerID();
|
1336 |
FLayer layer = (FLayer) _terrainLayerMap.get(layerID); |
1337 |
if (layer == null) |
1338 |
return null; |
1339 |
if (!layer.isVisible())
|
1340 |
return null; |
1341 |
|
1342 |
Layer3DProps props3D = getLayer3DProps(layer); |
1343 |
|
1344 |
// get/create cache service
|
1345 |
FLayerCacheService cacheService = (FLayerCacheService) props3D |
1346 |
.getCacheService(); |
1347 |
if (cacheService == null) { |
1348 |
|
1349 |
cacheService = new FLayerCacheService(_terrain, props3D
|
1350 |
.getCacheName(), layer, _viewProjection); |
1351 |
cacheService.setCacheRootDir(Layer3DProps.m_cacheDir); |
1352 |
props3D.setCacheService(cacheService); |
1353 |
|
1354 |
} |
1355 |
|
1356 |
|
1357 |
|
1358 |
int dataType = props3D.getType();
|
1359 |
Point tileIndices = new Point(rde.getTileX(), rde.getTileY()); |
1360 |
TileNum tileNum = new TileNum(rde.getTileLevel(), tileIndices);
|
1361 |
|
1362 |
String tileFileName = ""; |
1363 |
|
1364 |
double minX, minY, width, height;
|
1365 |
|
1366 |
if (_terrain.getCoordinateSystemType() == CoordinateSystemType.PROJECTED) {
|
1367 |
minX = rde.getExtent().xMin(); |
1368 |
minY = rde.getExtent().yMin(); |
1369 |
width = rde.getExtent().xMax() - rde.getExtent().xMin(); |
1370 |
height = rde.getExtent().yMax() - rde.getExtent().yMin(); |
1371 |
} else {
|
1372 |
minX = Math.toDegrees(rde.getExtent().xMin());
|
1373 |
minY = Math.toDegrees(rde.getExtent().yMin());
|
1374 |
width = Math.toDegrees(rde.getExtent().xMax()
|
1375 |
- rde.getExtent().xMin()); |
1376 |
height = Math.toDegrees(rde.getExtent().yMax()
|
1377 |
- rde.getExtent().yMin()); |
1378 |
} |
1379 |
boolean failData = false; |
1380 |
|
1381 |
Rectangle2D extent = new Rectangle2D.Double(minX, minY, width, height); |
1382 |
if (cacheService.intersectsLayer(extent)) { // extent test |
1383 |
try {
|
1384 |
tileFileName = cacheService.getTileAsFName(tileNum, extent); |
1385 |
if (tileFileName == null) |
1386 |
failData = true;
|
1387 |
} catch (Exception e) { |
1388 |
failData = true;
|
1389 |
NotificationManager.addInfo("Problem reading tile:" + tileNum,
|
1390 |
e); |
1391 |
e.printStackTrace(); |
1392 |
} |
1393 |
|
1394 |
if (failData) {
|
1395 |
return null; |
1396 |
// ude.setFailData();
|
1397 |
} else {
|
1398 |
switch (dataType) {
|
1399 |
case Layer3DProps.layer3DImage:
|
1400 |
ude.setRasterData(tileFileName, "gdal");
|
1401 |
break;
|
1402 |
case Layer3DProps.layer3DElevation:
|
1403 |
ude.setHeightfieldData(tileFileName, "gdal");
|
1404 |
break;
|
1405 |
case Layer3DProps.layer3DVectorMR:
|
1406 |
ude.setVectorData(tileFileName, "osg");
|
1407 |
break;
|
1408 |
} |
1409 |
} |
1410 |
} |
1411 |
|
1412 |
return ude;
|
1413 |
} |
1414 |
} |