0

I have a script that allows me to obtain a CSV file containing the time series of monthly MODIS NDVI values for several points. The problem here is that the CSV file I get is organized like this:

ID Mean date
P1 xxx  year1/month1
P2 xxx  year1/month1
P3 xxx  year1/month1
.  .    .    .  .   
P1 xxx  year1/month2
P2 xxx  year1/month2
P3 xxx  year1/month2

However, I would prefer to have a structure where each cologne is the average NDVI value of each point and each line is the date:

date          P1  P2  P3 
year1/month1  xxx xxx xxx 
year1/month2  xxx xxx xxx

Here is the code:

var ptsTopo = ee.FeatureCollection(point)    

// var modis= ee.ImageCollection("MODIS/006/MOD13Q1");
// var dataset=ee.ImageCollection(modis.filterDate('2006-01-01', '2016-12-31').select('NDVI'));

var modis = ee.ImageCollection("MODIS/006/MOD13Q1")
            .select('NDVI')
            .filterDate('2006-01-01','2016-12-31')
            .map(function(img){var d = ee.Date(ee.Number(img.get('system:time_start')));
              var m = ee.Number(d.get('month'));
              var y = ee.Number(d.get('year'));
              return img.set({'month':m, 'year':y});
            });


var months = ee.List.sequence(1, 12);
var start_year = 2006;
var start_date = '2006-01-01';
var end_year = 2016;
var end_date = '2016-12-31';

var years = ee.List.sequence( start_year, end_year);


var byYearMonth = ee.ImageCollection.fromImages(
      years.map(function(y){
        return months.map(function(m){
          return modis.filterMetadata('year', 'equals', y)
                      .filterMetadata('month', 'equals', m)
                      .select('NDVI').mean()
                      .set('year', y)
                      .set('month', m)
                      .set('date', ee.Date.fromYMD(y,m,1));
        });
      
      }).flatten()
      );
      
      
var proj = ee.Image(modis.first()).projection()

var NDVI_pts = byYearMonth.map(function(img){
  var features = ptsTopo.map(function(f) {return f.set('date', img.get('date'))})
  return img.reduceRegions(features, ee.Reducer.mean(), 500, proj);
  
}).flatten();

print(NDVI_pts.limit(10));

Export.table(NDVI_pts, 'NDVI_pts', {fileFormat: 'CSV'});

1
  • Can you please be more specific.
    – Bouram
    May 3 at 13:00

1 Answer 1

0

I'm going to consider a Feature Collection with 3 arbitrary points in USA geography created in GEE because your geometry is not available for me. As you are using a monthly product then you are going to obtain 12 values per year independently of number of points. As you are considering 11 years of data the number of features to export will be 11*12 = 132.

Following code transpose the data in NDVI_pts feature collection to a new feature collection (mapped) where the data are organized as you desire.

var p1 = ee.Feature(ee.Geometry.Point([-97.44070668220519, 41.93324324360744]));
var p2 = ee.Feature(ee.Geometry.Point([-93.92508168220519, 40.612183748474436]));
var p3 = ee.Feature(ee.Geometry.Point([-97.08914418220519, 38.5807048157763]));

var point = ee.FeatureCollection([p1, p2, p3]);

var n_point = point.size();

var ptsTopo = ee.FeatureCollection(point);    

// var modis= ee.ImageCollection("MODIS/006/MOD13Q1");
// var dataset=ee.ImageCollection(modis.filterDate('2006-01-01', '2016-12-31').select('NDVI'));

var modis = ee.ImageCollection("MODIS/006/MOD13Q1")
            .select('NDVI')
            .filterDate('2006-01-01','2016-12-31')
            .map(function(img){var d = ee.Date(ee.Number(img.get('system:time_start')));
              var m = ee.Number(d.get('month'));
              var y = ee.Number(d.get('year'));
              return img.multiply(0.0001)
                        .set({'month':m, 'year':y});
            });

var months = ee.List.sequence(1, 12);
var start_year = 2006;
var start_date = '2006-01-01';
var end_year = 2016;
var end_date = '2016-12-31';

var years = ee.List.sequence( start_year, end_year);

var byYearMonth = ee.ImageCollection.fromImages(
      years.map(function(y){
        return months.map(function(m){
          return modis.filterMetadata('year', 'equals', y)
                      .filterMetadata('month', 'equals', m)
                      .select('NDVI').mean()
                      .set('year', y)
                      .set('month', m)
                      .set('date', ee.Date.fromYMD(y,m,1).format().slice(0,10));
        });
      
      }).flatten()
      );
      
var proj = ee.Image(modis.first()).projection();

var NDVI_pts = byYearMonth.map(function(img){
  var features = ptsTopo.map(function(f) {return f.set('date', img.get('date'))});
  return img.reduceRegions(features, ee.Reducer.mean(), 500, proj);
  
}).flatten();

print(NDVI_pts);

var NDVI_pts_list = NDVI_pts.toList(NDVI_pts.size());

var dates = NDVI_pts_list.map(function(ele) {
  
  return ee.Feature(ele).get('date');
  
}).distinct();

print("dates", dates);

var means = NDVI_pts_list.map(function(ele) {
  
  return ee.Feature(ele).get('mean');
  
});

//print(means);

var list = ee.List.sequence(0, ee.List(NDVI_pts_list).size().subtract(1), n_point);

//print(list);

var group_list = list.map(function(ele){

  var start = ee.Number(ele).int(); 
  var end = ee.Number(ele).add(n_point).int(); 

  var new_list = ee.List([]);
  var element = means.slice(start, end);

  new_list = new_list.add(element);

  return new_list.get(0);

});

//print(group_list);

var list = ee.List.sequence(0, ee.Number(dates.size()).subtract(1));

var features = ee.FeatureCollection(list
      .map(function (i) {
        return ee.Feature(null).set('date', dates.get(i));
      })
    );

//print(features);

var mapped = features.map(function (feature) {
  return ee.Feature(
    ee.List.sequence(0, point.size().subtract(1)).iterate(
      function (i, feature) {
        feature = ee.Feature(feature);
        var idx = feature.id();
        var key = ee.String('p').cat(ee.Number(i).format('%d'));
        var value = ee.List(ee.List(group_list).get(ee.Number.parse(idx))).get(i);
        return ee.Feature(feature).set(key, value);
      },
      
      feature
    )
  );
});

print(mapped);

Export.table(mapped, 'NDVI_pts', {fileFormat: 'CSV'});

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

enter image description here

1
  • Thank you , the code works perfectly but only for a limited number of points. I actually have 1600 points, and when I run the code I get the error message 'FeatureCollection (Error) Collection query aborted after accumulating over 5000 elements'. Is there any way to fix this.
    – Bouram
    May 4 at 10:42

Your Answer

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

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