Setting up the Whitebox Environment

We saw in an earlier section that the WbEnvironment class is used to set up the Whitebox geoprocessing environment. This important initialization step should be one of the first things that we do in most WbW scripts.

from whitebox_workflows import WbEnvironment

wbe = WbEnvironment() # Create a WbEnvironment object, here named wbe.
# We can use this object to provide settings to tools that are run later in the script.
# If you aren't on the computer that you registered WbW on, you may use your
# floating-license user ID as a parmeter, e.g. wbe = wbw.WbEnvironment('cute-flying-pig').
# This ID will have been emailed to you upon registration.

# `max_procs` determines the number of processors used by functions that are parallelized. 
# If set to -1, the default, all available processors will be used.
wbe.max_procs = -1

# To limit tools to a certain number of processors, set `max_procs` to a positive whole 
# number less than the number of system processors. This can be important in cloud-based
# and distributed computing environments where you may not want WbW to fully utilize all
# available processors.
wbe.max_procs = 4

# `verbose` determines whether tool output sto stdout (`wbe.verbose=True`), or if
# output is suppressed (`wbe.verbose=False`). Tools are often very chatty, outputting
# frequent updates of progress. When you're running long workflows, it can be useful to
# turn this stream of tool output on and off during critical parts of the workflow.
wbe.verbose = True
# Run something critical...
wbe.verbose = False
# Run something less critical...

# Of course, when verbose=False, you will not be able to see any warnings or errors 
# issued by the tool, so this may not be desirable while testing a script.

# You can set and get the working directory as follows:
wbe.working_directory = '/path/to/my/data'


# How much time is remaining on your license?
print(wbw.license_info()) # Also takes the optional floating-license user ID.

# The working directory is the current path used to find data resources. When reading and 
# writing data, the working directory is the default location in which the data are read 
# from and written to.

my_raster = wbe.read_raster('image1.tif') # Will search for file "/path/to/my/data/image1.tif"

# You can also use full path names when specifying data.
my_raster = wbe.read_raster('/other/path/to/my/data/image2.tif')

# You can update the working directory multiple times in a script.
wbe.working_directory = '/other/path/to/my/data'
my_vector = wbe.read_vector('vector1.shp') # Will search for file "/other/path/to/my/data/vector1.shp"