1

I'm trying to apply the ndvi function on Google Earth Engine but it shows up the following error:

ndvi is not defined

This is what I have written:

var L5 = ee.ImageCollection("LANDSAT/LT05/C01/T1_TOA"),
var geometry = ee.Geometry.Polygon
var visL5 = B3,B2 and B1 from 0.031541384756565094 to 0.7842386960983276

var imagen = L5.filterBounds(geometry)
               .filterDate('2010-12-01','2011-03-31')
               .filterMetadata('CLOUD_COVER','less_than', 8);

var addNDVI = function(imagen) {   
var ndvi = imagen.normalizedDifference(['B4', 'B3'])
                 .rename('NDVI');
    return imagen.addBands(ndvi); };

Map.addLayer(ndvi,{},'NDVI'); --> here is where the error is ("ndvi is not defined").

Can you help me?

1
  • You need to map the addNDVI function to the image collection stored in image. You might want to read about the scope of the variables. Commented Dec 20, 2021 at 16:07

1 Answer 1

1

As Jhonathan said, you need to map the collection. So far, ndvi is a function variable, not a global variable. With map(), the function iterates the whole collection adding NDVI:

var addNDVI = function(imagen) {   
var ndvi = imagen.normalizedDifference(['B4', 'B3'])
                 .rename('NDVI');
    return imagen.addBands(ndvi); };

var imagen_ndvi = imagen.map(addNDVI)
Map.addLayer(imagen_ndvi,{bands: ['NDVI'],min: -1, max: 1, },'NDVI'); 

Also, I set some configuration parameters for you

By the way, I strongly recommend use another objectname here:

var imagen = L5.filterBounds(geometry)
               .filterDate('2010-12-01','2011-03-31')
               .filterMetadata('CLOUD_COVER','less_than', 8);

Since imagen is defined as a collection and a function variable, use other names, such as: col, collection, coleccion, whatever you want.

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.