0

The code use for calculate annual EVI vegetation index in Google Earth Engine but there's a problem not define getEVI8

// Define Study Area -- MainProject
var west = 21.691531038847305;
var east = 21.691531038847305; //-103;
var north = 30.563767723564865;
var south = 30.563767723564865;
var resolution = 30;
var projection = "EPSG:5070"; //" "EPSG:4269";                   // max 65336
var MainProject = ee.Geometry.Rectangle(west,north,east,south);
Map.addLayer(MainProject);
Map.setCenter(30.563767723564865, 21.691531038847305, 8);

//SATURATES MASK (L5/7)
var maskSaturate57 = function(image){
image = image.select('B1','B3', 'B4');
var mask = image.lt(10000).and(image.gte(0));
return image.updateMask(mask);
};

//SATURATES MASK (L8)
var maskSaturate8 = function(image){
image = image.select('B2','B4', 'B5');
var mask = image.lt(10000).and(image.gte(0));
return image.updateMask(mask);
};

//PIXEL MASK (L5/L7) 
var maskPixels57 = function(image){ 
  var pixel_qa = image.select('pixel_qa');
  var mask = pixel_qa.eq(66);
return image.updateMask(mask); };

//PIXEL MASK (L8) 
var maskPixels8 = function(image){
  var pixel_qa = image.select('pixel_qa');
  var mask = pixel_qa.eq(322);
  return image.updateMask(mask);
};

/*For Landsat 5 and 7, the ‘sr_atmos_opacity’ band was used to find and mask hazy pixels.*/

//ATMOS OPACITY MASK (L5/L7) -to get rid of haze
var maskHaze = function(image){ 
  //Select band and multiply by scaling factor 
  var atmos_qa = image.select('sr_atmos_opacity').multiply(0.0010); 
  //Mask for non-hazy pixels and remove fill vals 
  var mask = atmos_qa.lte(0.1).and(atmos_qa.gt(-9.9)); 
  return image.updateMask(mask);
};

/*For Landsat 8’s aerosol band, this function was used to remove pixels labelled as hazy.*/
//AEROSOL MASK (L8)
var maskAerosol = function(image){ 
  var aero_qa = image.select('sr_aerosol'); 
  var mask = aero_qa.neq(194).and(aero_qa.neq(224)).and(aero_qa.neq(160)).and(aero_qa.neq( 130)); 
  return image.updateMask(mask); 
  
};

// EVI Functions
/*These 2 functions are used to calculate the NDVI for Landsat 5/7 and Landsat 8, respectively.*/
// For L5 and L7
var getEVI57 = function(image){
  //Apply saturate function
  image = maskSaturate8(image);
};

var evi = getEVI8.expression(
  '2.5 * ((NIR-RED) / (NIR + 6 * RED - 7.5* BLUE +1))', {
    NIR:getEVI57.select('B4'),
    RED:getEVI57.select('B3'),
    BLUE:getEVI57.select('B1')
    
  });
  //.rename('nd');
//return(evi);

// For L8
var getEVI8 = function(image){
  //Apply saturate function
  image = maskSaturate8(image);
};

var evi = getEVI8.expression(
  '2.5 * ((NIR-RED) / (NIR + 6 * RED - 7.5* BLUE +1))', {
    NIR:getEVI8.select('B5'),
    RED:getEVI8.select('B4'),
    BLUE:getEVI8.select('B2')
  });
  //.rename('nd');
//return(evi); 


// Sensor calibration functions
//Coefficient Function (L5)
var applyCoefficientL5 = function(image){
  var image_adjusted = image.select('evi').multiply(1.036);
  return(image_adjusted); 
  
} 

//Coefficient Function (L8)
var applyCoefficientL8 = function(image){
  var image_adjusted = image.select('evi').multiply(1.0863);
  return(image_adjusted); 
  
}

