2

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);

2
  • Your issue is probably related to your ee.FeatureCollection("users/GISSpecialist/tza"). Please, see my answer.
    – xunilk
    Commented 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.
    – ahmed
    Commented Jan 10, 2021 at 7:10

1 Answer 1

2

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"); 

// 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);
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
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);

Link to script: https://code.earthengine.google.com/007559e5d8c55bf64a39acfb6bc7f88f

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

enter image description here

1
  • 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.
    – ahmed
    Commented Jan 10, 2021 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.