您可以从 Earth Engine 导出 GeoTIFF 或 TFRecord 格式的图片。如需了解更多输出选项,请参阅配置参数。
设置示例
首先,定义要导出的图片数据:
代码编辑器 (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]);
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 值定义。查看仿射矩阵示例。
代码编辑器 (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();
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():
代码编辑器 (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 });
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()
运行此代码后,系统会在代码编辑器的任务标签页中创建一个导出任务。点击任务旁边的运行按钮,启动任务。(如需详细了解任务管理器,请参阅代码编辑器部分)。系统将在您的云端硬盘账号中创建具有指定 fileFormat 的图片。
到 Cloud Storage
如需将映像导出到 Google Cloud Storage 存储桶,请使用 Export.image.toCloudStorage()。如需将上一个示例中的 Landsat 影像导出到 Cloud Storage 而不是云端硬盘,请使用以下代码:
代码编辑器 (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 });
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()
与导出到云端硬盘一样,请从任务标签页开始导出。如果您计划将导出的文件重新读取到 Earth Engine 中(使用 ee.Image.loadGeoTIFF() 或将其注册为 COG 支持的资产),则存储桶必须位于受支持的位置。
到资产
如需将图片导出到 Earth Engine 素材资源文件夹中的素材资源,请使用 Export.image.toAsset()。如需管理 Earth Engine 资产或查看已使用的存储空间配额,请使用资产管理器。以下示例展示了如何使用不同的金字塔政策为同一波段导出 Landsat 图像的部分内容。金字塔政策指明了 Earth Engine 如何计算资产的较低分辨率版本。如需详细了解 Earth Engine 如何处理多种分辨率,请参阅比例文档。
代码编辑器 (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' } });
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' 键。例如,如需将所有波段默认设置为“sample”金字塔政策,请使用 {'.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 Optimized GeoTIFF (COG)
如需导出云优化 GeoTIFF (COG),请在 formatOptions 中将 cloudOptimized 键设置为 true。继续前面的示例:
代码编辑器 (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 } });
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()
COG 可以从 Cloud Storage 重新加载到 Image 中,也可以注册为资产。如需了解完整的导出规范和选项,请参阅 COG 概览。
Nodata
使用 formatOptions 参数中的 noData 键指定 GeoTIFF nodata 值。例如:
代码编辑器 (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, } });
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,以获取与代码编辑器地图中显示的投影相同的图片。如需详细了解如何指定输出的分辨率和坐标系,请参阅有关配置图片导出的部分。