Google Earth Engine(GEE)——在线面板实现NDVI值(点)的获取!

如果您在小部件中使用地球引擎结果(例如减少的数字输出),您将需要从服务器获取值。(有关地球引擎中客户端与服务器的详细信息,请参阅 此页面)。为了避免在计算该值时挂起整个 UI,您可以使用该evaluate()函数异步获取该值。该evaluate()函数开始请求一个值,当该值准备好时调用一个回调函数来处理结果。例如,考虑一个应用程序来获取 NDVI 时间序列在某个点的平均值:

函数:

evaluate(callback)

从服务器异步检索此对象的值并将其传递给提供的回调函数。

Asynchronously retrieves the value of this object from the server and passes it to the provided callback function.

Arguments:

参数:

这个:计算对象(ComputedObject): ComputedObject 实例。

回调(功能): 形式为 function(success, failure) 的函数,在服务器返回答案时调用。如果请求成功,则成功参数包含评估结果。如果请求失败,则失败参数将包含错误消息。

this:computedobject (ComputedObject):

The ComputedObject instance.

callback (Function):

A function of the form function(success, failure), called when the server returns an answer. If the request succeeded, the success argument contains the evaluated result. If the request failed, the failure argument will contains an error message.

widgets()

返回面板中当前的小部件列表。返回面板中的列表

Returns the list of widgets currently in the panel.

Arguments:

ui.panel(ui.Panel):
ui.Panel 实例。

this:ui.panel (ui.Panel):

The ui.Panel instance.

Returns: ui.data.ActiveList<ui.Widget>

代码:

// 加载 NDVI 影像
var ndvi = ee.ImageCollection('LANDSAT/LC8_L1T_8DAY_NDVI')
    .filterDate('2014-01-01', '2015-01-01');
var vis = {min: 0, max: 1, palette: ['99c199', '006400']};
Map.addLayer(ndvi.median(), vis, 'NDVI');

// 配置地图
Map.setCenter(-94.84497, 39.01918, 8);
Map.style().set('cursor', 'crosshair');

// 创建一个面板并将其添加到地图中
var inspector = ui.Panel([ui.Label('Click to get mean NDVI')]);
Map.add(inspector);

Map.onClick(function(coords) {
  // 显示加载标签。
  inspector.widgets().set(0, ui.Label({
    value: 'Loading...',
    style: {color: 'gray'}
  }));

  // 确定平均 NDVI,一个长期运行的服务器操作。
  var point = ee.Geometry.Point(coords.lon, coords.lat);
  var meanNdvi = ndvi.reduce('mean');
  var sample = meanNdvi.sample(point, 30);
  var computedValue = sample.first().get('NDVI_mean');

  // 从服务器请求值。
  computedValue.evaluate(function(result) {
    // 当服务器返回值时,显示它。
    inspector.widgets().set(0, ui.Label({
      value: 'Mean NDVI: ' + result.toFixed(2),
    }));
  });
});

代码中有一个需要说明的:result.toFixed(3),这里指的就是你的ndvi值显示的小数位数

最终结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

此星光明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值