0

I have calculated the NDVI for my study area. However, I am having challenges to export the NDVI images.

I want to export a single image with several NDVI layers. I have tried the mosaic function but it's giving me an image with a single layer. Can someone assist me on this one?

var l8collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(Border) // study area
.filterDate('2013-04-01', '2018-08-31'); // period

print(l8collection);

// Mask pixels with clouds and cloud shadows
// Surface reflectance products come with a 'pixel_qa' band based on CFMask

// create function to mask clouds, cloud shadows, snow
var maskClouds = function(image){
  // make a new single band image from the pixel qa band
  var pixel_qa = image.select('pixel_qa');
  // keep clear (0) and water (1) pixels
  return image.updateMask(pixel_qa.eq(322));   
};

// use "map" to apply the function to each image in the collection
var l8masked = l8collection.map(maskClouds);

print (l8masked);

// visualize the first image in the collection, pre- and post- mask
var visParams = {bands: ['B4','B3','B2'], min: 150, max: 2000}
 Map.centerObject(Border, 9);
Map.addLayer(ee.Image(l8masked.first()).clip(Border), visParams, 'clouds masked', false)
Map.addLayer(ee.Image(l8collection.first()).clip(Border), visParams, 'original', false)

// Compute the Normalized Difference Vegetation Index (NDVI).
var getNDVI = function(img){
  return img.addBands(img.normalizedDifference(['B5','B4']).rename('NDVI'));
};

// map over image collection
var l8ndvi = l8masked.map(getNDVI);

print(l8ndvi);

var ndvi = l8ndvi.select('NDVI');

print(ndvi);

// Create a task that you can launch from the Tasks tab.
Export.image.toDrive({
  image: ndvi.mosaic().clip(Border),
  description: 'NDVI',
  scale: 30,
  maxPixels:1e13,
  skipEmptyTiles: true
  });
  • Yes it's Google Earth Engine. – Archford Sep 13 '18 at 7:10
  • Do you want the export to be a single image with multiple NDVI layers or multiple single-band images of NDVI? Both are options if you want to export collections from GEE which only allows single-image export. However, depending on what your goal is it might be more sensible to perform further calculatuions within GEE and only export the final result. What do you aim to do with the NDVI image stack? – Kersten Sep 13 '18 at 8:40
  • I want a single image with multiple NDVI layers. However, both options are OK for me as my aim is to export the NDVI image stack for ISODATA clustering (unsupervised classification). Thank you for the question. – Archford Sep 13 '18 at 8:46
1

One image with as many NDVI bands as images in the collection

Take in count that the resulting collection has 764 image, which will result in a very large image. I made a test with 10 images and the resulting image weighted 70 MB and took 4 min to finish. If you export it to Drive you'll loose the band names, but if you export it to an Asset, every band will have a name formatted as: 'NDVI_yyyyMMdd', for example: 'NDVI_20130611'

var l8collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterBounds(Border) // study area
.filterDate('2013-04-01', '2018-08-31'); // period

print(l8collection);

// Mask pixels with clouds and cloud shadows
// Surface reflectance products come with a 'pixel_qa' band based on CFMask

// create function to mask clouds, cloud shadows, snow
var maskClouds = function(image){
  // make a new single band image from the pixel qa band
  var pixel_qa = image.select('pixel_qa');
  // keep clear (0) and water (1) pixels
  return image.updateMask(pixel_qa.eq(322));   
};

// use "map" to apply the function to each image in the collection
var l8masked = l8collection.map(maskClouds);

print (l8masked);

// visualize the first image in the collection, pre- and post- mask
var visParams = {bands: ['B4','B3','B2'], min: 150, max: 2000}
 Map.centerObject(Border, 9);
Map.addLayer(ee.Image(l8masked.first()).clip(Border), visParams, 'clouds masked', false)
Map.addLayer(ee.Image(l8collection.first()).clip(Border), visParams, 'original', false)

// Compute the Normalized Difference Vegetation Index (NDVI).
var getNDVI = function(img){
  return img.addBands(img.normalizedDifference(['B5','B4']).rename('NDVI'));
};

// map over image collection
var l8ndvi = l8masked.map(getNDVI);

print(l8ndvi);

var ndvi = l8ndvi.select('NDVI')
print(ndvi);

var first = ee.Image(ndvi.first())
var rest = ee.ImageCollection(ndvi.toList(ndvi.size()).slice(1))

var stack = ee.Image(rest.iterate(function(img, ini){
  var ini = ee.Image(ini)
  var img = ee.Image(img)
  var date = img.date().format('yyyyMMdd')
  return ini.addBands(img.rename(ee.String('NDVI_').cat(date)))
}, first.rename(ee.String('NDVI_').cat(first.date().format('yyyyMMdd')))))

// Create a task that you can launch from the Tasks tab.
Export.image.toDrive({
  image: stack,
  description: 'NDVI',
  // region: Border.getInfo()['coordinates'], // cutting it to the border may reduce the image weight
  scale: 30,
  maxPixels:1e13,
  skipEmptyTiles: true
  });
  • Hi Rodrigo, i have tried to export to both Asset and Drive but it's giving me this error message "Error: String: Unable to convert object to string". Maybe, i should try to export the images as single band images for all the bands. – Archford Sep 13 '18 at 17:35
  • yep, now should be ok. Worked for me. – Rodrigo E. Principe Sep 13 '18 at 17:49
  • make a test with var ndvi = l8ndvi.select('NDVI').limit(3) just to see if it is what you need – Rodrigo E. Principe Sep 13 '18 at 17:50
  • Is this for selecting single band images? – Archford Sep 13 '18 at 17:53
  • 1
    This worked well for me. Much appraciated – Archford Sep 13 '18 at 18:41

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.