0

i want to conduct supervised classification, but i intended to calculate NDVI, NDWI, and NDBI indices prio to classification using the code below;

Map.addLayer(hjrb);
var image = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
.filterDate("2014-01-01", "2015-12-31")
.filterMetadata("CLOUD_COVER", "less_than", 1)
.filterBounds(hjrb)
.mean();
var visParamfalse = {bands: ["SR_B3","SR_B4","SR_B5"]};
Map.addLayer (image.clip(hjrb), visParamfalse, 'study2015');
Map.centerObject(hjrb, 8);

//indices
var addIndices = function(image){
  var gndvi = image.normalizedDifference(['SR_B5', 'SR_B3']).rename(['gndvi']);
  var ndbi = image.normalizedDifference(['SR_B5', 'SR_B4']).rename(['ndbi']);
  var ndwi = image.normalizedDifference(['SR_B3', 'SR_B5']).rename(['ndwi']);

  return image.addBands(gndvi).addBands(ndbi).addBands(ndwi);
  };

var composite = addIndices (composite);

after running this code, i got this error "Cannot read property 'normalizedDifference' of undefined"

undefined" but i remove var composite = addIndices (composite); the code runs successfully. please kindly help me through.

  • well, yes, if you don't call a function or method, the errors in that method won't cause trouble. apparently 'composite' is not defined yet when you add that line, which would be odd, since you only define it there. – Stultuske Jun 02 '23 at 09:02
  • thank you, I corrected it and it run successfully – idris wada Jun 02 '23 at 09:25
  • it might be helpful for the next person to run into this, if you edit your question to show the solution you've implemented, or add it as an answer and accept it – Stultuske Jun 02 '23 at 09:54

0 Answers0