The code that you copied uses an Earth Engine object of type "Image", and Image objects have a method/function named clip. Your reworked code (i.e. Landsat) uses an Earth Engine object of type "ImageCollection", and ImageCollection objects do not have a method named clip, so it produces an error. To get around this, you can either:
- Convert the ImageCollection to an Image using a reducing method (i.e. median, mode, mosaic, max, etc.)
- Write a function that clips an Image object, and map that function over the collection using ee.ImageCollection.map().
Also, it probably doesn't make sense to add the entire Landsat 5 collection as a layer on the interactive map... it contains over 25 years of data. Add a step that filters the Landsat collection down to a shorter time interval, using ImageCollection.filterDate().
You currently have an image which has an NDVI value for each pixel in the original image. In order to get a single number, you need to reduce the image — to take the mean, median, maximum, minimum, or other such operation on pixels in the image, within a region of interest. Here is the documentation on reduceRegion
, which is the way to do this.
Since you have an image collection and want a table, you also need to map over the image collection. Whenever you want a number or other value for each image in a collection, the way you do that is by adding a property to each image, using set()
, or by producing new features that have only the data you want.
This example I wrote is in JavaScript since I wrote it in the Earth Engine Code Editor, but the differences with Python are fairly small so I hope you can follow along.
var meanNDVICollection = imgs.map(function (img) {
// Your code.
var nir = img.select('B5');
var red = img.select('B4');
var ndviImage = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
// Compute the mean of NDVI over the 'region'
var ndviValue = ndviImage.reduceRegion({
geometry: region,
reducer: ee.Reducer.mean(),
}).get('NDVI'); // result of reduceRegion is always a dictionary, so get the element we want
// Add the value as a property to each image in the collection.
// return img.set('NDVI', ndviValue);
// Or, create a new feature with only the properties we want.
return ee.Feature(null, {
// Adding property we computed.
'NDVI': ndviValue
}).copyProperties(img, [
// Picking properties from the original image.
'system:time_start',
'SUN_ELEVATION'
])
});
// Export, or you can use any other method for getting a table out of Earth Engine.
Export.table.toDrive({
collection: meanNDVICollection
});
https://code.earthengine.google.com/580f2f06c9978caf43eac42b5dd0ff44
(Disclaimer: I do not have training in the scientific and statistical aspects of remote sensing. The choices of mean
and scale: 20
in this script are for demonstration of Earth Engine only and should not be assumed to produce scientifically valid results.)
Best Answer
You can get the minimum values similar to how you got the max values, but instead you'll need the first position to use arraySlice.
Then you can continue with the script similar as you did with the max values.
Here you can find a link to the full script.