Google Earth Engine 下载问题,这是由不可变的服务器端对象引起的吗?

Google Earth Engine download problems, is this caused by immutable server side objects?

我有一个功能可以将图像集下载为 TFrecord 或 geotiff。

这是函数 -

def download_image_collection_to_drive(collection, aois, bands, limit, export_format):
    if collection.size().lt(ee.Number(limit)):
        bands = [band for band in bands if band not in ['SCL', 'QA60']]
        for aoi in aois:
            cluster = aoi.get('cluster').getInfo()
            geom = aoi.bounds().getInfo()['geometry']['coordinates']
            aoi_collection = collection.filterMetadata('cluster', 'equals', cluster)

            for ts in range(1, 11):
                print(ts)
                ts_collection = aoi_collection.filterMetadata('interval', 'equals', ts)
                if ts_collection.size().eq(ee.Number(1)):
                    image = ts_collection.first()
                    p_id = image.get("PRODUCT_ID").getInfo()
                    description = f'{cluster}_{ts}_{p_id}'
                    task_config = {
                        'fileFormat': export_format,
                        'image': image.select(bands),
                        'region': geom,
                        'description': description,
                        'scale': 10,
                        'folder': 'output'
                    }
                    if export_format == 'TFRecord':
                        task_config['formatOptions'] = {'patchDimensions': [256, 256], 'kernelSize': [3, 3]}
                    task = ee.batch.Export.image.toDrive(**task_config)
                    task.start()
                else:
                    logger.warning(f'no image for interval {ts}')
    else:
        logger.warning(f'collection over {limit} aborting drive download')

似乎每当它到达第二个 aoi 时就失败了,我对此感到困惑,因为 if ts_collection.size().eq(ee.Number(1)) 确认那里有一张图片,所以它应该设法从中获取产品 ID。

line 24, in download_image_collection_to_drive
    p_id = image.get("PRODUCT_ID").getInfo()
  File "/lib/python3.7/site-packages/ee/computedobject.py", line 95, in getInfo
    return data.computeValue(self)
  File "/lib/python3.7/site-packages/ee/data.py", line 717, in computeValue
    prettyPrint=False))['result']
  File "/lib/python3.7/site-packages/ee/data.py", line 340, in _execute_cloud_call
    raise _translate_cloud_exception(e)
ee.ee_exception.EEException: Element.get: Parameter 'object' is required.

我是否在某处与不可变的服务器端对象发生冲突?

这是一个服务器端值,问题,是的,但不可变性与它无关 — 您的 if 语句未按预期工作。

ts_collection.size().eq(ee.Number(1)) 是服务器端值 - 您描述了尚未发生的比较。这意味着执行任何像 Python if 语句这样的本地操作不能将比较结果考虑在内,只会将其视为真实值。

使用 getInfo 将是一个快速修复:

if ts_collection.size().eq(ee.Number(1)).getInfo():

但是通过只获取一次整个集合的信息(包括图像信息)来避免使用 getInfo 会更有效。

...
ts_collection_info = ts_collection.getInfo()
if ts_collection['features']:  # Are there any images in the collection?
    image = ts_collection.first()
    image_info = ts_collection['features'][0]  # client-side image info already downloaded
    p_id = image_info['properties']['PRODUCT_ID']  # get ID from client-side info
    ...

这样,每个 ts 您只需要发出两个请求:一个用于检查匹配,一个用于开始导出。

请注意,我实际上没有 运行 这个 Python 代码,可能会有一些小错误;如果它给您带来任何麻烦,print(ts_collection_info) 并检查您实际收到的结构以确定如何解释它。