如何在GEE中将Javascript转换为FeatureCollection的Python API?

问题描述

我有一个运行良好的JS代码

dataset = ee.Image('USGS/SRTMGL1_003');
elevation = dataset.select('elevation');

var means_of_tea = tea.map(function(field){
  var elevation_mean = elevation.reduceRegion({
    reducer: ee.Reducer.mean(),geometry: field.geometry(),scale: 30,maxPixels: 1e9
  });
  var slope_mean = slope.reduceRegion({
    reducer: ee.Reducer.mean(),maxPixels: 1e9
  });
  return field.set({elevation:elevation_mean,slope:slope_mean});

我试图将代码转换为python:

def map_fc(field):
  elevation_mean = elevation.reduceRegion({
      'reducer': ee.Reducer.mean(),'geometry': field.geometry(),'scale': 30,'maxPixels': 1e9
  })
  return field.set({'elevation': elevation_mean})
teawithmean = tea.map(map_fc)

但给出错误

<ipython-input-36-e999072d4723> in <module>()
      9   })
     10   return field.set({'elevation': elevation_mean})
---> 11 teawithmean = tea.map(inmap)

17 frames
/usr/local/lib/python3.6/dist-packages/ee/__init__.py in init(self,*args)
    397           raise EEException(
    398               'Invalid argument for ee.{0}(): {1}.  '
--> 399               'Must be a Computedobject.'.format(name,args))
    400         else:
    401           result = args[0]

EEException: Invalid argument for ee.Reducer(): ({'reducer': <ee.Reducer object at 0x7f3b4699e4a8>,'geometry': ee.Geometry({
  "functionInvocationValue": {
    "functionName": "Feature.geometry","arguments": {
      "feature": {
        "argumentReference": null
      }
    }
  }
}),'maxPixels': 1000000000.0},).  Must be a Computedobject.

我已经阅读了有关将JS格式转换为python的Google指南,但不知道为什么会这样。错误是因为语法错误吗?

解决方法

在Python EE API中,使用Python命名参数语法而不是字典作为参数。

  elevation_mean = elevation.reduceRegion(
      reducer=ee.Reducer.mean(),geometry=field.geometry(),scale=30,maxPixels=1e9
  )

请注意,属性名称不是命名参数,因此set()的工作方式与JavaScript中的工作方式相同;您可以使用或不使用字典,但不能使用=

return field.set({'elevation': elevation_mean})
# or
return field.set('elevation',elevation_mean)