1

I want to export as a CSV the mean NDVI values for the sub-districts contained within the NAME_3 feature of my shapefile. Currently I have the following code, but I am not being able to export it with one column per year (between 1994 and 2011):

var shp4 = ee.FeatureCollection('users/dermont/GADMVGrassSimplify');

Map.addLayer(shp4, {}, 'My Polygon');
var districts = shp4.filter(filter);
var districtsindo = shp4.select('NAME_3');


var startYear = 1994
var endYear = 2011

var means = ee.Image(
  ee.List.sequence(startYear, endYear)
  .iterate(
    function(year, acc) {
      return ee.Image(acc).addBands(
        yearlyMeans(ee.Number(year))
      )
    },
    ee.Image([])
  )
)

var ndviPerdistrict = means.reduceRegions({
  collection: districtsindo,
  reducer: ee.Reducer.mean().forEachBand(means),
  scale: 30
})

function removegeo(feature) {
  feature = feature.setGeometry(null);
  return feature
}


 
 var ndvi_district = ndviPerdistrict.map(removegeo);   


var columns = ['district'].concat(sequence(startYear, endYear))

Export.table.toDrive({
  collection: ndvi_district, 
  description: 'ndviPerParish',
  selectors: columns
})


function yearlyMeans(year) {
  var startDate = ee.Date.fromYMD(year, 1, 1)
  var endDate = startDate.advance(1, 'year')

  
  var landsat = ee.ImageCollection(ee.ImageCollection("LANDSAT/LT05/C02/T2_TOA"))
    .filterBounds(districtsindo)
    .filterDate(startDate, endDate)

  var addNDVI = function(image) {
    return image
      .normalizedDifference(['B4', 'B3'])
      .rename(year.format('%d')) // Rename NDVI band to the current year
  }

  return landsat.map(addNDVI).mean()
}

// Create client-side sequence
function sequence(from, to) {
  return Array.apply(null, {length: to - from + 1})
    .map(function (_, i) {
      return i + from
    })
}

1 Answer 1

1

You are not sharing your asset so, I had to create an arbitrary shp4 variable as it is specified in my complete code. An extract of it, with slightly modifications compared with your code, looks as follows:

var feat1 = ee.Feature(geometry).set('NAME_3', 'district1');
var feat2 = ee.Feature(geometry2).set('NAME_3', 'district2');
var feat3 = ee.Feature(geometry3).set('NAME_3', 'district3');

//var shp4 = ee.FeatureCollection('users/dermont/GADMVGrassSimplify');

var shp4 = ee.FeatureCollection([feat1, feat2, feat3]);

Map.addLayer(shp4, {}, 'My Polygon');
//var districts = shp4.filter(filter);
var districtsindo = shp4.select('NAME_3');

print(districtsindo);

var startYear = 1994
var endYear = 2011

var means = ee.Image(
  ee.List.sequence(startYear, endYear)
  .iterate(
    function(year, acc) {
      return ee.Image(acc).addBands(
        yearlyMeans(ee.Number(year))
      )
    },
    ee.Image([])
  )
)

var ndviPerdistrict = means.reduceRegions({
  collection: districtsindo,
  reducer: ee.Reducer.mean().forEachBand(means),
  scale: 30
})

function removegeo(feature) {
  feature = feature.setGeometry(null);
  return feature
}


 
 var ndvi_district = ndviPerdistrict.map(removegeo);   


var columns = ['district'].concat(sequence(startYear, endYear))

Export.table.toDrive({
  collection: ndvi_district, 
  description: 'ndviPerParish',
  //selectors: columns
})


function yearlyMeans(year) {
  var startDate = ee.Date.fromYMD(year, 1, 1)
  var endDate = startDate.advance(1, 'year')

  
  var landsat = ee.ImageCollection(ee.ImageCollection("LANDSAT/LT05/C02/T2_TOA"))
    .filterBounds(districtsindo)
    .filterDate(startDate, endDate)

  var addNDVI = function(image) {
    return image
      .normalizedDifference(['B4', 'B3'])
      .rename(year.format('%d')) // Rename NDVI band to the current year
  }

  return landsat.map(addNDVI).mean()
}

// Create client-side sequence
function sequence(from, to) {
  return Array.apply(null, {length: to - from + 1})
    .map(function (_, i) {
      return i + from
    })
}

After running above code in GEE code editor, I got following CSV file.

enter image description here

It can be observed "one column per year (between 1994 and 2011)"; as you expect.

If in your case you are not being able to export the CSV probably there are one or more districts without any information and you should fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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