使用 Earth Engine REST API 進行表格運算

注意: REST API 包含許多新功能和進階功能,可能不適合所有使用者。如果您是 Earth Engine 新手,請先參閱 JavaScript 指南

Earth Engine REST API 快速入門導覽課程說明如何存取 Earth Engine 資產中的像素區塊。計算像素範例說明如何在取得結果前,將計算作業套用至像素。這個範例示範如何取得 FeatureCollection 中每個特徵的 ImageCollection 中每個圖片的像素平均值。具體來說,這是對 computeFeatures 端點發出的 POST 要求。

事前準備

請按照這些指示操作,以:

  1. 申請使用 Earth Engine
  2. 建立 Google Cloud 專案
  3. 在專案中啟用 Earth Engine API
  4. 建立服務帳戶
  5. 授予服務帳戶專案層級權限,執行 Earth Engine 運算作業

注意:如要完成本教學課程,您需要已註冊 Earth Engine 存取權的服務帳戶。請先按照這些操作說明註冊服務帳戶,再繼續操作。

向 Google Cloud 進行驗證

首先,請登入,以便向 Google Cloud 提出經過驗證的要求。您將同時設定專案。按照輸出內容中的操作說明完成登入。

# INSERT YOUR PROJECT HERE
PROJECT = 'your-project'

!gcloud auth login --project {PROJECT}

取得服務帳戶的私密金鑰檔案

您應該已註冊服務帳戶,才能使用 Earth Engine。如果沒有,請按照這些操作說明取得。將服務帳戶的電子郵件地址複製到下列儲存格。(服務帳戶必須已註冊才能使用 Earth Engine)。在下列儲存格中,gsutil 指令列用於產生服務帳戶的金鑰檔案。金鑰檔案會在筆記本 VM 上建立。

# INSERT YOUR SERVICE ACCOUNT HERE
SERVICE_ACCOUNT='your-service-account@your-project.iam.gserviceaccount.com'
KEY = 'key.json'

!gcloud iam service-accounts keys create {KEY} --iam-account {SERVICE_ACCOUNT}

啟動 AuthorizedSession 並測試憑證

使用私密金鑰取得憑證,藉此測試私密金鑰。使用憑證建立授權工作階段,發出 HTTP 要求。透過工作階段發出 GET 要求,確認憑證是否有效。

from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(KEY)
scoped_credentials = credentials.with_scopes(
    ['https://www.googleapis.com/auth/cloud-platform'])

session = AuthorizedSession(scoped_credentials)

url = 'https://earthengine.googleapis.com/v1beta/projects/earthengine-public/assets/LANDSAT'

response = session.get(url)

from pprint import pprint
import json
pprint(json.loads(response.content))

序列化運算作業

如要傳送運算要求,必須先將運算內容轉換為 Earth Engine 運算式圖表格式。以下說明如何取得運算式圖表。

向 Earth Engine 進行驗證

從服務帳戶取得 Earth Engine 範圍憑證。並用來初始化 Earth Engine。

import ee

# Get some new credentials since the other ones are cloud scope.
ee_creds = ee.ServiceAccountCredentials(SERVICE_ACCOUNT, KEY)
ee.Initialize(ee_creds)

定義運算作業

使用用戶端 API 製作簡單的運算原型。請注意,計算結果為 FeatureCollection。如要確認計算作業是否能順利完成,請從第一個 Feature 取得值 (多邊形中的平均 NDVI)。

# A collection of polygons.
states = ee.FeatureCollection('TIGER/2018/States')
maine = states.filter(ee.Filter.eq('NAME', 'Maine'))

# Imagery: NDVI vegetation index from MODIS.
band = 'NDVI'
images = ee.ImageCollection('MODIS/006/MOD13Q1').select(band)
image = images.first()

computation = image.reduceRegions(
  collection=maine, 
  reducer=ee.Reducer.mean().setOutputs([band]), 
  scale=image.projection().nominalScale()
)

# Print the value to test.
print(computation.first().get(band).getInfo())

序列化運算式圖表

這會建立代表 Earth Engine 運算式圖形的物件 (具體來說是 Expression)。一般來說,您應該使用其中一個用戶端 API 建構這些物件。

# Serialize the computation.
serialized = ee.serializer.encode(computation)

傳送要求

computeFeatures 端點發出 POST 要求。請注意,要求包含 Expression,這是序列化運算。

import json

url = 'https://earthengine.googleapis.com/v1beta/projects/{}/table:computeFeatures'

response = session.post(
  url = url.format(PROJECT),
  data = json.dumps({'expression': serialized})
)

import json
pprint(json.loads(response.content))

回應包含以 GeoJSON 格式呈現的結果 FeatureCollection,可供其他應用程式或程序使用。