This question already has an answer here:

Thansk I'm using ArcGIS 10.

My task is to map forest cover with the image classification tool. The problem is some of the patches of forest are dark and are been classified as cloud shadows in the image classification. I need to convert raster values that represent cloud shadows into the raster value for forests. But only only want to do this for an area that falls within in a polygon.

Is there way to reclassify a raster but only the cells that fall within a polygon?

marked as duplicate by PolyGeo arcgis-desktop Aug 29 '16 at 0:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


I think this answer published by @Zbynek completely response your question:

Reclassifying only part of raster keeping rest unchanged?

library (raster)

# use state bounds from gadm website:
# us = shapefile("USA_adm1.shp")
us <- getData("GADM", country="USA", level=1)
# extract states (need to uppercase everything)
nestates <- c("Maine", "Vermont", "Massachusetts", "New Hampshire" ,"Connecticut",
              "Rhode Island","New York","Pennsylvania", "New Jersey",
              "Maryland", "Delaware", "Virginia", "West Virginia")

ne = us[match(toupper(nestates),toupper(us$NAME_1)),]

# create a random raster over the space:        
r = raster(xmn=-85,xmx=-65,ymn=36,ymx=48,nrow=100,ncol=100)

# plot it with the boundaries we want to clip against:

# now use the mask function
rr <- mask(r, ne)

# classification of masked cells
rr[] <- ifelse(rr[]>0.5,1,0)

# if you have only one value you want to change, use this:
rr[rr == 1] <- 5

# add classification to the original file - "join" the original, and reclassified raster file 
r@data@values[!is.na(rr@data@values)] <- rr@data@values[!is.na(rr@data@values)]

# plot, and overlay:

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