3

I am making a image collection of NDVI from Landsat 5 over a particular location. I want to run a regression on each pixel through the NDVI using the per pixel NDVI value as the dependent variable and the year of image acquisition as the independent variable.

So far I am doing this:

// Define start and end dates and geometry to select images by
var start = '1984-05-01';
var end = '2011-09-30';
var polygon = ee.Geometry.Polygon([[
 [-97.49404907226562, 46.59473135600069],
 [-97.09442138671875, 46.59567501063883],
 [-97.05734252929688, 46.31184150036163],
 [-97.53524780273438, 46.30709840788667]
]]);

// function which will add a band to original image stack based on start year
function createTimeBand(img) {
  var year = ee.Date(img.get('system:time_start')).get('year').subtract(1984);
  return ee.Image(year).byte().addBands(img);
}

//Get an image collection of images of interest and add the band from function created above
var images = ee.ImageCollection('LANDSAT/LT5_L1T_TOA_FMASK')
.filterDate(start, end)
.filter(ee.Filter.dayOfYear(120, 275))
.filter(ee.Filter.eq('WRS_PATH', 30))
.filter(ee.Filter.eq('WRS_ROW', 28))
.filter(ee.Filter.lessThanOrEquals('CLOUD_COVER', 10))
.filterBounds(polygon).map(createTimeBand);

//make ndvi with a function
var make_ndvi = function(image) {
   return image.normalizedDifference(['B4', 'B3']);
 };

var ndvi = images.map(make_ndvi);

//get an image collection of the bands added which represent years since start of time series

function get_constant(images){
  return images.select('constant');
}
var constants = images.map(get_constant)

This code results in two image collections with 103 images each. The collections are namedndvi, and constants and each have 1 band per image, I want to combine the individual bands from the two collections in a sort of zipped list (in python its called a zip list at least) so that my output is one image collection with 103 images and with two bands per image. Then I can use regression on the bands through the collection. This problem is similar to the linear_fit code in the examples of Earth Engine, but I have to modify their code since I lose the original metadata when making NDVI. If I can keep the metadata when making my NDVI then I can solve it from there too.

1 Answer 1

4

You could either join those two imageCollections or add the NDVI and time bands in one function and then just keep those two bands. Here's how to do it in one function.

// Define start and end dates and geometry to select images by
var start = '1984-05-01';
var end = '2011-09-30';
var polygon = ee.Geometry.Polygon([[
 [-97.49404907226562, 46.59473135600069],
 [-97.09442138671875, 46.59567501063883],
 [-97.05734252929688, 46.31184150036163],
 [-97.53524780273438, 46.30709840788667]
]]);

// add NDVI and time bands
function timeNDVI(img) {
  var year = ee.Date(img.get('system:time_start')).get('year').subtract(1984);
  var img = img.normalizedDifference(['B4', 'B3']).addBands(img)
  return ee.Image(year).byte().addBands(img);
}

//Get an image collection of images of interest and add the band from function created above
var images = ee.ImageCollection('LANDSAT/LT5_L1T_TOA_FMASK')
.filterDate(start, end)
.filter(ee.Filter.dayOfYear(120, 275))
.filter(ee.Filter.eq('WRS_PATH', 30))
.filter(ee.Filter.eq('WRS_ROW', 28))
.filter(ee.Filter.lessThanOrEquals('CLOUD_COVER', 10))
.filterBounds(polygon).map(timeNDVI);

//keep NDVI and time bands and rename
var images = images.select(["nd", "constant"], ["ndvi", "time"])

print(images)

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.