匯出圖片

您可以使用 GeoTIFFTFRecord 格式,從 Earth Engine 匯出圖片。如需其他輸出選項,請參閱「設定參數」。

設定範例

首先定義要匯出的圖片資料:

程式碼編輯器 (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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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] 取得的參數清單。圖片的起源由 xTranslationyTranslation 值定義,圖片的像素大小則由 xScaleyScale 值定義。請參閱仿射矩陣範例

程式碼編輯器 (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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 參數用於填入 crsTransformxScaleyScale 值,但 xTranslationyTranslation 值的計算方式是,如果將這些值除以相應的 xScaleyScale 值,其餘數會是零。這些參數會指定像素格線,其中投影的起點位於像素的角落。這個慣例與部分資料供應商使用的轉譯參數不同,後者使用的是與投影座標原點有偏移的格線。舉例來說,美國地質調查局 (USGS) 提供的 Landsat 影像,其轉譯參數會從投影的來源偏移 1/2 像素 (30 公尺頻帶的偏移為 15 公尺),而歐洲太空總署 (ESA) 提供的 Sentinel-2 影像,其轉譯參數會與投影的來源對齊。如果匯出作業中指定的 crsTransform 與原始圖片的 crsTransform 不符,系統會重新取樣輸出像素 (預設使用最近鄰法),導致產生的圖片相對於原始圖片產生位移。

總而言之,如果您需要將匯出的圖片像素與特定圖片相符,請務必使用 crscrsTransform 參數,全面控制格線。

到雲端硬碟

如要將圖片匯出至雲端硬碟帳戶,請使用 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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 值區,請使用 Export.image.toCloudStorage()。如要將前述範例中的 Landsat 圖像匯出至 Cloud Storage (而非 Google 雲端硬碟),請使用以下指令:

程式碼編輯器 (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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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

如同匯出至雲端硬碟,請從「Tasks」分頁開始匯出。Cloud Storage 值區位置可能會影響效能和儲存空間費用,詳情請參閱 常見問題:位置考量因素

到「資產」

如要將圖片匯出至 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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 會從要匯出的圖像第一個頻帶取得。

您也可以指定匯出圖片的 dimensionscrs 和/或 crsTransform。如要進一步瞭解 crscrsTransform,請參閱詞彙解釋。舉例來說,如要讓某個像素區塊精確對齊其他資料來源,請指定 dimensionscrscrsTransform。如要取得涵蓋區域的預先定義大小 (例如 256x256 縮圖圖片) 的像素區塊,請指定 dimensionsregion

您可以使用 fileFormat 參數 (預設為 'GeoTIFF') 指定圖片輸出格式 (如果目的地不是 toAsset())。

formatOptions 參數

其他設定選項則是使用 formatOptions 參數設定,該參數應為以其他格式選項做為索引的字典,並針對每個 fileFormat 設定特定值,如下所述。

GeoTIFF

雲端最佳化 GeoTIFF

如要匯出雲端最佳化 GeoTIFF,請傳遞 formatOptions 的 JavaScript 文字常值,其中 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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 無資料值。例如:

程式碼編輯器 (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 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 的格式,其中 xMinyMin 是匯出圖片整體邊界框內每個圖塊的座標。

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

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

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