python - 无法在python中使用google Earth引擎的reduceRegion函数

标签 python google-earth-engine

我尝试将 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 代码 from this tutorial返回带有结果的字符串值。

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

最佳答案

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

(In Python, it's necessary to call getInfo() on the object being printed; otherwise the request JSON is printed).

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

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

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

关于python - 无法在python中使用google Earth引擎的reduceRegion函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59301285/

相关文章:

python - 字典理解 Python

python - 安装 PyObjc 时出错 : command '/usr/bin/clang' failed

gis - 在 Google Earth Engine 中按系统索引拆分特征集合?

anaconda - 使用代理服务器初始化 Google Earth Engine API

python - 使用 iloc() 根据范围和单个整数选择列

python - 如何从父类访问子类方法?

javascript - 使用谷歌地球引擎在列表中查找与感兴趣的日期最接近的日期

google-earth-engine - 谷歌地球引擎 : ee. List() 输出为整数?

python - HTTP 错误 999 : Request denied