导出图片

您可以将图片从 Earth Engine 导出为 GeoTIFF 或 TFRecord 格式。如需了解更多输出选项,请参阅配置参数。

设置示例

首先,定义要导出的图片数据:

Code Editor (JavaScript)

// Load a landsat image and select three bands.
var landsat = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_123032_20140515')
  .select(['B4', 'B3', 'B2']);

// Create a geometry representing an export region.
var geometry = ee.Geometry.Rectangle([116.2621, 39.8412, 116.4849, 40.01236]);

Python 设置

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

import ee
import geemap.core as geemap

Colab (Python)

# Load a landsat image and select three bands.
landsat = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_123032_20140515').select(
    ['B4', 'B3', 'B2']
)

# Create a geometry representing an export region.
geometry = ee.Geometry.Rectangle([116.2621, 39.8412, 116.4849, 40.01236])

接下来,定义将在以下导出操作中使用的投影参数。我们使用 crs 参数指定坐标系,并使用 crsTransform 参数精确指定像素网格。crsTransform 参数是行主序 [xScale, xShearing, xTranslation, yShearing, yScale, yTranslation] 的仿射转换矩阵中的参数列表。图片的起点由 xTranslation 和 yTranslation 值定义,图片的像素大小由 xScale 和 yScale 值定义。请参阅仿射矩阵示例。

Code Editor (JavaScript)

// Retrieve the projection information from a band of the original image.
// Call getInfo() on the projection to request a client-side object containing
// the crs and transform information needed for the client-side Export function.
var projection = landsat.select('B2').projection().getInfo();

Python 设置

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

import ee
import geemap.core as geemap

Colab (Python)

# Retrieve the projection information from a band of the original image.
# Call getInfo() on the projection to request a client-side object containing
# the crs and transform information needed for the client-side Export function.
projection = landsat.select('B2').projection().getInfo()

设置 scale

为简化起见,您可以指定 scale 参数,Earth Engine 会为您计算 crsTransform 参数。不过,仅设置图片的缩放比例并不能指定投影的原点,并且可能会导致图片相对于另一张像素大小相同的图片发生偏移!

可能发生偏移的原因在于,scale 参数用于填充 crsTransform 的 xScale 和 yScale 值,但 xTranslation 和 yTranslation 值的计算方式是,如果将它们除以相应的 xScale 和 yScale 值,余数将为零。这些参数用于指定一个像素网格,其中投影的原点位于像素的角落。此惯例不同于某些数据提供程序使用的平移参数,后者使用与投影原点偏移的网格。例如,USGS 提供的 Landsat 图像使用与投影原点相距 1/2 个像素的平移参数(对于 30 米波段,相距 15 米),而 ESA 提供的 Sentinel-2 图像使用与投影原点对齐的平移参数。如果导出操作中指定的 crsTransform 与原始图片的 crsTransform 不匹配,系统会重新采样输出像素(默认使用最近邻方法),这会导致生成的图片相对于原始图片发生偏移。

总而言之,如果您需要将导出的图片的像素与特定图片进行匹配,请务必使用 crs 和 crsTransform 参数来完全控制网格。

到云端硬盘

如需将图片导出到您的云端硬盘账号,请使用 Export.image.toDrive()。例如,如需导出 Landsat 图片的部分内容,请定义要导出的区域,然后调用 Export.image.toDrive():

Code Editor (JavaScript)

// Export the image, specifying the CRS, transform, and region.
Export.image.toDrive({
  image: landsat,
  description: 'imageToDriveExample_transform',
  crs: projection.crs,
  crsTransform: projection.transform,
  region: geometry
});

Python 设置

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

import ee
import geemap.core as geemap

Colab (Python)

# Export the image, specifying the CRS, transform, and region.
task = ee.batch.Export.image.toDrive(
    image=landsat,
    description='imageToDriveExample_transform',
    crs=projection['crs'],
    crsTransform=projection['transform'],
    region=geometry,
)
task.start()

运行此代码后,代码编辑器的 Tasks(任务)标签页中会创建一个导出任务。点击相应任务旁边的运行按钮即可启动该任务。(如需详细了解任务管理器,请参阅“代码编辑器”部分)。系统会在您的云端硬盘账号中使用指定的 fileFormat 创建图片。

