3

我正在尝试在 Google 地球引擎 (GEE) 代码编辑器中获取图像集中的图像数量。图像集filteredCollection包含 GEE 上覆盖格林威治的所有 Landsat 8 图像(仅作为示例)。

图像数量打印为 113,但它似乎不是整数类型,我也无法将其强制为整数。看起来是这样的:

var imageCollection = ee.ImageCollection("LANDSAT/LC8_SR");
var point = ee.Geometry.Point([0.0, 51.48]);
var filteredCollection = imageCollection.filterBounds(point);

var number_of_images = filteredCollection.size();
print(number_of_images); // prints 113
print(number_of_images > 1); // prints false
print(+number_of_images); // prints NaN
print(parseInt(number_of_images, 10)); // prints NaN
print(Number(number_of_images)); // prints NaN
print(typeof number_of_images); // prints object
print(number_of_images.constructor); // prints <Function>
print(number_of_images.constructor.name); // prints Ik

var number_of_images_2 = filteredCollection.length;
print(number_of_images_2); // prints undefined

知道这里发生了什么以及如何将集合中的图像数量作为整数获取吗?

PS:Collection.size() 是获取GEE 文档中图像数量的推荐函数。

4

1 回答 1

8

这是由于 GEE 架构,GEE 客户端和服务器端相互交互的方式。您可以在docs中了解它。

但简而言之:

如果您正在写作Collection.size(),那么您基本上是在您身边(客户端)构建一个JSON对象,该对象本身不包含任何信息。调用该print函数后,您会将JSON对象发送到服务器端,在服务器端对其进行评估并返回输出。这也适用于包含变量的任何其他函数number_of_images。如果该函数在服务器端进行评估,它将起作用(因为它将在那里进行评估),如果该函数仅在本地执行(as number_of_images > 1),它将失败。这对于如何在 GEE 中使用循环也有“大”含义,这在文档(上面的链接)中有更好的描述。

至于解决方案:

您可以使用.getInfo()基本上从服务器检索结果的函数,并让您将其分配给变量。

所以

var number_of_images = filteredCollection.size().getInfo();

会把你带到你想要的地方。如文档中所述,应谨慎使用此方法:

getInfo()除非绝对需要,否则不应使用。如果您在代码中调用 getInfo(),Earth Engine 将打开容器并告诉您里面有什么,但它会阻止您的其余代码,直到完成

高温高压

于 2017-05-15T10:08:27.060 回答