TransWikia.com

Mapping composite over many years in an image collection

Geographic Information Systems Asked on April 16, 2021

I am looking to make an image collection that holds the 95th percentile of NDVI for each year from 1985 – 2011 for the Landsat 5 record. I am able to create the 95th percentile composites, but I would like to avoid copy and pasting to change the year. This is what a single year would look like. How do I easily loop/map over each year to create an image collection that holds finalmask_1985, finalmask_1986, finalmask_1987…. and so on?

//////////////////////////////////////////////////////////////////////////
//1985
//////////////////////////////////////////////////////////////////////////
//Load Landsat 5 image collection
var collection_1985= ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
  .filterDate('1985-01-01','1985-12-31')
  .map(addQualityBand);
//Isolate NDVI and time band
var only_NDVI_1985 = collection_1985.select('nd','system:time_start');
//Create image counts mask
var count_1985 = only_NDVI_1985.select('nd').reduce(ee.Reducer.count());
var countmask_1985 = count_1985.select('nd_count').gte(10);
//Make 95th percentile NDVI composite
var ninety_five_percent_1985 = only_NDVI_1985.reduce(ee.Reducer.percentile([95]));
//Create NDVI <.1 mask
var mask95_1985 = ninety_five_percent_1985.select('nd_p95').gte(0.1);
//Apply masks
var lte95_1985 = ninety_five_percent_1985.updateMask(mask95_1985);
var finalmask_1985 = lte95_1985.updateMask(countmask_1985);

Here is the link to the full script: https://code.earthengine.google.com/3674fa123d228d3a5a17b0fbcdeb3f60

One Answer

You want to create a function, taking a year as argument, which creates the image for that year. Then you can map that function over a list of years. Something like this:

var ndviCollection = ee.ImageCollection(
  ee.List.sequence(1985, 2011).map(ndviForYear)
)

print(ndviCollection)
Map.addLayer(ndviCollection.first(), {palette: 'red, orange, yellow, green'})

function processImage(image) {
  return image
    .addBands(
      image.normalizedDifference(['B4', 'B3']).rename('ndvi')
    )
    .updateMask(
      ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud']).lt(20)
    )
}

function ndviForYear(year) {
  var startDate = ee.Date.fromYMD(year, 1, 1)
  var collection = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
    .filterDate(startDate, startDate.advance(1, 'year'))
    .map(processImage)
  var reduced = collection
    .select('ndvi')
    .reduce(ee.Reducer.count()
      .combine(ee.Reducer.percentile([95]), null, true)
    )
    .rename(['count', 'ndvi'])
    
  return reduced
    .select('ndvi')
    .updateMask(
      reduced.expression('i.count >= 10 and i.ndvi > 0.1', {i: reduced})
    )
    .set('year', year)
}

https://code.earthengine.google.com/c8abc56c59ddf9db6bd40093c9de0632

Correct answer by Daniel Wiell on April 16, 2021

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