到 Cloud Storage

如需将映像导出到 Google Cloud Storage 存储分区,请使用 Export.image.toCloudStorage()。如需将前例中的 Landsat 图片导出到 Cloud Storage(而非云端硬盘),请使用以下命令:

Code Editor (JavaScript)

// Export the image to Cloud Storage.
Export.image.toCloudStorage({
  image: landsat,
  description: 'imageToCloudExample',
  bucket: 'your-bucket-name',
  fileNamePrefix: 'exampleExport',
  crs: projection.crs,
  crsTransform: projection.transform,
  region: geometry
});

Python 设置

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

import ee
import geemap.core as geemap

Colab (Python)

# Export the image to Cloud Storage.
task = ee.batch.Export.image.toCloudStorage(
    image=landsat,
    description='imageToCloudExample',
    bucket='your-bucket-name',
    fileNamePrefix='exampleExport',
    crs=projection['crs'],
    crsTransform=projection['transform'],
    region=geometry,
)
task.start()

与导出到云端硬盘一样,您可以从任务标签页开始导出。Cloud Storage 存储分区位置可能会影响性能和存储费用,如需了解详情,请参阅关于位置注意事项的常见问题解答条目。

为资产

如需将图片导出到 Earth Engine 资产文件夹中的资产,请使用 Export.image.toAsset()。如需管理 Earth Engine 资产或查看存储空间配额的使用情况,请使用资产管理器。以下示例展示了如何针对同一波段使用不同的金字塔策略导出 Landsat 图像的部分内容。金字塔型政策指示 Earth Engine 如何计算资源的较低分辨率版本。如需详细了解 Earth Engine 如何处理多种分辨率,请参阅“缩放”文档。

Code Editor (JavaScript)

// Get band 4 from the Landsat image, copy it.
var band4 = landsat.select('B4').rename('b4_mean')
  .addBands(landsat.select('B4').rename('b4_sample'))
  .addBands(landsat.select('B4').rename('b4_max'));

// Export the image to an Earth Engine asset.
Export.image.toAsset({
  image: band4,
  description: 'imageToAssetExample',
  assetId: 'exampleExport',
  crs: projection.crs,
  crsTransform: projection.transform,
  region: geometry,
  pyramidingPolicy: {
    'b4_mean': 'mean',
    'b4_sample': 'sample',
    'b4_max': 'max'
  }
});

Python 设置

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

import ee
import geemap.core as geemap

Colab (Python)

# Get band 4 from the Landsat image, copy it.
band_4 = (
    landsat.select('B4')
    .rename('b4_mean')
    .addBands(landsat.select('B4').rename('b4_sample'))
    .addBands(landsat.select('B4').rename('b4_max'))
)

# Export the image to an Earth Engine asset.
task = ee.batch.Export.image.toAsset(
    image=band_4,
    description='imageToAssetExample',
    assetId='projects/your-project/assets/exampleExport',
    crs=projection['crs'],
    crsTransform=projection['transform'],
    region=geometry,
    pyramidingPolicy={
        'b4_mean': 'mean',
        'b4_sample': 'sample',
        'b4_max': 'max',
    },
)
task.start()

您可以使用 '.default' 键为未明确指定的每个频段提供默认的金字塔式折扣政策。您也可以仅传入 '.default' 键。例如,如需将所有频段默认设为“样本”金字塔式广告系列政策,请使用 {'.default': 'sample'}。

配置参数

请注意,传递给 Export.image 的配置参数字典包含 scale(以米为单位)和导出区域(作为 ee.Geometry)。导出的图片将使用指定比例的像素覆盖指定区域。如果未明确指定,输出的 CRS 将从要导出的图片的第一波段中获取。

您还可以指定导出的图片的 dimensions、crs 和/或 crsTransform。如需详细了解 crs 和 crsTransform,请参阅术语表。例如,如需获取与另一个数据源精确对齐的像素块,请指定 dimensions、crs 和 crsTransform。如需获取覆盖某个区域且具有预定义大小(例如 256x256 缩略图)的像素块,请指定 dimensions 和 region。

您可以使用 fileFormat 参数(默认为 'GeoTIFF')指定图片输出格式(如果目标位置不是 toAsset())。

