I have a slope raster and a road raster. The black pixels in the road raster are NoData values and the pink pixels have a value of 1. They don't cover exactly the same area. enter image description here

I want to create a new raster based on the slope raster, except for pixels where there is a road pixel. If there is a road pixel, I want to assign a value of zero to the slope raster.

I am not quite sure how the formula needs to look like. I tried the following, put it didn't give me the expected results:

r.mapcalc "output = ((Slope+(Road*null())))"

Any ideas?

1 Answer 1


Use this r.mapcalc expression:

r.mapcalc "output = not(if(Road))*Slope"


  • if(Road)is equal to 1 if Road not zero, 0 otherwise.
  • not(if(Road))is equal to 0 if Road not zero, 1 otherwise.
  • Though the part of the Slope Raster not covered by the Road Raster gets cut off. Is there a way to keep that part?
    – ustroetz
    Commented Oct 30, 2013 at 22:24
  • 2
    You could use r.patch before in order to make the Road raster cover the lower part of the Slope raster. Commented Oct 30, 2013 at 22:31
  • 1
    more simply, g.region rast=slope.
    – gene
    Commented Oct 31, 2013 at 16:17
  • 2
    Note that for GRASS 7, the command should be r.mapcalc "output = not(if(Road))*Slope" (both spaces and quotes are needed).
    – wenzeslaus
    Commented Feb 20, 2014 at 23:23
  • @wenzeslaus Thanks for the suggestion, I've updated the answer. Commented Feb 21, 2014 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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