在 Colab 中使用带地球引擎 Python API 的波段的“ee.ee_list.List”循环“ee.image.Image”

问题描述 投票:0回答:1

我想使用一个函数来缩放图像的波段。图像波段的数量和名称可能不同。以下(有效)代码几乎可以满足我的要求,除了我想遍历(数量)乐队名称,而不是预定义的范围大小:

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.List'。

我尝试过使用枚举、长度、toList 的双循环,...但我无法弄清楚如何根据我的目的调整循环。

python for-loop google-colaboratory nested-lists libgee
1个回答
0
投票

我们需要使用

list
toBands()
将乐队名称作为
getInfo()

ee_object = ee.Image('JAXA/ALOS/AW3D30/V2_2')
bandnames = [band['id'][-band['id'][::-1].find('_'):] for band in ee.ImageCollection(ee_object).toBands().getInfo()['bands']]

而且我们不需要

for
loop
来迭代乐队,因为
select
可以采用乐队名称的
list

scaledbands = ee_object.select(bandnames).unitScale(min_max[0], min_max[1]).multiply(255).toUint8()
new_ee_object = ee_object.addBands(scaledbands)
© www.soinside.com 2019 - 2024. All rights reserved.