1

I am trying to reduce a region by clipping an image from copernicus to get NDVI and then calculate a Kc value. However, Once I use a polygon I imported with the "Draw a Shape" tool, the calculations cannot be performed. I get the same error for all.

Dictionary.toArray: Unable to convert dictionary to array, key 'NDVI' has a null value

Does anyone have an idea why this may be happening?

These are the sources I have in my script: enter image description here

This is the actual script:

var image = ee.Image('COPERNICUS/S2/20170519T173911_20170519T174800_T13RBM');
var NDVI = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
var NDVI_LI = NDVI.clip(Lixin);
var image=image.addBands(NDVI);

var maxNDVI = NDVI.reduceRegion({
  reducer: ee.Reducer.max(),
  geometry: Lixin,
  scale: 10,
  maxPixels: 1e9
});

var minNDVI = NDVI.reduceRegion({
  reducer: ee.Reducer.min(),
  geometry: Lixin,
  scale: 10,
  maxPixels: 1e9
});

var meanNDVI = NDVI.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: Lixin,
  scale: 10,
  maxPixels: 1e9
});

meanNDVI = meanNDVI.toArray();
minNDVI = minNDVI.toArray();
maxNDVI = maxNDVI.toArray();
print('maxNDVI',maxNDVI);
print('minNDVI',minNDVI);
print('meanNDVI',meanNDVI);

var  Kc = ((NDVI_LI.subtract(minNDVI)).divide((maxNDVI).subtract(minNDVI))).multiply(1.20);
var Kc = Kc.arrayGet([0]);
var image = image.add(Kc);
Map.centerObject(Lixin,15);

And these are the errors I'm getting in the console: enter image description here

1

reduceRegion function returns a dictionary object and not a numeric value. The problem with the code is that you are using the dictionaries to add and multiply, not the values contained in them. It can be fixed by extracting those values to the minNDVI, meanNDVI and maxNDVI objects and converting them to ee.Number objects.

For example,

var meanNDVI = ee.Number(NDVI.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: Lixin,
  scale: 10,
  maxPixels: 1e9
}).get('NDVI'));

This way you won't have to convert them to arrays either.

You can see a working version of the script here. Although, i did create my own geometry but yours should work the same way.

  • Thank you. This worked well. – cdcerv Jun 27 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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