javascript - 无法使用其密钥访问对象的属性?

时间:2017-02-13 00:43:52

标签: javascript google-earth-engine

在Google地球编辑器中,我们使用reduceRegion()函数创建了一个对象:

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

我的问题是,meanValue2015似乎是一个对象 - 我输入

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

得到“对象(1属性) - NDVI:0.3177 ...”,“对象”,然后分别为“[]”?然后

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

未定义?有什么明显的东西我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

meanValue2015是一个地球引擎对象:一个词典。所以..

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)