注意: REST API 包含許多新功能和進階功能,可能不適合所有使用者。如果您是 Earth Engine 新手,請先參閱 JavaScript 指南。
Earth Engine REST API 快速入門導覽課程說明如何存取 Earth Engine 資產中的像素區塊。假設您想在取得結果前,對像素套用運算。本指南說明如何使用其中一個用戶端程式庫,製作運算的原型、序列化運算圖,以及使用 REST API 取得運算結果。透過 REST API 發出運算要求,相當於對其中一個運算端點發出 POST
要求,例如 computePixels
、computeFeatures
或一般 value.compute
。具體來說,這個範例會示範如何取得小區域中 Sentinel-2 影像的中位數合成影像。
事前準備
請按照這些指示操作,以:
- 申請使用 Earth Engine
- 建立 Google Cloud 專案
- 在專案中啟用 Earth Engine API
- 建立服務帳戶
- 授予服務帳戶專案層級權限,執行 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 製作簡單的運算原型。請注意,運算結果是 Image
。
coords = [
-121.58626826832939,
38.059141484827485,
]
region = ee.Geometry.Point(coords)
collection = ee.ImageCollection('COPERNICUS/S2')
collection = collection.filterBounds(region)
collection = collection.filterDate('2020-04-01', '2020-09-01')
image = collection.median()
序列化運算式圖表
這會建立代表 Earth Engine 運算式圖形的物件 (具體來說是 Expression
)。一般來說,您應該使用其中一個用戶端 API 建構這些物件。
serialized = ee.serializer.encode(image)
以所需比例 (Sentinel-2 為 10 公尺) 建立所需投影 (WGS84)。這只是為了找出以度為單位的所需比例,也就是投影的單位。這些比例會用於在要求中指定仿射轉換。
# Make a projection to discover the scale in degrees.
proj = ee.Projection('EPSG:4326').atScale(10).getInfo()
# Get scales out of the transform.
scale_x = proj['transform'][0]
scale_y = -proj['transform'][4]
傳送要求
向 computePixels
端點發出 POST
要求。請注意,要求包含 Expression
,這是序列化運算。此外,該結果也包含 PixelGrid
。PixelGrid
包含所需輸出的 dimensions
,以及以所要求座標系統單位表示的 AffineTransform
。這裡的座標系統是地理座標,因此轉換會以角度和所要求圖片修補程式左上角的地理座標指定比例。
import json
url = 'https://earthengine.googleapis.com/v1beta/projects/{}/image:computePixels'
url = url.format(PROJECT)
response = session.post(
url=url,
data=json.dumps({
'expression': serialized,
'fileFormat': 'PNG',
'bandIds': ['B4','B3','B2'],
'grid': {
'dimensions': {
'width': 640,
'height': 640
},
'affineTransform': {
'scaleX': scale_x,
'shearX': 0,
'translateX': coords[0],
'shearY': 0,
'scaleY': scale_y,
'translateY': coords[1]
},
'crsCode': 'EPSG:4326',
},
'visualizationOptions': {'ranges': [{'min': 0, 'max': 3000}]},
})
)
image_content = response.content
如果您在筆記本中執行這項操作,可以使用 IPython
圖片顯示小工具顯示結果。
# Import the Image function from the IPython.display module.
from IPython.display import Image
Image(image_content)