将看起来像数字但不是整数的内容转换为 (Google Earth Engine)
Convert what looks like a number but isn't to an integer (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
知道这里发生了什么,以及如何将集合中的图像数量作为整数获取吗?
P.S.: Collection.size() 是获取 GEE docs.
中图像数量的推荐函数
这是由于 GEE 架构,即 GEE 客户端和服务器端相互交互的方式。您可以在 docs.
中阅读相关信息
但简而言之:
如果您正在编写 Collection.size()
,您基本上是在您这边(客户端)构建一个本身不包含任何信息的 JSON
对象。调用 print
函数后,您会将 JSON
对象发送到服务器端,在服务器端对其进行计算并 returns 输出。这也适用于包含变量 number_of_images
的任何其他函数。如果该函数在服务器端求值,它将起作用(因为它将在那里求值),如果该函数仅在本地执行(如 number_of_images > 1
),它将失败。
这对于如何在 GEE 中使用循环也有 "big" 的含义,这在文档中有更好的描述(link 以上)。
所以解决办法:
您可以使用函数 .getInfo()
,它基本上从服务器检索结果,并允许您将其分配给变量。
所以
var number_of_images = filteredCollection.size().getInfo();
会带你去你想去的地方。如文档中所述,请谨慎使用此方法:
You shouldn't use getInfo()
unless you absolutely need to. If you call getInfo() in your code, Earth Engine will open the container and tell you what's inside, but it will block the rest of your code until that's done
HTH
我正在尝试在 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
知道这里发生了什么,以及如何将集合中的图像数量作为整数获取吗?
P.S.: Collection.size() 是获取 GEE docs.
中图像数量的推荐函数这是由于 GEE 架构,即 GEE 客户端和服务器端相互交互的方式。您可以在 docs.
中阅读相关信息但简而言之:
如果您正在编写 Collection.size()
,您基本上是在您这边(客户端)构建一个本身不包含任何信息的 JSON
对象。调用 print
函数后,您会将 JSON
对象发送到服务器端,在服务器端对其进行计算并 returns 输出。这也适用于包含变量 number_of_images
的任何其他函数。如果该函数在服务器端求值,它将起作用(因为它将在那里求值),如果该函数仅在本地执行(如 number_of_images > 1
),它将失败。
这对于如何在 GEE 中使用循环也有 "big" 的含义,这在文档中有更好的描述(link 以上)。
所以解决办法:
您可以使用函数 .getInfo()
,它基本上从服务器检索结果,并允许您将其分配给变量。
所以
var number_of_images = filteredCollection.size().getInfo();
会带你去你想去的地方。如文档中所述,请谨慎使用此方法:
You shouldn't use
getInfo()
unless you absolutely need to. If you call getInfo() in your code, Earth Engine will open the container and tell you what's inside, but it will block the rest of your code until that's done
HTH