匯出圖片

您可以從 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]);

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 值定義。請參閱仿射矩陣範例。

程式碼編輯器 (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():

程式碼編輯器 (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」分頁中建立匯出工作。按一下工作旁的「Run」(執行) 按鈕,即可啟動工作。(如要進一步瞭解工作管理員,請參閱「程式碼編輯器」一節)。系統會在您的雲端硬碟帳戶中建立圖片,並使用指定的 fileFormat。

到 Cloud Storage

如要將映像檔匯出至 Google Cloud Storage bucket,請使用 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
});

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()

與匯出至雲端硬碟一樣,請從「工作」分頁開始匯出。如果您打算將匯出的檔案讀回 Earth Engine (使用 ee.Image.loadGeoTIFF() 或將檔案註冊為 COG 支援的資產),bucket 必須位於支援的位置。

到素材資源

如要將圖片匯出至 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'
  }
});

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' 鍵。舉例來說,如要將所有頻帶預設為「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 (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
  }
});

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()

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,
  }
});

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,系統會將 -00000、-00001、... -0000N 附加至 N+1 個檔案。如果您打算對檔案執行推論,並將預測結果上傳回 Earth Engine 做為圖片,請務必維持這個順序。詳情請參閱「將圖片上傳為 TFRecord 檔案」。

匯出程式碼編輯器中顯示的圖片

如要匯出 Earth Engine 畫面中顯示的圖像,請按照「視覺化圖像」和「合成和拼接」章節的說明,建立視覺化圖像。由於程式碼編輯器使用 'EPSG:3857' CRS,因此請在匯出時指定 'EPSG:3857' CRS,取得與程式碼編輯器地圖中顯示的投影相同的圖片。如要瞭解如何指定輸出內容的解析度和座標系統,請參閱設定圖片匯出作業一節。