jblindsay / whitebox-tools

An advanced geospatial data analysis platform
https://www.whiteboxgeo.com/
MIT License
942 stars 161 forks source link

Error in `wbt_reclass_from_file()` with whitebox 2.3.0 #345

Closed chrisschuerz closed 1 year ago

chrisschuerz commented 1 year ago

I've been using wbt_reclass_from_file() in R (whitebox R frontend v2.2.0) with whitebox tools 2.2.0 without any issues. After the update to version 2.3.0 the following error message is returned:

Reading data...
thread 'main' panicked at 'The filename, directory name, or volume label syntax is incorrect. (os error 123)', whitebox-tools-app\src\main.rs:72:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I tested the routine for different raster layers and reclass files which ran without issues before and all end up in the same error message.

I tried to install a previous version of the whitebox R package from the package's CRAN archive. It seems that the frontend version and the actual whitebox versions are not linked, but whitebox::install_whitebox() will install the most recent version of whitebox. Is there a way to get an older version of whitebox without having to compile it?

jblindsay commented 1 year ago

The source code for this tool has not been changed in any way since July 2022, and certainly not in the time frame from v2.3.0. I suspect therefore that this issue lies with the R frontend. Are you able to please share a sample dataset for which you experienced this issue? I have tested the tool using the Whitebox Runner frontend and it works without issue.

brownag commented 1 year ago

@chrisschuerz Can you please show the output of the command that is failing with command_only=TRUE e.g. wbt_reclass_from_file(..., command_only=TRUE)?

The error is being emitted by WBT itself, not the R frontend, but it is possible there is an issue with paths,

That said, if you can install whitebox 2.2.0 from CRAN, and still have the errors, it is not likely to be something to do with the tool code in the frontend itself, but probably your system configuration. See this recent issue with similar issues related to file paths, which can be fixed by specifying a whitebox working directory in the settings.json file of the new WBT installation: https://github.com/opengeos/whiteboxR/issues/108

chrisschuerz commented 1 year ago

The command print is as follows for whitebox version 2.3.0:

[1] "\"C:/Users/schuerz/AppData/Local/R/win-library/4.2/whitebox/wbt_2_3_0/whitebox_tools.exe\" --run=ReclassFromFile  --input=\"./data/iso_cmt_500.tif\" --reclass_file=\"./data/acc_reclass.txt\" --output=\"./data/iso_cmt_acc.tif\" -v"

