我正在尝试在Google Earth Engine(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


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

附言:Collection.size()是用于获取GEE docs中图像数量的推荐功能。

最佳答案

这归因于GEE体系结构,即GEE客户端和服务器端相互交互的方式。您可以在docs中阅读。

简而言之:

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

因此,作为一个解决方案:

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

所以

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

将带您到想要的地方。如文档所述,应谨慎使用此方法:


  除非绝对需要,否则不应该使用getInfo()。如果您在代码中调用getInfo(),则Earth Engine将打开容器并告诉您其中的内容,但是它将阻止其余代码,直到完成操作为止


高温超导

08-07 22:46