公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
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.Number
的 add()
方法,则会收到类似如下的错误:
这在 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 条件和迭代。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[[["\u003cp\u003eLearn how to use JavaScript objects and primitives within Earth Engine containers for server-side processing in Google Earth Engine.\u003c/p\u003e\n"],["\u003cp\u003eUnderstand the concept of Earth Engine constructors like \u003ccode\u003eee.String()\u003c/code\u003e, \u003ccode\u003eee.Number()\u003c/code\u003e, \u003ccode\u003eee.List()\u003c/code\u003e, and \u003ccode\u003eee.Dictionary()\u003c/code\u003e to create and manipulate Earth Engine objects.\u003c/p\u003e\n"],["\u003cp\u003eUtilize Earth Engine methods for processing Earth Engine objects, such as using \u003ccode\u003elog()\u003c/code\u003e on an \u003ccode\u003eee.Number\u003c/code\u003e instead of JavaScript's \u003ccode\u003eMath.log()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eExplore the use of \u003ccode\u003eee.Date\u003c/code\u003e objects for representing time in Earth Engine and their application in filtering collections.\u003c/p\u003e\n"],["\u003cp\u003eDiscover how to pass parameters to Earth Engine methods by name using JavaScript objects for improved readability and reusability.\u003c/p\u003e\n"]]],[],null,["# Earth Engine Objects\n\nNow that you're comfortable with JavaScript, learn how to put JavaScript objects and\nprimitives into Earth Engine containers for sending to the server and processing at\nGoogle.\n\nStrings\n-------\n\nFor example, define a string, then put it into the `ee.String()`\ncontainer to be sent to Earth Engine:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Define a string, then put it into an EE container.\nvar aString = 'To the cloud!';\nvar eeString = ee.String(aString);\nprint('Where to?', eeString);\n```\n\nThink of `ee.Thing` as a container for a thing that exists on the server. In\nthis example, the string is defined first, then put into the container. You can also\ndefine the container and its contents all at once. For example:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Define a string that exists on the server.\nvar serverString = ee.String('This is on the server.');\nprint('String on the server:', serverString);\n```\n\nAlthough the first argument to `print()` is just a string on the client, the\nsecond argument is actually sent to the server to be evaluated, then sent back.\n\nNumbers\n-------\n\nUse `ee.Number()` to create number objects on the server. For example, use the\n`Math.E` JavaScript method to create a constant value on the server:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Define a number that exists on the server.\nvar serverNumber = ee.Number(Math.E);\nprint('e=', serverNumber);\n```\n\nThe `ee.String()` and `ee.Number()` methods are\n*constructors* . A constructor takes its argument (and possibly other parameters),\nputs it in a container, and returns the container and its contents as an Earth Engine\nobject that you can manipulate in your code. Any constructor starting with `ee`\nreturns an Earth Engine object.\n\n\nMethods on Earth Engine objects\n-------------------------------\n\nNote that once you've created an Earth Engine object, you have to use Earth\nEngine methods to process it. In this example, you can't use JavaScript's\n[`Math.log()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log)\nto process that Earth Engine object. You have to use the equivalent method defined for an\n`ee.Number`:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Use a built-in function to perform an operation on the number.\nvar logE = serverNumber.log();\nprint('log(e)=', logE);\n```\n\nIn this example, `log()` is a method for a `ee.Number` object. (Use\nthe [**Docs** tab](/earth-engine/guides/playground#api-reference-docs-tab) at the left\nside of the code editor to see a list of all the methods for every Earth Engine object type,\nfor example ee.Number \\\u003e log()). Note that the methods of Earth Engine objects return\nother Earth Engine objects.\n\nLists\n-----\n\nTo make a JavaScript list into an `ee.List` object on the server, you can\nput a JavaScript literal into a container as with numbers and strings. Earth Engine\nalso provides server-side convenience methods for making sequences of numbers. For\nexample:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Make a sequence the hard way.\nvar eeList = ee.List([1, 2, 3, 4, 5]);\n// Make a sequence the easy way!\nvar sequence = ee.List.sequence(1, 5);\nprint('Sequence:', sequence);\n```\n\nSince the `ee.List` objects only exist on the server, use Earth Engine\nprovided functions to interact with them. For example, to get something out of the list,\nuse the `get()` method of the `ee.List` object:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Use a method on an ee.List to extract a value.\nvar value = sequence.get(2);\nprint('Value at index 2:', value);\n```\n\nCasting\n-------\n\nSometimes, Earth Engine doesn't know the type of an object that gets returned from a\nmethod. You, as the programmer, know that the `value` variable\nin the previous example is a number object. But if you try to use the `add()`\nmethod of an `ee.Number`, you'll get an error like:\n| value.add is not a function\n\nThis is common with the `get()` function, which could return all sorts of\nEarth Engine objects. To correct it, use the `ee.Number` constructor to\n*cast* the result:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Cast the return value of get() to a number.\nprint('No error:', ee.Number(value).add(3));\n```\n\nDictionaries\n------------\n\nYou can construct an Earth Engine `Dictionary` from a JavaScript object, as with\nstrings, numbers and lists. At construction time, you can use JavaScript functionality to\ninitialize the Earth Engine object. In this case an `ee.Dictionary` is\nconstructed directly from a JavaScript literal object:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Make a Dictionary on the server.\nvar dictionary = ee.Dictionary({\n e: Math.E,\n pi: Math.PI,\n phi: (1 + Math.sqrt(5)) / 2\n});\n\n// Get some values from the dictionary.\nprint('Euler:', dictionary.get('e'));\nprint('Pi:', dictionary.get('pi'));\nprint('Golden ratio:', dictionary.get('phi'));\n\n// Get all the keys:\nprint('Keys: ', dictionary.keys());\n```\n\nIn this example, observe that once you have an `ee.Dictionary`, you must\nuse methods on the `ee.Dictionary` to get values (unlike the JavaScript\ndictionary in the previous lesson). Specifically, `get(key)` returns the value\nassociated with `key`. Since the type of object returned by `get()`\ncould be be anything, if you're going to do anything to the object other then print it,\nyou need to cast it to the right type. Also note that the `keys()` method\nreturns an `ee.List`.\n\nDates\n-----\n\nDate objects are the way Earth Engine represents time. As in the previous examples,\nit is important to distinguish between a JavaScript\n[`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)\nobject and an Earth Engine `ee.Date` object. Construct an `ee.Date`\nfrom a string, from a JavaScript `Date`, or using static methods provided by the\n`ee.Date` class. (See the Date section in the\n[**Docs** tab](/earth-engine/guides/playground#api-reference-docs-tab) for\ndetails). This example illustrates the construction of dates from strings or a JavaScript\ndate representing milliseconds since midnight on January 1, 1970:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Define a date in Earth Engine.\nvar date = ee.Date('2015-12-31');\nprint('Date:', date);\n\n// Get the current time using the JavaScript Date.now() method.\nvar now = Date.now();\nprint('Milliseconds since January 1, 1970', now);\n\n// Initialize an ee.Date object.\nvar eeNow = ee.Date(now);\nprint('Now:', eeNow);\n```\n\nDates are useful for filtering collections, specifically as arguments to the\n`filterDate()` method. See [this\nsection of the Get Started page](/earth-engine/guides/getstarted#filtering-and-sorting) for more information about sorting collections.\n\nDigression: passing parameters by name\n--------------------------------------\n\nArguments to Earth Engine methods can be passed in order, for example to create an\n`ee.Date` from year, month and day, you can pass parameters of the\n`fromYMD()` static method in the order year, month, day:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar aDate = ee.Date.fromYMD(2017, 1, 13);\nprint('aDate:', aDate);\n```\n\nAlternatively, you can pass the parameters by name, in any order. While it might be more\ncode, it can improve readability and reusability. To pass parameters by name, pass in a\nJavaScript object in which the keys of the object are the names of the method parameters\nand the values are the arguments to the method. For example:\n\n### Code Editor (JavaScript)\n\n```javascript\nvar theDate = ee.Date.fromYMD({\n day: 13,\n month: 1,\n year: 2017\n});\nprint('theDate:', theDate);\n```\n\nNote that the names of the object properties (the keys) match the names specified in the\n[`ee.Date.fromYMD()` docs](/earth-engine/apidocs/ee-date-fromymd). Also note that\nthe object that is passed as an argument can be saved in a variable for reuse, as\nillustrated by the [JavaScript object example](/earth-engine/tutorials/tutorial_js_01#objects).\n\nYou now have enough of an introduction to JavaScript to start using Earth Engine!\nSee the [Client vs. Server\npage](/earth-engine/guides/client_server) for a more detailed explanation of JavaScript vs. Earth Engine objects.\n\nIn the next section, learn more about [Functional programming\nconcepts](/earth-engine/tutorials/tutorial_js_03) to effectively use for-loops, if/else conditions and iterations in Earth\nEngine."]]