opengeos / whiteboxR

WhiteboxTools R Frontend
https://whiteboxR.gishub.org
Other
168 stars 30 forks source link

I can't generate watershed stream order by the whitebox package in R #111

Closed Heitorlc closed 1 year ago

Heitorlc commented 1 year ago

Dear I'm having trouble generating the drain order by the whitebox package in R. Could you help me? When I run the wbt_strahler_stream_order function, the result is not limited to the drainage networks but is generated for the entire watershed with orders much higher than what exists.

I performed the following tests:

  1. Modified the input DEM;
  2. I did the procedure in ArcGIS and QGIS and it worked. However, I need to generate in R.
  3. I did all the procedures in ArcGIS and QGIS. And when I got to the drainage order part, I ran it in R with the wbt_strahler_stream_order function and it went wrong.
  4. I installed other versions of whitebox, not all possible ones. Still, it went wrong.
  5. I did the procedure with the whitebox package in Python, but it still went wrong. I'm not as knowledgeable in Python as I am in R.
  6. I tried the RGRASS package and other packages in R, but I was unsuccessful.

I'll leave the data below with the script, if you could help me I would be very grateful.

Data_Stream_Order.zip

brownag commented 1 year ago

Looking at your code it appears that the setwd() call and the paths you are using in wbt_* functions could be inconsistent. Your code and data work fine for me with some updates to paths to work for where i downloaded your data to. I didn't need to do anything special, I actually just removed the specific parts of your code/paths.

This works without modifying the R or Whitebox working directory. Assuming I am Knitting from the Rmd document directory and all files from your Data_Stream_Order.zip can be found in same folder. Check knit settings dropdown menu: in projects this defaults to project directory; or you can set up a project with Rmd and inputs in the project dir.

---
title: "Stream order"
output: html_document
date: "2023-06-14"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# Installing and loading packages
library(whitebox)

# assume the .Rmd and input files are in same directory
# 1. wbt_fill_depressions
wbt_fill_depressions(dem = "./dem_watershed.tif", output = "./dem_fill.tif")
# 2. wbt_d_inf_flow_accumulation

wbt_d_inf_flow_accumulation(input = "./dem_fill.tif", output = "./flow_acc.tif", out_type = "cells")
# 3. wbt_d8_pointer

wbt_d8_pointer(dem = "./dem_fill.tif", output = "./flow_dir.tif", esri_pntr = T)
# 4. wbt_extract_streams

wbt_extract_streams(flow_accum = "./flow_acc.tif", output = "./stream.tif", threshold = 5000)
# 4. wbt_extract_streams
wbt_strahler_stream_order(d8_pntr = "./flow_dir.tif", streams = "./stream.tif", output = "./stream_order.tif", esri_pntr = T)


Alternately you can use `setwd(...)` and `wbt_wd(getwd())` to point your R working directory wherever you want, and have whitebox use that same directory. 
brownag commented 1 year ago

Also, I did notice that you are possibly installing Whitebox from R-Forge? Please use CRAN or GitHub version, the R-forge repository is no longer maintained. Though I think ultimately your code should work fine on WBT 1.4.x...

install.packages("whitebox") #CRAN
if (!require("remotes")) install.packages('remotes')
remotes::install_github("opengeos/whiteboxR", build = FALSE) # github
brownag commented 1 year ago

Also, I am realizing that you might take issue with the output generated by Whitebox, however I don't think I fully understand what output you are expecting. I can confirm the tool runs with your data, as expected, from R. If the values are not what you expect, or do not correspond to the region you expect, you probably need to use different input data or some other pre-treatment of the data. If you think there is a problem with the strahler_stream_order tool itself, the place to create an issue for that topic is here: https://github.com/jblindsay/whitebox-tools/issues

Heitorlc commented 1 year ago

Hi Brown, Thank you for getting back to me.

I followed the steps you recommended, but I haven't achieved the desired outcome yet. The image below illustrates the stream order I was expecting. However, it still encompasses the entire watershed area.

image

I will contact the person you provided to explore further insights.