0

I want to apply the following condition statement to all the pixels of an indices image having only 1 band. The condition is as follows-

Condition I want to apply

For that first I have calculated the mean value of my image. Next I have printed the bands existing and then used the expression by substituting the band values.

var mean = MNDWI.reduceRegion(ee.Reducer.mean(),clip_layer,28);
print('mean', mean)

var band_names = MNDWI.bandNames();
print(band_names)

var index = MNDWI

var recode = ee.Image().expression(
'(mean > 0 and index >= mean) or ' +
'(mean <= 0 and index >= (mean**5 - 0.02)) ' +
'? 254 : 0', {
mean: mean, 
index: index
})

Map.addLayer(recode)

Here (MNDWI) represents the index, and "B3" is the single band that index is having which can be observed in the print statement output.

Print output values

But I am getting this error-

Layer error: Image.gt, argument 'image1': Invalid type. Expected type: Image. Actual type: Dictionary. Actual value: {B3=-0.14653535282222163}

Error image

0

2 Answers 2

1

The error message is almost certainly accurate. MBUI probably doesn't have a B3 band. But your code isn't complete enough to say.

As for the expression, to do power, you don't use ^ but **. It also doesn't correctly implement your specified condition. You could perhaps implement it like this:

var mean = ee.Image(0)
var index = ee.Image(0)

var recode = ee.Image().expression(
  '(mean > 0 and index >= mean) or ' +
  '(mean <= 0 and index >= (mean**5 - 0.02)) ' +
  '? 254 : 0', {
  mean: mean, 
  index: index
})

Map.addLayer(recode)

https://code.earthengine.google.com/dd2052391c15a82c4d9d53cc9c1797fc

5
  • I have updated my code as per your suggestion still It fails. Although with a different error this time. Jul 27, 2022 at 10:55
  • It helps if you specify which error message. At a glance, this is wrong: var index = ee.Image(MNDWI[0]). MNDWI is a server-side list of band names. You use get() to access elements in a server-side list. Secondly, you don't want a band name at all there. I don't know which bands MNDWI contains, but I assume it's only one. If that's the case, just do var index = MNDWI. Jul 27, 2022 at 11:35
  • yes I have updated the error message also in my question. Second yes MNDWI has 1 single band which is named as B3 there in the print statement image in the question. Third I tried var index = MNDWI, also gives an error. Please help. Jul 27, 2022 at 13:26
  • Try to cast both your images to float: image.float() Jul 27, 2022 at 13:48
  • Can you plase show how to convert to float and do the expression because whenever I am trying it is showing the same error. I tried to use this function developers.google.com/earth-engine/apidocs/ee-image-float Jul 28, 2022 at 5:47
0

The issue was in conversion to float values.

var mean = MBUI.reduceRegion(ee.Reducer.mean(),clip_layer,28); print('mean', mean)

var mean_Img = mean.toImage();

var band_names = MBUI.bandNames(); print(band_names)

var index = MBUI.float()

var recode = ee.Image().expression( '(mean > 0 and index >= mean) or ' + '(mean <= 0 and index >= (mean**5 - 0.02)) ' + '? 254 : 0', { mean: mean_Img, index: index })

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.