# Calculating the Area of a Raster/Vector in Google Earth Engine

How could we calculate the area of a vector in Google Earth Engine? It seems to be quite simple for polygon features, but I could not find a way for doing it for a vector object.

First, we obtain the image:

``````var s1 = ee.Image('COPERNICUS/S2/20160422T084804_20160422T123809_T36TVK')
``````

Then, we clip it to focus on our region of interest (water dam) based on a given polygon (`dam`) to reduce the analysis time:

``````var s1_clipped = s1.clip(dam)
``````

We determine the water pixels:

``````var w1 =  s1_clipped.select('B8').lte(threshold)
``````

Now we can calculate the number of pixels in this new variable:

``````var stats = w1.reduceRegion(ee.Reducer.sum());
``````

However, we further process to get a vector representation:

``````var v1 = w1.addBands(w1).reduceToVectors({
crs: s1.select('B8').projection(),
scale: s1.select('B8').projection().nominalScale(),
geometryType: 'polygon',
eightConnected: false,
labelProperty: 'zone',
reducer: ee.Reducer.mean()
});
``````

We need to calculate the area we think belonging to water pixels. How could we do it either in the raster (through the variable `w1`) or vector (through the variable `v1`) objects?

## 2 Answers

``````// I made up this threshold, use yours
var area_threshold = 20000

var v1 = w1.addBands(area_w1).reduceToVectors({
crs: s1.select('B8').projection(),
scale: s1.select('B8').projection().nominalScale(),
geometryType: 'polygon',
eightConnected: false,
labelProperty: 'zone',
reducer: ee.Reducer.sum()
}).filterMetadata("sum","greater_than", area_threshold)

var area = ee.Number(v1.aggregate_sum("sum"))
print("area greater than x (m2):", area)
print("area greater than x (ha):", area.divide(10000))
``````

I made up a polygon and a threshold just to use as example, use yours:

``````var s1 = ee.Image('COPERNICUS/S2/20160422T084804_20160422T123809_T36TVK')
var s1_clipped = s1.clip(dam)

// I made up the threshold (use yours)
var threshold = 2000

var w1 =  s1_clipped.select('B8').lte(threshold)

// Sentinel's nir band has 10m of spatial resolution
var pixel_area = ee.Image.constant(100)

var area_w1 = w1.multiply(pixel_area)

// Visualize area layer
Map.addLayer(area_w1)

var total_area = area_w1.reduceRegion({
reducer: ee.Reducer.sum(),
geometry: dam,
scale: 10
}).get("B8")

print("area greater than threshold: (m2)", total_area)
print("area greater than threshold: (ha)", ee.Number(total_area).divide(10000))
``````

I would'n use the vector approach, it is much more computation for getting the same result I think.

• I would still appreciate if you could let me know whether or not we could find the area using the vector representation. The reason why we are relying on vector data is to focus on the dam and ignore the small water bodies around the dam. As far as I understand, the raster approach includes those small water bodies. Jul 17, 2017 at 14:12