I have here a FeatureCollection containing some productive forests of the company I work for.
The age in month for each forest site is important to me, since my study only looks for forests that are between 1 and 5 month of age.
With this age I calculated, at a desktop GIS software, the maximum date and the minimum date to filter the Landsat 8 images inside a period of the year (September to May).
So, with all these informations together as a FeatureCollection in my Assets, I have to define a function to be mapped to the whole FeatureCollection containing company's forests.
The function needs to get values from columns MIN_DATE
and MAX_DATE
to filter the Landsat images, apply cloud masking, calculate NDVI and return the feature to the collection with the mean of NDVI computed as a column.
I've already developed something here, but it keeps returning me some weird result.
The code:
/ Script to get historical NDVI for each productive area
// Import FeatureCollection
var IPTalhoes = ee.FeatureCollection('users/ee_rootDir/toiFinal');
// Define function to map over collection
function calcHistoricalNDVI(feature) {
// Get initial and end date
var dateIni = feature.get('MIN_DATE');
var dateEnd = feature.get('MAX_DATE');
// Import Landsat 8 ImageCollection
var imgCollection = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA");
// Filter imgCollection
var imgFiltered = imgCollection
.filterDate(dateIni, dateEnd)
.filterBounds(feature)
.map(function updateCloudMask(image) {
var mask = ee.Algorithms.Landsat.simpleCloudScore(image);
var imgUpdated = image.updateMask(mask.select('cloud').lt(20));
return(imgUpdated);
});
// Calculate NDVI
var imgNDVI = imgFiltered
.map(function calcNDVI(image) {
return(image.expression(
'(b(4) - b(3)) / (b(4) + b(3))'
).select(['B5'], ['NDVI']));
});
// NDVI mean inside a period
var NDVIMean = imgNDVI.mean();
// Reducer statistics - mean by talhao
var NDVIMean2 = NDVIMean.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: feature.geometry(),
scale: 30
});
// Turn dictionary with NDVI values to FeatureCollection
var NDVIMean2Feat = feature.set('NDVI', NDVIMean2);
// Return Feature as function result
return(NDVIMean2Feat);
}
// Map main function over collection
var NDVITalhoes = IPTalhoes.map(calcHistoricalNDVI);
// Print results
print(NDVITalhoes);
The issue:
When I print the Feature Collection, what I see is that there is a column named 'NDVI' in FeatureCollection with an "Object" within, but when I open each feature in console, I can't find any property with this name, and indeed there are only 27 properties while there are 29 columns for the whole FeatureCollection.
Can anyone help me out?