I have a classified raster file and a polygon vector file. I need to overlay polygon and find out area occupied under each classification in each polygon. I am not familiar with R. Can you please suggest a solution in QGIS?
If you have ArcGIS with Spatial Analyst extension, you can use tabulate area to calculate the area of each class under each polygon.
In QGIS, there is no direct way to do that, but you can try "Cross-classification and tabulation" under Processing Toolbox -> SAGA -> Grid - Analysis. It may do the same thing.
You could try the following:
Polygonize your raster
Raster > Conversion > Polygonize)
Intersect your polygonized layer with your main polygon layer
Vector > Geoprocessing Tools > Intersect)
Download/install the GroupStats plugin and use the result layer to calculate the sum of area for each classification:
Here's an attribute table for my polygonized layer (the
DNfield contains the raster values):
And here's using the GroupStats plugin to calculate and sum the area for each value:
I have done this sort of task by combining QGIS and R.
- Split your vector layer by feature (Vector-->Data Management Tools-->Split Vector Layer)
- Using batch processing (Processing-->Toolbox-->GDAL-->Extraction-->Clip raster by mask layer), clip your raster by each of your split vector features and save each raster clip to file in a common directory.
# Load packages; install if necessary using install.packages() library(raster) library(rgdal) # Set working directory to the directory containing your raster clips setwd("/Users/name/.../clips_directory") # Load your raster clips into R workspace temp = list.files(pattern = ".tif") list2env(lapply(setNames(temp, make.names(gsub(".tif", "", temp))), raster), envir = .GlobalEnv) # Or load them one at a time clip1 <- raster("clip1.tif") # Use freq() to get the total number of pixels in each class; convert output to data frame clip1_df <- data.frame(freq(clip1)) # Export as csv if you want write.csv(counts_clip1, "file_name.csv")
In QGIS, you could use the
pktools plugin (Under Experimental Plugins) to calculate the proportion of classified pixels per polygon. In the following example, the classification output has three classes (1:3) and there are three polygons for which I would like to calculate the proportion of pixels per class in each polygon.
extract vector sample from raster tool from the pktools Processing Toolbox:
You can then use the menu to specify your input options (raster and vector). Note, that you need to specify the classes for which you require proportions to be calculated under
Additional parameters [optional] as follows
-c 1 -c 2 -c 3 (update depending on the number of classes you want):
This will then create a vector polygon with the a field for each raster class and its associated proportion in each polygon, e.g.:
N.B. you need to have pktools installed on your OS to run the Plugin correctly. More information about installation can be found here