I am looking to combine two single-band, 1-bit rasters of the same spatial extent based on their relationship to a polygon (mask). The image shows a polygon displayed with a cross-hatch and the two input rasters (Raster 1 & Raster 2) as well as the desired output raster (Raster 3). I need to keep the Raster 2 cells that are covered by the polygon mask otherwise keep the Raster 1 cells. This seems like it could be accomplished using Con or map algebra with a mask, although I have not been able to find a solution. How can I combine two input rasters based on their relationship to a polygon (or mask)?
1 Answer
First and foremost, it's important to understand that Spatial Analyst tools that use vector data always convert the vectors to rasters internally. For example, even though the Zone Data in the Zonal Statistics tool accepts feature classes, they are rasterized internally; this is why very small zones are excluded and overlapping polygons are problematic.
With that in mind, here's what I would do:
- Convert your polygon mask to a raster using the Feature to Raster tool. Make sure to set the extent and snap raster environment variables to be the same as your "Input Raster 1" and "Input Raster 2" (see screenshot below). The output will have the value of the selected field in the polygon's extent and
Null
everywhere else.
- Using Raster Calculator, use a combination of
Con
andIsNull
to set your value using the expressionCon(IsNull("Rasterized polygon"), "Raster 2", "Raster 1")
. This is just like anIF
statement in Excel, where the first argument is the boolean expression, the second argument is the value if the expression returnsTrue
, and the third argument is the value if the expression returnsFalse
.