0

I've created a mask over a vector shapefile and have then created another one which zooms in to the relevant area. The problem I have with this new mask is that the masked area is the bit that I want, so I need to invert it somehow. (Note that I imported numpy as np)

This is the code I used for the 2nd mask:

catchment_mask = gmask[minrow:minrow+nrow, mincol:mincol+ncol]
catchment_mask = np.where(catchment_mask == 1, 1., np.nan)

I've tried using this code to invert it but it has applied a mask to the entire area:

inverse_catchment_mask = (np.logical_not(catchment_mask))

I'd be really grateful for any suggestions!

1
  • Your indentation is off. There is a np.ma, MaskedArray subclass, but I don't think that's what you have. It looks like your mask is a slice of gmask, but then you make a new array using where. Why? That's no longer a view. Why the np.nan? logical_not of that is False. Commented Dec 16, 2017 at 19:12

1 Answer 1

1

If your cachement_mask consisted of boolean (True/False) values, then you solution based on logical_not would work as expected

But your mask consists of 1 or NaN, so you have to invert this more carefully. For example:

inverse_cachement_mask = np.where(np.isnan(cachement_mask), 1, np.nan)

This will create an inverse of your original mask, swapping the 1 and NaN values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation. I removed the np.where and changed it for logical_not with the relevant arguments and it has inverted it as expected with boolean values.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.