0

Sorry if my question was poorly phrased, but basically I'm using Python with GDAL to identify vegetation in a TIF file (just using RGB bands). I've looked at the band values in the image, and looking at some random sample points have found that vegetation tends to fall within certain ranges for each band (ex: 30-71 blue, 125-175 green, etc). So this is how I wrote it out, with "arrays" being an array that contains the three bands:

vegetation = (arrays[0] > 67 & arrays[0] < 133) & (arrays[1] > 125 & arrays[1] < 175) & (arrays[2] > 30 & arrays[2] < 71)

And then I create a new file and just use band.WriteArray(vegetation). However, for the vegetation line I get the following error:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

What exactly does this mean? Should I be formatting it differently? Python isn't exactly my strength so the simpler the better.

AMC
  • 2,642
  • 7
  • 13
  • 35
Szpw
  • 1
  • Does this answer your question? [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – AMC Mar 31 '20 at 20:35
  • Please provide a [mcve], as well as the entire error message. – AMC Mar 31 '20 at 20:35

1 Answers1

1

& has higher precedence than >. So

arrays[0] > 67 & arrays[0] < 133

reads as

arrays[0] > (67 & arrays[0]) < 133

and chaining boolean operators (x > y > z) forces all operands to be convert to Booleans. For example

(arrays[0] > 67 & arrays[0]) < 133

is legal. Converting an array to Boolean is ambiguous, as your error says. What you probably want is

(arrays[0] > 67) & (arrays[0] < 133)

You can also drop all the other parenthesis, unless you prefer the style, since (x & y) & z == x & (y & z)

kabanus
  • 24,623
  • 6
  • 41
  • 74