Note
Go to the end to download the full example code.
Python arithmetic#
This example demonstrates arithmetic operations using raster arithmetic on xarray.DataArray objects with the rst accessor. See Python operators for more details.
We open a raster
import geoutils as gu
filename_rast = gu.examples.get_path("everest_landsat_b4")
rast = gu.open_raster(filename_rast)
rast
rast.rst.plot(cmap="Greys_r")

Performing arithmetic operations implicitly loads the data.
rast = (rast + 1.0) ** 0.5 / 5
rast.rst.plot(cmap="Greys_r")

Important
Arithmetic operations cast to new dtypes automatically following Xarray and NumPy coercion rules.
Logical comparison operations will naturally return a boolean xarray.DataArray.
Boolean xarray.DataArray objects support Python logical operators to be combined together.
mask = (rast >= 3) | (rast % 2 == 0) & (rast != 80)
mask.rst.plot()

Finally, boolean rasters can be used for selecting values from a raster.
values = rast.where(mask)
Total running time of the script: (0 minutes 1.917 seconds)