首页 > 解决方案 > 无法在python中使用google earth引擎的reduceRegion功能

问题描述

我尝试将 javascript 谷歌地球引擎工作流程转换为 python,但我遇到了一些奇怪的错误。更具体地说,我使用以下脚本来计算预定义区域的平均海拔:

feature_geometry = {
    'type': 'MultiPolygon',
    'coordinates': [[[
        [-113.11777746091163,35.924059850042575],
        [-112.43662511716161,35.924059850042575],
        [-112.43662511716161, 36.52671462113273],
        [-113.11777746091163, 36.52671462113273],
        [-113.11777746091163,35.924059850042575]
    ]]]
}

#Compute the mean elevation in the polygon.
meanDict = srtm.reduceRegion(
  reducer= ee.Reducer.mean(),
  geometry= feature_geometry,
  scale= 90
)


mean = meanDict.get('elevation');
print(mean)

当我执行上述操作时,我得到一个如下字典:

ee.ComputedObject({
  "type": "Invocation",
  "arguments": {
    "dictionary": {
      "type": "Invocation",
      "arguments": {
        "image": {
          "type": "Invocation",
          "arguments": {
            "id": "CGIAR/SRTM90_V4"
          },
          "functionName": "Image.load"
        },
        "reducer": {
          "type": "Invocation",
          "arguments": {},
          "functionName": "Reducer.mean"
        },
        "geometry": {
          "type": "MultiPolygon",
          "coordinates": [
            [
              [
                [
                  -113.11777746091163,
                  35.924059850042575
                ],
                [
                  -112.43662511716161,
                  35.924059850042575
                ],
                [
                  -112.43662511716161,
                  36.52671462113273
                ],
                [
                  -113.11777746091163,
                  36.52671462113273
                ],
                [
                  -113.11777746091163,
                  35.924059850042575
                ]
              ]
            ]
          ]
        },
        "scale": 90
      },
      "functionName": "Image.reduceRegion"
    },
    "key": "elevation"
  },
  "functionName": "Dictionary.get"
})

相反,本教程中的 javascript 代码会返回一个字符串值和结果。

在 python 中执行此操作的正确方法是什么?

标签: pythongoogle-earth-engine

解决方案


在 python 地球引擎 API 中,print不会像在 javascript 中那样执行服务器端代码并​​返回值。来自https://developers.google.com/earth-engine/deferred_execution(靠近底部的旁边):

(在 Python 中,需要对正在打印的对象调用 getInfo();否则会打印请求 JSON)。

因此,要获取值,您需要显式调用.getInfo(),如下所示:

mean = meanDict.get('elevation').getInfo();
print(mean)

为了更好地了解正在发生的事情,我建议您研究上面的链接以及此页面。根据我的经验,EE 中的客户端与服务器端的东西是最难关注的。


推荐阅读