#### Tutorials/Post - Remote Sensing, GIS, Earth System, Geo-AI/ML

There are a couple of ways to calculate the area of the image in the Google Earth Engine. The full implementation of both method can be accessed using this link.

1. Pixel Count Method
We can calculate the area of the image by counting the total number of unmasked pixels in that image. Then, multiply the total number of unmasked pixels by the scale factor to get the area in square meters. An implementation for this is shown below.

```var countries = ee.FeatureCollection('ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw');
var Nepal = countries.filterMetadata('Country', 'equals', 'Nepal');

var testAreaImage = ee.Image(1).clip(Nepal);
var scaleforTestArea = 30;

var reducer = testAreaImage.reduceRegion({
reducer: ee.Reducer.sum(),
geometry: Nepal,
crs: 'EPSG:32645', // WGS Zone N 45
scale: scaleforTestArea,
maxPixels: 1E13
});
// km square
var area = ee.Number(reducer.get('constant')).multiply(scaleforTestArea).multiply(scaleforTestArea).divide(1000000);
// gives an area of 147502.63 km2
print('area of Nepal using pixel count method: ', area.getInfo() + ' km2');
```
2. Pixel Area Method (Recommended)
The other method to calculate the area in the Google Earth Engine is to calculate the area of each unmasked pixel. Then, get the sum of all the pixel areas to calculate the area of the image. An implementation for the same area as above is shown below. Clearly, one can see that the second method works better than the first method.

```var countries = ee.FeatureCollection('ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw');
var Nepal = countries.filterMetadata('Country', 'equals', 'Nepal');
var scaleforTestArea = 30;

// km square
var img = ee.Image.pixelArea().divide(1000000);

var area2 = img.reduceRegion({
reducer: ee.Reducer.sum(),
geometry: Nepal,
crs: 'EPSG:32645', // WGS Zone N 45
scale: scaleforTestArea,
maxPixels: 1E13
});
// gives an area of 147134.49 km2
print('area of Nepal using pixel area method: ', ee.Number(area2.get('area')).getInfo() + ' km2');
```

Previous

Next

1. #### omer

Dear sir,

This is very informative. I didnt understand why u divide it by 1000000.

• #### biplovbhandari

To convert it to km2.

• #### omer

Dear Biplovbhandari sir,
i want to calculate the ndvi area, can u please check my code, i got error.

var dataset = ee.Image(‘MODIS/MCD43A4_006_NDVI/2018_05_14’);
var colorized=dataset.select(‘NDVI’).clip(SA);
var scale = 30;
var colorizedVis = {
min: 0.0,
max: 1.0,
palette: [
‘FFFFFF’, ‘CE7E45’, ‘DF923D’, ‘F1B555’, ‘FCD163′, ’99B718′, ’74A901′,
’66A000’, ‘529400’, ‘3E8601’, ‘207401’, ‘056201’, ‘004C00’, ‘023B01’,
‘012E01’, ‘011D01’, ‘011301’
],
};
Map.setCenter(-7.03125, 31.0529339857, 2);
var reducer = colorized.reduceRegion({
reducer: ee.Reducer.sum(),
geometry: SA,
scale: 30,
maxPixels: 1E13
});
// km square
var area = ee.Number(reducer.get(‘SA’)).multiply(scale).multiply(scale).divide(1000000);
// gives an area of 147502.63 km2
print(‘area of Riyadh: ‘, area.getInfo() + ‘ km2’);

error Is:
Dictionary.get: Dictionary does not contain key: SA.

u can check this code on ur area of interest

2. #### omer

sir I need your help to calculate the area :

//………..NDVI……2016…….

var l8 = ee.ImageCollection(‘LANDSAT/LC08/C01/T1_TOA’)
.filterDate(‘2016-01-01’, ‘2016-12-31’)
.filterBounds(SA)
.median();

//var visParams = {bands: [‘B4’, ‘B3’, ‘B2’], max: 0.3};

var extract= l8.clip(SA);

var ndvi= extract.normalizedDifference([‘B5′,’B4’]).rename(‘NDVI’);

var visParam = {
min: -0.2,
max: 0.8,
palette: ‘FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,’ +
‘3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301’
};

//………..Histogram..l……
var histogram= ui.Chart.image.histogram(ndvi,SA,1000);
print(histogram,”Histogram”);

{min: -0.2,
max: 0.8,
palette: ‘FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,’ +
‘3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301′

// Calculate the area
var stats = areaImage.reduceRegion({
reducer: ee.Reducer.sum(),
geometry: SA.geometry(),
scale: 30,
maxPixels: 1e9
});

//print(‘pixels representing ndvi: ‘, stats.get(‘NDVI’), ‘square meters’);
//…..problem is
var area = ee.Number(stats.get(‘NDVI’)).multiply(scale).multiply(scale).divide(1000000);
print(‘area of Riyadh: ‘, stats.getInfo() + ‘ km2’);
print(stats)