使用 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 命令行生成服务账号的密钥文件。系统将在笔记本虚拟机上创建密钥文件。

# 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,可供其他应用或进程使用。