I'm trying to create a new raster from an existing slope raster where slopes are between 5 and 20 degrees. However, I'm receiving the error "ValueError: The truth value of a raster is ambiguous. Invalid use of raster with Boolean operator or function. Check the use of parentheses where applicable."

It's obvious it just has to do with my Map Algebra syntax, but I can't figure it out. Here's a snippet of my code:

slope - Slope("elevation")    # creates slope raster, elevation is raster object
goodslope = (slope < 20) & (slope > 5)

Maybe I have to use a Con() statement?

  • 1
    Please add tag(s) for the software and version. – Tom Apr 4 '16 at 4:03
  • Please say in the body of your question precisely the software that you are using. If you tag it correctly that will help a potential answerer find your question too. – PolyGeo Apr 4 '16 at 4:07
  • Please provide a minimal complete example that will reproduce the problem. I don't think that snippet is your actual code, otherwise you'd get a NameError: name 'slope' is not defined as the slope variable is not getting defined in the slope - Slope("elevation") line (notice the - symbol instead of the = symbol). – user2856 Apr 4 '16 at 4:23
  • 1
    Are you sure you are using parentheses...? Can you run that exact snippet (with 1st line fixed to define slope correctly)? Your code works fine for me in 10.2.2. I get the ValueError: The truth value of a raster is ambiguous... exception if I don't have parentheses around the (slope < 20) and (slope > 5) relational expressions, i.e. goodslope = slope < 20 & slope > 5. This is because Boolean (~, &, ^, |) operators have a higher precedence level than Relational (<, <=, >, >=, ==, !=) operators. – user2856 Apr 4 '16 at 4:26

Depends on what you want to create. If you want a raster where your goodslope areas are "1" and the rest are NoData, Con is a good way to go about it:

slope = Slope("elevation") # elevation is the name of a raster!
goodslope = Con((slope < 20) & (slope > 5), 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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