I have a rasterbrick
s
(10000 raster layers) that actually is a time series
raster. Accompanying s
is a SpatialPolygonsDataFrame
with many sub-polygons
which can be identified by names and/or IDs etc. All data sets have the same spatial extent.
Now I have a SpatialPointsDataFrame
wherein lat
and lon
for each site is specified. The sites are also named
.
r <- raster(ncol=36, nrow=18)
r[] <- 1:ncell(r)
s <- stack(r, r*2)
cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
polys <- SpatialPolygons(list(Polygons(list(Polygon(cds1)), 1),
Polygons(list(Polygon(cds2)), 2)))
plot(r)
plot(polys,add=T)
#v <- extract(s, polys, small=TRUE)
Assume site names in SpatialPointsDataFrame
can be generated using:
site_names=replicate(nrow(SpatialPointsDataFrame), paste(sample(LETTERS, 5, replace=TRUE), collapse=""))
Then values for a particular xy
pair can be obtained using for e.g:
vals <- extract(s, matrix(c(-106.43, 52.1), ncol = 2))
How can I extract raster time series values for each xy point
in SpatialPointsDataFrame
per sub-polygon
. The column names of the extracted values per point should be identified by the names of sites in SpatialPointsDataFrame
. Extracted values for each polygon should be saved in a separate dataframe which I can use to form a list object containing data for all sub-polygons then do further analysis on it. So if I have 2 sub-polygons then I will have 2 dataframesbut with different # of sites (columns) depending on how many sites are in each sub-polygon.