Reading and writing data

WbW supports reading and writing many types of raster, vector, and LiDAR data formats.

import glob
from whitebox_workflows import WbEnvironment

wbe = WbEnvironment()

wbe.working_directory = '/path/containing/data/files'

# Reading raster data
raster = wbe.read_raster('file_name.tif')

# Or 'read_rasters' (with an 's'!) for reading multiple rasters at once...
raster1, raster2 = wbe.read_rasters('file_name1.tif', 'file_name2.tif')

# Using 'read_rasters', 'read_lidars', and 'read_vectors' is preferred to running
# the single-file read operations multiple times because they are parallelized (as
# of v1.2.9) and will result in significantly faster read-times on multi-core systems.

# You can also read directly into a list.
rasters = wbe.read_rasters('file_name1.tif', 'file_name2.tif')
print(rasters[0].file_name)

# Similarly, you can input a file name list to 'read_rasters' using a splat (*). This
# is ideal when you don't know how many files you will be reading until the script
# is run, e.g. if you are reading all of the files of a certain type from a directory.
path = rf'{wbe.working_directory}/*.tif'
file_list = glob.glob(path)
rasters = wbe.read_rasters(*file_list)

# Writing raster data
wbe.write_raster(raster, 'output.tif')

# You can also ask WbW to compress the written raster.
wbe.write_raster(raster, 'output.tif', compress=True) # Only GeoTIFF compression is supported.

# Reading vector data
vector = wbe.read_vector('file_name.shp')

vector1, vector2 = wbe.read_vectors('file_name1.shp', 'file_name2.shp')

vector_list = ['file_name1.shp', 'file_name2.shp']
vectors = = wbe.read_vectors(*vector_list)

# Writing vector data
wbe.write_vector(vector, 'output.shp')

# Reading LiDAR data
lidar = wbe.read_lidar('file_name.laz')

lidar1, lidar2 = wbe.read_lidars('file_name1.laz', 'file_name2.laz')

lidar_list = ['file_name1.laz', 'file_name2.laz']
lidars = wbe.read_lidars(*lidar_list)

# Writing LiDAR data
wbe.write_lidar(lidar, 'output.laz')

Supported Data Formats

Raster formats

WbW can currently support reading/writing raster data in several common formats.

FormatExtensionReadWrite
GeoTIFF*.tif, *.tiffXX
Big GeoTIFF*.tif, *.tiffXX
Esri ASCII*.txt, *.ascXX
Esri BIL*.bil, *.hdrXX
Esri Binary*.flt and *.hdrXX
GRASS ASCII*.txt, *.ascXX
Idrisi*.rdc and *.rstXX
SAGA Binary*.sdat and *.sgrdXX
Surfer ASCII*.grdXX
Surfer Binary*.grdXX
Whitebox*.tas and *.depXX

Throughout this manual code examples that manipulate raster files all use the GeoTIFF format (.tif) but any of the supported file extensions can be used in its place.

WbW is able to read GeoTIFFs compressed using the PackBits, DEFLATE, and LZW methods. Compressed GeoTIFFs, created using the DEFLATE algorithm, can also be output from any tool that generates raster output files by using compress=True.

Vector Formats

At present, there is limited support in WbW for working with vector geospatial data formats. The only supported vector format is the ESRI Shapefile. Shapefiles geometries (.shp) and attributes (.dbf) can be read and written.

While the Shapefile format is extremely common, it does have certain limitations for vector representation. For example, owing to their 32-bit indexing, Shapefiles are limited in the number of geometries that can be stored in these files. Furthermore, Shapefiles are incapable of storing geometries of more than one type (point, lines, polygons) within the same file. As such, the vector-related tools in WhiteboxTools also carry these same limitations imposed by the Shapefile format.

Point Cloud (LiDAR) Formats

LiDAR data can be read/written in the common LAS and compressed LAZ data formats.