MiKatt / openSTARS

open source implementation of the STARS ArcGIS toolbox

Home Page:https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0239237

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with correct_compl_confluences()

hydropatch opened this issue · comments

Hi there,
I run into an error when running correct_compl_confluences.

Here's my console output:
Fixing 24 complex confluences with 3 upstream segments each
WARNING: Vector map <complex_flows> already exists and will be overwritten
Original stream topology file moved to 'streams_v_o3'.
Breaking lines and moving vertices ...
Updating topology ...
Error in if (dt.move_streams[jj, cut_stream_prev] != i) { :
argument is of length zero

I have gone into the function and run things line by line. The error occurs on line 455: if(dt.move_streams[jj, cut_stream_prev] != i){

At this point dt.move_streams is empty. It is empty because cat_small is empty and cat small results in df.move_streams becoming empty on line 408: df.move_streams <- merge(df.move_streams, dt.smallcut[, .(stream, cat_small, str_new_small)], by.x = "cut_stream", by.y = "stream")

Let me know if I can provide some data for you to work with to replicate the problem. Hoping you can help me sort this out!
Thanks
Patrick

Dear Patrick,
sorry for the late reply.
I cannot reproduce this error. Is there a way to provide me with your files and code so I can try?
All the best,
Mira

Hi Mira,

Thank you so much for looking into this.
Now it's my turn to apologize for the late reply!
I've put together a zip file with the data and some code that throws the error. It's too large to upload here so I will get in touch over email.

Thanks again for looking into this for me,
Patrick

Hi Patrick,
I have tested your code on both Windows 10 and Linux and it runs without error. I will have a closer look at the GRASS files you have send me.
However, if the code up to correct_compl_confluences runs smoothly at your machine, I currently do not have any idea what could go wrong. I will come back to you later.
Best, Mira

Hi Mira and Pat,

I have the same error issue. Any help would be much appreciated at this point.

Any time check_compl_confluence returns complex confluences exist and i run

if (cp) correct_compl_confluences() to correct streams it throws same error, however when i increase the threshold to 3000 no complex cons are found and it works.

Thanks! files are attached dem and sites

demshed.tif.aux.zip

sites_obs.zip

Hello,
I will try to have a look during this week.
Could you please also send me the settings for deriving the streams? Or, even better, the script you use? What GRASS, R and GDAL version are you using? What OS?
Thanks, Mira

Thanks Mikatt! Really excited to publish this work. Great tool!

r version = 4.1.2
windows 10
grass = 8.2
GDAL 201 maybe?

crs = +proj=utm +zone=10 +datum=NAD83 +units=m +no_defs

Here is code:

# must be downloaded
devtools::install_github("MiKatt/openSTARS", ref = "dev")
library("openSTARS")
# give paths to GRASS and where to store the GRASS data base
# Windows e.g.
# grass_program_path <- "c:/Program Files/GRASS GIS 7.6"
grass_program_path <- "C:/Program Files/GRASS GIS 8.2"

working_dir <- file.path(tempdir(), "taku_workflow")
grass_db_path <- file.path(working_dir, "grassDB")
dir.create(working_dir)
setwd(tempdir())

# OPEN GRASS and set up session with grass_db_path
grass_db_path
# grass_db_path -> updata temp file name in grass session
# copy paste  C:\Users\KGB\AppData\Local\Temp\Rtmp86ddbL\taku_workflow

# specify the path to the digital elevation model
dem_path <- system.file("extdata", "taku_riv", "demshed.tif", package = "openSTARS")


setup_grass_environment(dem = dem_path, 
                        gisBase = grass_program_path,
                        gisDbase = grass_db_path,
                        location = "tak_location1",
                        remove_GISRC = TRUE,
                        override = TRUE
)
# the observation sites taku watersamples
sites_path <- system.file("extdata", "taku_riv", "water_obs.shp", package = "openSTARS")
import_data(dem = dem_path, sites = sites_path)
library(sp)
dem <- read_RAST("dem", ignore.stderr = TRUE)
sites <- read_VECT("sites_o", ignore.stderr = TRUE)
#derive_streams() #runs with default arguments
derive_streams(accum_threshold = 3000, condition = T, clean = TRUE, burn = 10)
#check complex confluences
cp <- check_compl_confluences() 

if (cp) correct_compl_confluences()

dem <- readRAST('dem', ignore.stderr = TRUE)

streams <- read_VECT('streams_v', ignore.stderr = TRUE)
streams_orig <- read_VECT('streams_v_o3', ignore.stderr = TRUE)
calc_edges()
edges <- readVECT("edges", ignore.stderr = TRUE)

Hi,
this is so strange. I cannot reproduce the error (Linux, Grass 7.8.6).
I will try on my Windows machine with Grass 8. Sorry, this will need some more time.

Hi, sorry again for the late reply.

I tried on Windows 10, but using GRASS 7.6 and that worked (it also produces 2 complex junctions with threshold 3000).
My only guess is that there is an issue with newer GDAL and/or GRASS versions. I need to update openSTARS for the new 'rgrass' package and all the new developments going on with R spatial but this will take a while.
Hence, my only quick and dirty suggestion right now is that you try an older grass version.

Best, Mira

So.... after playing with different versions of GRASS, OSGEO, I ended up going back to 8.2 out of pure frustration. Yet, I believe i found a work around. What fixed this issue is I made sure my watershed delineated dem was not incorporating other stream network flowing out of the primary network. This took some dem cleaning (fill), plus generating stream networks in GISpro with lesser flow thresholds eg 500 vs 1500. Then when using this clean dem and accompanying stream network (for burning) GRASS/openSTARS is able to generate finer scale stream networks via derive_streams() without generating complex confluences. THIS IS A WORK AROUND! If I happen to generate complex confluence and try to correct them, the same code is thrown.

WARNING: Vector map <complex_flows> already exists and will be overwritten
Original stream topology file moved to 'streams_v_o3'.
Breaking lines and moving vertices ...
Updating topology ...
Error in if (dt.move_streams[jj, cut_stream_prev] != i) { :
argument is of length zero

Thought this might help someone dealing with the same issue.