匯出地圖圖塊

如要瞭解如何匯出為其他格式 (包括圖片、影片和表格 (向量資料)),請參閱「匯出資料」。

總覽

如要在線上地圖上顯示圖片資料 (光柵資料),您可以使用 Export.map.toCloudStorage() 匯出地圖圖塊。這項函式會將地圖圖塊金字塔匯出至 Cloud Storage bucket,並可從該處公開或私下提供。如果圖片資料過大,無法疊加為單一圖片,就很適合使用這個方法。這些圖塊採用 Google 地圖圖塊座標,適合使用 Google Maps Platform (Maps API)、Google 地球網頁版和其他地圖平台顯示。您可以使用匯出時提供的 HTML 檔案,在 Google 地圖或 Google 地球中輕鬆預覽圖塊。

匯出時有許多設定和步驟,視您打算如何使用或發布圖塊而定,有些可能不必要。部分地圖平台要求圖塊必須可公開存取,有些則可使用私人圖塊。如果使用其中一個 Google Maps API,您可能需要 Google Maps Platform API 金鑰。如果您要將圖塊提供給 Google 地球或其他地圖平台,則可能需要根據圖塊的使用方式,設定適當的存取層級和 JS 存取權 (CORS) 選項。如需必要設定,請參閱下方的平台專屬指南一節。

將地圖圖塊匯出至 Cloud Storage

以下範例會從加州某個區域的 Landsat 圖片匯出圖塊。

程式碼編輯器 (JavaScript)

// --- Example Export Map Tiles - basic ---
// Specify area to clip/export, setup image and preview on map.
var exportRegion = ee.Geometry.BBox(-122.9, 37.1, -121.2, 38.2);
var landsatImage = ee.Image('LANDSAT/LC09/C02/T1_TOA/LC09_044034_20220111')
  .select(['B4', 'B3', 'B2'])
  .visualize({min: 0.02, max: 0.4, gamma: 1.2})
  .clip(exportRegion);
Map.addLayer(landsatImage, {}, 'landsatImage');
Map.centerObject(exportRegion);

// Set up Export task.
Export.map.toCloudStorage({
  image: landsatImage,
  description: 'mapTilesForEE',
  bucket: 'yourBucketName',  // replace with your GCS bucket name
  fileFormat: 'auto',
  maxZoom: 13,
  region: exportRegion,
  writePublicTiles: true
});

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

import ee
import geemap.core as geemap

Colab (Python)

# --- Example Export Map Tiles - basic ---
# Specify area to clip/export, setup image and preview on map.
export_region = ee.Geometry.BBox(-122.9, 37.1, -121.2, 38.2)
landsat_image = (
    ee.Image('LANDSAT/LC09/C02/T1_TOA/LC09_044034_20220111')
    .select(['B4', 'B3', 'B2'])
    .visualize(min=0.02, max=0.4, gamma=1.2)
    .clip(export_region)
)
m = geemap.Map()
m.add_layer(landsat_image, {}, 'landsatImage')
m.center_object(export_region)
display(m)

# Set up Export task.
task = ee.batch.Export.map.toCloudStorage(
    image=landsat_image,
    description='mapTilesForEE',
    bucket='yourBucketName',  # replace with your GCS bucket name
    fileFormat='auto',
    maxZoom=13,
    region=export_region,
    writePublicTiles=True,
)
task.start()

請注意,這項操作會在指定的 Cloud Storage bucket 中建立新目錄,預設會根據 description 參數命名。您可以使用 path 參數變更目錄名稱或指定子目錄。您可以使用輸出目錄中的 index.html 和 earth.html 檔案,在 Google 地圖或 Google 地球中預覽圖塊。

ACL 和 CORS 選項

上述範例設定了圖塊的 ACL (存取控管清單),讓圖塊可透過直接要求公開存取,但未設定 bucket 的 JS 存取權 (CORS 設定),允許網頁應用程式存取圖塊。也就是說,這些匯出的圖塊可以在地圖和地球預覽畫面中查看,但無法用於需要 JS 存取權的其他平台。系統會假設您是輸出 bucket 的擁有者。 如果您是指定輸出 bucket 的寫入者 (但不是擁有者),請將 writePublicTiles 設為 false,以便使用輸出 bucket 的預設物件 ACL。

如要開放所有網頁應用程式公開存取圖塊,請在工作參數中加入 bucketCorsUris: ['*']。請注意,如果第三方可能使用您的圖塊並產生雲端費用,則不建議提供完整的公開 JS 存取權。如要限制這項功能,您可以將 JS 存取權限制為僅限特定網域或網址的網頁應用程式。請注意,JS 存取權 (CORS) 設定會套用至整個 bucket 層級,而非資料夾或物件層級。

