How to get the extent of the current camera position

2,959 views
Skip to first unread message

linde...@gmail.com

unread,
Jun 7, 2013, 1:22:55 PM6/7/13
to cesiu...@googlegroups.com
Is there any way to get the extent of the current camera view? Also, is there any good way to subscribe to an event to know when this changes?

Daniel Bagnell

unread,
Jun 10, 2013, 3:52:33 PM6/10/13
to cesiu...@googlegroups.com, linde...@gmail.com
There is no existing code to get the extent for the current camera view. I can post some code for you, but it would only work for top down views. Is that OK?

Currently, there are no events when the camera is changed, but that may be added in the future. You could check to see if it changed in the render loop.

linde...@gmail.com

unread,
Jun 10, 2013, 3:57:06 PM6/10/13
to cesiu...@googlegroups.com, linde...@gmail.com
I'm not sure if it would be helpful or not, but I'll take any help you're willing to offer. What I'm trying to write some code that would make a call to a WFS server and I need to be able to pass it the bounding box for what data I want back. I'm guessing this would be somewhat similar to how it's done for WMS, but I'm not positive.

smith....@gmail.com

unread,
Oct 23, 2013, 3:47:01 PM10/23/13
to cesiu...@googlegroups.com, linde...@gmail.com
Is there an update on this? I need to do something similar. I'm trying to fetch an export map function from an ArcGIS mapservice, and need a bounding box.

linde...@gmail.com

unread,
Oct 25, 2013, 8:38:40 AM10/25/13
to cesiu...@googlegroups.com, linde...@gmail.com, smith....@gmail.com
On Wednesday, October 23, 2013 3:47:01 PM UTC-4, smith....@gmail.com wrote:
> Is there an update on this? I need to do something similar. I'm trying to fetch an export map function from an ArcGIS mapservice, and need a bounding box.

Here's some code I use that does a "pretty good" job of solving this problem. This is borrowed heavily from this forum post: http://cesium.agi.com/forum.html?place=msg%2Fcesium-dev%2FjYlLEnyv7lM%2FwUKX0EKUqQQJ.

var c2 = new Cesium.Cartesian2(0, 0);
var leftTop = scene.getCamera().controller.pickEllipsoid(c2, ellipsoid);
c2 = new Cesium.Cartesian2(scene.getCanvas().width, scene.getCanvas().height);
var rightDown = scene.getCamera().controller.pickEllipsoid(c2, ellipsoid);

if (leftTop != null && rightDown != null) { //ignore jslint
leftTop = ellipsoid.cartesianToCartographic(leftTop);
rightDown = ellipsoid.cartesianToCartographic(rightDown);
return new Cesium.Extent(leftTop.longitude, rightDown.latitude, rightDown.longitude, leftTop.latitude);
} else {
//The sky is visible in 3D
return null;
}

As the code comment mentions this does not work if the sky is in view and it may not work for other situations as well so use at your own risk and make sure to test heavily before using in production.

raghu....@gmail.com

unread,
Oct 9, 2015, 2:20:28 PM10/9/15
to cesium-dev, linde...@gmail.com, smith....@gmail.com
Hi All,

As per my requirement i have to show the placemarks with in the bounding box. The solution for the given by you for earth covering all 4 corners(sky is not in view) is working fine.. but when i tilting or rotating the map in same area where i can see all placemarks i could not get the correct bounding box values as i could see only few placemarks there. Does it need to do something with rotate/tilt angle..?

Could you please help me to get bounding box values after tilting/rotating map.

Thanks,
Raghu.

Willem van der Gugten

unread,
Oct 9, 2015, 4:43:40 PM10/9/15
to cesium-dev, linde...@gmail.com, smith....@gmail.com, raghu....@gmail.com
If the map is not North-Up, you'll have to get the coordinates of all four corners, so also leftBottom and rightTop.
You can then use Rectangle.fromCartographicArray() to have Cesium sort out the box that contains the map area.
This box will be North-Up, so it will cover a larger area than what you have on screen. Whether that is a problem depends on your use-case.

raghu....@gmail.com

unread,
Oct 10, 2015, 9:47:13 AM10/10/15
to cesium-dev, linde...@gmail.com, smith....@gmail.com, raghu....@gmail.com
Thanks Willem..

I tried earlier calculating coordinates of four corners but not used Rectangle.fromCartographicArray(). Now i could see them and also observed getting some other placemarks data also as you said it covers larger area.

Could you please suggest me any work around for getting bbox when sky is in view/visible..?

Regards,
Raghu.

