Thursday, 7 November 2019

modis - Making stack from many images in Google Earth Engine?


Using the code below, I've computed 20 ndvi images for modis data.


How can I stack and save them in a single file?



var mod13 = ee.ImageCollection("MODIS/006/MOD13Q1")
.filterBounds(geometry)
.filterDate("2000-01-01","2001-01-01")
.select("NDVI");

var mod13ndvi = mod13.map( function(img){
return img.multiply(0.0001)
.copyProperties(img,['system:time_start','system:time_end']);
});


print(mod13ndvi);

Answer



You just need the function 'toBands()' and apply that on the image collection. Unfortunately, the function cannot deal with similar band names (at least, for your MODIS collection: it works fine for landsat and Sentinel), so you will need to change the band names. For simplicity, I changed the band names to the date the image was acquired.


Update: bandnames are now renamed to only their date, eliminating the 'null_' the function toBands() appends to it when image number lacks.


// Define a random geometry
var geometry = /* color: #0b4a8b */ee.Geometry.Polygon(
[[[-111.6384952013949, 33.80963070262341],
[-111.7153994982699, 32.92883163730542],
[-109.0017764513949, 33.526181694205995]]]);


// make the image collection
var mod13 = ee.ImageCollection("MODIS/006/MOD13Q1") .filterBounds(geometry)
.filterDate("2000-01-01","2001-01-01") .select("NDVI");
print(mod13);

// change the name of the NDVI band to the image ID (date it is acquired)
var mod13ndvi = mod13.map( function(img){
var id = img.id()
img = img.select('NDVI').rename(id);
return img.multiply(0.0001).copyProperties(img,['system:time_start','system:time_end']); });

print(mod13ndvi);

// Apply the function toBands() on the image collection to set all bands into one image
var multiband = mod13ndvi.toBands();
print(multiband)

// Reset the bandnames
var names = multiband.bandNames();
// rename the bandnames
var newNames = names.map(function(name){

var ind = names.indexOf(name);
return ee.String(names.get(ind)).slice(5);
});
var multiband = multiband.rename(newNames);
print(multiband);

https://code.earthengine.google.com/48569dcfe14420f0ed8f6729e3ec7d15


No comments:

Post a Comment