// Main function
var createEVI_Composite_ = function(){
  //Set year range
  var yearrangeStart = 1985;
  var yearrangeStop = 2019;

// Moving into the loop, the next variables of importance are:
  for(var loopYear = yearrangeStart; loopYear <= yearrangeStop; loopYear +=1){
    //Set years date range
    var start = ee.Date.fromYMD(loopYear, 1, 1);
    var end = ee.Date.fromYMD(loopYear, 12, 31);
  
    //Landsat 8
    var l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR") 
    //Filter AOI (imported at the top) 
    .filterBounds(MainProject) 
    //July and August only 
    .filterDate(start, end) 
    //Filter CLOUD_COVER 
    .filterMetadata('CLOUD_COVER', 'less_than', 80) 
    //Apply Pixel Mask 
    .map(maskPixels8) 
    //Apply Aerosol Mask 
    .map(maskAerosol) 
    //Calculate EVI 
    .map(getEVI8) 
    //Apply Sensor Callibration Coefficient 
    .map(applyCoefficientL8);
    
    //Landsat7
    var l7 = ee.ImageCollection("LANDSAT/LE07/C01/T1_SR") 
    //Filter AOI (imported at the top) 
    .filterBounds(MainProject) 
    //July and August only 
    .filterDate(start, end) 
    //Filter CLOUD_COVER 
    .filterMetadata('CLOUD_COVER', 'less_than', 80) 
    //Apply Pixel Mask 
    .map(maskPixels57)
    //Apply Haze Mask
    .map(maskHaze)
    //Calculate EVI
    .map(getEVI57);
    
    //Landsat5
    var l5 = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
    //Filter AOI (imported at the top)
    .filterBounds(MainProject)
    //July and August only
    .filterDate(start, end)
    //Filter CLOUD_COVER
    .filterMetadata('CLOUD_COVER', 'less_than', 80)
    //Apply Pixel Mask
    .map(maskPixels57)
    //Apply Haze Mask
    .map(maskHaze)
    //Calculate EVI
    .map(getEVI57)
    //Apply Sensor Callibration Coefficient
    .map(applyCoefficientL5);
  
    //Merge collections
    var mergedCollection = ee.ImageCollection(l8.merge(l7).merge(l5)).reduce(ee.Reducer.percentile([90]));
    var finalOutput = mergedCollection.reduce(ee.Reducer.median()).clip(MainProject).rename(loopYear.toString());

    Map.addLayer(finalOutput, {'min': 50,'max': [10000]},'EVI_Composite_'+loopYear, false);
    
    //Generate filename for export
    var filename = ("EVI_Composite_").concat(loopYear.toString());
    
    Export.image.toDrive({
      image: finalOutput,
      description: filename,
      scale: 30,
      region: MainProject,
      crs: projection,
      maxPixels: 800000000
      
    });
  }
};

//-----------------------------------// 
// 5. MAIN FUNCTION EXECUTION // 
//-----------------------------------// 
var comp = createEVIComposite();

1 Answer 1

2

getEVI8 is called before it is defined. Move the following section of code around so that var getEVI8 = function(image){... comes before var evi = getEVI8.expression(.... You are also defining two evi variables using different expressions; you probably intended to name the vars differently(?)

Fix this section of code per above recommendation:

var evi = getEVI8.expression(
  '2.5 * ((NIR-RED) / (NIR + 6 * RED - 7.5* BLUE +1))', {
    NIR:getEVI57.select('B4'),
    RED:getEVI57.select('B3'),
    BLUE:getEVI57.select('B1')
    
  });
  //.rename('nd');
//return(evi);

// For L8
var getEVI8 = function(image){
  //Apply saturate function
  image = maskSaturate8(image);
};

var evi = getEVI8.expression(
  '2.5 * ((NIR-RED) / (NIR + 6 * RED - 7.5* BLUE +1))', {
    NIR:getEVI8.select('B5'),
    RED:getEVI8.select('B4'),
    BLUE:getEVI8.select('B2')
  });
3
  • code.earthengine.google.com/… Jun 26, 2020 at 6:28
  • I try to change it but get another error getEVI57.select is not a function the link code above Jun 26, 2020 at 6:29
  • There are many thing wrong with your script. The getEVI57 function does not return anything. When it is called, you are not providing an image input, you are calling is multiple times when it could be called just once, etc. You script is sort of complex - I recommend going back and learning EE basics and breaking your script into small sections to simplify implementation and debugging. Jun 26, 2020 at 18:17

Your Answer

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

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