0

I have a continuous raster with a range of 0.135 - 0.921 and I want to flip the range or invert it, so the values are min becomes max and max becomes min. I've tried using np.flip() but that results in flipping the array instead of the values, so I was thinking about the following workflow:

  1. Get unique values and sort them
  2. Use (max + min) - x to generate flipped values
  3. Place original and flipped values into a list and reclass values

I feel like there's an easier way to accomplish this.

            old                                 new             
0.23    0.21    0.48    0.55    |   0.82    0.85    0.57    0.51
0.70    0.48    0.96    0.23    |   0.37    0.63    0.09    0.82
0.23    0.23    0.76    0.42    |   0.82    0.82    0.29    0.67
0.91    0.91    0.05    0.68    |   0.14    0.14    1.00    0.50
0.54    0.38    0.53    1.00    |   0.51    0.67    0.52    0.09
0.91    0.54    0.73    0.22    |   0.14    0.52    0.32    0.84
0.17    0.20    0.09    0.76    |   0.96    0.88    0.96    0.29
0.23    0.91    0.91    0.23    |   0.82    0.14    0.14    0.82


old values  | new values ((max+min) - x)
-----------------------------------------
0.05        |            1.00
0.09        |            0.96
0.17        |            0.88
0.2         |            0.85
0.21        |            0.84
0.22        |            0.83
0.23        |            0.82
0.38        |            0.67
0.42        |            0.63
0.48        |            0.57
0.48        |            0.57
0.53        |            0.52
0.54        |            0.51
0.55        |            0.50
0.68        |            0.37
0.7         |            0.35
0.73        |            0.32
0.76        |            0.29
0.76        |            0.29
0.91        |            0.14
0.91        |            0.14
0.96        |            0.09
1           |            0.05

1 Answer 1

2

You can use broadcasting to apply your formula:

import numpy as np

image = np.random.random(100).reshape((10, 10))  # fake a 10x10 raster

inverted = (np.max(image) - np.min(image)) - image
1
  • 1
    Thanks! This worked like a charm. It threw me off at first because the numbers were off a little but then I realized my source data is in float point e.g. 0.0001 which was causing a slight mismatch.
    – user81784
    Commented Sep 3, 2021 at 14:46

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.