We compiled a whitebox_tools.exe version 2.2.0 which does not show the issue. If I update whitebox tools to version 2.3.0 (e.g. with whitebox::wbt_install() rerunning the same code returns the error above.

I prepared a small demo R project which causes the error, maybe this helps for reproducability. The script included in the project gives the following outputs when running it:

# Load whitebox
library(whitebox)

# Find all available whitebox tools executable files
wbt_exe <- list.files(path = find.package("whitebox"), 
                      pattern = 'whitebox_tools.exe', 
                      recursive = TRUE, full.names = TRUE)

# Two versions are available on my computer in two separate folders
wbt_exe
#> [1] "C:/Users/schuerz/AppData/Local/R/win-library/4.2/whitebox/wbt_2_2_0/whitebox_tools.exe"
#> [2] "C:/Users/schuerz/AppData/Local/R/win-library/4.2/whitebox/wbt_2_3_0/whitebox_tools.exe"

# Running reclass with whitbox 2.2.0
wbt_init(exe_path = wbt_exe[1])

wbt_version()
#> WhiteboxTools v2.2.0 by Dr. John B. Lindsay (c) 2017-2023
#> 
#> WhiteboxTools is an advanced geospatial data analysis platform developed at
#> the University of Guelph's Geomorphometry and Hydrogeomatics Research 
#> Group (GHRG). See www.whiteboxgeo.com for more details.

wbt_reclass_from_file(input        = './data/iso_cmt_500.tif',
                      reclass_file = './data/acc_reclass.txt',
                      output       = './data/iso_cmt_acc.tif')
#> reclass_from_file - Elapsed Time (excluding I/O): 0.57s

# Running reclass with whitebox version 2.3.0
wbt_init(exe_path = wbt_exe[2])
wbt_version()
#> WhiteboxTools v2.3.0 (c) Dr. John Lindsay 2017-2023
#> 
#> WhiteboxTools is an advanced geospatial data analysis platform developed at
#> the University of Guelph's Geomorphometry and Hydrogeomatics Research 
#> Group (GHRG). See www.whiteboxgeo.com for more details.

wbt_reclass_from_file(input        = './data/iso_cmt_500.tif',
                      reclass_file = './data/acc_reclass.txt',
                      output       = './data/iso_cmt_acc.tif')
#> Error running WhiteboxTools (ReclassFromFile)
#> whitebox.exe_path: "C:/Users/schuerz/AppData/Local/R/win-library/4.2/whitebox/wbt_2_3_0/whitebox_tools.exe"; File exists? TRUE
#> Arguments: --run=ReclassFromFile  --input="./data/iso_cmt_500.tif" --reclass_file="./data/acc_reclass.txt" --output="./data/iso_cmt_acc.tif" -v
#> 
#> System command had status 101
#> ******************************
#>   * Welcome to ReclassFromFile *
#>   * Powered by WhiteboxTools   *
#>   * www.whiteboxgeo.com        *
#>   ******************************
#>   Reading data...
#> thread 'main' panicked at 'The system cannot find the path specified. (os error 3)', whitebox-tools-app\src\main.rs:72:21
#> note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#> reclass_from_file - Elapsed Time: NA [did not run]
brownag commented 1 year ago

OK, thanks @chrisschuerz, I can reproduce this on a Windows machine I have with completely fresh install of 2.3.0 via wbt_install(). However I had no issues with Linux. Using your same relative paths preceded with "./"

> library(whitebox)
> wbt_install()
Performing one-time download of WhiteboxTools binary from
     https://www.whiteboxgeo.com/WBT_Windows/WhiteboxTools_win_amd64.zip 
(This could take a few minutes, please be patient...)
trying URL 'https://www.whiteboxgeo.com/WBT_Windows/WhiteboxTools_win_amd64.zip'
Content type 'application/zip' length 26587019 bytes (25.4 MB)
downloaded 25.4 MB

WhiteboxTools binary is located here:  C:/Users/Andrew/AppData/Roaming/R/data/R/whitebox/WBT/whitebox_tools.exe 
You can now start using whitebox
    library(whitebox)
    wbt_version()
> wbt_version()
WhiteboxTools v2.3.0 (c) Dr. John Lindsay 2017-2023

WhiteboxTools is an advanced geospatial data analysis platform developed at
the University of Guelph's Geomorphometry and Hydrogeomatics Research 
Group (GHRG). See www.whiteboxgeo.com for more details.
> wbt_reclass_from_file(input        = './data/iso_cmt_500.tif',
+                       reclass_file = './data/acc_reclass.txt',
+                       output       = './data/iso_cmt_acc.tif')

Error running WhiteboxTools (ReclassFromFile)
  whitebox.exe_path: "C:\Users\Andrew\AppData\Roaming/R/data/R/whitebox/WBT/whitebox_tools.exe"; File exists? TRUE
  Arguments: --run=ReclassFromFile  --input="./data/iso_cmt_500.tif" --reclass_file="./data/acc_reclass.txt" --output="./data/iso_cmt_acc.tif" -v

System command had status 101
******************************
* Welcome to ReclassFromFile *
* Powered by WhiteboxTools   *
* www.whiteboxgeo.com        *
******************************
Reading data...
thread 'main' panicked at 'The system cannot find the path specified. (os error 3)', whitebox-tools-app\src\main.rs:72:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
reclass_from_file - Elapsed Time: NA [did not run]

If we set the WhiteboxTools working directory to the current R working directory with wbt_wd("."), or setting wd via wbt_init() or similar, it works again. (This is essentially same "fix" from https://github.com/opengeos/whiteboxR/issues/108)

> wbt_wd(".")
> wbt_reclass_from_file(input        = './data/iso_cmt_500.tif',
+                       reclass_file = './data/acc_reclass.txt',
+                       output       = './data/iso_cmt_acc.tif')
reclass_from_file - Elapsed Time (excluding I/O): 0.64s

I would suggest that perhaps the settings.json for your 2.2.0 v.s. 2.3.0 install are different? The input/output paths are likely being interpreted relative to a directory that does not exist on your computer. In a fresh install of 2.3.0 the default settings.json (and working_directory) are set as:

{
  "verbose_mode": true,
  "working_directory": "/Users/johnlindsay/Downloads/",
  "compress_rasters": true,
  "max_procs": -1
}
brownag commented 1 year ago

@chrisschuerz setting "working_directory": "" in C:/Users/Andrew/AppData/Roaming/R/data/R/whitebox/WBT/settings.json (in my example) makes your code work as expected (without wbt_wd(".")) on 2.3.0

chrisschuerz commented 1 year ago

@brownag Thank you! Yes setting the "working_directory": "" solves the issue.