opengeos / whitebox-python

WhiteboxTools Python Frontend

Home Page:https://pypi.org/project/whitebox/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Whitebox resetting working directory to whitebox directory in Anaconda environment

arojas314 opened this issue · comments

  • whitebox version: 2.0.3
  • Python version: 3.9
  • Operating System: RedHat Enterprise

Description

When using any whitebox functions for python, the current working directory is reset to the whitebox directory in my anaconda environment (e.g. /home/user_name/minconda3/envs/osgeo-env-v1/lib/python3.8/site-packages/whitebox). This causes many problems, since I am using whitebox functions across multiple scripts for my automated workflow.

To clarify, after importing whitebox, the current working directory is set to my project directory where my python file is calling whitebox, which is what I want. But after using a function it is reset to the whitebox directory, so the functions do not end up running.

I am attempting this in jupyter notebook.

What I Did

from whitebox import whitebox_tools
wbt = whitebox_tools.WhiteboxTools()
CWD = "/home/user_name/project/data"
wbt.set_working_dir(CWD)
wbt.clip_lidar_to_polygon("./filename.las", polygons="./poly_shapefile.shp", output="./lidarClip.las")

Below is the error messages I receive.

image

Thank you for your time in reading this.

I just tested it, and everything seems to work fine. Your CWD and --wd in the output are different directories. They are supposed to be the same. Not sure why they are different on your computer.

image

Ok, that makes sense. Thanks for the quick response!

Just to follow up:
The working directory is my project directory, and then I use a relative path to the data folder where all the data is. Is that not correct? I am working with a lot of data in separate directories, should I explicitly set the wbt.set_working_dir() each time I run a whitebox function?

You can either provide absolute paths to input files or use wbt.set_working_dir("/absolute/path/to/wd"), and then you can use relative paths

I noticed that every time a tool is executed, the working directory is changed to the wbt exe path. Therefore, you can no longer use os.getcwd() to set the working directory. This is the behavior of whitebox_tools.py. It needs to be fixed by @jblindsay. os.chdir(self.exe_path) is used 10 times in the script. One potential solution is to reset the working directory at the end of a wbt function.
https://github.com/jblindsay/whitebox-tools/blob/master/whitebox_tools.py#L171

cwd = os.getcwd()
os.chdir(self.exe_path)

# at the end
os.chdir(cwd)

This resolved my issue completely. However, I still cannot use relative paths after I use wbt.set_working_dir("/absolute/path/to/wd"). I was able to work around this, but here is a quick example of when using a relative path after setting the working directory fails:

cwd = os.getcwd()
wbt.set_working_dir(cwd)

wbt.clip_lidar_to_polygon(
   i="./data/lidarFile.las",
   polygons="./airsheds/TEAK_airshed.shp"
   output="./data/lidarClipped.las"
)

Thanks!