0

I generate a raster map in R with some shaded portion, then i plot my shape file on the raster file to show boundaries of the map. I can calculate the the overall shaded area with a code but I want to calculate the shaded region coming under the separate polygons when i plot shape file on raster. Please help me with the code.

I am using maxent in R to have an idea of suitable area of certain crop for whole country. when I generate map, it is a raster file and I can calculate suitable area for whole country with a code, but I want to calculate the area for provinces as well for which i plot province vise shape file on the raster map.

I want help with the area calculation for each shaded polygon when i plot shape file on raster

pred_me2 [pred_me2 <=0.33] <- NA

pred_me2 [pred_me2 >0.66] <- NA

cell_size<-area (pred_me2, na.rm=TRUE, weights=FALSE)

cell_size<-cell_size[!is.na (cell_size)]

suitable<-length (cell_size)*median(cell_size)
bharatk
  • 4,202
  • 5
  • 16
  • 30

1 Answers1

0

You can try with this:

cell_size <- xres(pred_me2)*yres(pred_me2)
area_NA<- sum(is.na(values(pred_me2))) * cell_size 
area_non_NA <- sum(!is.na(values(pred_me2))) * cell_size
francisco corvalan
  • 300
  • 1
  • 4
  • 10
  • Thank you so much but this code still doesn't help me to calculate area coming under different polygons when i paste shape file on my raster. This code works exactly in the same way my previous code does (code that is mentioned above). – tayyabakhalil4 Oct 17 '19 at 00:03
  • You have to mask your raster with your polygon and then calculate their área. Maked <- mask(raster, polygon) – francisco corvalan Oct 18 '19 at 23:05