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");
// Step 1 - Loading the images using filters
var filtered = S2
.filterDate('2020-01-01','2020-12-31')
.filterBounds(region)
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than',10)
.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
var NDVI = NIR.subtract(RED).divide(NIR.add(RED));
// Clip NDVI Map to Egypt boundaries
var NDVIc = NDVI.clip(region);
// Step 3 - Extract Vegetation
// Select NDVI cells with greater than 0.3 value
NDVIc = ee.Image(1).mask(NDVIc.gte(0.3));
// Display vegetation cells extracted from NDVI Map
Map.addLayer(NDVIc);
// 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);
ee.FeatureCollection("users/GISSpecialist/tza")
. Please, see my answer.