Google Earth Engine Python API:reduceToVectors整数波段报错修复咨询
修复Google Earth Engine中
reduceToVectors的积分类型错误 你当前的代码尝试基于SRTM坡度数据生成矢量多边形,但运行时抛出了Image.reduceToVectors: First band ('slope') of image must be integral错误,原因是reduceToVectors方法要求输入图像的第一个波段必须是整数类型,而ee.Terrain.slope()计算出的坡度值是浮点型,后续的where操作也没有改变其数据类型。
原代码
dataset = ee.Image('CGIAR/SRTM90_V4') elevation = dataset.select('elevation') slope = ee.Terrain.slope(elevation) slope1 = slope.where(slope.lte(200), 1) tree_vectors = slope1.reduceToVectors( geometry=roi, crs=slope1.projection(), scale=40, geometryType='polygon', eightConnected=True, labelProperty='zone', maxPixels=1e12 )
错误信息
EEException("Image.reduceToVectors: First band ('slope') of image must be integral.")
修复方案
只需将处理后的坡度图像转换为整数类型即可,使用.toInt32()方法(GEE中常用32位整数类型)修改slope1的定义:
dataset = ee.Image('CGIAR/SRTM90_V4') elevation = dataset.select('elevation') slope = ee.Terrain.slope(elevation) # 将浮点型图像转换为32位整数类型 slope1 = slope.where(slope.lte(200), 1).toInt32() tree_vectors = slope1.reduceToVectors( geometry=roi, crs=slope1.projection(), scale=40, geometryType='polygon', eightConnected=True, labelProperty='zone', maxPixels=1e12 )
说明
reduceToVectors用于将栅格图像的同质区域转换为矢量多边形,它要求输入的分类标识值必须是整数(比如土地利用分类的类别ID),因此需要确保传入的图像波段为整数类型。通过.toInt32()方法可以将浮点型的坡度数据转换为32位整数,满足方法的参数要求。
内容的提问来源于stack exchange,提问作者arc py
相关产品推荐
相关产品推荐

