ee.Image.remap

从输入值到输出值的映射,由两个并行列表表示。如果输入值未包含在输入列表中,则会将其设置为 defaultValue(如果已提供),否则会将其屏蔽。请注意,由于浮点精度误差,包含浮点值的输入有时可能无法匹配。

用法返回
Image.remap(from, to, defaultValue, bandName)图片
参数类型详细信息
此:image图片应用重映射的图片。
from列表源值(数字或 ee.Array)。此列表中的所有值都将映射到“to”中的相应值。
to列表目标值(数字或 ee.Array)。用于替换“from”中的相应值。必须具有与“from”相同数量的值。
defaultValue对象,默认值:null用于替换“from”中没有匹配的值的默认值。如果未指定,则会屏蔽不匹配的值。
bandName字符串,默认值:null要重新映射的频段的名称。如果未指定,则使用图片中的第一个波段。

示例

代码编辑器 (JavaScript)

// A land cover image.
var img = ee.Image('ESA/WorldCover/v100/2020');

// A list of pixel values to replace.
var fromList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100];

// A corresponding list of replacement values (10 becomes 1, 20 becomes 2, etc).
var toList =   [ 1,  2,  2,  2,  3,  2,  4,  5,  6,  6,  2];

// Replace pixel values in the image. If the image is multi-band, only the
// remapped band will be returned. The returned band name is "remapped".
// Input image properties are retained in the output image.
var imgRemap = img.remap({
  from: fromList,
  to: toList,
  defaultValue: 0,
  bandName: 'Map'
});

// Display the original and remapped images. Note that similar land cover
// classes in the original image are grouped into aggregate classes by
// from → to value mapping.
Map.addLayer(img, null, 'Original image');
Map.addLayer(imgRemap, {
    min: 1, max: 6,
    palette:'darkgreen, lightgreen, red, white, blue, lightblue'
  }, 'Remapped image');

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# A land cover image.
img = ee.Image('ESA/WorldCover/v100/2020')

# A list of pixel values to replace.
from_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100]

# A corresponding list of replacement values (10 becomes 1, 20 becomes 2, etc).
to_list = [1, 2, 2, 2, 3, 2, 4, 5, 6, 6, 2]

# Replace pixel values in the image. If the image is multi-band, only the
# remapped band will be returned. The returned band name is "remapped".
# Input image properties are retained in the output image.
img_remap = img.remap(from_list, to_list, defaultValue=0, bandName='Map')

# Display the original and remapped images. Note that similar land cover
# classes in the original image are grouped into aggregate classes by
# from → to value mapping.
m = geemap.Map()
m.add_layer(img, None, 'Original image')
m.add_layer(
    img_remap,
    {
        'min': 1,
        'max': 6,
        'palette': [
            'darkgreen',
            'lightgreen',
            'red',
            'white',
            'blue',
            'lightblue',
        ],
    },
    'Remapped image',
)
m