raster_extract {rSPARCS} | R Documentation |
Extract Values from a Raster Map
Description
Crop the raster with the boundary of areas of your interest, and extract the values from the raster to each of these areas.
Usage
raster_extract(rastermap,refmap,ID.var,ID.code,cutpoint)
Arguments
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. |
Details
Usually for extracting data which are available as rasters such as the land coverage or land usage data.
Value
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. |
Examples
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)