Earth Engine 对象

现在,您已经熟练掌握 JavaScript,接下来将学习如何将 JavaScript 对象和基元放入 Earth Engine 容器中,以便发送到服务器并在 Google 上进行处理。

字符串

例如,定义一个字符串,然后将其放入 ee.String() 容器中以发送到 Earth Engine:

代码编辑器 (JavaScript)

// Define a string, then put it into an EE container.
var aString = 'To the cloud!';
var eeString = ee.String(aString);
print('Where to?', eeString);

可以将 ee.Thing 视为服务器上存在的事物的容器。在此示例中,系统会先定义字符串,然后将其放入容器中。您还可以一次性定义容器及其内容。例如:

代码编辑器 (JavaScript)

// Define a string that exists on the server.
var serverString = ee.String('This is on the server.');
print('String on the server:', serverString);

虽然 print() 的第一个实参在客户端上只是一个字符串,但第二个实参实际上会发送到服务器进行评估,然后再发送回来。

Numbers

使用 ee.Number() 在服务器上创建数字对象。例如,使用 Math.E JavaScript 方法在服务器上创建常量值:

代码编辑器 (JavaScript)

// Define a number that exists on the server.
var serverNumber = ee.Number(Math.E);
print('e=', serverNumber);

ee.String()ee.Number() 方法是构造函数。构造函数会获取其实参(以及可能存在的其他形参),将其放入容器中,然后返回该容器及其内容,作为您可以在代码中操控的 Earth Engine 对象。任何以 ee 开头的构造函数都会返回一个 Earth Engine 对象。

Earth Engine 对象的方法

请注意,创建 Earth Engine 对象后,您必须使用 Earth Engine 方法来处理该对象。在此示例中,您无法使用 JavaScript 的 Math.log() 来处理该 Earth Engine 对象。您必须使用为 ee.Number 定义的等效方法:

代码编辑器 (JavaScript)

// Use a built-in function to perform an operation on the number.
var logE = serverNumber.log();
print('log(e)=', logE);

在此示例中,log()ee.Number 对象的方法。(使用代码编辑器左侧的 Docs 标签页可查看所有 Earth Engine 对象类型的方法列表,例如 ee.Number > log())。请注意,Earth Engine 对象的方法会返回其他 Earth Engine 对象。

列表

若要将 JavaScript 列表转换为服务器上的 ee.List 对象,您可以像处理数字和字符串一样,将 JavaScript 字面量放入容器中。Earth Engine 还提供了一些用于生成数字序列的便捷服务器端方法。例如:

代码编辑器 (JavaScript)

// Make a sequence the hard way.
var eeList = ee.List([1, 2, 3, 4, 5]);
// Make a sequence the easy way!
var sequence = ee.List.sequence(1, 5);
print('Sequence:', sequence);

由于 ee.List 对象仅存在于服务器上,因此请使用 Earth Engine 提供的函数与其交互。例如,如需从列表中获取内容,请使用 ee.List 对象的 get() 方法:

代码编辑器 (JavaScript)

// Use a method on an ee.List to extract a value.
var value = sequence.get(2);
print('Value at index 2:', value);

投屏

有时,Earth Engine 不知道从方法返回的对象的类型。作为程序员,您知道上例中的 value 变量是一个数字对象。但如果您尝试使用 ee.Numberadd() 方法,则会收到类似如下的错误:

这在 get() 函数中很常见,该函数可能会返回各种 Earth Engine 对象。如需更正此问题,请使用 ee.Number 构造函数来转换结果:

代码编辑器 (JavaScript)

// Cast the return value of get() to a number.
print('No error:', ee.Number(value).add(3));

字典

您可以像处理字符串、数字和列表一样,从 JavaScript 对象构建 Earth Engine Dictionary。在构建时,您可以使用 JavaScript 功能来初始化 Earth Engine 对象。在这种情况下,ee.Dictionary 直接从 JavaScript 字面量对象构建:

代码编辑器 (JavaScript)

// Make a Dictionary on the server.
var dictionary = ee.Dictionary({
  e: Math.E,
  pi: Math.PI,
  phi: (1 + Math.sqrt(5)) / 2
});

// Get some values from the dictionary.
print('Euler:', dictionary.get('e'));
print('Pi:', dictionary.get('pi'));
print('Golden ratio:', dictionary.get('phi'));

// Get all the keys:
print('Keys: ', dictionary.keys());

在此示例中,请注意,获得 ee.Dictionary 后,您必须使用 ee.Dictionary 的方法来获取值(与上一课中的 JavaScript 字典不同)。具体而言,get(key) 会返回与 key 关联的值。由于 get() 返回的对象类型可以是任何类型,因此如果您要对该对象执行打印以外的任何操作,则需要将其强制转换为正确的类型。另请注意,keys() 方法会返回 ee.List

日期

日期对象是 Earth Engine 表示时间的方式。与之前的示例一样,务必区分 JavaScript Date 对象和 Earth Engine ee.Date 对象。通过字符串、JavaScript Date 或使用 ee.Date 类提供的静态方法来构造 ee.Date。(如需了解详情,请参阅“文档”标签页中的“日期”部分)。此示例说明了如何从字符串或 JavaScript 日期(表示自 1970 年 1 月 1 日午夜以来的毫秒数)构建日期:

代码编辑器 (JavaScript)

// Define a date in Earth Engine.
var date = ee.Date('2015-12-31');
print('Date:', date);

// Get the current time using the JavaScript Date.now() method.
var now = Date.now();
print('Milliseconds since January 1, 1970', now);

// Initialize an ee.Date object.
var eeNow = ee.Date(now);
print('Now:', eeNow);

日期可用于过滤集合,特别是作为 filterDate() 方法的实参。如需详细了解如何对集合进行排序,请参阅“入门”页面的这一部分

题外话:按名称传递参数

可以按顺序传递 Earth Engine 方法的实参,例如,若要根据年、月和日创建 ee.Date,可以按年、月、日的顺序传递 fromYMD() 静态方法的参数:

代码编辑器 (JavaScript)

var aDate = ee.Date.fromYMD(2017, 1, 13);
print('aDate:', aDate);

或者,您也可以按名称传递参数,顺序不限。虽然这可能会增加代码量,但可以提高可读性和可重用性。如需按名称传递参数,请传入一个 JavaScript 对象,其中对象的键是方法参数的名称,值是方法的实参。例如:

代码编辑器 (JavaScript)

var theDate = ee.Date.fromYMD({
  day: 13,
  month: 1,
  year: 2017
});
print('theDate:', theDate);

请注意,对象属性(键)的名称与 ee.Date.fromYMD() 文档中指定的名称一致。另请注意,作为实参传递的对象可以保存在变量中以供重复使用,如 JavaScript 对象示例所示。

现在,您已经对 JavaScript 有了足够的了解,可以开始使用 Earth Engine 了! 如需详细了解 JavaScript 对象与 Earth Engine 对象,请参阅客户端与服务器页面

在下一部分中,您将详细了解函数式编程概念,以便在 Earth Engine 中有效地使用 for 循环、if/else 条件和迭代。