1

I recently sourced Terra-MODIS NDVI images from Google Earth Engine. My analysis required both average July NDVI and annual Maximum NDVI. The JavaScript I used to download the images is below:


//Loading MODIS data 
var modis = ee.ImageCollection('MODIS/006/MOD13Q1');

//Creating an array of feature collections 
//One for each region
var regions = [
    ee.FeatureCollection("users/fossaal/us_region1_10km_buffer"),
    ee.FeatureCollection("users/fossaal/us_region2_10km_buffer"),
    ee.FeatureCollection("users/fossaal/us_region3_10km_buffer"),
    ee.FeatureCollection("users/fossaal/us_region4_10km_buffer")
];

//Use the forEach method to apply the innner function to all 4 regions
regions.forEach(
  function(region){ 
      for (var year = 2000; year <= 2016; year=year+2) { 
      var startDate = year + '-07-01'; //Start July 1st 
      var endDate = year + '-07-31'; //End July 31
      var image = modis.filter(ee.Filter.date(startDate,endDate)) //Filter by dates
      .mean() //Take the mean    
      .clipToCollection(region) //Clip to regions 
      .select('NDVI'); //Selection only NDVI band
      Export.image.toDrive({
        image: image,
        description: 'region'+(regions.indexOf(region)+1).toString()+'_'+year,
        folder: 'MODIS NDVI MEAN', // should match your Google Drive folder
        scale: 250, // this should match resolution of your satellite product
        region: region,
        crs: 'EPSG:4326', // can be whatever you want 4269 might be better for NA 
        maxPixels: 1e10}); // may need to boost to meet size
      }
  }
  );
  
//Taking the yearly max for each region rather than the mean as above
regions.forEach(
  function(region){ 
      for (var year = 2000; year <= 2016; year=year+2) { 
      var startDate = year + '-01-01'; //Start Jan 1st
      var endDate = year + '-12-31'; //Ends Dec 31st 
      var image = modis.filter(ee.Filter.date(startDate,endDate)) //Filter by dates
      .max() //Take the MAX   
      .clipToCollection(region) //Clip to regions 
      .select('NDVI'); //Selection only NDVI band
      Export.image.toDrive({
        image: image,
        description: 'region'+(regions.indexOf(region)+1).toString()+'_'+year+'_MAX',
        folder: 'MODIS NDVI MAX', // should match your Google Drive folder
        scale: 250, // this should match resolution of your satellite product
        region: region,
        crs: 'EPSG:4326', // can be whatever you want 4269 might be better for NA 
        maxPixels: 1e10}); // may need to boost to meet size
      }
  }
  );

After I download the images, combine all four regions, and then plot them in R there are some unexpected differences in the way that water is coded in terms of NDVI band values. It seems like the July average images have NA values for pixels outside the clipping regions while these regions are coded as 0 in the MAX images (see below). I'm confused by these differences as I would expect the Max images to be similar to the July images because of the clipToCollection. Any ideas about the source of this discrepancy? The same code is used to combine and plot the data in R.

enter image description here

enter image description here

0

I haven't figured out why GEE is coding masked pixels as NA when using the mean() reducer and as 0 when using the max() reducer but a work around can be found in this thread: https://stackoverflow.com/questions/36960974/how-to-replace-raster-values-less-than-0-to-na-in-r-code/49159943.

I edited my code to set the masked values to 20000 using the unmask() function. NDVI values range from -10000 to 10000 in these data so 20000 clearly stands out for replacement. Once I import into R I replace values of 20000 with NA Example below:

//Taking the yearly max using qualityMosaic for each region rather than the mean as above
regions.forEach(
  function(region){ 
      for (var year = 2000; year <= 2016; year=year+2) { 
      var startDate = year + '-01-01'; //Start Jan 1st
      var endDate = year + '-12-31'; //Ends Dec 31st 
      var image = modis.filter(ee.Filter.date(startDate,endDate)) //Filter by dates
      .qualityMosaic('NDVI') //Take the MAX   
      .clipToCollection(region) //Clip to regions 
      .select('NDVI')//Selection only NDVI band
      .unmask(20000);//Set masked pixels to 20000
      Export.image.toDrive({
        image: image,
        description: 'region'+(regions.indexOf(region)+1).toString()+'_'+year+'_MAX_MOSAIC',
        folder: 'MODIS NDVI MAX Quality Mosaics', // should match your Google Drive folder
        scale: 250, // this should match resolution of your satellite product
        region: region,
        crs: 'EPSG:4326', // can be whatever you want 4269 might be better for NA 
        maxPixels: 1e10}); // may need to boost to meet size
      }
  }
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.