### 4.1.1 Shapefiles, one shapefile for each class. ### Get the names of the training shapefiles train.files <- list.files(pattern="*train.*shp$") ### Read in the raster image. Replace name of image file with the name of the image. image <- "name of image file" img <- stack(image) ### Create an object to receive the training data train.full <- NULL ### This loop extracts the training data for each shapefile and adds it sequentially to the training dataset. for (x in seq_along(train.files)) { ### Read in the a shapefile train.locations <- st_read(train.files[x]) ### Extract values from the image for the shapefile area. train.predictors <- raster::extract(img, train.locations) ### Combine the data from multiple polygons in the shapefile. If the data is in a point shapefile instead of polygons, skip this next line. train.predictors <- as.data.frame(do.call(rbind, train.predictors)) ### Add a column for the class type and populate it. train.by.class <- as.data.frame(cbind(train.predictors, "types" = substr(train.files[x], 1, nchar(train.files[x])-9))) ### Combine the data with data from previous loops. train.full <- rbind(train.full, train.by.class) ### Make all your response variables numeric (categorical variables will be addressed later) train.full <- train.full %>% mutate_each(funs(as.numeric), starts_with(substr(image, 1, nchar(image)-4))) } ### 4.1.2 One shapefile with multiple classes. ### Read in the raster image. Replace name of image file with the name of the image. image <- "name of image file" img <- stack(image) ### Read in the training shapefile. Replace shapefile with the name of the shapefile. The shapefile should have an attribute named "types" for class designations (e.g., water). train.locations <- st_read("shapefile") ### Create an object to receive the training data. train.full <- NULL ### This loop extracts the training data for each class and adds it sequentially to the training dataset. for (i in 1:length(unique(train.locations[["types"]]))){ ### Sequentially extract the name for each unique class. train.type <- unique(train.locations[["types"]])[i] ### Sequentially create a spatial polygon object for each class. train.type.locations <- train.locations [train.locations[["types"]] == train.type,] ### ### Extract values from the image for the subset of the shapefile area. train.by.class <- raster::extract(img, train.type.locations) ### Combine the data from multiple polygons in the shapefile. If the data is in a point shapefile instead of polygons, skip this next line. train.by.class <- as.data.frame(do.call("rbind", train.by.class)) ### Add a column for the class type and populate it. train.by.class <- cbind(train.by.class, "types" = as.character(unique(train.locations[["types"]])[i])) ### Combine the data with data from previous loops. train.full <- rbind(train.by.class, train.full) ### Make all your response variables numeric (categorical variables will be addressed later) train.full <- train.full %>% mutate_each(funs(as.numeric), starts_with(substr(image, 1, nchar(image)-4))) } ### 4.1.3 Csv files, one csv file for each class. ### Get the names of the training csv files. train.files <- list.files(pattern="*train.*csv") ### Read in the raster image. Replace image with the name of the image. img <- stack("image") ### Create an object to receive the training data. train.full <- NULL ### This loop extracts the training data for each csv file and adds it sequentially to the training dataset. for (x in seq_along(train.files)) { ### Read in the csv file. train.locations <- read.csv(train.files[x]) ### Set the values in the csv data to be coordinates. coordinates(train.locations) <- c("x","y") ### Extract values from the image for the point locations. train.predictors <- as.data.frame(raster::extract(img, train.locations)) ### Add a column for the class type and populate it. train.by.class <- cbind(train.predictors, "types" = substr(train.files[x], 1, nchar(train.files[x])-9)) ### Combine the data with data from previous loops. train.full <- rbind(train.full, train.by.class) } ### 4.1.4 Csv file with training data already extracted and assembled. ### Read in the training data, replacing the filename with the name of the csv file. train.full <- read.csv("my-data-file.csv")