python - 无法在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 中执行此操作的正确方法是什么?
解决方案
在 python 地球引擎 API 中,print
不会像在 javascript 中那样执行服务器端代码并返回值。来自https://developers.google.com/earth-engine/deferred_execution(靠近底部的旁边):
(在 Python 中,需要对正在打印的对象调用 getInfo();否则会打印请求 JSON)。
因此,要获取值,您需要显式调用.getInfo()
,如下所示:
mean = meanDict.get('elevation').getInfo();
print(mean)
为了更好地了解正在发生的事情,我建议您研究上面的链接以及此页面。根据我的经验,EE 中的客户端与服务器端的东西是最难关注的。
推荐阅读
- flutter - 在库比蒂诺路线之间导航不起作用
- python - 三维输出损耗
- express - Swagger openapi 3.0.x 空体
- javascript - JavaScript 数组有元素但长度为零
- c++ - 读取十六进制文件 C++
- xamarin - 关于如何处理框架资源提取的任何想法都失败了。神秘地开始出现在 UWP 中的错误
- themes - 如何将颜色更改为括号/括号?
- apache - 如何为 apache2.4 设置工作且安全的 CORS 配置?
- android - Android 错误 Java 编译器:使用或覆盖已弃用的 API
- ansible - 如何在 Ansible 中跳过剩余任务并创建某些条件