0

I calculate the mean ndvi values of the images and extracted it with ee.Number() Algorithms but the type of my variables named sayı_sonrası_2 and sayı_sonrası_3 is an object instead of number. I want them as number because I want to use them in some if functions like sayı_sonrası_2 > sayı_sonrası_3 or something like that. Can I convert it to number version (float, integer... instead of object). Currently my if condition not working well. It has no error message but logically working wrong. Condition not working true.

Part of the code below here with comments that explain how the code works:


//Calculating NDVI images with formulas of ndvi: (NIR-RED)/(NIR+RED)
var nir_sonrası_2 = subset_sonrası_2.select('B8');
var red_sonrası_2 = subset_sonrası_2.select('B4');
var ndvi_sonrası_2 = nir_sonrası_2.subtract(red_sonrası_2).divide(nir_sonrası_2.add(red_sonrası_2)).rename('ndvi_sonrası_2');

var nir_sonrası_3 = subset_sonrası_3.select('B8');
var red_sonrası_3 = subset_sonrası_3.select('B4');
var ndvi_sonrası_3 = nir_sonrası_3.subtract(red_sonrası_3).divide(nir_sonrası_3.add(red_sonrası_3)).rename('ndvi_sonrası_3');

// This part calculating mean ndvi values as object for each Image. 
var c_sonrası_2=ndvi_sonrası_2.reduceRegion({
  reducer:ee.Reducer.mean(),
  geometry:geometry,
  scale:10
});

var c_sonrası_3=ndvi_sonrası_3.reduceRegion({
  reducer:ee.Reducer.mean(),
  geometry:geometry,
  scale:10
});

// This part extracting the number values from an object which named "ndvi_sonrası_2 and ndvi_sonrası_3"
var sayı_sonrası_2 = ee.Number(c_sonrası_2.get('ndvi_sonrası_2'));
var sayı_sonrası_3 = ee.Number(c_sonrası_2.get('ndvi_sonrası_3'));

//If Conditions

if (sayı_sonrası_2 > sayı_sonrası_3 ){
  print('It worked')
  print(typeof sayı_sonrası_2,'sayı2')
  print(typeof sayı_sonrası_3,'sayı3')
}

2 Answers 2

0

This is to be expected because sayı_sonrası_2 and sayı_sonrası_3 are Earth Engine ("server-side") objects.

I suggest you read the GEE documentation: https://developers.google.com/earth-engine/guides/client_server

2
  • I tried again with ee.Algorithms.if() function and debuged it. The error is still the same. Function work but logically wrong.
    – berkK
    Nov 4, 2020 at 14:16
  • @berkK a ee.Number is an object. The first two code blocks in the link I posted explain your problem - unless I have misinterpreted your question.
    – korndog
    Nov 4, 2020 at 17:22
0

I used getInfo() method. The code slowed a lot and sometimes it crash but conditions work well.

var sayı_sonrası_2 = ee.Number(c_sonrası_2.get('ndvi_sonrası_2')).getInfo();
var sayı_sonrası_3 = ee.Number(c_sonrası_2.get('ndvi_sonrası_3')).getInfo();

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.