TransWikia.com

Obtaining time series of water extents per plot using landsat NDWI data available on GEE

Geographic Information Systems Asked by Batbr on December 5, 2020

I wanted to obtain a monthly time series estimate of the water surface for each of 67 survey plots (500m circles). However, I could not figure out how to correctly code this in the google earth engine. My code below does not seem to count the number of water pixels per plot.

Here is my code link: https://code.earthengine.google.com/b62cd66b38e974cfbb402100dc10e3bd

// // Load from Fusion table
var surveyPlots = ee.FeatureCollection(table, 'geometry');

// Create water mask
var water = function(image) {
  return image.updateMask(image.gt(0));
};

// Load a dataset.
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_8DAY_NDWI')
  .filterBounds(surveyPlots)
  .filterDate('2014-01-01','2020-08-01')
  .map(water)
  .select('NDWI');

var bandName = ee.Image(dataset.first()).bandNames().get(0);

var startDate = ee.Date('2014-04-01'); // set analysis start time
var endDate = ee.Date('2014-10-01'); // set analysis end time

// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();

// get a list of time strings to pass into a dictionary later on
var monList = ee.List(ee.List.sequence(0,nMonths).map(function (n){
  return startDate.advance(n,'month').format('YYYMMdd');
}))
print(monList)

var result = surveyPlots.map(function(feature){
  // map over each month
  var timeSeries = ee.List.sequence(0,nMonths).map(function (n){
    // calculate the offset from startDate
    var ini = startDate.advance(n,'month');
    // advance just one month
    var end = ini.advance(1,'month');
    // filter and reduce
    var data = dataset.filterDate(ini,end).median().reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: feature.geometry(),
      scale: 30
    });
    // get the value and check that it has data
    var val = ee.Number(data.get(bandName));
    val = ee.Number(ee.Algorithms.If(val,val,-999));
    // return value with a time key
    return val;
  });
// create new dictionary with date strings and values
  var timeDict = ee.Dictionary.fromLists(monList,ee.List(timeSeries));
  // return feature with a timeseries property and results
  return feature.set(timeDict);
});

// print to see if it is doing what we expect...
print(result);

// drop .geo column
var polyOut = result.select(['.*'],null,false);

// Export the data to a table for further analysis
Export.table.toDrive({
  collection:polyOut,
  description:"time_series_NDWI",
  fileFormat:"CSV",
});

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP