在 Google 地球引擎中使用图像集合计算归一化差异
Computing normalizeddifference using image collections in Google earth engine
我是 Google Earth Engine 和 SO 的新手,但这里有:
我正在尝试使用 MODIS 波段 1 和 11 计算一种新型的归一化差异。它们是 500m 和 1km 空间分辨率,来自两个不同的 MODIS 图像集(sur_refl_b01 来自 MOD09GA,分辨率为 500m,sur_refl_b11 来自 MODOCGA,分辨率为 1km)。
我先把1km的数据重采样到500m。
我可以正确显示 sur_refl_b01 和重采样的 sur_refl_b11 数据。
然后我将这两个图像集合并为一个图像集,我称之为“modis_combined”
它具有正确数量的波段,但显示不正确。
Inspector 指示以下错误:
“预期是同质图像集合,但遇到了带不兼容波段的图像:
第一种图像类型:2 波段 ([sur_refl_b01, sur_refl_b01_1])。
当前图像类型:2 波段 ([sur_refl_b11, sur_refl_b11_1])。
图片 ID:2_2_2001_02_28
有些乐队可能需要显式演员表。"
我收到与调用 cci 函数相关的错误,我在其中使用了 normalizedDifference:
ImageCollection(错误)
地图错误(ID=1_2001_03_01):
Image.normalizedDifference:没有名为 'sur_refl_b11' 的乐队。可用波段名称:[sur_refl_b01].
我猜想当我合并图像集时,乐队名称并没有保留下来。如何标记合并的 imageCollection 中的波段,以便 normalizedDifference 函数识别它们?
或者还有什么乱七八糟的事情发生?
// Define a sample Region-of-Interest
var roi = ee.Geometry.Polygon(
[[[-122.0, 45.0],
[-122.1, 32.0],
[-108.9, 32.0],
[-108.9, 45.0]]]);
// Load a collection of MODIS land surface reflectance data (500m)
// Select band 1 (red)
// Filter by a date range
// Subset by the region of interest
var modis_b01_500m = ee.ImageCollection('MODIS/006/MOD09GA')
.select(['sur_refl_b01'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in the collection
print('modis_b01_500m', modis_b01_500m);
print('Number of images in modis_b01_500m:', modis_b01_500m.size());
// Define a collection of MODIS ocean color reflectance data (1km)
var modis_b11_1km = ee.ImageCollection('MODIS/006/MODOCGA')
.select(['sur_refl_b11'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in this collection.
print('modis_b11_1km', modis_b11_1km);
print('Number of images in modis_b11_1km:', modis_b11_1km.size());
//Resample the MODIS 1km collection to 500m spatial resolution
var modis_b11_resampled = modis_b11_1km.map(function(image) {
var modis_pro = image.projection();
var image_resampled = image.reproject(modis_pro, null, 500)
.copyProperties(image);
return image_resampled
})
//Check on the number of bands in this resampled collection.
print('modis_b11_resampled',modis_b11_resampled);
print('Number of images in modis_b11_resampled:', modis_b11_resampled.size());
//combine the resampled collection with the modis 500m collection
var modis_combined = modis_b01_500m.merge(modis_b11_resampled);
//Check on the number of bands in this combined collection.
print('modis_combined', modis_combined);
print('Number of images in modis_combined:', modis_combined.size());
//Display the MODIS combined image collection
//Display the layers
Map.addLayer(modis_b01_500m,{min:0,max:10000},'modis_b01_500m');
Map.addLayer(modis_b11_1km,{min:0,max:2000},'modis_b11_1km');
Map.addLayer(modis_b11_resampled, {min:0, max:10000}, 'modis_resampled');
Map.addLayer(modis_combined, {min:0, max:10000}, 'MODIS combined');
var cci = modis_combined.map(function(image){
var cci = image.normalizedDifference(['sur_refl_b11','sur_refl_b01']);
return cci;
})
print('cci', cci);
print('Number of images in cci:', cci.size());
在您的代码中,您使用 .merge()
命令组合两个 ImageCollection。两个 ImageCollection 组合在一起,但是两个集合中的图像彼此分开,因此无法执行需要两个波段图像的 .normalizedDifference
函数。
您需要做的是根据日期加入两个集合,以便将两个集合中具有相同日期的图像合并为一个具有 2 个波段的图像。这将允许您执行 .normalizedDifference
功能。这是怎么回事:
// Define a sample Region-of-Interest
var roi = ee.Geometry.Polygon(
[[[-122.0, 45.0],
[-122.1, 32.0],
[-108.9, 32.0],
[-108.9, 45.0]]]);
// Load a collection of MODIS land surface reflectance data (500m)
// Select band 1 (red)
// Filter by a date range
// Subset by the region of interest
var modis_b01_500m = ee.ImageCollection('MODIS/006/MOD09GA')
.select(['sur_refl_b01'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in the collection
print('modis_b01_500m', modis_b01_500m);
print('Number of images in modis_b01_500m:', modis_b01_500m.size());
// Define a collection of MODIS ocean color reflectance data (1km)
var modis_b11_1km = ee.ImageCollection('MODIS/006/MODOCGA')
.select(['sur_refl_b11'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in this collection.
print('modis_b11_1km', modis_b11_1km);
print('Number of images in modis_b11_1km:', modis_b11_1km.size());
//Resample the MODIS 1km collection to 500m spatial resolution
var modis_b11_resampled = modis_b11_1km.map(function(image) {
var modis_pro = image.projection();
var image_resampled = image.reproject(modis_pro, null, 500)
.copyProperties(image);
return image_resampled
})
//Check on the number of bands in this resampled collection.
print('modis_b11_resampled',modis_b11_resampled);
print('Number of images in modis_b11_resampled:', modis_b11_resampled.size());
//combine the resampled collection with the modis 500m collection
// 1. Join datasets. First, define filter. This is based on the date.
var filter = ee.Filter.equals({
leftField: 'system:time_start',
rightField: 'system:time_start'
});
// 2. Create the join.
var innerJoin = ee.Join.inner('primary', 'secondary');
// 3. Apply the join.
var merge = innerJoin.apply(modis_b01_500m, modis_b11_resampled, filter)
// 4. Merge both collections.
var merged = merge.map(function(f){
var b01 = ee.Image(f.get('primary')).rename('b01')
var b11 = ee.Image(f.get('secondary')).rename('b11')
return b01.addBands(b11).copyProperties(b01)
})
print(merged, 'merged')
// 5. Add NormalizedDifference band
var normalizedBand = merged.map(function(image) {
var normalize = ee.Image(image).normalizedDifference(["b01", "b11"]).rename("normalizedBand")
return ee.Image(image).addBands(normalize).copyProperties(image)
})
print(normalizedBand, 'normalized band added')
我是 Google Earth Engine 和 SO 的新手,但这里有: 我正在尝试使用 MODIS 波段 1 和 11 计算一种新型的归一化差异。它们是 500m 和 1km 空间分辨率,来自两个不同的 MODIS 图像集(sur_refl_b01 来自 MOD09GA,分辨率为 500m,sur_refl_b11 来自 MODOCGA,分辨率为 1km)。
我先把1km的数据重采样到500m。 我可以正确显示 sur_refl_b01 和重采样的 sur_refl_b11 数据。 然后我将这两个图像集合并为一个图像集,我称之为“modis_combined” 它具有正确数量的波段,但显示不正确。 Inspector 指示以下错误: “预期是同质图像集合,但遇到了带不兼容波段的图像: 第一种图像类型:2 波段 ([sur_refl_b01, sur_refl_b01_1])。 当前图像类型:2 波段 ([sur_refl_b11, sur_refl_b11_1])。 图片 ID:2_2_2001_02_28 有些乐队可能需要显式演员表。"
我收到与调用 cci 函数相关的错误,我在其中使用了 normalizedDifference:
ImageCollection(错误) 地图错误(ID=1_2001_03_01): Image.normalizedDifference:没有名为 'sur_refl_b11' 的乐队。可用波段名称:[sur_refl_b01].
我猜想当我合并图像集时,乐队名称并没有保留下来。如何标记合并的 imageCollection 中的波段,以便 normalizedDifference 函数识别它们? 或者还有什么乱七八糟的事情发生?
// Define a sample Region-of-Interest
var roi = ee.Geometry.Polygon(
[[[-122.0, 45.0],
[-122.1, 32.0],
[-108.9, 32.0],
[-108.9, 45.0]]]);
// Load a collection of MODIS land surface reflectance data (500m)
// Select band 1 (red)
// Filter by a date range
// Subset by the region of interest
var modis_b01_500m = ee.ImageCollection('MODIS/006/MOD09GA')
.select(['sur_refl_b01'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in the collection
print('modis_b01_500m', modis_b01_500m);
print('Number of images in modis_b01_500m:', modis_b01_500m.size());
// Define a collection of MODIS ocean color reflectance data (1km)
var modis_b11_1km = ee.ImageCollection('MODIS/006/MODOCGA')
.select(['sur_refl_b11'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in this collection.
print('modis_b11_1km', modis_b11_1km);
print('Number of images in modis_b11_1km:', modis_b11_1km.size());
//Resample the MODIS 1km collection to 500m spatial resolution
var modis_b11_resampled = modis_b11_1km.map(function(image) {
var modis_pro = image.projection();
var image_resampled = image.reproject(modis_pro, null, 500)
.copyProperties(image);
return image_resampled
})
//Check on the number of bands in this resampled collection.
print('modis_b11_resampled',modis_b11_resampled);
print('Number of images in modis_b11_resampled:', modis_b11_resampled.size());
//combine the resampled collection with the modis 500m collection
var modis_combined = modis_b01_500m.merge(modis_b11_resampled);
//Check on the number of bands in this combined collection.
print('modis_combined', modis_combined);
print('Number of images in modis_combined:', modis_combined.size());
//Display the MODIS combined image collection
//Display the layers
Map.addLayer(modis_b01_500m,{min:0,max:10000},'modis_b01_500m');
Map.addLayer(modis_b11_1km,{min:0,max:2000},'modis_b11_1km');
Map.addLayer(modis_b11_resampled, {min:0, max:10000}, 'modis_resampled');
Map.addLayer(modis_combined, {min:0, max:10000}, 'MODIS combined');
var cci = modis_combined.map(function(image){
var cci = image.normalizedDifference(['sur_refl_b11','sur_refl_b01']);
return cci;
})
print('cci', cci);
print('Number of images in cci:', cci.size());
在您的代码中,您使用 .merge()
命令组合两个 ImageCollection。两个 ImageCollection 组合在一起,但是两个集合中的图像彼此分开,因此无法执行需要两个波段图像的 .normalizedDifference
函数。
您需要做的是根据日期加入两个集合,以便将两个集合中具有相同日期的图像合并为一个具有 2 个波段的图像。这将允许您执行 .normalizedDifference
功能。这是怎么回事:
// Define a sample Region-of-Interest
var roi = ee.Geometry.Polygon(
[[[-122.0, 45.0],
[-122.1, 32.0],
[-108.9, 32.0],
[-108.9, 45.0]]]);
// Load a collection of MODIS land surface reflectance data (500m)
// Select band 1 (red)
// Filter by a date range
// Subset by the region of interest
var modis_b01_500m = ee.ImageCollection('MODIS/006/MOD09GA')
.select(['sur_refl_b01'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in the collection
print('modis_b01_500m', modis_b01_500m);
print('Number of images in modis_b01_500m:', modis_b01_500m.size());
// Define a collection of MODIS ocean color reflectance data (1km)
var modis_b11_1km = ee.ImageCollection('MODIS/006/MODOCGA')
.select(['sur_refl_b11'])
.filterDate('2001-02-28','2001-12-31')
.filterBounds(roi)
.map(function(image){return image.clip(roi)});
//Check on the number of bands in this collection.
print('modis_b11_1km', modis_b11_1km);
print('Number of images in modis_b11_1km:', modis_b11_1km.size());
//Resample the MODIS 1km collection to 500m spatial resolution
var modis_b11_resampled = modis_b11_1km.map(function(image) {
var modis_pro = image.projection();
var image_resampled = image.reproject(modis_pro, null, 500)
.copyProperties(image);
return image_resampled
})
//Check on the number of bands in this resampled collection.
print('modis_b11_resampled',modis_b11_resampled);
print('Number of images in modis_b11_resampled:', modis_b11_resampled.size());
//combine the resampled collection with the modis 500m collection
// 1. Join datasets. First, define filter. This is based on the date.
var filter = ee.Filter.equals({
leftField: 'system:time_start',
rightField: 'system:time_start'
});
// 2. Create the join.
var innerJoin = ee.Join.inner('primary', 'secondary');
// 3. Apply the join.
var merge = innerJoin.apply(modis_b01_500m, modis_b11_resampled, filter)
// 4. Merge both collections.
var merged = merge.map(function(f){
var b01 = ee.Image(f.get('primary')).rename('b01')
var b11 = ee.Image(f.get('secondary')).rename('b11')
return b01.addBands(b11).copyProperties(b01)
})
print(merged, 'merged')
// 5. Add NormalizedDifference band
var normalizedBand = merged.map(function(image) {
var normalize = ee.Image(image).normalizedDifference(["b01", "b11"]).rename("normalizedBand")
return ee.Image(image).addBands(normalize).copyProperties(image)
})
print(normalizedBand, 'normalized band added')