我在减去两个图像时遇到问题。任何人都可以更正我的代码吗?
我想从一幅图像中减去另一幅图像,然后打印减去的图像。但我收到这个错误:
lst2020.subtract 不是函数
解析:
这里主要的问题是影像集合而不是影像,所以我们无法进行直接相减,我们这里需要注意的一个问题是要么单景影像,或者我们可以用镶嵌后的应先该选择相同的而波段进行相减分析或者其它四则运算。否则还会出现其它错误。
原始代码:
注意哦这里面的研究区需要自己替换下,所以你要加入研究区进去,也就是把下面代码中的US换成自己的研究区去尝试代码,避免其它的错误。
var modis = ee.ImageCollection("MODIS/061/MOD11A2") /// Temperature Changes /// Year 2011-2020 // Add Study area map outline to the Map as a layer. Map.centerObject(US,4); // Map.addLayer(US); var lst2001 = modis .filterDate('2001-01-01','2001-12-31') .filterBounds(US) // .select(['LST_Night_1km','LST_Day_1km']) .map(function(img){ var bands = img.select(['LST_Night_1km']) .multiply(0.02) .subtract(273.15) .clip(US) return bands .copyProperties(img,['system:time_start','system:time_end']); }); // print (lst) Map.addLayer(lst2001.select('LST_Night_1km').mean(),{palette: ['blue','limegreen','yellow','darkorange','red']},'LST Night 2020', false); var lst2020 = modis .filterDate('2020-01-01','2020-12-31') .filterBounds(US) // .select(['LST_Night_1km','LST_Day_1km']) .map(function(img){ var bands = img.select(['LST_Night_1km']) .multiply(0.02) .subtract(273.15) .clip(US) return bands .copyProperties(img,['system:time_start','system:time_end']); }); // print (lst) Map.addLayer(lst2020.select('LST_Night_1km').mean(),{palette: ['blue','limegreen','yellow','darkorange','red']},'LST Night 2001', false); /// Subtraction var diff = lst2020.subtract(lst2001); print('diff', diff);
修改后的 代码:
var modis = ee.ImageCollection("MODIS/061/MOD11A2") /// Temperature Changes /// Year 2011-2020 // Add Study area map outline to the Map as a layer. //Map.centerObject(US,4); // Map.addLayer(US); var lst2001 = modis .filterDate('2001-01-01','2001-12-31') .filterBounds(US) // .select(['LST_Night_1km','LST_Day_1km']) .map(function(img){ var bands = img.select(['LST_Night_1km']) .multiply(0.02) .subtract(273.15) .clip(US) return bands .copyProperties(img,['system:time_start','system:time_end']); }); // print (lst) Map.addLayer(lst2001.select('LST_Night_1km').mean(),{palette: ['blue','limegreen','yellow','darkorange','red']},'LST Night 2020', false); var lst2020 = modis .filterDate('2020-01-01','2020-12-31') .filterBounds(US) // .select(['LST_Night_1km','LST_Day_1km']) .map(function(img){ var bands = img.select(['LST_Night_1km']) .multiply(0.02) .subtract(273.15) .clip(US) return bands .copyProperties(img,['system:time_start','system:time_end']); }); // print (lst) Map.addLayer(lst2020.select('LST_Night_1km').mean(),{palette: ['blue','limegreen','yellow','darkorange','red']},'LST Night 2001', false); /// Subtraction var diff1 = lst2020.select(['LST_Night_1km']).first() var diff2 =lst2001.select(['LST_Night_1km']).first(); var diff =diff1.subtract(diff2) print('diff', diff); //或者下面的方法 /// Subtraction var diff = lst2020.mosaic().subtract(lst2001.mosaic()); print('diff', diff);
修改后的结果: