I have a shapefile containing 38 polygons i.e. 38 states of a country. This shapefile is overlaid on a raster. I need to extract/reclassify pixel above a certain value, specific to each polygon. For example, I need to extract the raster pixels> 120 for state/polygon 1, pixels> 189 for polygon 2 etc with the resulting raster being the extracted pixels with value 1 and everything else as NoData. Hence, it seems like I need to extract first and then reclassify.
I have the values, for extraction, saved as a data frame with a column containing names, matching the names of the states,which is stored as an attribute "Name"
in the shapefile.
Any suggestion on how I could go about this? Should I extract the raster for each state into 38 separate rasters, then do reclassify()
and then mosaic
to make one raster i.e. the country?
Best Answer
Here's a way to do it:
Need these (I'd use
sf
butraster
doesn't play withsf
):Make some sample data. Use the scottish boundaries:
And add a field which is your threshold value:
Now make a sample raster data, 1km cells, over the area of the polygons:
Right. Now we can actually get to the work since we've got data that should be like yours. First make a raster on the same grid as
r
but with the threshold values from the polygons:and then you can do:
which is a grid of 1 where the raster is greater than the underlying polygon's threshold value, and zero elsewhere, and nodata where there's nodata in the raster. Converting 0 to nodata is easy enough, or possibly not necessary. Anyway, the trick is rasterizing and then comparing.