raster 이미지의 일부를 추출해보자. 특히, shapefile에 담겨져 있는 polygon에 해당하는 raster 를 추출해보자. raster 패키지의 crop 과 mask 함수를 이용할 수 있다.
# Create some data using meuse library(raster)data(meuse)coordinates(meuse) <-~x+yproj4string(meuse) <-CRS("+init=epsg:28992")data(meuse.grid)coordinates(meuse.grid) =~x+yproj4string(meuse.grid) <-CRS("+init=epsg:28992")gridded(meuse.grid) =TRUEr <-raster(meuse.grid) r[] <-runif(ncell(r))# Create a polygonf <- rgeos::gBuffer(meuse[10,], byid=FALSE, id=NULL, width=250, joinStyle="ROUND", quadsegs=10) # Plot full raster and polygon plot(r)plot(f,add=T)
# Crop using extent, rasterize polygon and finally, create poly-raster# **** This is the code that you are after **** cr <-crop(r, extent(f), snap="out") fr <-rasterize(f, cr) lr <-mask(x=cr, mask=fr)# Plot resultsplot(lr)plot(f,add=T)