You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask应用中GEE Python API对象转Python变量的报错问题

问题:Flask + GEE API 部署时getInfo()报错Element.get: Parameter 'object' is required

问题背景

通过Flask搭建API端点,利用Google Earth Engine(GEE)Python API处理托管数据,接收地理坐标后返回同像素值的农业地块信息。Colab测试正常,但本地运行或通过ngrok部署时触发错误,尝试改用异步Flask也未解决。

相关代码

async def get_mapBiomas_feature(latitude, longitude):
      point = ee.Geometry.Point(longitude, latitude)
      image_final = ee.Image('projects/mapbiomas-workspace/public/collection8/mapbiomas_collection80_integration_v1').select("classification_2022")
      point_class_value_2022 = image_final.sample(point, 1).first().get("classification_2022")
      if point_class_value_2022 in culturas:
        cultura_2022 = class_names.get(str(point_class_value_2022))
      else:
        cultura_2022 = "Coberta do solo nao contem cultura agricola"
      masked_image_2022 = image_final.updateMask(image_final.eq(ee.Image.constant(point_class_value_2022)))
      connected_image_labels_2022 = masked_image_2022.connectedComponents(ee.Kernel.square(1), 1024).select("labels")
      await asyncio.sleep(5)
      buffered_point = point.buffer(50000)
      clipped_image_2022 = connected_image_labels_2022.clip(buffered_point)
      vectors_2022 = await reduceVectors(clipped_image_2022)
      selected_feature_2022 = vectors_2022.filterBounds(point).first()
      geometry_2022 = selected_feature_2022.geometry().simplify(1000)
      coordinates_2022 = geometry_2022.coordinates()
      area_hectares_2022 = geometry_2022.area(maxError=100).divide(10000)
      properties_2022 = {'area_hectares': area_hectares_2022, 'origem': cultura_2022, "poligono": coordinates_2022}
      print("Properties 2022", properties_2022)
      results = ee.Dictionary(properties_2022).getInfo()      
      await asyncio.sleep(5)
      print(results)
      return results

报错信息

dict = ee.Dictionary(properties).getInfo()
  File "/usr/local/lib/python3.10/dist-packages/ee/computedobject.py", line 105, in getInfo
    return data.computeValue(self)
  File "/usr/local/lib/python3.10/dist-packages/ee/data.py", line 1040, in computeValue
    return _execute_cloud_call(
  File "/usr/local/lib/python3.10/dist-packages/ee/data.py", line 356, in _execute_cloud_call
    raise _translate_cloud_exception(e)  # pylint: disable=raise-missing-from
ee.ee_exception.EEException: Element.get: Parameter 'object' is required.
INFO:werkzeug:127.0.0.1 - - [25/Oct/2023 15:36:23] "GET /MapBiomas_cultura?lat=-55.09&lon=-7.73 HTTP/1.1" 500 -

错误分析

  1. 核心问题:point_class_value_2022是GEE服务器端ComputedObject,直接用Python本地的in运算符判断是否在culturas列表中会失效——服务器端对象无法直接与本地列表做成员比较,导致cultura_2022的逻辑判断错误。
  2. 连锁反应:后续构建properties_2022时,混合了未解析的GEE服务器端对象(area_hectares_2022、coordinates_2022)和本地字符串,传给ee.Dictionary后调用getInfo()时触发参数异常。
  3. Colab特殊情况:Colab环境中GEE可能存在隐式缓存或临时转换机制,偶然让错误逻辑正常运行,但部署环境无此特性。

修复方案

1. 提前解析服务器端对象为本地值

对需要本地逻辑判断的GEE对象,先调用getInfo()转为Python原生类型:

# 先解析为本地数值
point_class_value_2022 = image_final.sample(point, 1).first().get("classification_2022").getInfo()
if point_class_value_2022 in culturas:
  cultura_2022 = class_names.get(str(point_class_value_2022))
else:
  cultura_2022 = "Coberta do solo nao contem cultura agricola"

2. 避免混合构建ee.Dictionary

单独解析所有GEE服务器端对象为本地值,直接构建Python字典返回,无需再用ee.Dictionary包装:

# 单独解析每个GEE对象
coords_2022 = geometry_2022.coordinates().getInfo()
area_2022 = geometry_2022.area(maxError=100).divide(10000).getInfo()

# 直接构建本地字典
results = {
    'area_hectares': area_2022,
    'origem': cultura_2022,
    'poligono': coords_2022
}

3. 移除无效异步休眠

asyncio.sleep(5)对GEE的异步处理无意义,直接删除,避免不必要的延迟。

4. 确保reduceVectors函数正确解析

检查reduceVectors函数内部,确保返回的是已解析的本地对象,而非未处理的GEE服务器端对象。

修复后完整代码示例

async def get_mapBiomas_feature(latitude, longitude):
    point = ee.Geometry.Point(longitude, latitude)
    image_final = ee.Image('projects/mapbiomas-workspace/public/collection8/mapbiomas_collection80_integration_v1').select("classification_2022")
    
    # 提前解析为本地值
    point_class_value_2022 = image_final.sample(point, 1).first().get("classification_2022").getInfo()
    if point_class_value_2022 in culturas:
        cultura_2022 = class_names.get(str(point_class_value_2022))
    else:
        cultura_2022 = "Coberta do solo nao contem cultura agricola"
    
    masked_image_2022 = image_final.updateMask(image_final.eq(ee.Image.constant(point_class_value_2022)))
    connected_image_labels_2022 = masked_image_2022.connectedComponents(ee.Kernel.square(1), 1024).select("labels")
    
    buffered_point = point.buffer(50000)
    clipped_image_2022 = connected_image_labels_2022.clip(buffered_point)
    vectors_2022 = await reduceVectors(clipped_image_2022)
    
    selected_feature_2022 = vectors_2022.filterBounds(point).first()
    geometry_2022 = selected_feature_2022.geometry().simplify(1000)
    
    # 单独解析GEE对象为本地值
    coords_2022 = geometry_2022.coordinates().getInfo()
    area_2022 = geometry_2022.area(maxError=100).divide(10000).getInfo()
    
    # 构建本地字典返回
    results = {
        'area_hectares': area_2022,
        'origem': cultura_2022,
        'poligono': coords_2022
    }
    print(results)
    return results

内容的提问来源于stack exchange,提问作者Pedro C.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 22:45:55