I am trying to select pixels from a MODIS raster (with a 250 meters resolution and covering all Europe) based on their location. For that I am using a shapefile that contains ca. 200 points (also distributed across all Europe) representing the location of different time series. I am particularly interested in selecting all pixels that intersect with a 1000m-buffer around each point. Finally, for further analysis, I need that the selected pixels (those that intersect with the buffers) will have the id of the point (i.e., the id of the time series). To save time I am trying to avoid working with vector data. So I have rasterized the shapefile using the extension and resolution of the MODIS raster and ratify it as suggested here. The problem I have is that after using raster::buffer the RAT (raster attribute table) disappears and all pixel has a value = 1. Does anyone know how can I buffer a raster and keep the RAT?
Below a reproducible code showing the problem. The layers are subset form my data and are accessible here:
library(raster) library(rgdal) library(rasterVis) # load Alps Modis MODIS_Alps <- raster("/ModisAlpsCrop.tif") plot(MODIS_Alps) # reproject sr <- "+proj=longlat +datum=WGS84" MODIS_Alps_LatLong <- projectRaster(MODIS_Alps, crs = sr) plot(MODIS_Alps_LatLong) # load TS BeechAlps <- read_csv("/BeechAlps.csv") BeechAlps <- BeechAlps %>% st_as_sf(coords = c("X", "Y"), crs = 4326) %>% dplyr::select(ID) plot(BeechAlps, col= "black", add=TRUE) # get names nam <- unique(BeechAlps$ID) # create a data.frame nam_df <- data.frame(ID2 = 1:length(nam), nam = nam) # Place IDs BeechAlps$ID2 <- nam_df$ID2[match(BeechAlps$ID,nam_df$nam)] # rasterize MODIS_Alps_LatLong r <- raster(ncol = 185, nrow = 162, ext = extent(MODIS_Alps_LatLong)) # resolution and extent from MODIS ras <- rasterize(x = BeechAlps, y = r, field = "ID2") # ratify raster r2 <- ratify(ras) # Create levels rat <- levels(r2)[[1]] rat$names <- nam_df$nam rat$IDs <- nam_df$ID2 rat levels(r2) <- rat rasterVis::levelplot(r2) r2 # buffer b <- buffer(r2, width=1000) b plot(b, col="black") 