无法在 python 中使用 google 地球引擎的 reduceRegion 函数
Unable to use the reduceRegion function of google earth engine in python
我尝试将 javascript google 地球引擎工作流程转换为 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 returns 带有结果的字符串值。
在 python 中执行此操作的正确方法是什么?
在 python 地球引擎 API 中,print
不执行服务器端代码和 return 与 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)
为了更好地了解正在发生的事情,我建议研究上面的 link 和 this page。根据我的经验,EE 中的客户端与服务器端内容是最难密切关注的。
我尝试将 javascript google 地球引擎工作流程转换为 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 returns 带有结果的字符串值。
在 python 中执行此操作的正确方法是什么?
在 python 地球引擎 API 中,print
不执行服务器端代码和 return 与 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)
为了更好地了解正在发生的事情,我建议研究上面的 link 和 this page。根据我的经验,EE 中的客户端与服务器端内容是最难密切关注的。