I have many point locations with time-date stamps and 18 raster layers each containing the annual NDVI values from the year 2002 till 2019. I want to extract the values for the locations from the raster layer of the year similar to the year of the location time stamp.
Example:
location1 time stamp 12.04.2003 --> extract value from raster layer 2003
location2 time stamp 12.06.2012 --> extract value from raster layer 2012
This is a sample of my location data (EPSG 3035):
X id areas sex x_ y_ case time
1 1 1 13 f 4141879 2762606 1 2010-11-16 00:59:00
2 2 1 13 f 4142395 2759956 1 2010-11-16 21:59:00
3 3 1 13 f 4143634 2761615 1 2010-11-17 04:59:00
4 4 1 13 f 4143171 2761593 1 2010-11-17 11:59:00
5 5 1 13 f 4144488 2762547 1 2010-11-17 18:59:00
6 6 1 13 f 4143885 2761944 1 2010-11-18 08:59:00
First I created a raster stack with all 18 raster layers and added the date for each layer:
#create raster stack NDVI
setwd("E:/MA_2Try/annual_NDVI/average")
grids <- list.files("E:/MA_2Try/annual_NDVI/average" , pattern = "*.tif$")
NDVI_stack=stack(grids)
#Add years to stack
dt<-as.data.frame(
as.POSIXct(c('2002-01-01','2003-01-01','2004-01-01','2005-01-01','2006-01-01',
'2007-01-01','2008-01-01','2009-01-01','2010-01-01','2011-01-01',
'2012-01-01','2013-01-01','2014-01-01','2015-01-01','2016-01-01',
'2017-01-01','2018-01-01','2019-01-01')))
NDVI_stack <- setZ(NDVI_stack, dt[,1], "SampleDate")
Here is the resulting info for raster [[1]]
> NDVI_stack[[1]]
class : RasterLayer
dimensions : 4125, 5503, 22699875 (nrow, ncol, ncell)
resolution : 250, 250 (x, y)
extent : 3530500, 4906250, 2229250, 3260500 (xmin, xmax, ymin, ymax)
crs : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs
source : ndvi_average_2002.tif.tif
names : ndvi_average_2002.tif
SampleDate : 2002-01-01
How can I achieve this?