TransWikia.com

Google Earth Engine ee.Number() function produce an object instead of number values

Geographic Information Systems Asked on January 28, 2021

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

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

Correct answer by korndog on January 28, 2021

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();

Answered by berkK on January 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP