将看似数字但不是整数的内容转换为整数(Google Earth Engine)

Joo*_*eey 3 javascript google-earth-engine

我正在尝试在 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
Run Code Online (Sandbox Code Playgroud)

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

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

Val*_*Val 8

这是由于 GEE 架构,即 GEE 客户端和服务器端相互交互的方式。你可以在文档中阅读它。

但简而言之:

如果你在写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 将打开容器并告诉您里面有什么,但它会阻止您的其余代码,直到完成

HTH