1

in Google Earth Editor, we've created an object using the reduceRegion() function:

var meanValue2015 = ndvi2015.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: justEC15.geometry(),
  crs: 'EPSG:4326',
  scale: 30,
});

My issue is that meanValue2015 appears to be an object - I input

print(meanValue2015);
print(typeof(meanValue2015));
print(Object.keys(meanValue2015));

and get "Object (1 property) - NDVI: 0.3177...", "Object", and then "[ ]" respectively? And then

print(meanValue2015.NDVI);
print(meanValue2015['NDVI']);

is undefined? Is there something obvious I'm doing wrong here?

Conor
  • 33
  • 5
  • `print(meanValue2015('NDVI');` to access properties you need to use square brackets – Alexander Derck Feb 13 '17 at 00:49
  • @AlexanderDerck sorry that's what I meant, typed it in wrong, getting undefined with the above edit – Conor Feb 13 '17 at 00:52
  • are you sure that `meanValue2015` is going to be an object? after all, `typeof [] === 'object'` – dargue3 Feb 13 '17 at 00:54
  • @dargue3 That is interesting, but the first print statement must mean it's an object right? It should be an object with one key - NDVI - no? – Conor Feb 13 '17 at 01:00
  • everything you're doing seems right in theory, so there must be something weird going on with what `reduceRegion()` is returning. Do the docs specify what the keys are going to be on `meanValue2015`? – dargue3 Feb 13 '17 at 01:09
  • I know nothing about Google Earth Editor, but it looks odd to me that you assign a property called `reducer` the **result** of calling `ee.Reducer.mean()`. Should it be `reducer: ee.Reducer.mean` (without the parentheses?) And the same for `geometry`? It's just a guess. – Scott Sauyet Feb 13 '17 at 02:09

1 Answers1

1

meanValue2015 is a Earth Engine object: a Dictionary. So..

var meanValue2015 = ndvi2015.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: justEC15.geometry(),
  crs: 'EPSG:4326',
  scale: 30,
});

var realValue = ee.Number(meanValue2015.get("ndvi"));
print("real value (object):", realValue)

// realValue is a EE object, so you have to use it as is..
// this would be easy, but it is wrong..
var newValueWrong = realValue + 1

// this is the right way..
var newValueRight = realValue.add(ee.Number(1))

print("wrong:", newValueWrong)
print("right:", newValueRight)

// there is another way, but it is not recommendable because you'd
// be wasting EE computing capacity (Google's gift! don't waste it)

var localValue = realValue.getInfo() + 1

print("value in the local scope (try to avoid)", localValue)
Rodrigo E. Principe
  • 1,281
  • 16
  • 26
  • Thanks! Sorry I haven't been on stack in a while, but you're right this comment is spot on. – Conor May 11 '17 at 22:17