gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:44:25 30 4
gpt4 key购买 nike

我尝试将 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com