Background of 0.

Do remote sensing, around the past NDVI. How to quickly and conveniently obtain the ANNUAL NDVI of a study area? Download raw data through USGS? Try GEE to batch export each issue of NDVI in the study area.

1. Data filtering

First of all, import the REQUIRED VECTOR boundary ROI into GEE, which can be uploaded to SHP or drawn in GEE. Then select the satellite data type and time. All Landsat8 images covering the study area after 2021 were selected for this paper.

Map.addLayer(ROI)
Map.centerObject(ROI,9)

// Filter data
var roi_collection =landsat.filter(ee.Filter.date('2021-01-01'.'2021-12-31'))
                            .filter(ee.Filter.bounds(ROI))
print(roi_collection)
Copy the code

2. Calculation function of NDVI

This part includes the selection of band and NDVI calculation. If NDVI, you can modify it in the BANDS parameter.

// The band to be exported
var bands=["B1"."B2"."B3"."B4"."B5"."B6"."B7"."B8"."B9"."NDVI"]
// Or just choose NDVI
var bands=["NDVI"]
Copy the code

Next, calculate NDVI for each image in the collection

//NDVI calculation function
var get_NDVI = function(image) {
        image=image.clip(ROI);
        var NDVI=image.normalizedDifference(['B4'.'B5']).rename(['NDVI']);
        image=image.addBands(NDVI)
        return image.select(bands)
      };
// For each image in the image set, cut ROI area, calculate NDVI, and export corresponding band
var NDVI_Collection = ee.ImageCollection(roi_collection)
          .map(get_NDVI);
print(NDVI_Collection)
Copy the code

3. Introduce batch export functions

var batch = require('users/fitoprincipe/geetools:batch')
Copy the code

This function comes from a great god at Github. He made an arrangement for gee batch export and made an API, which we can call directly.

The specific code of the batch export is as follows:

Download.ImageCollection.toDrive = function(collection, folder, options) {
    
    var defaults = {
      scale: 1000.maxPixels: 1e13.type: 'float'.region: null.name: '{id}'.crs: null.dateFormat: 'yyyy-MM-dd'.async: false
    }
    var opt = tools.get_options(defaults, options)
    var colList = collection.toList(collection.size());
    
    var wrap = function(n, img) {
      var geom = opt.region || img.geometry()
      var region = getRegion(geom)
      var imtype = IMAGE_TYPES(img, opt.type)
      var description = helpers.string.formatTask(n)
      var params = {
        image: imtype,
        description: description,
        folder: folder,
        fileNamePrefix: n,
        region: region,
        scale: opt.scale,
        maxPixels: opt.maxPixels
      }
      if (opt.crs) {
        params.crs = opt.crs
      }
      Export.image.toDrive(params)
    }
    
    var i = 0
    while (i >= 0) {
      try {
        var img = ee.Image(colList.get(i));
        var n = tools.image.makeName(img, opt.name, opt.dateFormat).getInfo()
        wrap(n, img)
        i++
      } catch (err) {
        var msg = err.message
        if (msg.slice(0.36) = = ='List.get: List index must be between') {
          break
        } else {
          print(msg)
          break}}}}Copy the code

4. Set the export function

Mainly set the image resolution and export region.

// Export image
batch.Download.ImageCollection.toDrive(NDVI_Collection, 'Landsat_NDVI', {
  scale: 30.region: ROI
})
Copy the code

5. Complete code display

Map.addLayer(ROI)
Map.centerObject(ROI,9)

// Filter data
var roi_collection =landsat.filter(ee.Filter.date('2021-01-01'.'2021-12-31'))
.filter(ee.Filter.bounds(ROI))
print(roi_collection)
// The band to be exported
var bands=["NDVI"]
//NDVI calculation function
var get_NDVI = function(image) {
        image=image.clip(ROI);
        var NDVI=image.normalizedDifference(['B5'.'B4']).rename(['NDVI']);
        image=image.addBands(NDVI)
        return image.select(bands)
      };
// For each image in the image set, cut ROI area, calculate NDVI, and export corresponding band
var NDVI_Collection = ee.ImageCollection(roi_collection)
          .map(get_NDVI);
print(NDVI_Collection)

// Introduce a batch export function
var batch = require('users/fitoprincipe/geetools:batch')

// Export image
batch.Download.ImageCollection.toDrive(NDVI_Collection, 'Landsat_NDVI', {
  scale: 30.region: ROI
})
// View the general effect
var NDVI_IMAGE=NDVI_Collection.mosaic()
print(NDVI_IMAGE)
Map.addLayer(NDVI_IMAGE)
Copy the code

6. Download

For example, we use the administrative region of ziyang City lezhi County in Sichuan Province. You can first see the general effect through Gee (not a single period).

// View the general effect
var NDVI_IMAGE=NDVI_Collection.mosaic()
print(NDVI_IMAGE)
Map.addLayer(NDVI_IMAGE)
Copy the code

Then download the video for each issue. Click Run to download the video directly.Note that in order to keep the name of each issue the same as the original image. The downloaded image is trimmed according to ROI area, not Mosaic according to study area.

7. Test links

Code.earthengine.google.com/39c66eee53c…