错误:
FeatureCollection (Error)
Error in map(ID=0): Feature, argument 'geometry': Invalid type. Expected type: Geometry. Actual type: Feature.
原始代码:
var boten = ee.FeatureCollection("projects/ee-suraj7-28/assets/Boten"); var boten_shp = boten.geometry() var Lunagprabang = ee.FeatureCollection("projects/ee-suraj7-28/assets/Luangprabang"); var Lunagprabang_shp = Lunagprabang.geometry() var Vientiana = ee.FeatureCollection("projects/ee-suraj7-28/assets/Vientiana"); var Vientiana_shp = Vientiana.geometry() // Buffer// var boten_buffer=boten_shp.buffer(20000) var Lunagprabang_buffer=Lunagprabang_shp.buffer(20000) var Vientiana_buffer=Vientiana_shp.buffer(20000) var features = ee.FeatureCollection([ ee.Feature(boten_buffer, {label: 'Boten'}), ee.Feature(Lunagprabang_buffer, {label: 'Lunagprabang'}), ee.Feature(Vientiana_buffer, {label: 'vientiana'}), ]) print(features) var years = ee.List.sequence(2021,2022) var calculateClassAreaByYear = features.map(function(feature){ var dw2=years.map(function(year){ var dw1= ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1') .filterDate(ee.Date.fromYMD(year, 1, 1), ee.Date.fromYMD(year, 12, 1)).filter(ee.Filter.bounds(feature)).select('label').reduce(ee.Reducer.mode()).eq(6); return dw1 }) return ee.FeatureCollection(dw2) }) print(calculateClassAreaByYear.flatten())
我们这里可以看到提示期待的类型不对,需求的类型是几何图形,但是这里给的确实矢量,所以我们需要再后面加一个.geometry()
修改后的代码:
var years = ee.List.sequence(2021,2022) var calculateClassAreaByYear = features.map(function(feature){ var dw2=years.map(function(year){ var dw1= ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1') .filterDate(ee.Date.fromYMD(year, 1, 1), ee.Date.fromYMD(year, 12, 31)).filter(ee.Filter.bounds(feature.geometry())).select('label').reduce(ee.Reducer.mode()).eq(6); var areaImage = dw1.multiply(ee.Image.pixelArea()) var area = areaImage.reduceRegion({ reducer: ee.Reducer.sum(), geometry: feature.geometry(), scale: 10, maxPixels: 1e10 })
这里有一个矢量面积的计算函数:
ee.Image.pixelArea()
Generate an image in which the value of each pixel is the area of that pixel in square meters. The returned image has a single band called "area."
ee.Image.pixelArea()
生成一个图像,其中每个像素的值是该像素的面积,单位是平方米。返回的图像有一个称为 "面积 "的单带。