View source: R/raster_extract.R
raster_extract | R Documentation |
Crop the raster with the boundary of areas of your interest, and extract the values from the raster to each of these areas.
raster_extract(rastermap,refmap,ID.var,ID.code,cutpoint)
rastermap |
a raster map containing the information you need, such as the National Land Cover Database 2011. |
refmap |
"SpatialPolygonsDataFrame" object. A reference map containing the boundary information of your study areas. |
ID.var |
the name of variable in the refmap indicating the unique ID for each of your study areas. |
ID.code |
a character vector containing the unique ID for areas that you want to extract the values to. ID.code=ALL" by default where all areas in the reference map are of interest. |
cutpoint |
a number to dichotomize the values in the raster; specified ONLY when those values are continuous. |
Usually for extracting data which are available as rasters such as the land coverage or land usage data.
ID.code |
the column indicating the unique ID for each area, followed by the number of cells for each category/colour within that area. |
Total cells |
the total number of cells within each area. |
library(raster)
set.seed(4715)
rast=raster(matrix(rnorm(500),100,100))
extent(rast)=c(50,100,10,60)
crs(rast)=CRS("+proj=longlat +datum=WGS84")
ref=cbind(x=c(60,80,80,70), y=c(20,25,40,30))
p=Polygon(ref)
ps=Polygons(list(p),ID="ID")
ref=SpatialPolygons(list(ps))
data=data.frame(value=1, ID="10086",row.names="ID")
ref=SpatialPolygonsDataFrame(ref,data)
proj4string(ref)=CRS("+proj=longlat +datum=WGS84")
raster_extract(rastermap=rast,refmap=ref,ID.var="ID",ID.code="ALL",cutpoint=0.5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.