您可以将 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()
与导出到云端硬盘一样,从任务 标签页启动导出。Cloud Storage 存储桶位置可能会影响性能和存储费用,如需了解详情,请参阅有关位置 注意事项 的常见问题解答 条目。
导出到 Asset
如需将图片导出到 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
云优化的 GeoTIFF
如需导出云优化的 GeoTIFF,请为 JavaScript
字面量 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()
云优化的 GeoTIFF 可以从 Cloud Storage 重新加载到 Image 中,也可以注册为资产。如需了解完整的导出规范和选项,请参阅
云优化的 GeoTIFF (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,文件将附加 -00000、-00001、... -0000N,共 N+1 个文件。如果您打算对文件执行推理并将预测结果作为图片上传回 Earth Engine,请务必保持此顺序。如需了解详情,请参阅以 TFRecord 文件
形式上传图片。
按代码编辑器中显示的方式导出图片
如需按 Earth Engine 中屏幕上呈现的方式导出图像,请创建可视化
图片,如可视化
图片以及合成和
镶嵌部分所示。由于代码编辑器使用
'EPSG:3857' CRS,因此请在导出中指定 'EPSG:3857' CRS,以获取与代码编辑器地图中显示的投影相同的图片。如需详细了解如何指定输出的分辨率和坐标系,请参阅配置图片导出部分。