formatOptions 参数

其他配置选项使用 formatOptions 参数进行设置,该参数应为按其他格式选项键值对的字典,具体取决于每个 fileFormat,如下所述。

GeoTIFF

经过 Cloud 优化的 GeoTIFF

如需导出经过云端优化的 GeoTIFF,请为 formatOptions 传递一个 JavaScript 字面量,其中 cloudOptimized 键设置为 true。继续上例:

Code Editor (JavaScript)

// Export a cloud-optimized GeoTIFF.
Export.image.toDrive({
  image: landsat,
  description: 'imageToCOGeoTiffExample',
  crs: projection.crs,
  crsTransform: projection.transform,
  region: geometry,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});

Python 设置

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

import ee
import geemap.core as geemap

Colab (Python)

# Export a cloud-optimized GeoTIFF.
task = ee.batch.Export.image.toDrive(
    image=landsat,
    description='imageToCOGeoTiffExample',
    crs=projection['crs'],
    crsTransform=projection['transform'],
    region=geometry,
    fileFormat='GeoTIFF',
    formatOptions={'cloudOptimized': True},
)
task.start()

您可以将经过 Cloud 优化的 GeoTIFF 从 Cloud Storage 重新加载到 Image。如需了解详情,请参阅 Image 概览文档。

Nodata

使用 formatOptions 参数中的 noData 键指定 GeoTIFF NoData 值。例如:

Code Editor (JavaScript)

// Set a nodata value and replace masked pixels around the image edge with it.
var noDataVal = -9999;
landsat = landsat.unmask(noDataVal);

Export.image.toDrive({
  image: landsat,
  description: 'imageNoDataExample',
  crs: projection.crs,
  scale: 2000,  // large scale for minimal demo
  region: landsat.geometry(),  // full image bounds
  fileFormat: 'GeoTIFF',
  formatOptions: {
    noData: noDataVal,
  }
});

Python 设置

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

import ee
import geemap.core as geemap

Colab (Python)

# Set a nodata value and replace masked pixels around the image edge with it.
no_data_val = -9999
landsat = landsat.unmask(no_data_val)

task = ee.batch.Export.image.toDrive(
    image=landsat,
    description='imageNoDataExample',
    crs=projection['crs'],
    scale=2000,  # large scale for minimal demo
    region=landsat.geometry(),  # full image bounds
    fileFormat='GeoTIFF',
    formatOptions={'noData': no_data_val},
)
task.start()

请注意,nodata 值应在图片的 PixelType 的有效范围内。您可以通过输出图片元数据并查看第一个波段的 data_type 属性来检查 PixelType。例如,您还可以使用图片方法 toShort() 或 toInt() 将数据转换为特定类型,从而设置图片的 PixelType。

TFRecord

请参阅 TFRecord 数据格式页面。

maxPixels

maxPixels 参数旨在防止无意中创建非常大的导出内容。如果默认值对于预期的输出图片来说太低,您可以提高 maxPixels。例如:

Export.image.toDrive({
  image: landsat,
  description: 'maxPixelsExample',
  crs: projection.crs,
  crsTransform: projection.transform,
  region: geometry,
  maxPixels: 1e9
});

导出大型文件

如果输出图片较大,则会导出为多个文件。如果您要导出为 GeoTIFF 文件,系统会将图片拆分为图块。每个图块的文件名将采用 baseFilename-yMin-xMin 的格式,其中 xMin 和 yMin 是导出图片的总边界框中每个图块的坐标。

如果您要导出到 TFRecord,则系统会在 N+1 个文件后面附加 -00000、-00001、… -0000N。如果您打算对文件执行推理并将预测结果作为图片上传回 Earth Engine,请务必保持此顺序。如需了解详情,请参阅将图片作为 TFRecord 文件上传。

以代码编辑器中显示的形式导出图片

如需导出在 Earth Engine 中屏幕上渲染的图像,请按照可视化图像和合成和拼接部分中所示创建可视化图像。由于代码编辑器使用 'EPSG:3857' CRS,因此请在导出时指定 'EPSG:3857' CRS,以便获得与代码编辑器地图中显示的图像采用相同投影的图片。如需详细了解如何指定输出的分辨率和坐标系,请参阅“配置图片导出”部分。