With a lot of help I recently figured out how to extract NDVI values for a specific date for a collection of GPS points (Extracting NDVI value for GPS points - how can I extract NDVI value for specific date and time for each GPS point?)
However, when sampling the image to extract the NDVI value I lose my ID column - so I do not know for which GPS point I have just extracted the NDVI value.
After a suggestion I tried using:
var extract = filtIm.sampleRegions({
collection: pts,
properties: ['OBJECTID'],
scale: 30
});
but in my final feature collection, the 'ObjectID' column still does not pop up.Is there a way in which I could append my NDVI value back into the original GPS point feature collection?
My full code:
/////////////////////////////
//// Obtain NDVI layer //////
/////////////////////////////
var filtered = L8.filterDate('2017-01-01', '2021-06-01').filterBounds(pts);
var rgb_vis = {min:0, max: 0.3, bands:['B4', 'B3', 'B2']};
var image = ee.Image(filtered.first());
function addNDVI(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']);
return image.addBands(ndvi)
}
var NDVI = addNDVI(image);
var with_ndvi = filtered.map(addNDVI);
Map.addLayer(filtered, rgb_vis, 'RGB');
Map.addLayer(NDVI, {bands: 'nd', min:0, max:1}, 'NDVI');
Map.addLayer(with_ndvi, {bands: 'nd', min:0, max:1}, 'withNDVI');
/////////////////////// EXTRACT NDVI /////////
// Use the pts date field to filter the image collection
// and then extract the image values of the filtered image
var extractpts = pts.map(function(feat){
// Get date from each pts feature
var date = feat.get('Fixtime');
// Convert it to date format, indicating the format
date = ee.Date.parse('d/M/y H:m', date);
// Create range to filter image, e.g, one week before or after
var start = date.advance(-1, 'month');
var end = date.advance(1, 'month');
// Filter using the date ranges and get first image
var filtIm = with_ndvi.filterDate(start, end)
.first();
// Sample image info using the feature
var extract = filtIm.sampleRegions({
collection: pts,
properties: ['OBJECTID'],
scale: 30
});
return extract;
});
// Flatten FeatureCollections into a single FeatureCollection
extractpts = extractpts.flatten();
print(extractpts.first());
Due to the sensitive nature of my GPS dataset I can only provide a subset: https://code.earthengine.google.com/?asset=users/u17177121/LIW_Subset
extractpts
object. Try running this line at the end of your code to verify it:print(extractpts.select(['OBJECTID', 'nd']));