Google Earth Engine (GEE) - Export image collection data

Image data in GEE can be exported to Google Drive, Google Assets or Google Cloud Storage through Export. However, the platform only supports exporting single image data. If you want to export an image collection, you can only use the loop traversal method to solve the problem.

By querying the GEE API, we can find that there is an asynchronous operation method in the collection - evaluate. What is special about this method is that in its callback method, the GEE object will be converted into an ordinary JavaScript object, so that it can be used Loop through to export the desired image collection data.

First, we can define a download function:

function exportImage(image, roi, fileName) {  
    Export.image.toDrive({  
       image: image,  
       description: "Drive-image-"+fileName,  
       fileNamePrefix: fileName,  //文件命名
       folder: "Landsat 8",  //保存的文件夹
       scale: 30,  //分辨率
       region: roi,  //研究区
       maxPixels: 1e13,  //最大像元素
       crs: "EPSG:4326"  //设置投影
   });  
 }

Then, by generating a list, the batch download function is called to complete the export of the image collection data:

//生成列表,迭代下载
//调用影像集合的reduceColumns()方法,结合ee.Reducer.toList(),就可以根据指定的属性生成一个列表
//在回调方法中,index这个参数就变成了一个普通的JavaScript对象
//接下来就可以利用for循环来获取每一景的影像,同时调用exportImage()函数,导出影像
var indexList = data_selected.reduceColumns(ee.Reducer.toList(), ["system:index"]).get("list"); 
print("indexList", indexList);
indexList.evaluate(function(indexs) { 
    for (var i=0; i<indexs.length; i++) {  
        var image = data_selected.filter(ee.Filter.eq("system:index", indexs[i]))
              .first()
              .toInt16()
              .clip (roi); 
        exportImage(image, roi, indexs[i]);  
   }
 });

Sample code

//研究区
var roi = ee.FeatureCollection("users/sihaixiang/shandong").geometry(); //("users/....")自己填入研究shp文件路径
Map.centerObject(roi, 7);
Map.addLayer(roi, {color:"black"}, "roi");
 
//批量下载函数
function exportImage(image, roi, fileName) {  
    Export.image.toDrive({  
       image: image,  
       description: "Drive-image-"+fileName,  
       fileNamePrefix: fileName+'_EVI',  //文件命名
       folder: "MODIS_EVI",  //保存的文件夹
       scale: 250,  //分辨率
       region: roi,  //研究区
       maxPixels: 1e13,  //最大像元素
       crs: "EPSG:4326"  //设置投影
   });  
 } 
 
//加载数据集
//MODIS13Q1产品下的EVI产品,16天/幅,分辨率为250米
var data = ee.ImageCollection("MODIS/006/MOD13Q1");
 
//筛选数据
var data_selected = data.filterBounds(roi) 
               .filterDate("2022-01-01", "2022-06-30")
               .select("EVI")
               
print("data_selected", data_selected); 
 
//生成列表,迭代下载
var indexList = data_selected.reduceColumns(ee.Reducer.toList(), ["system:index"]).get("list"); 
print("indexList", indexList);
indexList.evaluate(function(indexs) { 
    for (var i=0; i<indexs.length; i++) {  
        var image = data_selected.filter(ee.Filter.eq("system:index", indexs[i]))
              .first()
              .toInt16()  //设置数据类型 
              .clip (roi);   //裁剪数据
        exportImage(image, roi, indexs[i]);  //保存图像至Google网盘
   }
 });

result

Guess you like

Origin blog.csdn.net/weixin_48048649/article/details/128994661