2

I am working with a simple piece of code for obtaining EVI index from two Landsat8 images and then calculating the difference between both.

var img_t0 = ee.Image('LANDSAT/LC8_L1T_TOA_FMASK/LC82330892017001LGN00');
var img_t1 = ee.Image('LANDSAT/LC8_L1T_TOA_FMASK/LC82330892017081LGN00');

function EVI(image) {
  var step1 = image.expression(
    '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
      'NIR': image.select('B5'),
      'RED': image.select('B4'),
      'BLUE': image.select('B2')
      });
  var step2 = step1.updateMask(image.select(['fmask']).eq(0));  // mask everything different than 'clear'
  return step2
}

var n0 = EVI(img_t0);
var n1 = EVI(img_t1);

var diff = n0.subtract(n1);

// Mean and standard deviation reducers.
var meanReducer = ee.Reducer.mean();
var sigmaReducer = ee.Reducer.stdDev();

// Use the reducer to get the mean and SD of the image.
var mean = ee.Number(diff.reduceRegion({
  reducer: meanReducer,
  bestEffort: true,
}).get('constant'));

var sigma = ee.Number(diff.reduceRegion({
  reducer: sigmaReducer,
  bestEffort: true,
}).get('constant'));

print(mean, sigma, mean+2*sigma)

From the resulting image, I need to get the mean and standard deviation for further calculations. Despite the fact that I can print the parameters (as numbers), I can not retrieve their values as such type. When I use them in an arithmetic operation, I am getting NaN as result.

I understand that the reducer property gives you a dictionary as a result, but I have tried several methods (link casting the variables with ee.Number or using brackets [ ] for getting the key value without success).

So the question is ¿How can I retrieve those values like numbers or additional variables for using them later?

4

The cause of your issue is that you are mixing server-side Earth Engine objects (mean, sigma) with client-side operators (+ 2 *) in the following line:

print(mean, sigma, mean+2*sigma)

If you want to do the arithmetic operations on the client, you can call .getInfo() on the objects to convert them from Earth Engine objects to client-side Javascript objects. This is generally not recommended for a variety of reasons: it is less efficient given that it makes multiple network requests to the server; the approach doesn't scale to large data instructions; it will block execution of the Code Editor script until it completes.

// Example of forcing client-side evaluation (discouraged).
print(mean, sigma, mean.getInfo()+2*sigma.getInfo());

Instead, use server-side methods to describe the arithmetic:

// Server-side evaluation.
print(mean, sigma, mean.add(sigma.multiply(2)));

For a detailed discussion of this topic, see the Client vs. Server section of the Earth Engine documentation.

  • Tyler, you gave me an excellent explanation of the issue as well as an appropriate solution. – gtapko Jul 17 '17 at 11:58

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.