This repository contains a single function that can be sourced in your R environment. The function extract digital values from a single or multi layer raster file (e.g. geotiff) and produces a data frame containing a class column (Class: Factor) and one or multiple predictor columns (Band1: num, Band2: num, ...), depending on the number of layers that the raster file contains. The final dataframe can be used for subsequent statistical analysis in R, such as land cover classification.
NOTE: The class column is created based on the attribute table that you probably created using a GIS software. The attribute table lists e.g. polygons that define your land cover classes. You must name the columns in your attribute table id and Class for the function to work.
install.packages(c("raster"))
source("FUN_extract_polyclass.R")
img <- brick("data/rasterfile")
shp <- shapefile("data/shapefile.shp")
testdf <- extract_polyclass(shp, img)
If you find the function useful, please give it a "Star" and tell others. Let me know if there is room for improvement (there always is). Cheers, René