对于在“ee.image.Image”上循环使用“ee.ee_list.在 Colab 中使用 Earth Engine Python API 的乐队列表

For loop over 'ee.image.Image' with 'ee.ee_list.List' of bands with Earth Engine Python API in Colab

提问人:Jude O'Brien 提问时间:4/20/2023 更新时间:4/21/2023 访问量:272

问:

我想使用函数来缩放图像的波段。图像波段的数量和名称可能不同。以下(工作)代码几乎可以完成我想要的操作,除了我想遍历(数量)带名,而不是预定义的范围大小:

from scipy.stats.distributions import randint_gen
# Function to scale the images (one mosaicked ee.Image) to a Uint8 format scaled from 0-255
def scale_composites(image):

  image_copy = image
  bandnames = image.bandNames()
  image = image.select(0).multiply(0).rename('base')
  min_max = [-5,5] # Min and Max values for S1 change ratios (list)

  bandlist = []
   for band in range(0, 4):
     scaledband = image_copy.select(band).unitScalemin_max[0], min_max[1]).multiply(255).toUint8()
     bandlist.append(scaledband)

  image = image.addBands(bandlist)
  image = image.select(bandnames)

  return image

该函数的输入是“ee.image.Image”,而“bandnames”是“ee.ee_list。列表“。

我尝试过带有枚举、长度、toList 的双循环......但我无法弄清楚如何使循环适应我的目的。

Python for 循环 google-colaboratory 嵌套列表 libgee

评论


答:

0赞 Ori Yarden PhD 4/21/2023 #1

我们可以通过以下方式获取乐队名称:listgetInfo()

ee_object = ee.Image('JAXA/ALOS/AW3D30/V2_2')
bandnames = ee_object.bandNames().getInfo()

我们不需要 a 来迭代波段,因为可以将 a(波段名称)作为参数:forloopselectlist

scaledbands = ee_object.select(bandnames).unitScale(min_max[0], min_max[1]).multiply(255).toUint8()
new_ee_object = ee_object.addBands(scaledbands)

print(new_ee_object)输出:

ee.Image({
  "functionInvocationValue": {
    "functionName": "Image.addBands",
    "arguments": {
      "dstImg": {
        "functionInvocationValue": {
          "functionName": "Image.load",
          "arguments": {
            "id": {
              "constantValue": "JAXA/ALOS/AW3D30/V2_2"
            }
          }
        }
      },
      "srcImg": {
        "functionInvocationValue": {
          "functionName": "Image.toUint8",
          "arguments": {
            "value": {
              "functionInvocationValue": {
                "functionName": "Image.multiply",
                "arguments": {
                  "image1": {
                    "functionInvocationValue": {
                      "functionName": "Image.unitScale",
                      "arguments": {
                        "high": {
                          "constantValue": 5
                        },
                        "input": {
                          "functionInvocationValue": {
                            "functionName": "Image.select",
                            "arguments": {
                              "bandSelectors": {
                                "constantValue": [
                                  "AVE_DSM",
                                  "AVE_STK",
                                  "AVE_MSK"
                                ]
                              },
                              "input": {
                                "functionInvocationValue": {
                                  "functionName": "Image.load",
                                  "arguments": {
                                    "id": {
                                      "constantValue": "JAXA/ALOS/AW3D30/V2_2"
                                    }
                                  }
                                }
                              }
                            }
                          }
                        },
                        "low": {
                          "constantValue": -5
                        }
                      }
                    }
                  },
                  "image2": {
                    "functionInvocationValue": {
                      "functionName": "Image.constant",
                      "arguments": {
                        "value": {
                          "constantValue": 255
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
})