您可以使用 writePublicTiles 和 bucketCorsUris 參數分別設定 ACL 和 CORS,也可以透過匯出工作對話方塊設定,如下所示:

Export.map 對話方塊
Export.map.toCloudStorage() 工作對話方塊。
  • 查看/下載存取權。如要存取 (例如透過程式碼編輯器或 Earth Engine 應用程式),則必須將圖塊設為「公開存取」。
  • JS 存取權。如要讓程式碼編輯器、Earth Engine 應用程式或其他網頁應用程式存取圖塊,匯出地圖的 bucket 必須允許讀取。這些跨來源要求設定 (CORS) 必須在 bucket 層級設定。為方便起見,Earth Engine 匯出對話方塊可讓您在匯出時設定 bucket 的 CORS。
  • 注意:JS 存取權/CORS 設定是在 bucket 層級套用,而非資料夾或物件層級,因此變更設定會更新 bucket 中的所有資料夾和物件。如要瞭解詳情,請參閱 Cloud Storage CORS 說明文件,包括如何查看 bucket 目前的 CORS 設定。

參數清單

以下列出地圖圖塊匯出工作的所有參數,並顯示程式碼中的參數 (引數) 名稱,以及工作對話方塊中的對應欄位 (如適用)。請注意,部分欄位互斥 (例如 maxzoom 和 scale)。詳情請參閱 Export.map 說明文件。

