5

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?

2

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.

1
  • Hi, Thanks for the response. But it ran into error. I dont have ArcGis. Though I am not familiar I have got R. If someone can help in step-by-step instructions in R, it would be really helpful. May 9 '16 at 11:03
2

You could try the following:

  1. Polygonize your raster

    (Raster > Conversion > Polygonize)


  1. Intersect your polygonized layer with your main polygon layer

    (Vector > Geoprocessing Tools > Intersect)


  1. Download/install the GroupStats plugin and use the result layer to calculate the sum of area for each classification:

    Example:

    Here's an attribute table for my polygonized layer (the DN field contains the raster values):

    Attribute table

    And here's using the GroupStats plugin to calculate and sum the area for each value:

    GroupStats

6
  • Hi, I converted the classified raster into vector. It took some time and resulted in 63k polygons with DN as either 0 or 1. And when I tried intersecting it with boundary vector, all the attribute columns were empty. I did not come across any error message too. May 9 '16 at 11:50
  • @Saravanakumar - Are both your raster and polygon layers using the same CRS?
    – Joseph
    May 9 '16 at 11:57
  • Hi, you are right. CRS for the files are different. Now how should i reproject and which file - raster or vector? Can I use 'Set Layer CRS' in the menu? May 9 '16 at 12:30
  • @Saravanakumar - Don't use Set Layer CRS, instead use the Save As... option for the vector shapefile and select the relevant CRS. Or use Raster > Projections > Warp (Reproject) for the raster and again select the relevant CRS =)
    – Joseph
    May 9 '16 at 12:33
  • I tried converting the projection and intersecting it. The process went into hang mode at 100% and the output file was not created. May 10 '16 at 10:27
2

I have done this sort of task by combining QGIS and R.

In QGIS:

  1. Split your vector layer by feature (Vector-->Data Management Tools-->Split Vector Layer)
  2. 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.

In R:

# 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")
1

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.

Using the extract vector sample from raster tool from the pktools Processing Toolbox:

enter image description here

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):

enter image description here

This will then create a vector polygon with the a field for each raster class and its associated proportion in each polygon, e.g.:

enter image description here

N.B. you need to have pktools installed on your OS to run the Plugin correctly. More information about installation can be found here

4
  • Thanks for your new idea. I installed PKTools as per instructions and ran it. I got the following message in Log: The following layers were not correctly generated. Output vector data set You can check the log messages to find more information about the execution of the algorithm And in the logs , there is no specific error and only the executed commands. May 10 '16 at 10:28
  • It is hard to debug without more information regarding your input datasets and the parameters that you specified to run the tool. Are all of the inputs identical as in the figure above?
    – dmci
    May 10 '16 at 10:41
  • Yes. The inputs are identical to those given in your snapshot. I believe you can check the parameters from the following commands: processing.runalg("pktools:extractvectorsamplefromraster","C:/Users/Dr Veerasivam/Documents/QGIS Proj/Jalna_satel/cotton.img","C:/Users/Dr Veerasivam/Documents/QGIS Proj/Jalna_satel/New folder/Village_reproj.shp",True,3,0,False,0,"none","0","-c 1",None) May 10 '16 at 10:52
  • can you try to remove the spaces in your file paths to see if that resolves it.
    – dmci
    May 10 '16 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.