Using tool functions

Most of WbW's tools exist as functions of the WbEnvironment class.

from whitebox_workflows import WbEnvironment
import math

wbe = WbEnvironment()
wbe.verbose = True
wbe.max_procs = -1

# Let's begin by downloading the Whitebox Workflows 'Guelph_landsat' sample data
wbe.working_directory = wbw.download_sample_data('Guelph_landsat')
print(f'Data have been stored in: {wbe.working_directory}')

# Read some of the image bands into memory
band2, band3, band4, band5 = wbe.read_rasters('band2.tif', 'band3.tif', 'band4.tif', 'band5.tif')

# Now let's call the create_colour_composite tool
true_colour_composite = wbe.create_colour_composite(
    red=band4, 
    green=band3, 
    blue=band2,
    enhance=False,
    treat_zeros_as_nodata=False
)
# The result 'true_colour_composite' is an in-memory raster. If we want to
# save it to disc to visualize it, we need to call 'write_raster'.
# wbe.write_raster(true_colour_composite, 'true_cc.tif', compress=True) # Uncomment this line to save to file

# Notice, that we don't need to specify the argument names of positional
# arguments (red, green, and blue above), and that we don't need to specify
# values for optional arguments if we accept their default values.
false_colour_composite = wbe.create_colour_composite(band5, band4, band3, enhance=False)
# wbe.write_raster(false_colour_composite, 'false_cc.tif', compress=True) # Uncomment this line to save to file

# If we don't want to see all of the progress updates output to stdout, 
# turn verbose mode off. But we also won't see errors or warnings.
wbe.verbose = False

# Now let's perform some image enhancements...
bce = wbe.balance_contrast_enhancement(true_colour_composite)

dds = wbe.direct_decorrelation_stretch(bce, achromatic_factor=0.3)
wbe.write_raster(dds, 'final_true_cc.tif', True)

# We can overwrite objects as well.
bce = wbe.balance_contrast_enhancement(false_colour_composite)

dds = wbe.direct_decorrelation_stretch(bce, achromatic_factor=0.3)
wbe.write_raster(dds, 'final_false_cc.tif', True)

There are also a large number of functions associated with the Raster class for manipulating raster data sets. Each of these Raster functions allow for Python-based raster algebra operations. For example:

# Calculate the normalized difference vegetation index...
ndvi = (band5 - band4) / (band5 + band4)
wbe.write_raster(ndvi, 'ndvi.tif', compress=True)

# use a multiplier and offset to adjust the values of a raster
multiplier = 0.012152
offet = -60.76071
sun_elev = math.radians(64.31337609)
reflectance = (band1 * multiplier + offset) * math.sin(sun_elev)

max_val = band1.max(band2)