# Volume calculation

**URL:** <https://community.cesium.com/t/volume-calculation/7573>\
**Category:** CesiumJS\
**Created:** [October 17, 2018, 12:03pm UTC](https://community.cesium.com/t/volume-calculation/7573 "2018-10-17T12:03:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surajit\_Chowbey1](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Surajit\_Chowbey1](https://community.cesium.com/u/Surajit_Chowbey1)\
**Post date:** [October 17, 2018, 12:03pm UTC](https://community.cesium.com/t/volume-calculation/7573/1 "2018-10-17T12:03:21Z")

</div>

hello,

I want calculate volume of a polygon drawn upon terrain (reference image is attached).

 ![volume.JPG](https://global.discourse-cdn.com/cesium/original/1X/c501eda45e6c9cbfc5d2b09c95a8696226bc8553.jpeg)

Is there is any way to achieve this.

I’m using Cesium 1.50

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [October 18, 2018, 6:59pm UTC](https://community.cesium.com/t/volume-calculation/7573/2 "2018-10-18T18:59:41Z")

</div>

The image you uploaded looks like a flat polygon draped over terrain, so I think there isn’t any volume per se to measure there.

In any case, I don’t believe CesiumJS currently has any built in tools to do that. I was looking at Turfjs ([http://turfjs.org/](http://turfjs.org/)) which has a lot of useful tools but I’m not sure if it has anything for that particular case either.

You might try to compute this yourself by approximating the area and multiplying by the height, perhaps.

---

<div class="post-metadata">

**Author:** ![Hugo\_Mauricio](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/hugo_mauricio/32/3518_2.png) [@Hugo\_Mauricio](https://community.cesium.com/u/Hugo_Mauricio)\
**Post date:** [January 14, 2021, 4:17pm UTC](https://community.cesium.com/t/volume-calculation/7573/3 "2021-01-14T16:17:33Z")

</div>

Hi,

Also interested in this feature.

Are there any updates, since more than two years have passed?

Thanks,

Hugo

---

<div class="post-metadata">

**Author:** ![Alexander\_Johannesen](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/alexander_johannesen/32/2008_2.png) [@Alexander\_Johannesen](https://community.cesium.com/u/Alexander_Johannesen)\
**Post date:** [January 14, 2021, 10:18pm UTC](https://community.cesium.com/t/volume-calculation/7573/4 "2021-01-14T22:18:53Z")

</div>

You can do it by finding all coordinates within the polygon as a grid, then sampleHeightMostDetailed() of all of them (at some resolution of sensibility). There’s a max to min height of volume inside the perimeter (height at your polygon edges). It’s not going to be quick, but it’s certainly possible. I’m sure a bit of searching will get you some options, like this starting point;

> **[Polyhedra Volume Calculations - A JavaScript Implementation](https://www.codeproject.com/articles/13666/polyhedra-volume-calculations-a-javascript-impleme)**
>
> An article presenting a JavaScript implementation for calculating the volume of any polyhedron.

Cheers,

Alex

---

<div class="post-metadata">

**Author:** ![sx\_dev](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/sx_dev/32/3476_2.png) [@sx\_dev](https://community.cesium.com/u/sx_dev)\
**Post date:** [September 9, 2021, 8:04am UTC](https://community.cesium.com/t/volume-calculation/7573/5 "2021-09-09T08:04:22Z")

</div>

Hi @Alexander_Johannesen ,

How do you actually find the coordinates within the polygon as a grid?

Cheers,  
Hiroshi

---

<div class="post-metadata">

**Author:** ![Alexander\_Johannesen](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/alexander_johannesen/32/2008_2.png) [@Alexander\_Johannesen](https://community.cesium.com/u/Alexander_Johannesen)\
**Post date:** [September 9, 2021, 11:05am UTC](https://community.cesium.com/t/volume-calculation/7573/6 "2021-09-09T11:05:20Z")

</div>

Hiya,

Plenty of ways (there’s even Cesium intersection functions you can use), but I’ll pop here a simple implementation (public implementation) where we assume two arrays of a polygons lat and long values (where the index of each is of course the same coordinate), so our polygon is something like;

```auto
var longitudes = [10, 12, 14, 12, 11];
var latitudes = [34, 35, 36, 34, 33];

```

With the following function you can loop your points to see if they intersect (are inside) the above polygon;

```auto
var points = [{longitude:11,latitude:34}, {longitude:13,latitude:35}];

points.forEach ( point => {
   if ( pointInsidePolygon( point, longitudes, latitudes ) {
      console.log('!! point is inside!', point );
   } else {
      console.log('point is outside',point );
   }
});

pointInsidePolygon(point, vX, vY) {
    // ray-casting algorithm http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
    var x = point.longitude, y = point.latitude;
    var inside = false;
    for (var i = 0, j = vX.length - 1; i < vX.length; j = i++) {
      var xi = vX[i], yi = vY[i];
      var xj = vX[j], yj = vY[j];

      var intersect = ((yi >= y) != (yj >= y))
        && (x <= (xj - xi) * (y - yi) / (yj - yi) + xi);
      if (intersect) {
        inside = !inside;
      }
    }

    return inside;
  }

```

That should be more than plenty to get you on your way.

Cheers,

Alex
