1

I am attempting to do a means of NDVIS and a means of Thermal thermal for Summer (July &August) 2016.

When I run the annotated code below the error I get in the side console panel is: Use print (...) to write to this console. And in the red box right below: "image" is not defined in this scope.

What's wrong with my code?

//specify a portion of Landsat archive to work with as image collection
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
  .filterDate('2016-07-01','2016-08-31')
  .filterBounds(ee.Geometry.Point(-95.32287567601213,29.79395929049292));

//add a function for NDVI calculation & apply it to the collection using collection.map():
var addNDVI = function(image) {
  return image.addBands(image.normalizedDifference(['B4', 'B3']));
};
var ndviCollection = collection.map(addNDVI);

//add a function for NDVI calculation & apply it to the collection using collection.map():
var addIR = function(image) {
  return image.addBands(image.normalizedDifference(['B10', 'B11']));
};
var ndviCollection = collection.map(addIR);

//normalize difference
var addNDVI = image.normalizedDifference(['B4', 'B3']);
var addNDVI = image.normalizedDifference(['B10', 'B11']);

//calculate mean value of each band from the collection as a new multi-band image
var mean = ndviCollection.reduce(ee.Reducer.mean());

//select the mean NDVI band and Thermal band from this reduced image to visualize and display:
var vizParams = {bands: ['nd_mean'], min: -0.5, max: 1, palette: ['FF0000', '00FF00']};
Map.setCenter(-95.32287567601213,29.79395929049292);
Map.addLayer(mean, vizParams, 'NDVI');

Map.addLayer(addIR,{min:0,max:700}, 'IR');

1 Answer 1

1

The first part is fine, although I would rename the NDVI band..

//specify a portion of Landsat archive to work with as image collection
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
  .filterDate('2016-07-01','2016-08-31')
  .filterBounds(ee.Geometry.Point(-95.32287567601213,29.79395929049292));

//add a function for NDVI calculation & apply it to the collection using collection.map():
var addNDVI = function(image) {      
  // I'd rename the NDVI band to be more specific
  return image.addBands(image.normalizedDifference(['B4', 'B3']).rename('NDVI'));
};
var ndviCollection = collection.map(addNDVI);

Before computing IR, notice that redefining the ndviCollection variable, you loose the NDVI computation. You have two options:

  1. Make a new irCollection and have two collections at the end, or
  2. include the IR band in the images of ndviCollection (I'll show you this option)
//add a function for IR calculation & apply it to the collection using collection.map():
var addIR = function(image) {
  return image.addBands(image.normalizedDifference(['B10', 'B11']).rename('IR')); // also renames IR band
};
ndviCollection = ndviCollection.map(addIR);

The following part of the code is what's making the error come up:

//normalize difference
var addNDVI = image.normalizedDifference(['B4', 'B3']);
var addNDVI = image.normalizedDifference(['B10', 'B11']);

because you are working with collections, not with an image, in fact, the log says there is no image variable (you haven't define image anywhere), just erase that portion.. then

//calculate mean value of each band from the collection as a new multi-band image
var mean = ndviCollection.reduce(ee.Reducer.mean());

//select the mean NDVI band and Thermal band from this reduced image to visualize and display:
var vizParams = {bands: ['NDVI_mean'], min: -0.5, max: 1, palette: ['FF0000', '00FF00']};
Map.setCenter(-95.32287567601213,29.79395929049292);
// visualize NDVI band
Map.addLayer(mean, vizParams, 'NDVI');

And the last issue in the code is that you are trying to visualize a function (addIR), not an image, and also the range you are using for stretching is irrelevant, change that line for

Map.addLayer(mean, {bands:['IR_mean'], min:0,max:0.01}, 'IR');

complete code here

0

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.