Willem van der Gugten

unread,
Oct 12, 2015, 2:34:41 PM10/12/15
to cesium-dev, linde...@gmail.com, smith....@gmail.com, raghu....@gmail.com
Hi Raghu,

Sorry, I have no idea how to handle the case when the sky is visible. Not something trivial I'm afraid.

Mark Erikson

unread,
Oct 12, 2015, 2:55:27 PM10/12/15
to cesium-dev, linde...@gmail.com, smith....@gmail.com, raghu....@gmail.com
I actually wrote some code a while back to do this.  It uses the Bresenham algorithm to walk the pixels from upper-left to center, and lower-right to center.  It does a pickEllipsoid() at each screen pixel location, and if it finds a valid pixel on both the UL line and LR line, uses those to do screen -> Cartesian -> Cartographic -> Rectangle conversion.

Raghavendra Burgala

unread,
Oct 13, 2015, 2:29:27 PM10/13/15
to Mark Erikson, wil...@oculustechnologies.nl, cesium-dev, linde...@gmail.com, smith....@gmail.com
Thanks Mark Erikson.. for suggesting Bresenham algorithm. I used the answer posted here http://stackoverflow.com/questions/4672279/bresenham-algorithm-in-javascript to get the corners of map even sky is in view.. hope i am getting right values..Thanks..

Willem / Mark Erikson, as you said rectangle covering a little bit large area what we have in screen.. so can we have any solution to overcome this problem.. like subtracting any offset value from the coordinates..?

Thanks,
Raghu.

Premkumar Jayaseelan

unread,
Nov 16, 2015, 2:32:39 AM11/16/15
to cesium-dev, mark.e...@gmail.com, wil...@oculustechnologies.nl, linde...@gmail.com, smith....@gmail.com, raghu....@gmail.com
Hi Raghu,

I'm also working on to get the camera position (Top left, Top right, Bottom left, Bottom right) on the screen. I'm able to get the position when the sky is not on the scene. If the sky is visible it fails to get the position. 

Can you please share the code to handle this using Bresenham Algorithm (as mentioned in the above comment). I've tried the code, but I', getting error while initializing the new Coordinates. e.g. new Coordinates(0,0) => Error: Uncaught ReferenceError: Coordinates is not defined.

Appreciate your support.

Thanks,
Prem.

Raghavendra Burgala

unread,
Nov 16, 2015, 2:52:43 AM11/16/15
to Premkumar Jayaseelan, cesium-dev, Mark Erikson, wil...@oculustechnologies.nl, linde...@gmail.com, Todd Smith
Hi Prem,

I used the following code to traverse from canvas corners. For example calculating leftTop, traversing from lefttop to rightdown, i have given findCoordinate(new Cesium.Cartesian2(0,0), new Cesium.Cartesian2(canvasWidth, canvasHeight)), same way you can calculate other corners by providing appropraite values. Hope it helps you.

function (startCoordinates, endCoordinates) {
var coordinate = scene.camera.pickEllipsoid(startCoordinates, this.ellipsoid);
   // Translate coordinates
   var x1 = startCoordinates.x;
   var y1 = startCoordinates.y;
   var x2 = endCoordinates.x;
   var y2 = endCoordinates.y;
   // Define differences and error check
   var dx = Math.abs(x2 - x1);
   var dy = Math.abs(y2 - y1);
   var sx = (x1 < x2) ? 1 : -1;
   var sy = (y1 < y2) ? 1 : -1;
   var err = dx - dy;
   
   coordinate = scene.camera.pickEllipsoid({x:x1, y:y1}, this.ellipsoid);
   if(coordinate) {
    return coordinate;
   }
   
   // Main loop
   while (!((x1 == x2) && (y1 == y2))) {
     var e2 = err << 1;
     if (e2 > -dy) {
       err -= dy;
       x1 += sx;
     }
     if (e2 < dx) {
       err += dx;
       y1 += sy;
     }
     
     coordinate = scene.camera.pickEllipsoid({x:x1, y:y1}, this.ellipsoid);
if(coordinate) {
return coordinate;
}
   }
   return;
},



Thanks,
Raghu.

Premkumar Jayaseelan

unread,
Nov 16, 2015, 5:40:49 AM11/16/15
to cesium-dev, vlrp...@gmail.com, mark.e...@gmail.com, wil...@oculustechnologies.nl, linde...@gmail.com, smith....@gmail.com, raghu....@gmail.com
Hello Raghu,

Thank you for sharing the code. 
Reply all
Reply to author
Forward
0 new messages