Camera fly to rectangle on terrain

Hello,

I'm having trouble with moving camera to show some rectangle. I have rectangle/polygon coordinates and would like to move camera so that it shows the entire rectangle on screen. Consider the following sandcastle example:

var viewer = new Cesium.Viewer('cesiumContainer');

Sandcastle.addToolbarButton('Fly to', function(){
    var rectangle = new Cesium.Rectangle(0.1485961502596944, 0.823228424579245, 0.1486927730733333, 0.8232981854516427);

    viewer.camera.flyTo({
        destination: rectangle
    });

});

It works well on WGS84 ellipsoid (pressing the button zooms to target rectagle), but when I switch to STK World terrain mesh the camera crashes through the ground.

Is there a way to display rectangle on terrain when I only know its coordinates and not height?

Hi there,

Fortunately, Cesium has direct support for this! Take a look at this function: http://cesiumjs.org/Cesium/Build/Documentation/Camera.html?classFilter=camera#getRectangleCameraCoordinates

getRecatngleCameraCoordinates will give the camera parameters necessary to view the desired rectangle.

hope that helps,

  • Rachel

Dne středa 5. července 2017 3:21:25 UTC+2 Rachel Hwang napsal(a):

Hi there,

Fortunately, Cesium has direct support for this! Take a look at this function: http://cesiumjs.org/Cesium/Build/Documentation/Camera.html?classFilter=camera#getRectangleCameraCoordinates

getRecatngleCameraCoordinates will give the camera parameters necessary to view the desired rectangle.

hope that helps,
- Rachel

On Tuesday, July 4, 2017 at 8:12:48 AM UTC-4, jan.vy...@gmail.com wrote:Hello,
I'm having trouble with moving camera to show some rectangle. I have rectangle/polygon coordinates and would like to move camera so that it shows the entire rectangle on screen. Consider the following sandcastle example:
var viewer = new Cesium.Viewer('cesiumContainer');
Sandcastle.addToolbarButton('Fly to', function(){
var rectangle = new Cesium.Rectangle(0.1485961502596944, 0.823228424579245, 0.1486927730733333, 0.8232981854516427);
viewer.camera.flyTo({
destination: rectangle
});
});
It works well on WGS84 ellipsoid (pressing the button zooms to target rectagle), but when I switch to STK World terrain mesh the camera crashes through the ground.
Is there a way to display rectangle on terrain when I only know its coordinates and not height?

Hi,

thanks for the reply, but it doesn't seem to work for me, maybe I'm using it wrong? If I try this piece of code in the sandcastle:

var viewer = new Cesium.Viewer('cesiumContainer');

Sandcastle.addToolbarButton('Fly to', function(){
    var rectangle = new Cesium.Rectangle(0.14864258869032076,
                                         0.8232505706843248,
                                         0.14865195136604184,
                                         0.8232689020789328);
    var target = viewer.camera.getRectangleCameraCoordinates(rectangle);
    viewer.camera.flyTo({
        destination: target
    });
});

clicking 'Fly to' button works fine with WGS84 ellipsoid, if I switch on terrain the camera crashesh through the ground.

It seems like the function ignores terrain and resulting camera position ends up being under it (if the rectangle is small enough). My problem is that I only have these four coordinates, I don't know what altitude the terrain around that rectangle is at.

Hi there,

What terrain provider are you using? Here’s a working example: http://cesiumjs.org/Cesium/Apps/Sandcastle/?src=Hello%20World.html&label=Showcases&gist=31851ea4e2e77c8175d924d467a7e96e

You can use sampleTerrainMostDetailed to get the terrain height at a given location, and you can use this to correct the camera position to prevent it from going under the terrain.

Hope that helps,

  • Rachel

Dne středa 5. července 2017 22:24:48 UTC+2 Rachel Hwang napsal(a):

Hi there,

What terrain provider are you using? Here's a working example: http://cesiumjs.org/Cesium/Apps/Sandcastle/?src=Hello%20World.html&label=Showcases&gist=31851ea4e2e77c8175d924d467a7e96e

You can use sampleTerrainMostDetailed to get the terrain height at a given location, and you can use this to correct the camera position to prevent it from going under the terrain.

Hope that helps,
- Rachel

On Wednesday, July 5, 2017 at 10:09:08 AM UTC-4, jan.vy...@gmail.com wrote:Dne středa 5. července 2017 3:21:25 UTC+2 Rachel Hwang napsal(a):

> Hi there,

>

>

> Fortunately, Cesium has direct support for this! Take a look at this function: http://cesiumjs.org/Cesium/Build/Documentation/Camera.html?classFilter=camera#getRectangleCameraCoordinates

>

>

> getRecatngleCameraCoordinates will give the camera parameters necessary to view the desired rectangle.

>

>

> hope that helps,

> - Rachel

>

> On Tuesday, July 4, 2017 at 8:12:48 AM UTC-4, jan.vy...@gmail.com wrote:Hello,

> I'm having trouble with moving camera to show some rectangle. I have rectangle/polygon coordinates and would like to move camera so that it shows the entire rectangle on screen. Consider the following sandcastle example:

> var viewer = new Cesium.Viewer('cesiumContainer');

> Sandcastle.addToolbarButton('Fly to', function(){

> var rectangle = new Cesium.Rectangle(0.1485961502596944, 0.823228424579245, 0.1486927730733333, 0.8232981854516427);

> viewer.camera.flyTo({

> destination: rectangle

> });

> });

> It works well on WGS84 ellipsoid (pressing the button zooms to target rectagle), but when I switch to STK World terrain mesh the camera crashes through the ground.

> Is there a way to display rectangle on terrain when I only know its coordinates and not height?

Hi,

thanks for the reply, but it doesn't seem to work for me, maybe I'm using it wrong? If I try this piece of code in the sandcastle:

var viewer = new Cesium.Viewer('cesiumContainer');

Sandcastle.addToolbarButton('Fly to', function(){

var rectangle = new Cesium\.Rectangle\(0\.14864258869032076,

                                     0\.8232505706843248,

                                     0\.14865195136604184,

                                     0\.8232689020789328\);

var target = viewer\.camera\.getRectangleCameraCoordinates\(rectangle\);

viewer\.camera\.flyTo\(\{

    destination: target

\}\);

});

clicking 'Fly to' button works fine with WGS84 ellipsoid, if I switch on terrain the camera crashesh through the ground.

It seems like the function ignores terrain and resulting camera position ends up being under it (if the rectangle is small enough). My problem is that I only have these four coordinates, I don't know what altitude the terrain around that rectangle is at.

Thanks but unfortunately that example doesn't work for me - https://imgur.com/a/prBp0

So if I understand that correctly Camera.flyTo with rectangle won't work with terrain, instead I shoud do:
- query terrain provider for height of some point inside rectangle using sampleTerrainMostDetailed
- get position to view that rectangle using getRectangleCameraCoordinates
- move that poit up by height computed from sampleTerrainMostDetailed

Is there some easy way to move point in ECEF by X meters UP (in a direction perpendicular to ellipsoid) or should I just convert it to local frame, move it up and then transform it back to fixed frame?

Hi there,

You can move the camera up relative to the ground like this: http://cesiumjs.org/Cesium/Apps/Sandcastle/?src=Hello%20World.html&label=Showcases&gist=623217139324f5bc3b1a0e0fa345fdd4

Hope that helps,

  • Rachel