I have a raster map that represents land cover of a region, and different shapefiles (total 120) in the same region. I want to know the percentage of every land cover for each shapefile.
For example, I want to know % of forest, % of cultivated area, % of grassland, and so on, for each shapefile.
I already tried clipping the raster with the shapefiles in Qgis, but is a slow process. I think with R is possible too, but I have not been able to do it yet:
setwd("/home/shapefiles")
shps <- dir(getwd(), "*.shp") #looking for shapefiles
library(maptools)
list.shape<-list()
for (shp in shps){
list.shape[[shp]]<- readShapeSpatial(shp) #Reading shapefiles
}
#Opening raster
library(raster); library(rgeos); library(rgdal)
r.landcover <- raster('/home/raster')
shape.integrated<-c()
for (k in 1:length(list.shape)){
shape.integrated<-gUnaryUnion(list.shape[[k]]) # Joining shapefiles
r.vals[[k]]<- extract(r.landcover, shape.integrated) # Extract values of the raster according to the shapefile
percent[[k]]<- lapply(r.vals[[k]], FUN=I NEED AN OPTION THAT CALCULATE PERCENTAGE OR SOMETHING SIMILAR HERE) # Percentage
}
I can use Qgis, R, GRASS (Open source).
Best Answer
One solution would be to convert each of your polygons to rasters. Then take the portion of cell counts in each landcover class by the total cell count from the clip. Here is an example function:
With your data, copy and paste the above & then try this function in your loop: