图片概览

开始使用文档中所述,在 Earth Engine 中,栅格数据表示为 Image 对象。图像由一个或多个波段组成,每个波段都有自己的名称、数据类型、比例、掩码和投影。每个图片的元数据都存储为一组属性。

ee.Image 构造函数

您可以通过将 Earth Engine 资产 ID 粘贴到 ee.Image 构造函数中来加载图片。您可以在数据目录中找到图片 ID。 例如,如需加载 JAXA 的 ALOS DSM,请执行以下操作:

var loadedImage = ee.Image('JAXA/ALOS/AW3D30/V2_2');

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

import ee
import geemap.core as geemap
loaded_image = ee.Image('JAXA/ALOS/AW3D30/V2_2')

请注意,通过代码编辑器搜索工具查找图片也是可行的。导入资源后,系统会在代码编辑器的“导入”部分为您编写图片构建代码。您还可以将个人素材资源 ID 用作 ee.Image 构造函数的参数。

ee.ImageCollection 获取 ee.Image

从合集中获取图片的标准方法是过滤合集,过滤条件的具体性依次递减。例如,如需从 Sentinel-2 地表反射率集合中获取图片,请执行以下操作:

var first = ee.ImageCollection('COPERNICUS/S2_SR')
                .filterBounds(ee.Geometry.Point(-70.48, 43.3631))
                .filterDate('2019-01-01', '2019-12-31')
                .sort('CLOUDY_PIXEL_PERCENTAGE')
                .first();
Map.centerObject(first, 11);
Map.addLayer(first, {bands: ['B4', 'B3', 'B2'], min: 0, max: 2000}, 'first');

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

import ee
import geemap.core as geemap
first = (
    ee.ImageCollection('COPERNICUS/S2_SR')
    .filterBounds(ee.Geometry.Point(-70.48, 43.3631))
    .filterDate('2019-01-01', '2019-12-31')
    .sort('CLOUDY_PIXEL_PERCENTAGE')
    .first()
)

# Define a map centered on southern Maine.
m = geemap.Map(center=[43.7516, -70.8155], zoom=11)

# Add the image layer to the map and display it.
m.add_layer(
    first, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}, 'first'
)
display(m)

请注意,排序是在过滤条件之后进行的。避免对整个合集进行排序。

来自 Cloud GeoTIFF 的图片

您可以使用 ee.Image.loadGeoTIFF()Google Cloud Storage 中的经过 Cloud 优化的 GeoTIFF 加载图片。 例如,托管在 Google Cloud 中的公开 Landsat 数据集包含此 GeoTIFF,对应于 Landsat 8 场景中的第 5 个波段。您可以使用 ee.Image.loadGeoTIFF() 从 Cloud Storage 加载此图片:

var uri = 'gs://gcp-public-data-landsat/LC08/01/001/002/' +
    'LC08_L1GT_001002_20160817_20170322_01_T2/' +
    'LC08_L1GT_001002_20160817_20170322_01_T2_B5.TIF';
var cloudImage = ee.Image.loadGeoTIFF(uri);
print(cloudImage);

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

import ee
import geemap.core as geemap
uri = (
    'gs://gcp-public-data-landsat/LC08/01/001/002/'
    + 'LC08_L1GT_001002_20160817_20170322_01_T2/'
    + 'LC08_L1GT_001002_20160817_20170322_01_T2_B5.TIF'
)
cloud_image = ee.Image.loadGeoTIFF(uri)
display(cloud_image)

请注意,如果您想重新加载从 Earth Engine 导出到 Cloud Storage 的 Cloud Optimized GeoTIFF,请在导出时将 cloudOptimized 设置为 true,如此处所述。

常量图片

除了按 ID 加载图片外,您还可以使用常量、列表或其他合适的 Earth Engine 对象创建图片。以下示例展示了创建图片、获取波段子集和操作波段的方法:

// Create a constant image.
var image1 = ee.Image(1);
print(image1);

// Concatenate two images into one multi-band image.
var image2 = ee.Image(2);
var image3 = ee.Image.cat([image1, image2]);
print(image3);

// Create a multi-band image from a list of constants.
var multiband = ee.Image([1, 2, 3]);
print(multiband);

// Select and (optionally) rename bands.
var renamed = multiband.select(
    ['constant', 'constant_1', 'constant_2'], // old names
    ['band1', 'band2', 'band3']               // new names
);
print(renamed);

// Add bands to an image.
var image4 = image3.addBands(ee.Image(42));
print(image4);

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

import ee
import geemap.core as geemap
# Create a constant image.
image_1 = ee.Image(1)
display(image_1)

# Concatenate two images into one multi-band image.
image_2 = ee.Image(2)
image_3 = ee.Image.cat([image_1, image_2])
display(image_3)

# Create a multi-band image from a list of constants.
multiband = ee.Image([1, 2, 3])
display(multiband)

# Select and (optionally) rename bands.
renamed = multiband.select(
    ['constant', 'constant_1', 'constant_2'],  # old names
    ['band1', 'band2', 'band3'],  # new names
)
display(renamed)

# Add bands to an image.
image_4 = image_3.addBands(ee.Image(42))
display(image_4)