程式碼中的引數 工作對話方塊中的欄位 附註
圖片 不適用 要匯出為圖塊的圖片。必須是 Image,而非 ImageCollection。
說明 工作名稱 人類可讀的工作名稱。預設為「myExportMapTask」。 如果未指定「path」(輸出前置字元) 參數,這個名稱也會做為圖塊的資料夾名稱。
值區 GCS bucket 名稱 要寫入的目的地 bucket。
fileFormat 檔案格式 地圖圖塊的檔案格式,可為「auto」、「png」或「jpg」。 預設值為「auto」,表示不透明圖塊會編碼為「jpg」,透明圖塊則會編碼為「png」。
路徑 輸出內容前置字串 做為輸出內容路徑的字串。結尾不一定要加上「/」。 預設為工作說明 (工作名稱)
writePublicTiles 查看/下載存取層級 是否要寫入公開圖塊,而非使用 bucket 的預設物件 ACL。預設為 true,且叫用者為 bucket 的擁有者。
maxZoom 最高解析度和最大縮放比例 要匯出的地圖圖塊縮放等級上限。
比例 最大解析度和最大縮放比例 圖片解析度上限 (單位為公尺/像素),可做為「maxZoom」的替代選項。比例尺會轉換為赤道上最合適的最大縮放等級。
minZoom 最高解析度和最小縮放比例 要匯出的地圖圖塊縮放等級下限 (選用)。 預設值為零。
區域 不適用 代表要匯出區域的 LinearRing、多邊形或座標。 詳情請參閱說明文件。
skipEmptyTiles 不適用 如果設為 true,則略過寫入空白 (即完全透明) 的地圖圖塊。 預設值為 false。
mapsApiKey Maps API 金鑰 在 index.html 中用於初始化 Google Maps API。這樣一來,地圖上就不會顯示「僅供開發用途」訊息。
bucketCorsUris 新增 JS 存取權 (和允許的網站) 允許透過 JavaScript (CORS) 擷取匯出圖塊的網域清單 (例如 https://code.earthengine.google.com)。

縮放

在上述範例中,maxZoom 設為 13。 縮放等級對應不同大小的像素網格,用於顯示全球地圖。(詳情請參閱像素座標參考資料)。由於地球曲率,在特定縮放等級下,像素解析度會因緯度而異。具體來說,每像素公尺數會減少 cos(latitude) 倍。下表顯示 Google 麥卡托投影在赤道各縮放等級的每像素公尺數:

縮放級別 像素大小 (赤道) 縮放級別 像素大小 (赤道)
0 156 公里 10 152 公尺
1 78 公里 11 76 公尺
2 39 公里 12 38 公尺
3 20 公里 13 19 公尺
4 10 公里 14 9.6 公尺
5 4.9 公里 15 4.8 公尺
6 2.4 公里 16 2.4 公尺
7 1.2 公里 17 1.2 公尺
8 611 公尺 18 0.6 公尺
9 305 公尺 19 0.3 公尺
20 0.15 公尺

從上表可以看出,範例中設定的 maxZoom 對應赤道 19 公尺,緯度越高則越小。這低於輸入圖片每像素 30 弧秒的標稱解析度。因此,輸出 index.html 中顯示的地圖可以放大,直到地圖中顯示原生解析度的像素。如要將地圖顯示限制為原生像素解析度,請將 maxZoom 設為對應原生解析度或更低的值。



平台專屬規範

以下列出您可能想查看及使用地圖圖塊的平台。每個部分都會指出圖塊在該平台運作所需的設定,以及含有必要參數的範例程式碼副本。

在 Earth Engine (程式碼編輯器或 EE 應用程式) 中使用地圖圖塊

如果只想匯出地圖圖塊,以便在 Google Earth Engine 或 EE 應用程式中重複使用,請將「存取層級」設為「可公開存取」,並將「JS 存取層級」(在工作對話方塊中) 設為「新增 Earth Engine 存取權」或「新增公開存取權」。或者,您也可以在程式碼中設定這些參數:

writePublicTiles: true,
bucketCorsUris: ['https://code.earthengine.google.com','https://*.earthengine.app']

匯出後,即可在 Earth Engine 中存取。

程式碼編輯器 (JavaScript)

// --- Export Map Tiles for use in EE ---
// Use image setup code from example at top of the page.
// Set up Export task.
Export.map.toCloudStorage({
  image: landsatImage,
  description: 'mapTilesForEE',
  bucket: 'yourBucketName',  // replace with your GCS bucket name
  fileFormat: 'auto',
  maxZoom: 13,
  region: exportRegion,
  writePublicTiles: true,
  bucketCorsUris: ['https://code.earthengine.google.com','https://*.earthengine.app']
});

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

import ee
import geemap.core as geemap

Colab (Python)

# --- Export Map Tiles for use in EE ---
# Use image setup code from example at top of the page.
# Set up Export task.
task = ee.batch.Export.map.toCloudStorage(
    image=landsat_image,
    description='mapTilesForEE',
    bucket='yourBucketName',  # replace with your GCS bucket name
    fileFormat='auto',
    maxZoom=13,
    region=export_region,
    writePublicTiles=True,
    bucketCorsUris=[
        'https://code.earthengine.google.com',
        'https://*.earthengine.app',
    ],
)
task.start()

查看 Google 地圖預覽

如要在 Google 地圖上預覽地圖圖塊,請前往 Google Cloud Storage 的輸出資料夾,然後在瀏覽器中開啟「index.html」檔案。這會開啟全頁地圖,使用 Google 地圖平台 JavaScript API 在 Google 地圖上顯示圖塊,如下所示: index.html。基本匯出作業會產生灰色的底圖圖塊,並加上「僅供開發使用」浮水印。如要避免這種情況,並顯示標準 Google 基本地圖圖塊,請產生 API 金鑰,並將其納入匯出設定。

如果您在匯出時提供 API 金鑰,這個預覽頁面就會公開顯示,並可嵌入其他頁面,檢視者不需註冊 Earth Engine 即可查看。

程式碼編輯器 (JavaScript)

// --- Export Map Tiles for use with Map Preview ---
// Use image setup code from example at top of the page.
// Set up Export task.
Export.map.toCloudStorage({
  image: landsatImage,
  description: 'mapTilesForMapPreview',
  bucket: 'yourBucketName',  // replace with your GCS bucket name
  fileFormat: 'auto',
  maxZoom: 13,
  region: exportRegion,
  writePublicTiles: true,
  mapsApiKey: 'fakeMapsApiKey012345' // replace with a valid API Key
});

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

import ee
import geemap.core as geemap

Colab (Python)

# --- Export Map Tiles for use with Map Preview ---
# Use image setup code from example at top of the page.
# Set up Export task.
task = ee.batch.Export.map.toCloudStorage(
    image=landsat_image,
    description='mapTilesForMapPreview',
    bucket='yourBucketName',  # replace with your GCS bucket name
    fileFormat='auto',
    maxZoom=13,
    region=export_region,
    writePublicTiles=True,
    mapsApiKey='fakeMapsApiKey012345',  # replace with a valid API Key
)
task.start()

查看 Google 地球預先發布版

如要在 Google 地球網頁版中預覽地圖圖塊,請務必將存取層級設為「可公開存取」,再匯出圖塊。接著前往 Google Cloud Storage 的輸出資料夾,在瀏覽器中開啟「earth.html」檔案,如下所示: earth.html ,然後按一下顯示的「在 Google 地球中開啟」按鈕。系統會開啟 Google 地球,並在 3D 地圖上顯示圖塊。最佳做法是將「JS 存取權」層級設為「可公開存取」,這樣一來,您就能在 Google 地球中使用圖塊,而不必使用預覽檔案 (如果圖塊沒有必要的 JS 存取權/CORS 設定,則會使用 Proxy 伺服器)。

如要下載含有資料連結的 KML 檔案,請按一下 Google 地球中的三點圖示選單,然後選取「匯出為 KML 檔案」。注意:以這種方式產生的 KML 檔案與 Google 地球專業版 (地球電腦版) 不相容。

程式碼編輯器 (JavaScript)

// --- Export Map Tiles for use with Earth Preview ---
// Use image setup code from example at top of the page.
// Set up Export task.
Export.map.toCloudStorage({
  image: landsatImage,
  description: 'mapTilesForEarthPreview',
  bucket: 'yourBucketName',  // replace with your GCS bucket name
  fileFormat: 'auto',
  maxZoom: 13,
  region: exportRegion,
  writePublicTiles: true,
  bucketCorsUris: []  // leaving blank is ok for Earth Preview only
  // for direct use in Earth, set to: ['https://earth.google.com']
  // or set to public: ['*'] (risk of misuse)
});

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

import ee
import geemap.core as geemap

Colab (Python)

# --- Export Map Tiles for use with Earth Preview ---
# Use image setup code from example at top of the page.
# Set up Export task.
task = ee.batch.Export.map.toCloudStorage(
    image=landsat_image,
    description='mapTilesForEarthPreview',
    bucket='yourBucketName',  # replace with your GCS bucket name
    fileFormat='auto',
    maxZoom=13,
    region=export_region,
    writePublicTiles=True,
    bucketCorsUris=[],  # leaving blank is ok for Earth Preview only
    # for direct use in Earth, set to: ['https://earth.google.com']
    # or set to public: ['*'] (risk of misuse)
)
task.start()

使用 Google Maps Platform (Maps API) 發布

如要建立一組地圖圖塊,準備透過 Google 地圖平台公開發布,您必須擁有或建立 API 金鑰,並確認存取層級設為「公開存取」。視 Maps API 應用程式存取圖塊的方式而定,您可能也需要為網站設定適當的「JS 存取層級」。匯出圖塊後,程式碼編輯器「Task」(工作) 分頁中的輸出內容會提供圖塊網址,並附上適合與 Google Maps API 搭配使用的變數,例如: https://storage.googleapis.com/my_bucket/my_test_tiles/{Z}/{X}/{Y}

程式碼編輯器 (JavaScript)

// --- Export Map Tiles for use with Maps Platform APIs ---
// Use image setup code from example at top of the page.
// Set up Export task.
Export.map.toCloudStorage({
  image: landsatImage,
  description: 'mapTilesForMapsPlatform',
  bucket: 'yourBucketName',  // replace with your GCS bucket name
  fileFormat: 'auto',
  maxZoom: 13,
  region: exportRegion,
  writePublicTiles: true,
  bucketCorsUris: ['*'],  // '*' = All domains = risk of misuse
  // For better protection, specify the domain(s) where the
  // tiles will be used, eg: ['https://mysite.mydomain.com']
  mapsApiKey: 'fakeMapsApiKey012345' // replace with a valid API Key
});

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

import ee
import geemap.core as geemap

Colab (Python)

# --- Export Map Tiles for use with Maps Platform APIs ---
# Use image setup code from example at top of the page.
# Set up Export task.
task = ee.batch.Export.map.toCloudStorage(
    image=landsat_image,
    description='mapTilesForMapsPlatform',
    bucket='yourBucketName',  # replace with your GCS bucket name
    fileFormat='auto',
    maxZoom=13,
    region=export_region,
    writePublicTiles=True,
    bucketCorsUris=['*'],  # '*' = All domains = risk of misuse
    # For better protection, specify the domain(s) where the
    # tiles will be used, eg: ['https://mysite.mydomain.com']
    mapsApiKey='fakeMapsApiKey012345',  # replace with a valid API Key
)
task.start()

發布至 Google 地球 (網頁版)

如要建立一組地圖圖塊,以便透過 Google 地球公開發布,請將「存取層級」設為「可公開存取」,並將「JS 存取層級」設為允許「https://earth.google.com」(或「可公開存取」)。匯出後,您可以使用匯出目錄中的 earth.html 檔案 (請參閱上文),在 Google 地球中預覽。

程式碼編輯器 (JavaScript)

// --- Export Map Tiles for use with Google Earth web ---
// Use image setup code from example at top of the page.
// Set up Export task.
Export.map.toCloudStorage({
  image: landsatImage,
  description: 'mapTilesForEarthWeb',
  bucket: 'yourBucketName',  // replace with your GCS bucket name
  fileFormat: 'auto',
  maxZoom: 13,
  region: exportRegion,
  writePublicTiles: true,
  bucketCorsUris: ['https://earth.google.com']
  // ['*'] will also work, but risks misuse
});

Python 設定

如要瞭解 Python API 和使用 geemap 進行互動式開發,請參閱「Python 環境」頁面。

import ee
import geemap.core as geemap

Colab (Python)

# --- Export Map Tiles for use with Google Earth web ---
# Use image setup code from example at top of the page.
# Set up Export task.
task = ee.batch.Export.map.toCloudStorage(
    image=landsat_image,
    description='mapTilesForEarthWeb',
    bucket='yourBucketName',  # replace with your GCS bucket name
    fileFormat='auto',
    maxZoom=13,
    region=export_region,
    writePublicTiles=True,
    bucketCorsUris=['https://earth.google.com'],
    # ['*'] will also work, but risks misuse
)
task.start()

匯出圖塊後,即可將圖塊新增至 Google 地球網頁版中的專案。程式碼編輯器「Tasks」(工作) 分頁中的輸出內容 (如下所示) 會提供圖塊網址,並附上適合在 Google 地球中使用的變數,例如: https://storage.googleapis.com/my_bucket/my_test_tiles/$[level]/$[x]/$[y]

圖塊匯出工作的輸出網址
圖塊匯出工作的輸出網址。

如要在 Google 地球網頁版中使用,請建立圖塊疊加層地圖項目,並將這個網址新增為「疊加層網址」。如果資料集並非全球資料集,且您想避免不存在圖塊的圖塊要求錯誤,請務必開啟「疊加層選項」部分,並調整四個圖塊涵蓋範圍參數,以符合匯出圖塊的範圍。

Google 地球網頁版顯示的地圖圖塊
在 Google 地球網頁版中顯示的地圖圖塊。

發布至其他地圖平台

匯出圖塊以用於其他平台或應用程式時,必要設定取決於這些平台或應用程式存取圖塊的方式。如要盡可能讓更多人存取圖塊,請將「存取層級」和「JS 存取層級」設為「可公開存取」。



產生 Google Maps Platform API 金鑰

如果您打算搭配 Google 地圖平台使用地圖圖塊,或想在 Google 地圖上預覽圖塊,而不使用開發人員受限底圖,則需要擁有或產生 Google 地圖平台 API 金鑰,並將金鑰加入匯出設定,或在之後新增至網站。但這並非使用 Google 地球的必要條件。

從 Earth Engine 匯出地圖圖塊時,我們會產生範例 HTML 網頁,並儲存在輸出目錄中,方便您使用 Google Maps Platform JavaScript API 預覽圖塊。您可以在匯出時提供 Google Maps Platform API 金鑰,用於範例檢視器的 API 呼叫。

如要立即產生 Google 地圖平台 API 金鑰,請按照下列四個步驟操作:

  • 點選下方的「建立 Maps Platform API 金鑰」按鈕。
  • 選取 API 金鑰的雲端專案,然後點選「下一步」。
  • 按一下 複製新金鑰,然後貼到程式碼編輯器的「匯出地圖」對話方塊中。
  • (建議) 按一下新金鑰下方的 API 控制台連結,設定參照網址限制。詳情請參閱參照網址和應用程式限制。

建立地圖平台 API 金鑰

輸入專案名稱 車鑰已可使用

為 API 金鑰新增參照網址限制

新增 API 金鑰時,參照網址限制可確保只有指定的應用程式可以使用金鑰。您隨時可以前往 Cloud 控制台的「憑證」頁面,按照下列步驟設定或編輯參照網址限制:

  • 確認畫面頂端的下拉式選單中已選取正確的專案名稱。
  • 按一下上方產生的 Maps Platform API 金鑰,查看金鑰詳細資料。
  • 選取「HTTP referrers (websites)」(HTTP 參照網址 (網站)),然後使用下列範本指定 Export.map.toCloudStorage 中指定的 Cloud Storage bucket:https://storage.googleapis.com/{bucket}/*
  • 按一下「儲存」。系統應會顯示「儲存中...」指標。
新增參照網址