'extract RGB values from different raster-files with different shapefiles
I have a dataset with 50 different rasterfiles and 50 different shapfiles. The rasterfiles are orthophotos from a forest and each orthophoto has a belonging shapefile where the tree crwons are marked with points. I now want to extract the RGB values of the crowns with a for loop. Until now I wrote a normal for-loop but here is the problem that I just have one iterator but actually I need two (one for the list of shapefiles and one for the list of orthophotos). I also tried a nested for loop but then it is the problem that one shapefile is analysed with all orthophotos and so the previous data always gets overwritten.
So this is my code for the nested loop:
library(rgdal)
library(rgeos)
library(exactextractr)
library(sp)
library(sf)
library(raster)
#gets list of shapefiles
sites <- list.files("C:/Users/Kai/OneDrive - UT Cloud/Documents/Praktikum Mitacs/Data Files/ManualTreetops_2019_RGB/ManualTreetops_Finished",pattern = "*.shp")
#removes ".shp" file extension
sites <- sub(".shp","",sites)
orths <- list.files("C:/Users/Kai/OneDrive - UT Cloud/Documents/2019_ortho",pattern="*.tif", full.names = TRUE)
output <- list()
#loops through list of files
for (i.site in sites) {
#each time through i.site represents a different site
tt <- st_read("C:/Users/Kai/OneDrive - UT Cloud/Documents/Praktikum Mitacs/Data Files/ManualTreetops_2019_RGB/ManualTreetops_Finished", i.site)
#create 1 m radius circles around each treetop
crowns <- st_buffer(tt, 1)
for(i.orth in orths) {
setwd("C:/Users/Kai/OneDrive - UT Cloud/Documents/2019_ortho")
oo <- brick(i.orth)
#extracts the mean band values for each crown
#much faster than extract in raster package
crown.rgb <- exact_extract(oo,crowns,"mean")
}
output[[i.site]] <- crown.rgb
#add results to list of output from for loop
} #end of for loop
Does anyone has an advise how I can compare a shapefile just with one orthophoto, but doing it automaticaly in a loop?
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
