6

On Google Earth Engine, I'd like to accomplish two tasks:

  1. extract data from every pixel, across a time-series, within the given geometry bounds and save it as a table.

  2. I need a column which prints the date of the image.

For the first part here is my code snippet (borrowed from : Extract complete pixel values inside a geometry) ---

// the geometry bounds
var rectangle = ee.Geometry.Rectangle(96.01669, 18.52621, 96.04819, 18.49634);

//the time series dataset
var dataset = ee.ImageCollection("MODIS/006/MOD13Q1")
.filterDate('2000-01-01', '2000-03-31')
.filterBounds(rectangle)
.select('NDVI', 'SummaryQA');

// Sort the dataset
var sorteddataset = dataset.sort('system:time_start', true);

var first = sorteddataset.first();

// generate a new image containing lat/lon of the pixel and reproject 
// it to MODIS projection
var coordsImage = 
ee.Image.pixelLonLat().reproject(first.projection());

var joinedImage = coordsImage.addBands(first);

var valuesList = joinedImage.reduceRegion({
reducer: ee.Reducer.toList(4),
geometry: rectangle
}).values().get(0);

valuesList = ee.List(valuesList) // Cast valuesList

var myFeatures = ee.FeatureCollection(valuesList.map(function(el){
el = ee.List(el) // cast every element of the list
var geom = ee.Geometry.Point([ee.Number(el.get(0)), 
ee.Number(el.get(1))])
return ee.Feature(geom, {'NDVI':ee.Number(el.get(2)), 
'SummaryQA':ee.Number(el.get(3))})

}))

Export.table.toDrive(results,
"data",
"ndvi-testing",
"NDVI-SummaryQA", 
"CSV");

How do I modify the code to include the entire time-series instead of just the first image? And how can I include a command that prints the image date? Below is the image of the table I get from the given code. example table that i get from the given code

1 Answer 1

10

Here is a modification of the first example from this presentation about tables and vectors. Note that you can "transpose" the table if there are other properties in the points that are of interest:

var rectangle = ee.Geometry.Rectangle(96.01669, 18.52621, 96.04819, 18.49634);
Map.centerObject(rectangle);
Map.addLayer(rectangle, {}, 'rectangle')

var dictionary = ee.Image.pixelLonLat().reduceRegion({
  reducer: ee.Reducer.toCollection(['longitude', 'latitude']), 
  geometry: rectangle, 
  scale: 250
});

var points = ee.FeatureCollection(dictionary.get('features'))
    .map(function(feature) {
      var lon = feature.get('longitude');
      var lat = feature.get('latitude');
      return ee.Feature(ee.Geometry.Point([lon, lat]), {
        'featureID': ee.Number(lon).multiply(1000).round().format('%5.0f')
            .cat('_')
            .cat(ee.Number(lat).multiply(1000).round().format('%5.0f'))
      });
    });
print('points', points)
Map.addLayer(points);

var dataset = ee.ImageCollection("MODIS/006/MOD13Q1")
  .filterDate('2000-01-01', '2000-03-31')
  .select('NDVI')
print('dataset', dataset)

var triplets = dataset.map(function(image) {
  return image.reduceRegions({
    collection: points, 
    reducer: ee.Reducer.first().setOutputs(image.bandNames()), 
    scale: 250,
  }).map(function(feature) {
    return feature.set({
      'imageID': image.id(),
      'timeMillis': image.get('system:time_start')
    })
  });
}).flatten();
print(triplets) 

var format = function(table, rowId, colId, rowProperty, colProperty) {
  var rows = table.distinct(rowId); 
  var joined = ee.Join.saveAll('matches').apply({
    primary: rows, 
    secondary: table, 
    condition: ee.Filter.equals({
      leftField: rowId, 
      rightField: rowId
    })
  });
  return joined.map(function(row) {
      var values = ee.List(row.get('matches'))
        .map(function(feature) {
          feature = ee.Feature(feature);
          return [feature.get(colId), feature.get(colProperty)];
        }).flatten();
      return row.select([rowId, rowProperty]).set(ee.Dictionary(values));
    });
};

var results = format(triplets, 'imageID', 'featureID', 'timeMillis', 'NDVI');
print(results)

// Note that there's a dummy feature in there for the points ('null').
var transpose = format(triplets, 'featureID', 'imageID', 'null', 'NDVI');
print(transpose)

Export.table.toDrive({
  collection: results, 
  description: 'foo', 
  fileNamePrefix: 'foo', 
  fileFormat: 'CSV'
});

https://code.earthengine.google.com/09ae70e7bce823b8fbe763601620c732

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.