Hello Sam,
Your script looks good but there is one major problem with it. You are attempting to input a rasterio (GDAL) in-memory raster and are expecting the wbt.resample function to return one, but this isn’t the case. The WhiteboxTools Python API is really just a thin wrapper around the WhiteboxTools command line application, which takes command line arguments as inputs and outputs. This means that all inputs and outputs must be specified as file path & name strings. Here is an example of how you would write this script using WhiteboxTools:
import whitebox
wbt = whitebox.WhiteboxTools()
wbt.set_working_dir("/path/to/data/") # Sets the working directory
wbt.resample(
inputs="my_raster.tif",
output="my_resampled_raster.tif",
cell_size=10,
method="cc"
)
print("Done!")
The description of the Resample tool contains a description of the Python API for the tool here:
https://www.whiteboxgeo.com/manual/wbt_book/available_tools/image_processing_tools.html#Resample
There is also a very important document outlining how to use Whitebox with Python in the user manual here:
https://www.whiteboxgeo.com/manual/wbt_book/python_scripting/scripting.html
If you do in fact need to deal with in-memory rasters, so that you can read the properties of the raster, specify in-memory rasters as inputs to tools, and have those tools output in-memory rasters, then what you really need is Whitebox Workflows for Python (WbW) instead of WhiteboxTools.
https://www.whiteboxgeo.com/whitebox-workflows-for-python/
Unlike WbT, WbW is not open-source and costs $10 / user. Generally, because WbW is a native Python extension library, the equivalent WbW scripts are much more natural than the equivalent WbT script. For example, the equivalent resample WbW script would look like this:
import whitebox_workflows
wbe = whitebox_workflows.WbEnvironment('Geomorphometry2023')
wbe.set_working_dir("/path/to/data/") # Sets the working directory
raster = wbe.read_raster('my_raster.tif') # create an in-memory Whitebox raster object
# Get some information about the raster
print(f'Rows: {raster.configs.rows}')
print(f'Columns: {raster.configs.columns}')
print(f'Resolution (x direction): {raster.configs.resolution_x}')
print(f'Resolution (y direction): {raster.configs.resolution_y}')
print(f'North: {raster.configs.north}')
print(f'South: {raster.configs.south}')
print(f'East: {raster.configs.east}')
print(f'West: {raster.configs.west}')
print(f'Min value: {raster.configs.minimum}')
print(f'Max value: {raster.configs.maximum}')
print(f'EPSG code: {raster.configs.epsg_code}') # 0 if not set
print(f'Nodata value: {raster.configs.nodata}')
# Run the resample function
resampled = wbe.resample(
inputs=[raster],
cell_size=10,
method="cc"
)
# Save the file to disc
wbe.write_raster(resampled, "resampled_raster.tif", compress=True)
print("Done!")
WbW offers the ability to read in rasters (as well as vectors and lidar data) and to retrieve raster properties and even to manipulate individual grid cells. Take a look at the ‘Working with raster data’ section of the WbW user manual for more information:
https://www.whiteboxgeo.com/manual/wbw-user-manual/book/working-with-raster-data.html
I hope that heps.
Regards,
John
--
You received this message because you are subscribed to the Google Groups "WhiteboxTools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
whiteboxtool...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/whiteboxtools/35ed8128-b948-48eb-902c-4afbbd135f68n%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi Sam,
I see now in re-reading your email, that in fact you do have a license for WbW already. Okay, with that info in mind, then I would say that the issue is that you are using the WbT library (‘import whitebox’) rather than the Whitebox Workflows library (‘import whitebox_workflows’). And, as I said in the previous response, the two libraries result in very different types of scripts. You will need to use something that looks more like the second script in my response. Also, you don’t need to use rasterio to read in rasters and get their properties in your script, you can do it all from Whitebox Workflows instead. That’s one of the joys of using WbW. Again, for a reference and guidance on how to write WbW scripts, I’d really suggest looking over the WbW user manual, which I find quite useful. Best of luck.
Cheers,
John
From:
whiteb...@googlegroups.com <whiteb...@googlegroups.com> on behalf of Sam McClatchie <smccl...@fishocean.info>
Date: Friday, July 7, 2023 at 1:53 AM
To: WhiteboxTools <whiteb...@googlegroups.com>
Subject: resampling a geotiff to smaller cell size
--