# Calculating NDVI vegetation area in GEE generates error

I'm trying to calculate & extract the vegetation areas country wide (TZA) using Sentinel-2 imagery, and getting an error when trying to print that area. I'm listing the code below.

``````    var region = ee.FeatureCollection("users/GISSpecialist/tza");
var S2 = ee.ImageCollection("COPERNICUS/S2_SR");

var filtered = S2
.filterDate('2020-01-01','2020-12-31')
.filterBounds(region)
.median();

//Locate center and zoom level
Map.centerObject(region, 6.5);

// Step 2 - Calculating the NDVI
// Identify the values of NIR & RED
var NIR = filtered.select('B8');
var RED = filtered.select('B4');

// Calculate NDVI

// Clip NDVI Map to Egypt boundaries
var NDVIc = NDVI.clip(region);

// Step 3 - Extract Vegetation
// Select NDVI cells with greater than 0.3 value

// Display vegetation cells extracted from NDVI Map

// Step 4 - Calculate area of extracted cells

var area_pxa = NDVIc.multiply(ee.Image.pixelArea())
.reduceRegion(ee.Reducer.sum(),region,10,null,null,false, 1e30)
.get('constant')
;

area_pxa = ee.Number(area_pxa).divide(1e6);
print ('Area using ee.Image.pixelArea (km²)', area_pxa);

``````
• Your issue is probably related to your `ee.FeatureCollection("users/GISSpecialist/tza")`. Please, see my answer. Jan 9, 2021 at 13:57
• Thank you for taking the time to answer my question, however, I believe the issue here is the size of the region variable. If you enlarge your AOI (region variable) by drawing a rectangle around the whole country, you will see the error. I should have mentioned that in my question. I tried my code on smaller Region, and it worked. My question is more like is there a way around running this same calculation for larger regions such as entire countries. Jan 10, 2021 at 7:10

Your issue is probably related to your `ee.FeatureCollection("users/GISSpecialist/tza")`. I tried your script out with two different FeatureCollection in my assets without any error. As follows, you can observe your script with only two modifications and FeatureCollection in Churachandpur (India).

``````var region = ee.FeatureCollection("users/joseguerreroa/clima/Churachandpur");
var S2 = ee.ImageCollection("COPERNICUS/S2_SR");

var filtered = S2
.filterDate('2020-01-01','2020-12-31')
.filterBounds(region)
.median();

//Locate center and zoom level
//Map.centerObject(region, 6.5);
Map.centerObject(region, 9);

// Step 2 - Calculating the NDVI
// Identify the values of NIR & RED
var NIR = filtered.select('B8');
var RED = filtered.select('B4');

// Calculate NDVI

// Clip NDVI Map to Egypt boundaries
var NDVIc = NDVI.clip(region);

// Step 3 - Extract Vegetation
// Select NDVI cells with greater than 0.3 value

// Display vegetation cells extracted from NDVI Map

// Step 4 - Calculate area of extracted cells

var area_pxa = NDVIc.multiply(ee.Image.pixelArea())
.reduceRegion(ee.Reducer.sum(),region,10,null,null,false, 1e30)
.get('constant');

area_pxa = ee.Number(area_pxa).divide(1e6);
print ('Area using ee.Image.pixelArea (km²)', area_pxa);
``````

After running it, you can observe in following image that area is printed without any error.

• Thank you for taking the time to answer my question, however, I believe the issue here is the size of the region variable. If you enlarge your AOI (region variable) by drawing a rectangle around the whole country, you will see the error. I should have mentioned that in my question. I tried my code on smaller Region, and it worked. My question is more like is there a way around running this same calculation for larger regions such as entire countries. Jan 10, 2021 at 7:01