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)))

#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.

  • Did you have a look into loops? Because whenever you want to do something several times this is the way to go. You could put your workflow for extracting and saving as new dataframe within a for loop and the iterator would be your (list) of sub-polygons. If you are not familiar with it you may have a look at this tutorial: datacamp.com/community/tutorials/tutorial-on-loops-in-r
    – Matte
    Commented Apr 17, 2016 at 5:40
  • @Matte I can use loops so well but my problem is how to extract the data I want from each site in each polygon. I am not an expert manipulating raster data but can work well with text files etc.
    – code123
    Commented Apr 18, 2016 at 2:15


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.