Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Older SAGA versions (< 8) don't work well with GeoTIFF #166

Closed
tylerbhampton opened this issue Jul 11, 2023 · 22 comments
Closed

Older SAGA versions (< 8) don't work well with GeoTIFF #166

tylerbhampton opened this issue Jul 11, 2023 · 22 comments
Labels

Comments

@tylerbhampton
Copy link

@ambarja @florisvdh

I'm referencing the example from the cheatsheets. I'm unable to get qgisprocess to produce any outputs from raster analysis. I have been successful in reproducing the buffer vector analysis in the README. First, there are some correction s from the cheatsheet, as the topographic wetness index is TWI not TPI. I'm unable to see a file written from the analysis.

I'm hoping to perform analysis with SAGA Fill Sinks, but this example from the cheatsheet and a SAGA function is a good place to start with my issue.

`> version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
crt ucrt
system x86_64, mingw32
status
major 4
minor 3.1
year 2023
month 06
day 16
svn rev 84548
language R
version.string R version 4.3.1 (2023-06-16 ucrt)
nickname Beagle Scouts

library(qgisprocess)
Attempting to load the cache ... Success!
QGIS version: 3.28.8-Firenze
Having access to 1164 algorithms from 7 QGIS processing providers.
Run qgis_configure(use_cached_data = TRUE) to reload cache and get more details.

Run qgis_enable_plugins() to enable 1 disabled plugin and access its algorithms: otbprovider
library(stars)
Loading required package: abind
Loading required package: sf
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
dem = read_stars(

  • system.file("raster","srtm.tif",
  •           package="spDataLarge")
    
  • )

qgis_show_help("saga:sagawetnessindex")
SAGA Wetness Index (saga:sagawetnessindex)


Description


Arguments

DEM: Elevation
Argument type: raster
Acceptable values:
- Path to a raster layer
WEIGHT: Weights (optional)
Argument type: raster
Acceptable values:
- Path to a raster layer
AREA: Catchment Area
Argument type: rasterDestination
Acceptable values:
- Path for new raster layer
SLOPE: Catchment Slope
Argument type: rasterDestination
Acceptable values:
- Path for new raster layer
AREA_MOD: Modified Catchment Area
Argument type: rasterDestination
Acceptable values:
- Path for new raster layer
TWI: Topographic Wetness Index
Argument type: rasterDestination
Acceptable values:
- Path for new raster layer
SUCTION: Suction
Default value: 10
Argument type: number
Acceptable values:
- A numeric value
AREA_TYPE: Type of Area
Default value: 1
Argument type: enum
Available values:
- 0: [0] total catchment area
- 1: [1] square root of catchment area
- 2: [2] specific catchment area
Acceptable values:
- Number of selected option, e.g. '1'
- Comma separated list of options, e.g. '1,3'
SLOPE_TYPE: Type of Slope
Default value: 1
Argument type: enum
Available values:
- 0: [0] local slope
- 1: [1] catchment slope
Acceptable values:
- Number of selected option, e.g. '1'
- Comma separated list of options, e.g. '1,3'
SLOPE_MIN: Minimum Slope
Default value: 0
Argument type: number
Acceptable values:
- A numeric value
SLOPE_OFF: Offset Slope
Default value: 0.1
Argument type: number
Acceptable values:
- A numeric value
SLOPE_WEIGHT: Slope Weighting
Default value: 1
Argument type: number
Acceptable values:
- A numeric value


Outputs

AREA:
Catchment Area
SLOPE:
Catchment Slope
AREA_MOD:
Modified Catchment Area
TWI:
Topographic Wetness Index

qgis_run_algorithm(

  • "saga:sagawetnessindex",
  • DEM = dem,
  • TWI = 'twi.sdat'
  • ) |>
  • qgis_extract_output(name="TWI") -> OUTPUT
    Argument WEIGHT is unspecified (using QGIS default value).
    Using AREA = qgis_tmp_raster()
    Using SLOPE = qgis_tmp_raster()
    Using AREA_MOD = qgis_tmp_raster()
    Argument SUCTION is unspecified (using QGIS default value).
    Using AREA_TYPE = "[0] total catchment area"
    Using SLOPE_TYPE = "[0] local slope"
    Argument SLOPE_MIN is unspecified (using QGIS default value).
    Argument SLOPE_OFF is unspecified (using QGIS default value).
    Argument SLOPE_WEIGHT is unspecified (using QGIS default value).

OUTPUT
[1] "twi.sdat"
attr(,"class")
[1] "qgis_outputRaster"
OUTPUT %>% st_as_stars() %>% plot()
trying to read file: C:\Users\Tyler\Documents\twi.sdat
Error: file not found`

@ambarja
Copy link
Contributor

ambarja commented Jul 11, 2023

@florisvdh I have the same problem with the latest version of qgisprocess.
Do you have any idea what the problem is?
By other hand, @tylerbhampton, please Could you install the maintenanse version?

remotes::install_github("r-spatial/qgisprocess@maintenance")
qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = dem,
  TWI = 'twi.sdat') |>
  qgis_extract_output(name="TWI") |> 
  st_as_stars() |> 
  rast() |> 
  plot(col = cptcity::cpt(pal = 'ocal_blue'))

image

@tylerbhampton
Copy link
Author

tylerbhampton commented Jul 11, 2023

I installed the maintenance version.
My problem is still "Error: file not found".
Note, I get an error message when I have the algorithm as "sagang". I changed it to "saga". I get this message.

qgis_show_help("sagang:sagawetnessindex")
Error in assert_qgis_algorithm():
! Can't find QGIS algorithm 'sagang:sagawetnessindex'.

library(qgisprocess)
library(sf)
library(raster)
library(stars)

#qgis_show_help("saga:sagawetnessindex")
qgis_run_algorithm(

  • algorithm = "saga:sagawetnessindex",
  • DEM = system.file("raster","srtm.tif",
  •                 package="spDataLarge"),
    
  • TWI = 'twi.sdat') |>
  • qgis_extract_output(name="TWI") -> OUTPUT
    Argument WEIGHT is unspecified (using QGIS default value).
    Using AREA = qgis_tmp_raster()
    Using SLOPE = qgis_tmp_raster()
    Using AREA_MOD = qgis_tmp_raster()
    Argument SUCTION is unspecified (using QGIS default value).
    Using AREA_TYPE = "[0] total catchment area"
    Using SLOPE_TYPE = "[0] local slope"
    Argument SLOPE_MIN is unspecified (using QGIS default value).
    Argument SLOPE_OFF is unspecified (using QGIS default value).
    Argument SLOPE_WEIGHT is unspecified (using QGIS default value).

OUTPUT
[1] "twi.sdat"
attr(,"class")
[1] "qgis_outputRaster"
st_as_stars(OUTPUT)
trying to read file: C:\Users\Tyler\Documents\twi.sdat
Error: file not found

@ambarja
Copy link
Contributor

ambarja commented Jul 11, 2023

ohhhh , @tylerbhampton the algoritm is algorithm = "sagang:sagawetnessindex" not algorithm = "saga:sagawetnessindex"
👀

@tylerbhampton
Copy link
Author

I get this message when I ask for help with "sagang:sagawetnessindex"

> qgis_show_help("sagang:sagawetnessindex") Error in assert_qgis_algorithm(): ! Can't find QGIS algorithm 'sagang:sagawetnessindex'. Run qgis_algorithms() for a list of available algorithms

@tylerbhampton
Copy link
Author

image

@ambarja
Copy link
Contributor

ambarja commented Jul 11, 2023

Please install the next plugin of qgis .
image

@tylerbhampton
Copy link
Author

I updated the Processing Saga NextGen provider in QGIS. As seen in code attached below: I am able to get the qgisprocess package to SUCCESSFULLY work for the vector analysis example of buffer NC, but still CANNOT for the raster example. I show that the RSAGA package can successfully perform the analysis. My error message is that the qgisprocess "qgis_run_algorithm" function never writes an output file.
Thank you for all the help so far!

library(qgisprocess)

## Attempting to load the cache ... Success!
## QGIS version: 3.28.8-Firenze
## Having access to 1673 algorithms from 8 QGIS processing providers.
## Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.
## >>> Run `qgis_enable_plugins()` to enable 1 disabled plugin and access
##     its algorithms: otbprovider

library(sf)

## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

library(raster)

## Loading required package: sp
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)

library(stars)

## Loading required package: abind

qgis_enable_plugins(names=c("processing_saga_nextgen"))

## Ignoring plugins that are enabled already: processing_saga_nextgen
## No QGIS plugins to be handled; exiting.

# SHOW THAT PACKAGE WORKS WITH VECTOR
ncvect <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))

result <- qgis_run_algorithm(
  "native:buffer",
  INPUT = ncvect,
  DISTANCE = 1,
  DISSOLVE = TRUE
)

## Argument `SEGMENTS` is unspecified (using QGIS default value).
## Using `END_CAP_STYLE = "Round"`
## Using `JOIN_STYLE = "Round"`
## Argument `MITER_LIMIT` is unspecified (using QGIS default value).
## Using `OUTPUT = qgis_tmp_vector()`

result

## <Result of `qgis_run_algorithm("native:buffer", ...)`>
## List of 1
##  $ OUTPUT: 'qgis_outputVector' chr "C:\\Users\\Tyler\\AppData\\Local\\Temp\\RtmpMLslSL\\file482c7f901a2a\\file482c8a19d.gpkg"

output_sf <- sf::st_as_sf(result)
plot(sf::st_geometry(output_sf))

# SHOW THAT PACKAGE WORKS WITH WRITING PATHS

result <- qgis_run_algorithm(
  "native:buffer",
  INPUT = system.file("shape/nc.shp", package = "sf"),
  OUTPUT = "nc_buffer.shp",
  DISTANCE = 1,
  DISSOLVE = TRUE
)

## Argument `SEGMENTS` is unspecified (using QGIS default value).
## Using `END_CAP_STYLE = "Round"`
## Using `JOIN_STYLE = "Round"`
## Argument `MITER_LIMIT` is unspecified (using QGIS default value).

result

## <Result of `qgis_run_algorithm("native:buffer", ...)`>
## List of 1
##  $ OUTPUT: 'qgis_outputVector' chr "nc_buffer.shp"

output_sf <- st_read(result$OUTPUT)

## Reading layer `nc_buffer' from data source 
##   `D:\rtesting\rqgis_trouble\nc_buffer.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 14 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -85.31306 ymin: 32.88374 xmax: -74.46764 ymax: 37.58832
## Geodetic CRS:  NAD27

plot(sf::st_geometry(output_sf))

unnamed-chunk-1-1

# SHOW THAT PACKAGE DOES NOT WORK WITH RASTER

dem = raster(system.file("raster","srtm.tif",
                  package="spDataLarge"))
plot(dem)

unnamed-chunk-1-2

qgis_show_help("sagang:sagawetnessindex")

## Saga wetness index (sagang:sagawetnessindex)
## 
## ----------------
## Description
## ----------------
## 
## ----------------
## Arguments
## ----------------
## 
## DEM: Elevation
##  Argument type:  raster
##  Acceptable values:
##      - Path to a raster layer
## WEIGHT: Weights (optional)
##  Argument type:  raster
##  Acceptable values:
##      - Path to a raster layer
## AREA: Catchment Area
##  Argument type:  rasterDestination
##  Acceptable values:
##      - Path for new raster layer
## SLOPE: Catchment Slope
##  Argument type:  rasterDestination
##  Acceptable values:
##      - Path for new raster layer
## AREA_MOD: Modified Catchment Area
##  Argument type:  rasterDestination
##  Acceptable values:
##      - Path for new raster layer
## TWI: Topographic Wetness Index
##  Argument type:  rasterDestination
##  Acceptable values:
##      - Path for new raster layer
## SUCTION: Suction
##  Default value:  10
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## AREA_TYPE: Type of Area
##  Default value:  1
##  Argument type:  enum
##  Available values:
##      - 0: [0] total catchment area
##      - 1: [1] square root of catchment area
##      - 2: [2] specific catchment area
##  Acceptable values:
##      - Number of selected option, e.g. '1'
##      - Comma separated list of options, e.g. '1,3'
## SLOPE_TYPE: Type of Slope
##  Default value:  1
##  Argument type:  enum
##  Available values:
##      - 0: [0] local slope
##      - 1: [1] catchment slope
##  Acceptable values:
##      - Number of selected option, e.g. '1'
##      - Comma separated list of options, e.g. '1,3'
## SLOPE_MIN: Minimum Slope
##  Default value:  0
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## SLOPE_OFF: Offset Slope
##  Default value:  0.1
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## SLOPE_WEIGHT: Slope Weighting
##  Default value:  1
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## 
## ----------------
## Outputs
## ----------------
## 
## AREA: <outputRaster>
##  Catchment Area
## SLOPE: <outputRaster>
##  Catchment Slope
## AREA_MOD: <outputRaster>
##  Modified Catchment Area
## TWI: <outputRaster>
##  Topographic Wetness Index

result = qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = system.file("raster","srtm.tif",
                    package="spDataLarge"),
  TWI = "twi.sdat")

## Argument `WEIGHT` is unspecified (using QGIS default value).
## Using `AREA = qgis_tmp_raster()`
## Using `SLOPE = qgis_tmp_raster()`
## Using `AREA_MOD = qgis_tmp_raster()`
## Argument `SUCTION` is unspecified (using QGIS default value).
## Using `AREA_TYPE = "[0] total catchment area"`
## Using `SLOPE_TYPE = "[0] local slope"`
## Argument `SLOPE_MIN` is unspecified (using QGIS default value).
## Argument `SLOPE_OFF` is unspecified (using QGIS default value).
## Argument `SLOPE_WEIGHT` is unspecified (using QGIS default value).

result

## <Result of `qgis_run_algorithm("sagang:sagawetnessindex", ...)`>
## List of 4
##  $ AREA    : 'qgis_outputRaster' chr "C:\\Users\\Tyler\\AppData\\Local\\Temp\\RtmpMLslSL\\file482c7f901a2a\\file482c75128e8.tif"
##  $ AREA_MOD: 'qgis_outputRaster' chr "C:\\Users\\Tyler\\AppData\\Local\\Temp\\RtmpMLslSL\\file482c7f901a2a\\file482c5b004bf0.tif"
##  $ SLOPE   : 'qgis_outputRaster' chr "C:\\Users\\Tyler\\AppData\\Local\\Temp\\RtmpMLslSL\\file482c7f901a2a\\file482c24515608.tif"
##  $ TWI     : 'qgis_outputRaster' chr "twi.sdat"

OUTPUT = qgis_extract_output(result,name="TWI")
OUTPUT

## [1] "twi.sdat"
## attr(,"class")
## [1] "qgis_outputRaster"

OUTPUT %in% list.files()

## [1] TRUE

st_as_stars(OUTPUT)

## stars object with 2 dimensions and 1 attribute
## attribute(s):
##                Min.   1st Qu.    Median     Mean   3rd Qu.    Max.   NA's
## twi.sdat  -19.56863 -10.89841 -9.662588 -9.76137 -8.686182 3.82698 202705
## dimension(s):
##   from  to  offset        delta refsys x/y
## x    1 465 -113.24  0.000833333 WGS 84 [x]
## y    1 457 37.5129 -0.000833333 WGS 84 [y]

# 
#   st_as_stars() |> 
#   rast() |> 
#   plot(col = cptcity::cpt(pal = 'ocal_blue'))

# SHOW THAT RSAGA CAN PRODUCE THE CORRECT RESULT

library(RSAGA)

## Loading required package: gstat
## Loading required package: shapefiles
## Loading required package: foreign
## 
## Attaching package: 'shapefiles'
## 
## The following objects are masked from 'package:foreign':
## 
##     read.dbf, write.dbf
## 
## Loading required package: plyr

RSAGA::rsaga.wetness.index(
  in.dem = system.file("raster","srtm.tif",
                    package="spDataLarge"),
  out.wetness.index = "twi.sdat"
)

## Search for SAGA command line program and modules... 
## Done

## Warning in rsaga.geoprocessor(lib = "ta_hydrology", module = "SAGA Wetness Index", : This RSAGA version has been tested with SAGA GIS versions between 2.3.1 and 8.4.1.
## You seem to be using SAGA GIS 8.5.1, which may cause problems due to
## possible changes in names and definitions of SAGA module arguments, etc.

## Warning in rsaga.geoprocessor(lib, module = NULL, env = env, intern = TRUE, : This RSAGA version has been tested with SAGA GIS versions between 2.3.1 and 8.4.1.
## You seem to be using SAGA GIS 8.5.1, which may cause problems due to
## possible changes in names and definitions of SAGA module arguments, etc.

## 
## 
## SAGA Version: 8.5.1 (64 bit)
## 
## library path: C:\Progra~1\SAGA\tools\
## library name: ta_hydrology
## library     : ta_hydrology
## tool        : SAGA Wetness Index
## identifier  : 15
## author      : J.Boehner, O.Conrad (c) 2001
## processors  : 12 [12]
## 
## loading: srtm
## translation: srtm
## [SAGA Wetness Index] Execution started...
## 
## [SAGA Wetness Index] Parameters:
## 
## Grid System: 0.000833; 465x 457y; -113.239167x 37.132500y
## Elevation: srtm
## Weights: <not set>
## Catchment Area: Catchment Area
## Catchment Slope: Catchment Slope
## Modified Catchment Area: Modified Catchment Area
## Topographic Wetness Index: Topographic Wetness Index
## Suction: 10
## Type of Area: specific catchment area
## Type of Slope: catchment slope
## Minimum Slope: 0
## Offset Slope: 0.1
## Slope Weighting: 1
## 
## Create index: srtm
## catchment area and slope...
## pass 1 (206 > 0)
## pass 2 (34 > 0)
## pass 3 (34 > 0)
## pass 4 (33 > 0)
## pass 5 (27 > 0)
## pass 6 (20 > 0)
## pass 7 (16 > 0)
## pass 8 (13 > 0)
## pass 9 (11 > 0)
## pass 10 (8 > 0)
## pass 11 (3 > 0)
## pass 12 (3 > 0)
## pass 13 (0 > 0)
## post-processing...
## topographic wetness index...
## 
## total execution time: 1000 milliseconds (01s)
## 
## [SAGA Wetness Index] Execution succeeded (01s)
## Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpMLslSL\file482c5158212e...
## Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpMLslSL\file482c26ff7340...
## Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpMLslSL\file482c7c5042dc...
## Saving grid: twi.sdat...

"twi.sdat" %in% list.files()

## [1] TRUE

OUTPUT = raster("twi.sdat")
plot(OUTPUT)

unnamed-chunk-1-3

@florisvdh
Copy link
Member

florisvdh commented Jul 12, 2023

Thanks for your interest @tylerbhampton. From your output, it appears that "twi.sdat" is created and also read correctly by stars.

Maybe you didn't load terra before using rast(); however you don't need to coerce to SpatRaster or RasterLayer anyway after reading with stars. Further, terra is the successor of raster and to be preferred above raster.

Can you reproduce below reprex, that does it with stars, terra and raster (to report problems, best use reprex::reprex(si = TRUE)):

Reprex
library(terra)
#> terra 1.7.37
library(qgisprocess)
#> Attempting to load the cache ...
#> Success!
#> QGIS version: 3.32.0-Lima
#> Having access to 1868 algorithms from 13 QGIS processing providers.
#> Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.
dem <- rast(system.file("raster/dem.tif", package = "spDataLarge"))
twi_output <- qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = dem,
  TWI = 'twi.sdat') |>
  qgis_extract_output(name="TWI")
#> Argument `WEIGHT` is unspecified (using QGIS default value).
#> Using `AREA = qgis_tmp_raster()`
#> Using `SLOPE = qgis_tmp_raster()`
#> Using `AREA_MOD = qgis_tmp_raster()`
#> Argument `SUCTION` is unspecified (using QGIS default value).
#> Using `AREA_TYPE = "[0] total catchment area"`
#> Using `SLOPE_TYPE = "[0] local slope"`
#> Argument `SLOPE_MIN` is unspecified (using QGIS default value).
#> Argument `SLOPE_OFF` is unspecified (using QGIS default value).
#> Argument `SLOPE_WEIGHT` is unspecified (using QGIS default value).
twi_output |> 
  qgis_as_terra() |> 
  plot(col = cptcity::cpt(pal = 'ocal_blue'))

twi_output |> 
  stars::st_as_stars() |> 
  plot(col = cptcity::cpt(pal = 'ocal_blue'))

twi_output |> 
  qgis_as_raster() |> 
  plot(col = cptcity::cpt(pal = 'ocal_blue'))
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, will retire in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
#> The sp package is now running under evolution status 2
#>      (status 2 uses the sf package in place of rgdal)

# also the (unneeded) roundtripping works:
twi_output |> 
  stars::st_as_stars() |> 
  rast() |> 
  plot(col = cptcity::cpt(pal = 'ocal_blue'))

Created on 2023-07-12 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16)
#>  os       Linux Mint 21.1
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language nl_BE:nl
#>  collate  nl_BE.UTF-8
#>  ctype    nl_BE.UTF-8
#>  tz       Europe/Brussels
#>  date     2023-07-12
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  abind         1.4-5      2016-07-21 [3] CRAN (R 4.2.0)
#>  assertthat    0.2.1      2019-03-21 [3] CRAN (R 4.0.1)
#>  class         7.3-22     2023-05-03 [3] RSPM (R 4.2.0)
#>  classInt      0.4-9      2023-02-28 [3] RSPM (R 4.2.0)
#>  cli           3.6.1      2023-03-23 [3] RSPM (R 4.2.0)
#>  codetools     0.2-19     2023-02-01 [3] RSPM (R 4.2.0)
#>  cptcity       1.0.6      2020-10-02 [3] RSPM (R 4.2.0)
#>  curl          5.0.1      2023-06-07 [3] RSPM (R 4.2.0)
#>  DBI           1.1.3      2022-06-18 [3] RSPM (R 4.2.0)
#>  digest        0.6.32     2023-06-26 [3] RSPM (R 4.2.0)
#>  dplyr         1.1.2      2023-04-20 [3] RSPM (R 4.2.0)
#>  e1071         1.7-13     2023-02-01 [3] RSPM (R 4.2.0)
#>  evaluate      0.21       2023-05-05 [3] RSPM (R 4.2.0)
#>  fansi         1.0.4      2023-01-22 [3] RSPM (R 4.2.0)
#>  fastmap       1.1.1      2023-02-24 [3] RSPM (R 4.2.0)
#>  fs            1.6.2      2023-04-25 [3] RSPM (R 4.2.0)
#>  generics      0.1.3      2022-07-05 [3] RSPM (R 4.2.0)
#>  glue          1.6.2      2022-02-24 [3] RSPM (R 4.2.0)
#>  highr         0.10       2022-12-22 [3] RSPM (R 4.2.0)
#>  htmltools     0.5.5      2023-03-23 [3] RSPM (R 4.2.0)
#>  jsonlite      1.8.7      2023-06-29 [3] RSPM (R 4.2.0)
#>  KernSmooth    2.23-21    2023-05-03 [3] RSPM (R 4.2.0)
#>  knitr         1.43       2023-05-25 [3] RSPM (R 4.2.0)
#>  lattice       0.21-8     2023-04-05 [3] RSPM (R 4.2.0)
#>  lifecycle     1.0.3      2022-10-07 [3] RSPM (R 4.2.0)
#>  lwgeom        0.2-13     2023-05-22 [3] RSPM (R 4.2.0)
#>  magrittr      2.0.3      2022-03-30 [3] RSPM (R 4.2.0)
#>  pillar        1.9.0      2023-03-22 [3] RSPM (R 4.2.0)
#>  pkgconfig     2.0.3      2019-09-22 [3] CRAN (R 4.0.1)
#>  processx      3.8.2      2023-06-30 [3] RSPM (R 4.2.0)
#>  proxy         0.4-27     2022-06-09 [3] RSPM (R 4.2.0)
#>  ps            1.7.5      2023-04-18 [3] RSPM (R 4.2.0)
#>  purrr         1.0.1      2023-01-10 [3] RSPM (R 4.2.0)
#>  qgisprocess * 0.0.0.9000 2023-07-12 [1] Github (r-spatial/qgisprocess@2f7ea70)
#>  R.cache       0.16.0     2022-07-21 [3] RSPM (R 4.2.0)
#>  R.methodsS3   1.8.2      2022-06-13 [3] RSPM (R 4.2.0)
#>  R.oo          1.25.0     2022-06-12 [3] RSPM (R 4.2.0)
#>  R.utils       2.12.2     2022-11-11 [3] RSPM (R 4.2.0)
#>  R6            2.5.1      2021-08-19 [3] RSPM (R 4.2.0)
#>  rappdirs      0.3.3      2021-01-31 [3] RSPM (R 4.2.0)
#>  raster        3.6-23     2023-07-04 [3] RSPM (R 4.2.0)
#>  Rcpp          1.0.10     2023-01-22 [3] RSPM (R 4.2.0)
#>  reprex        2.0.2      2022-08-17 [3] RSPM (R 4.2.0)
#>  rlang         1.1.1      2023-04-28 [3] RSPM (R 4.2.0)
#>  rmarkdown     2.23       2023-07-01 [3] RSPM (R 4.2.0)
#>  rstudioapi    0.14       2022-08-22 [3] RSPM (R 4.2.0)
#>  sessioninfo   1.2.2      2021-12-06 [3] RSPM (R 4.2.0)
#>  sf            1.0-13     2023-05-24 [1] RSPM (R 4.3.0)
#>  sp            2.0-0      2023-06-22 [3] RSPM (R 4.2.0)
#>  stars         0.6-1      2023-04-06 [3] RSPM (R 4.2.0)
#>  stringi       1.7.12     2023-01-11 [3] RSPM (R 4.2.0)
#>  stringr       1.5.0      2022-12-02 [3] RSPM (R 4.2.0)
#>  styler        1.10.1     2023-06-05 [3] RSPM (R 4.2.0)
#>  terra       * 1.7-37     2023-06-18 [1] RSPM (R 4.3.0)
#>  tibble        3.2.1      2023-03-20 [3] RSPM (R 4.2.0)
#>  tidyselect    1.2.0      2022-10-10 [3] RSPM (R 4.2.0)
#>  units         0.8-2      2023-04-27 [3] RSPM (R 4.2.0)
#>  utf8          1.2.3      2023-01-31 [3] RSPM (R 4.2.0)
#>  vctrs         0.6.3      2023-06-14 [3] RSPM (R 4.2.0)
#>  withr         2.5.0      2022-03-03 [3] RSPM (R 4.2.0)
#>  xfun          0.39       2023-04-20 [3] RSPM (R 4.2.0)
#>  xml2          1.3.4      2023-04-27 [3] RSPM (R 4.2.0)
#>  yaml          2.3.7      2023-01-23 [3] RSPM (R 4.2.0)
#> 
#>  [1] /home/floris/lib/R/library
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Thanks @ambarja for solving several things!

@tylerbhampton
Copy link
Author

I reinstalled spDataLarge to make sure we're using the same DEM raster.
I am finding that qgis_run_algorithm does NOT produce twi.sdat, and none of the stars/terra/raster functions can find the files.

library(terra)
#> terra 1.7.39
library(qgisprocess)
#> Attempting to load the cache ...
#> Success!
#> QGIS version: 3.28.8-Firenze
#> Having access to 1673 algorithms from 8 QGIS processing providers.
#> Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.
#> >>> Run `qgis_enable_plugins()` to enable 1 disabled plugin and access
#>     its algorithms: otbprovider

qgis_enable_plugins(names=c("processing_saga_nextgen"))
#> Ignoring plugins that are enabled already: processing_saga_nextgen
#> No QGIS plugins to be handled; exiting.


dem = rast(system.file("raster/dem.tif",
                       package="spDataLarge"))
plot(dem)

twi_output = qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = system.file("raster/dem.tif",
                    package="spDataLarge"),
  TWI = "twi.sdat") |> qgis_extract_output(name="TWI")
#> Argument `WEIGHT` is unspecified (using QGIS default value).
#> Using `AREA = qgis_tmp_raster()`
#> Using `SLOPE = qgis_tmp_raster()`
#> Using `AREA_MOD = qgis_tmp_raster()`
#> Argument `SUCTION` is unspecified (using QGIS default value).
#> Using `AREA_TYPE = "[0] total catchment area"`
#> Using `SLOPE_TYPE = "[0] local slope"`
#> Argument `SLOPE_MIN` is unspecified (using QGIS default value).
#> Argument `SLOPE_OFF` is unspecified (using QGIS default value).
#> Argument `SLOPE_WEIGHT` is unspecified (using QGIS default value).

"twi.sdat" %in% list.files()
#> [1] FALSE

twi_output |> 
  qgis_as_terra() |>
  plot()
#> Warning:
#> C:/Users/Tyler/AppData/Local/Temp/RtmpOOwHcn/reprex-51e8309f44b5-white-boto/twi.sdat:
#> No such file or directory (GDAL error 4)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': [rast] file does not exist: C:/Users/Tyler/AppData/Local/Temp/RtmpOOwHcn/reprex-51e8309f44b5-white-boto/twi.sdat

twi_output |> 
  stars::st_as_stars() |> 
  plot()
#> trying to read file: C:\Users\Tyler\AppData\Local\Temp\RtmpOOwHcn\reprex-51e8309f44b5-white-boto\twi.sdat
#> Error in (function (cond) : error in evaluating the argument 'x' in selecting a method for function 'plot': file not found

twi_output |> 
  qgis_as_raster() |> 
  plot()
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, will retire in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
#> The sp package is now running under evolution status 2
#>      (status 2 uses the sf package in place of rgdal)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': C:/Users/Tyler/AppData/Local/Temp/RtmpOOwHcn/reprex-51e8309f44b5-white-boto/twi.sdat does not exist

Created on 2023-07-12 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_Canada.utf8
#>  ctype    English_Canada.utf8
#>  tz       America/Toronto
#>  date     2023-07-12
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package     * version    date (UTC) lib source
#>    abind         1.4-5      2016-07-21 [1] CRAN (R 4.0.0)
#>    assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.0)
#>    class         7.3-22     2023-05-03 [2] CRAN (R 4.3.1)
#>    classInt      0.4-9      2023-02-28 [1] CRAN (R 4.2.3)
#>    cli           3.6.1      2023-03-23 [1] CRAN (R 4.2.3)
#>    codetools     0.2-19     2023-02-01 [2] CRAN (R 4.3.1)
#>    curl          5.0.1      2023-06-07 [1] CRAN (R 4.3.1)
#>    DBI           1.1.3      2022-06-18 [1] CRAN (R 4.2.0)
#>    digest        0.6.32     2023-06-26 [1] CRAN (R 4.3.1)
#>    dplyr         1.1.2      2023-04-20 [1] CRAN (R 4.2.3)
#>    e1071         1.7-13     2023-02-01 [1] CRAN (R 4.2.3)
#>    evaluate      0.21       2023-05-05 [1] CRAN (R 4.2.3)
#>    fansi         1.0.4      2023-01-22 [1] CRAN (R 4.2.3)
#>    fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.2.3)
#>    fs            1.6.2      2023-04-25 [1] CRAN (R 4.2.3)
#>    generics      0.1.3      2022-07-05 [1] CRAN (R 4.2.1)
#>  D glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.0)
#>    highr         0.10       2022-12-22 [1] CRAN (R 4.2.2)
#>    htmltools     0.5.5      2023-03-23 [1] CRAN (R 4.2.3)
#>    jsonlite      1.8.7      2023-06-29 [1] CRAN (R 4.3.1)
#>    KernSmooth    2.23-21    2023-05-03 [2] CRAN (R 4.3.1)
#>    knitr         1.43       2023-05-25 [1] CRAN (R 4.2.3)
#>    lattice       0.21-8     2023-04-05 [1] CRAN (R 4.2.3)
#>    lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.2.2)
#>    lwgeom        0.2-13     2023-05-22 [1] CRAN (R 4.2.3)
#>  D magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.2.0)
#>    pillar        1.9.0      2023-03-22 [1] CRAN (R 4.2.3)
#>    pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.2.0)
#>    processx      3.8.2      2023-06-30 [1] CRAN (R 4.3.1)
#>    proxy         0.4-27     2022-06-09 [1] CRAN (R 4.2.0)
#>    ps            1.7.5      2023-04-18 [1] CRAN (R 4.2.3)
#>    qgisprocess * 0.0.0.9000 2023-07-11 [1] Github (r-spatial/qgisprocess@3b01cfe)
#>    R6            2.5.1      2021-08-19 [1] CRAN (R 4.2.0)
#>    rappdirs      0.3.3      2021-01-31 [1] CRAN (R 4.0.3)
#>    raster        3.6-23     2023-07-04 [1] CRAN (R 4.3.1)
#>    Rcpp          1.0.11     2023-07-06 [1] CRAN (R 4.3.1)
#>    reprex        2.0.2      2022-08-17 [1] CRAN (R 4.2.1)
#>    rlang         1.1.1      2023-04-28 [1] CRAN (R 4.2.3)
#>    rmarkdown     2.23       2023-07-01 [1] CRAN (R 4.3.1)
#>    rstudioapi    0.14       2022-08-22 [1] CRAN (R 4.2.1)
#>    sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.3)
#>    sf            1.0-14     2023-07-11 [1] CRAN (R 4.3.1)
#>    sp            2.0-0      2023-06-22 [1] CRAN (R 4.3.1)
#>    stars         0.6-1      2023-04-06 [1] CRAN (R 4.2.3)
#>    stringi       1.7.12     2023-01-11 [1] CRAN (R 4.2.2)
#>    stringr       1.5.0      2022-12-02 [1] CRAN (R 4.2.2)
#>    terra       * 1.7-39     2023-06-23 [1] CRAN (R 4.3.1)
#>    tibble        3.2.1      2023-03-20 [1] CRAN (R 4.2.3)
#>    tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.2.2)
#>    units         0.8-2      2023-04-27 [1] CRAN (R 4.2.3)
#>    utf8          1.2.3      2023-01-31 [1] CRAN (R 4.2.3)
#>    vctrs         0.6.3      2023-06-14 [1] CRAN (R 4.3.1)
#>    withr         2.5.0      2022-03-03 [1] CRAN (R 4.2.0)
#>    xfun          0.39       2023-04-20 [1] CRAN (R 4.2.3)
#>    xml2          1.3.4      2023-04-27 [1] CRAN (R 4.2.3)
#>    yaml          2.3.7      2023-01-23 [1] CRAN (R 4.2.3)
#> 
#>  [1] C:/Users/Tyler/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.1/library
#> 
#>  D ── DLL MD5 mismatch, broken installation.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

@florisvdh
Copy link
Member

Thanks for the reprex; note that your previous output showed TRUE for OUTPUT %in% list.files() for the QGIS approach, but maybe that was wrong.

I cannot reproduce your issue with SAGA 8.2.2.

So it may have to do with SAGA versions too, as in #149 (comment).

You could try to add options(qgisprocess.tmp_raster_ext, ".sdat") before calling qgis_run_algorithm(), although I doubt this would solve your problem. At least it will avoid the mixture between .sdat and .tif in outputs. Anyway, I think your SAGA version supports .tif output so you could as well drop TWI = "twi.sdat" in qgis_run_algorithm() and just use qgis_extract_output(result, "TWI").

Some more things you can do to debug this:

  • in a new R session or after running qgis_clean_tmp(), go to the temporary output directory (see qgis_tmp_base()) and inspect which files are written there, and with what file extension
  • return more information with qgis_run_algorithm(..., .quiet = FALSE). This will show the JSON string used, passed by the package to the qgis_process command of QGIS.
  • try running this in QGIS and see whether similar problems arise. In any case, you can copy the corresponding qgis_process command string from the processing dialog (see Advanced button at bottom) and paste it in a shell / terminal to see whether that generates the problem as observed from R.

Anyhow, from your output it appears that QGIS successfully runs the SAGA algorithm, but for some reason you miss the output files. Maybe this is because SAGA 8.5.1 behaves differently than expected.

@jannes-m
Copy link
Collaborator

This odd behavior is well-known. Best try here is to make the output of type .tif as @florisvdh already suggested.

@jannes-m
Copy link
Collaborator

One more observation. It often seemed to me as if the sdat output works on Unix systems, and the tif output under Windows for SAGA algorithms.

@tylerbhampton
Copy link
Author

tylerbhampton commented Jul 12, 2023

Hey everyone,
big big thanks for your help!!!

I was not having success with any combination of tif/sdat tif/tif as inputs/outputs. (I also checked that sdat/tif combo did not work).

Only when I re-wrote the input file as sdat and specified output as sdat was it successful.

load package
library(terra)
#> terra 1.7.39
library(qgisprocess)
#> Attempting to load the cache ...
#> Success!
#> QGIS version: 3.28.8-Firenze
#> Having access to 1673 algorithms from 8 QGIS processing providers.
#> Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.
#> >>> Run `qgis_enable_plugins()` to enable 1 disabled plugin and access
#>     its algorithms: otbprovider

qgis_enable_plugins(names=c("processing_saga_nextgen"))
#> Ignoring plugins that are enabled already: processing_saga_nextgen
#> No QGIS plugins to be handled; exiting.
FAILED ATTEMPT WITH TIF FILE FORMAT
#### TRY WITH TIF FILE FORMAT ####

unlink("twi*")
dem = rast(system.file("raster/dem.tif",
                       package="spDataLarge"))

twi_output = qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = dem,
  TWI = "twi.tif"
) |> qgis_extract_output(name="TWI")

"twi.tif" %in% list.files()
#> [1] FALSE

unlink("twi*")
twi_output = qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = dem,
  TWI = "twi.sdat"
) |> qgis_extract_output(name="TWI")

"twi.sdat" %in% list.files()
#> [1] FALSE
SUCCESSFUL ATTEMPT WITH SDAT FILE FORMAT
#### TRY WITH SDAT FILE FORMAT ####

unlink("twi*")
dem = rast(system.file("raster/dem.tif",
                       package="spDataLarge"))
writeRaster(dem,"dem.sdat")
#> Warning: Unable to set GeoTransform, SAGA binary grids only support the same cellsize in x-y.
#>  (GDAL error 6)
"dem.sdat" %in% list.files()
#> [1] TRUE

twi_output = qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = "dem.sdat",
  TWI = "twi.sdat"
  ) |> qgis_extract_output(name="TWI")
#> Argument `WEIGHT` is unspecified (using QGIS default value).
#> Using `AREA = qgis_tmp_raster()`
#> Using `SLOPE = qgis_tmp_raster()`
#> Using `AREA_MOD = qgis_tmp_raster()`
#> Argument `SUCTION` is unspecified (using QGIS default value).
#> Using `AREA_TYPE = "[0] total catchment area"`
#> Using `SLOPE_TYPE = "[0] local slope"`
#> Argument `SLOPE_MIN` is unspecified (using QGIS default value).
#> Argument `SLOPE_OFF` is unspecified (using QGIS default value).
#> Argument `SLOPE_WEIGHT` is unspecified (using QGIS default value).

"twi.sdat" %in% list.files()
#> [1] TRUE
SUCCESSFUL PLOTS
twi_output |> 
  qgis_as_terra() |>
  plot()

twi_output |> 
  stars::st_as_stars() |> 
  plot()

twi_output |> 
  qgis_as_raster() |> 
  plot()

OTHER FAILED FILE COMBINATIONS
# OTHER FAILED FILE COMBINATIONS
unlink("twi*")
twi_output = qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = "dem.sdat",
  TWI = "twi.tif"
) |> qgis_extract_output(name="TWI")

"twi.tif" %in% list.files()
#> [1] FALSE

unlink("twi*")
twi_output = qgis_run_algorithm(
  algorithm = "sagang:sagawetnessindex",
  DEM = "dem.sdat"#,
  # DON'T SPECIFY OUTPUT FILE
  #TWI = 
) |> qgis_extract_output(name="TWI")

list.files(path=qgis_tmp_base())
#> character(0)

Created on 2023-07-12 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_Canada.utf8
#>  ctype    English_Canada.utf8
#>  tz       America/Toronto
#>  date     2023-07-12
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package     * version    date (UTC) lib source
#>    abind         1.4-5      2016-07-21 [1] CRAN (R 4.0.0)
#>    assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.0)
#>    class         7.3-22     2023-05-03 [2] CRAN (R 4.3.1)
#>    classInt      0.4-9      2023-02-28 [1] CRAN (R 4.2.3)
#>    cli           3.6.1      2023-03-23 [1] CRAN (R 4.2.3)
#>    codetools     0.2-19     2023-02-01 [2] CRAN (R 4.3.1)
#>    curl          5.0.1      2023-06-07 [1] CRAN (R 4.3.1)
#>    DBI           1.1.3      2022-06-18 [1] CRAN (R 4.2.0)
#>    digest        0.6.32     2023-06-26 [1] CRAN (R 4.3.1)
#>    dplyr         1.1.2      2023-04-20 [1] CRAN (R 4.2.3)
#>    e1071         1.7-13     2023-02-01 [1] CRAN (R 4.2.3)
#>    evaluate      0.21       2023-05-05 [1] CRAN (R 4.2.3)
#>    fansi         1.0.4      2023-01-22 [1] CRAN (R 4.2.3)
#>    fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.2.3)
#>    fs            1.6.2      2023-04-25 [1] CRAN (R 4.2.3)
#>    generics      0.1.3      2022-07-05 [1] CRAN (R 4.2.1)
#>  D glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.0)
#>    highr         0.10       2022-12-22 [1] CRAN (R 4.2.2)
#>    htmltools     0.5.5      2023-03-23 [1] CRAN (R 4.2.3)
#>    jsonlite      1.8.7      2023-06-29 [1] CRAN (R 4.3.1)
#>    KernSmooth    2.23-21    2023-05-03 [2] CRAN (R 4.3.1)
#>    knitr         1.43       2023-05-25 [1] CRAN (R 4.2.3)
#>    lattice       0.21-8     2023-04-05 [1] CRAN (R 4.2.3)
#>    lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.2.2)
#>    lwgeom        0.2-13     2023-05-22 [1] CRAN (R 4.2.3)
#>  D magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.2.0)
#>    pillar        1.9.0      2023-03-22 [1] CRAN (R 4.2.3)
#>    pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.2.0)
#>    processx      3.8.2      2023-06-30 [1] CRAN (R 4.3.1)
#>    proxy         0.4-27     2022-06-09 [1] CRAN (R 4.2.0)
#>    ps            1.7.5      2023-04-18 [1] CRAN (R 4.2.3)
#>    qgisprocess * 0.0.0.9000 2023-07-11 [1] Github (r-spatial/qgisprocess@3b01cfe)
#>    R6            2.5.1      2021-08-19 [1] CRAN (R 4.2.0)
#>    rappdirs      0.3.3      2021-01-31 [1] CRAN (R 4.0.3)
#>    raster        3.6-23     2023-07-04 [1] CRAN (R 4.3.1)
#>    Rcpp          1.0.11     2023-07-06 [1] CRAN (R 4.3.1)
#>    reprex        2.0.2      2022-08-17 [1] CRAN (R 4.2.1)
#>    rlang         1.1.1      2023-04-28 [1] CRAN (R 4.2.3)
#>    rmarkdown     2.23       2023-07-01 [1] CRAN (R 4.3.1)
#>    rstudioapi    0.14       2022-08-22 [1] CRAN (R 4.2.1)
#>    sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.3)
#>    sf            1.0-14     2023-07-11 [1] CRAN (R 4.3.1)
#>    sp            2.0-0      2023-06-22 [1] CRAN (R 4.3.1)
#>    stars         0.6-1      2023-04-06 [1] CRAN (R 4.2.3)
#>    stringi       1.7.12     2023-01-11 [1] CRAN (R 4.2.2)
#>    stringr       1.5.0      2022-12-02 [1] CRAN (R 4.2.2)
#>    terra       * 1.7-39     2023-06-23 [1] CRAN (R 4.3.1)
#>    tibble        3.2.1      2023-03-20 [1] CRAN (R 4.2.3)
#>    tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.2.2)
#>    units         0.8-2      2023-04-27 [1] CRAN (R 4.2.3)
#>    utf8          1.2.3      2023-01-31 [1] CRAN (R 4.2.3)
#>    vctrs         0.6.3      2023-06-14 [1] CRAN (R 4.3.1)
#>    withr         2.5.0      2022-03-03 [1] CRAN (R 4.2.0)
#>    xfun          0.39       2023-04-20 [1] CRAN (R 4.2.3)
#>    xml2          1.3.4      2023-04-27 [1] CRAN (R 4.2.3)
#>    yaml          2.3.7      2023-01-23 [1] CRAN (R 4.2.3)
#> 
#>  [1] C:/Users/Tyler/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.1/library
#> 
#>  D ── DLL MD5 mismatch, broken installation.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

@tylerbhampton
Copy link
Author

Sharing outputs from QGIS as well. Just as with qgisprocess R package, QGIS failed to perform the SAGA Next Gen Wetness Index when reading the raster dem from its source in TIF format. But by reading the raster dem as SDAT file (that I had exported using R), the operation was successful (image attached).

PS You all also helped me learn how to do dropdowns in github comments 😄

Failed QGIS Attempt with reading raster as TIF

`QGIS version: 3.28.8-Firenze
QGIS code revision: 5ac45272b58
Qt version: 5.15.3
Python version: 3.9.5
GDAL version: 3.7.0
GEOS version: 3.11.2-CAPI-1.17.2
PROJ version: Rel. 9.2.1, June 1st, 2023
PDAL version: 2.5.3 (git-version: 7ec4e6)
SAGA Next Gen version: 0.0.7
Algorithm started at: 2023-07-12T14:09:02
Algorithm 'Saga wetness index' starting…
Input parameters:
{ 'AREA' : 'TEMPORARY_OUTPUT', 'AREA_MOD' : 'TEMPORARY_OUTPUT', 'AREA_TYPE' : 1, 'DEM' : 'C:/Users/Tyler/AppData/Local/R/win-library/4.3/spDataLarge/raster/dem.tif', 'SLOPE' : 'TEMPORARY_OUTPUT', 'SLOPE_MIN' : 0, 'SLOPE_OFF' : 0.1, 'SLOPE_TYPE' : 1, 'SLOPE_WEIGHT' : 1, 'SUCTION' : 10, 'TWI' : 'TEMPORARY_OUTPUT', 'WEIGHT' : None }

io_gdal 0 -TRANSFORM 1 -RESAMPLING 3 -GRIDS "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/101ea957b9f340a2ac586528939e3d98/dem.sgrd" -FILES "C:/Users/Tyler/AppData/Local/R/win-library/4.3/spDataLarge/raster/dem.tif"
ta_hydrology "SAGA Wetness Index" -DEM "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/101ea957b9f340a2ac586528939e3d98/dem.sgrd" -SUCTION 10.0 -AREA_TYPE 1 -SLOPE_TYPE 1 -SLOPE_MIN 0.0 -SLOPE_OFF 0.1 -SLOPE_WEIGHT 1.0 -AREA "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/733913777f344bdfbb00234ed2842006/AREA.sdat" -SLOPE "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/7d92f8c33d2145eead06c887e53e4d20/SLOPE.sdat" -AREA_MOD "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/6d512a0b11fd45778aef42298f1ad4ee/AREA_MOD.sdat" -TWI "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/3994e337771a4a61ab7d87c6063a4ee2/TWI.sdat"

C:\Users\Tyler\Documents>set SAGA=C:/OSGeo4W/apps\saga

C:\Users\Tyler\Documents>set SAGA_MLB=C:/OSGeo4W/apps\saga\modules

C:\Users\Tyler\Documents>PATH=C:\OSGeo4W\apps\qgis-ltr\bin;C:\OSGeo4W\apps\grass\grass83\lib;C:\OSGeo4W\apps\grass\grass83\bin;C:\OSGeo4W\apps\qt5\bin;C:\OSGeo4W\apps\Python39\Scripts;C:\OSGeo4W\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBem;C:/OSGeo4W/apps\saga;C:/OSGeo4W/apps\saga\modules

C:\Users\Tyler\Documents>saga_cmd io_gdal 0 -TRANSFORM 1 -RESAMPLING 3 -GRIDS "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/101ea957b9f340a2ac586528939e3d98/dem.sgrd" -FILES "C:/Users/Tyler/AppData/Local/R/win-library/4.3/spDataLarge/raster/dem.tif"


## #####

### ##

# ## ## #### #

##### ## #

# ## ##### #

SAGA Version: 7.8.2 (64 bit)

88 loaded tool libraries (775 tools):

  • tin_viewer
  • tin_tools
  • table_tools
  • table_calculus
  • ta_slope_stability
  • ta_profiles
  • ta_preprocessor
  • ta_morphometry
  • ta_lighting
  • ta_hydrology
  • ta_compound
  • ta_cliffmetrics
  • ta_channels
  • statistics_regression
  • statistics_points
  • statistics_kriging
  • statistics_grid
  • sim_rivflow
  • sim_qm_of_esp
  • sim_landscape_evolution
  • sim_ihacres
  • sim_hydrology
  • sim_geomorphology
  • sim_fire_spreading
  • sim_erosion
  • sim_ecosystems_hugget
  • sim_cellular_automata
  • sim_air_flow
  • shapes_transect
  • shapes_tools
  • shapes_polygons
  • shapes_points
  • shapes_lines
  • shapes_grid
  • pointcloud_viewer
  • pointcloud_tools
  • pj_proj4
  • pj_geotrans
  • pj_georeference
  • io_webservices
  • io_virtual
  • io_table
  • io_shapes_dxf
  • io_shapes
  • io_pdal
  • io_grid_image
  • io_grid
  • io_gps
  • io_esri_e00
  • imagery_tools
  • imagery_svm
  • imagery_segmentation
  • imagery_photogrammetry
  • imagery_maxent
  • imagery_isocluster
  • imagery_classification
  • grids_tools
  • grid_visualisation
  • grid_tools
  • grid_spline
  • grid_gridding
  • grid_filter
  • grid_calculus_bsl
  • grid_calculus
  • grid_analysis
  • garden_webservices
  • garden_learn_to_program
  • garden_games
  • garden_fractals
  • garden_3d_viewer
  • docs_pdf
  • docs_html
  • dev_tools
  • db_pgsql
  • db_odbc
  • contrib_perego
  • climate_tools
  • tta_tools *
  • toolchains *
  • terrain_analysis *
  • imagery *
  • group_files *
  • grid_filter *
  • gc_tools *
  • climate_tools *
  • Shapes Tools *
  • Polygon Tools *
  • Grid Tools *

*) tool chain libraries

Error: select a library
type -h or --help for further information

C:\Users\Tyler\Documents>saga_cmd ta_hydrology "SAGA Wetness Index" -DEM "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/101ea957b9f340a2ac586528939e3d98/dem.sgrd" -SUCTION 10.0 -AREA_TYPE 1 -SLOPE_TYPE 1 -SLOPE_MIN 0.0 -SLOPE_OFF 0.1 -SLOPE_WEIGHT 1.0 -AREA "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/733913777f344bdfbb00234ed2842006/AREA.sdat" -SLOPE "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/7d92f8c33d2145eead06c887e53e4d20/SLOPE.sdat" -AREA_MOD "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/6d512a0b11fd45778aef42298f1ad4ee/AREA_MOD.sdat" -TWI "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/3994e337771a4a61ab7d87c6063a4ee2/TWI.sdat"


## #####

### ##

# ## ## #### #

##### ## #

# ## ##### #

SAGA Version: 7.8.2 (64 bit)


library path: C:\OSGeo4W\apps\saga\tools
library name: ta_hydrology
library : ta_hydrology
tool : SAGA Wetness Index
identifier : 15
author : J.Boehner, O.Conrad (c) 2001
processors : 12 [12]


Loading grid: C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/101ea957b9f340a2ac586528939e3d98/dem.sgrd...
failed
Error: input file [C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/101ea957b9f340a2ac586528939e3d98/dem.sgrd]
Error: Elevation

Usage: saga_cmd ta_hydrology 15 [-DEM ] [-WEIGHT ] [-AREA ] [-SLOPE ] [-AREA_MOD ] [-TWI ] [-SUCTION ] [-AREA_TYPE ] [-SLOPE_TYPE ] [-SLOPE_MIN ] [-SLOPE_OFF ] [-SLOPE_WEIGHT ]
-DEM: Elevation
Grid, input
-WEIGHT: Weights
Grid, input, optional
-AREA: Catchment Area
Grid, output
-SLOPE: Catchment Slope
Grid, output
-AREA_MOD: Modified Catchment Area
Grid, output
-TWI: Topographic Wetness Index
Grid, output
-SUCTION: Suction
Floating point
Minimum: 0.000000
Default: 10.000000
-AREA_TYPE: Type of Area
Choice
Available Choices:
[0] total catchment area
[1] square root of catchment area
[2] specific catchment area
Default: 1
-SLOPE_TYPE: Type of Slope
Choice
Available Choices:
[0] local slope
[1] catchment slope
Default: 1
-SLOPE_MIN: Minimum Slope
Floating point
Minimum: 0.000000
Default: 0.000000
-SLOPE_OFF: Offset Slope
Floating point
Minimum: 0.000000
Default: 0.100000
-SLOPE_WEIGHT: Slope Weighting
Floating point
Minimum: 0.000000
Default: 1.000000

C:\Users\Tyler\Documents>exit
Execution completed in 5.00 seconds
Results:
{'AREA': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/733913777f344bdfbb00234ed2842006/AREA.sdat',
'AREA_MOD': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/6d512a0b11fd45778aef42298f1ad4ee/AREA_MOD.sdat',
'SLOPE': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/7d92f8c33d2145eead06c887e53e4d20/SLOPE.sdat',
'TWI': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/3994e337771a4a61ab7d87c6063a4ee2/TWI.sdat'}

Loading resulting layers
The following layers were not correctly generated.
• C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/3994e337771a4a61ab7d87c6063a4ee2/TWI.sdat
• C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/6d512a0b11fd45778aef42298f1ad4ee/AREA_MOD.sdat
• C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/733913777f344bdfbb00234ed2842006/AREA.sdat
• C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/7d92f8c33d2145eead06c887e53e4d20/SLOPE.sdat
You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.`

Successful QGIS Attempt with reading raster as SDAT

`QGIS version: 3.28.8-Firenze
QGIS code revision: 5ac45272b58
Qt version: 5.15.3
Python version: 3.9.5
GDAL version: 3.7.0
GEOS version: 3.11.2-CAPI-1.17.2
PROJ version: Rel. 9.2.1, June 1st, 2023
PDAL version: 2.5.3 (git-version: 7ec4e6)
SAGA Next Gen version: 0.0.7
Algorithm started at: 2023-07-12T14:14:02
Algorithm 'Saga wetness index' starting…
Input parameters:
{ 'AREA' : 'TEMPORARY_OUTPUT', 'AREA_MOD' : 'TEMPORARY_OUTPUT', 'AREA_TYPE' : 1, 'DEM' : 'D:/rtesting/rqgis_trouble/dem.sdat', 'SLOPE' : 'TEMPORARY_OUTPUT', 'SLOPE_MIN' : 0, 'SLOPE_OFF' : 0.1, 'SLOPE_TYPE' : 1, 'SLOPE_WEIGHT' : 1, 'SUCTION' : 10, 'TWI' : 'TEMPORARY_OUTPUT', 'WEIGHT' : None }

ta_hydrology "SAGA Wetness Index" -DEM "D:/rtesting/rqgis_trouble/dem.sgrd" -SUCTION 10.0 -AREA_TYPE 1 -SLOPE_TYPE 1 -SLOPE_MIN 0.0 -SLOPE_OFF 0.1 -SLOPE_WEIGHT 1.0 -AREA "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/f5b8d0b58b57441bb3791e0ab7f4e374/AREA.sdat" -SLOPE "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/9131e41b1c7d4be1bd014561139573d6/SLOPE.sdat" -AREA_MOD "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/eaebd7ce670b4788930f1e05b18489f3/AREA_MOD.sdat" -TWI "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/2c01c35cd32b4b1fb88d22f6d31b3efd/TWI.sdat"

C:\Users\Tyler\Documents>set SAGA=C:/OSGeo4W/apps\saga

C:\Users\Tyler\Documents>set SAGA_MLB=C:/OSGeo4W/apps\saga\modules

C:\Users\Tyler\Documents>PATH=C:\OSGeo4W\apps\qgis-ltr\bin;C:\OSGeo4W\apps\grass\grass83\lib;C:\OSGeo4W\apps\grass\grass83\bin;C:\OSGeo4W\apps\qt5\bin;C:\OSGeo4W\apps\Python39\Scripts;C:\OSGeo4W\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBem;C:/OSGeo4W/apps\saga;C:/OSGeo4W/apps\saga\modules

C:\Users\Tyler\Documents>saga_cmd ta_hydrology "SAGA Wetness Index" -DEM "D:/rtesting/rqgis_trouble/dem.sgrd" -SUCTION 10.0 -AREA_TYPE 1 -SLOPE_TYPE 1 -SLOPE_MIN 0.0 -SLOPE_OFF 0.1 -SLOPE_WEIGHT 1.0 -AREA "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/f5b8d0b58b57441bb3791e0ab7f4e374/AREA.sdat" -SLOPE "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/9131e41b1c7d4be1bd014561139573d6/SLOPE.sdat" -AREA_MOD "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/eaebd7ce670b4788930f1e05b18489f3/AREA_MOD.sdat" -TWI "C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/2c01c35cd32b4b1fb88d22f6d31b3efd/TWI.sdat"


## #####

### ##

# ## ## #### #

##### ## #

# ## ##### #

SAGA Version: 7.8.2 (64 bit)


library path: C:\OSGeo4W\apps\saga\tools
library name: ta_hydrology
library : ta_hydrology
tool : SAGA Wetness Index
identifier : 15
author : J.Boehner, O.Conrad (c) 2001
processors : 12 [12]


Loading grid: D:/rtesting/rqgis_trouble/dem.sgrd...

Parameters

Grid System: 1; 117x 117y; 0x 0y
Elevation: dem
Weights:
Catchment Area:
Catchment Slope:
Modified Catchment Area:
Topographic Wetness Index:
Suction: 10.000000
Type of Area: square root of catchment area
Type of Slope: catchment slope
Minimum Slope: 0.000000
Offset Slope: 0.100000
Slope Weighting: 1.000000

pass 2 (0 > 0)
post-processing...
topographic wetness index...
Saving grid: C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/f5b8d0b58b57441bb3791e0ab7f4e374/AREA.sdat...

Saving grid: C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/9131e41b1c7d4be1bd014561139573d6/SLOPE.sdat...

Saving grid: C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/eaebd7ce670b4788930f1e05b18489f3/AREA_MOD.sdat...

Saving grid: C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/2c01c35cd32b4b1fb88d22f6d31b3efd/TWI.sdat...

C:\Users\Tyler\Documents>exit
Execution completed in 3.01 seconds
Results:
{'AREA': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/f5b8d0b58b57441bb3791e0ab7f4e374/AREA.sdat',
'AREA_MOD': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/eaebd7ce670b4788930f1e05b18489f3/AREA_MOD.sdat',
'SLOPE': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/9131e41b1c7d4be1bd014561139573d6/SLOPE.sdat',
'TWI': 'C:/Users/Tyler/AppData/Local/Temp/processing_MYVVIh/2c01c35cd32b4b1fb88d22f6d31b3efd/TWI.sdat'}

Loading resulting layers
Algorithm 'Saga wetness index' finished`

image

@jannes-m
Copy link
Collaborator

OK, that is interesting and news to me. So thank you for sharing your solution!

@tylerbhampton
Copy link
Author

If it offers more confusion, the RSAGA package can successfully handle TIF as input file and writing to both TIF and SDAT files as output. I'm not sure if there's collaboration between the packages. I'm happy to answer any more questions if this is highlighting a problem with the most recent SAGA release?

Successful Wetness Index Calculation with RSAGA (TIF and SDAT file formats)
library(RSAGA)
#> Loading required package: gstat
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, will retire in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
#> The sp package is now running under evolution status 2
#>      (status 2 uses the sf package in place of rgdal)
#> Loading required package: shapefiles
#> Loading required package: foreign
#> 
#> Attaching package: 'shapefiles'
#> The following objects are masked from 'package:foreign':
#> 
#>     read.dbf, write.dbf
#> Loading required package: plyr
library(terra)
#> terra 1.7.39

unlink("twi*")
RSAGA::rsaga.wetness.index(
  in.dem = system.file("raster","dem.tif",
                       package="spDataLarge"),
  out.wetness.index = "twi.sdat"
)
#> Search for SAGA command line program and modules... 
#> Done
#> Warning in rsaga.geoprocessor(lib = "ta_hydrology", module = "SAGA Wetness Index", : This RSAGA version has been tested with SAGA GIS versions between 2.3.1 and 8.4.1.
#> You seem to be using SAGA GIS 8.5.1, which may cause problems due to
#> possible changes in names and definitions of SAGA module arguments, etc.
#> Warning in rsaga.geoprocessor(lib, module = NULL, env = env, intern = TRUE, : This RSAGA version has been tested with SAGA GIS versions between 2.3.1 and 8.4.1.
#> You seem to be using SAGA GIS 8.5.1, which may cause problems due to
#> possible changes in names and definitions of SAGA module arguments, etc.
#> 
#> 
#> SAGA Version: 8.5.1 (64 bit)
#> 
#> library path: C:\Progra~1\SAGA\tools\
#> library name: ta_hydrology
#> library     : ta_hydrology
#> tool        : SAGA Wetness Index
#> identifier  : 15
#> author      : J.Boehner, O.Conrad (c) 2001
#> processors  : 12 [12]
#> 
#> loading: dem
#> translation: dem
#> [SAGA Wetness Index] Execution started...
#> 
#> [SAGA Wetness Index] Parameters:
#> 
#> Grid System: 30.850000; 117x 117y; 794614.532615x 8931790.299603y
#> Elevation: dem
#> Weights: <not set>
#> Catchment Area: Catchment Area
#> Catchment Slope: Catchment Slope
#> Modified Catchment Area: Modified Catchment Area
#> Topographic Wetness Index: Topographic Wetness Index
#> Suction: 10
#> Type of Area: specific catchment area
#> Type of Slope: catchment slope
#> Minimum Slope: 0
#> Offset Slope: 0.1
#> Slope Weighting: 1
#> 
#> Create index: dem
#> catchment area and slope...
#> pass 1 (917 > 0)
#> pass 2 (493 > 0)
#> pass 3 (253 > 0)
#> pass 4 (143 > 0)
#> pass 5 (65 > 0)
#> pass 6 (32 > 0)
#> pass 7 (5 > 0)
#> pass 8 (1 > 0)
#> pass 9 (0 > 0)
#> post-processing...
#> topographic wetness index...
#> 
#> total execution time: 0 milliseconds (less than 1 millisecond)
#> 
#> [SAGA Wetness Index] Execution succeeded (less than 1 millisecond)
#> Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpeGuHQQ\file31ec14776bd3...
#> Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpeGuHQQ\file31ec711824a9...
#> Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpeGuHQQ\file31ec4f86f30...
#> Saving grid: twi.sdat...
"twi.sdat" %in% list.files()
#> [1] TRUE
OUTPUT = rast("twi.sdat")
plot(OUTPUT)


unlink("twi*")
RSAGA::rsaga.wetness.index(
  in.dem = system.file("raster","dem.tif",
                       package="spDataLarge"),
  out.wetness.index = "twi.tif"
)
#> Search for SAGA command line program and modules... 
#> Done
#> Warning in rsaga.geoprocessor(lib = "ta_hydrology", module = "SAGA Wetness Index", : This RSAGA version has been tested with SAGA GIS versions between 2.3.1 and 8.4.1.
#> You seem to be using SAGA GIS 8.5.1, which may cause problems due to
#> possible changes in names and definitions of SAGA module arguments, etc.

#> Warning in rsaga.geoprocessor(lib = "ta_hydrology", module = "SAGA Wetness Index", : This RSAGA version has been tested with SAGA GIS versions between 2.3.1 and 8.4.1.
#> You seem to be using SAGA GIS 8.5.1, which may cause problems due to
#> possible changes in names and definitions of SAGA module arguments, etc.
#> 
#> 
#> SAGA Version: 8.5.1 (64 bit)
#> 
#> library path: C:\Progra~1\SAGA\tools\
#> library name: ta_hydrology
#> library     : ta_hydrology
#> tool        : SAGA Wetness Index
#> identifier  : 15
#> author      : J.Boehner, O.Conrad (c) 2001
#> processors  : 12 [12]
#> 
#> loading: dem
#> translation: dem
#> [SAGA Wetness Index] Execution started...
#> 
#> [SAGA Wetness Index] Parameters:
#> 
#> Grid System: 30.850000; 117x 117y; 794614.532615x 8931790.299603y
#> Elevation: dem
#> Weights: <not set>
#> Catchment Area: Catchment Area
#> Catchment Slope: Catchment Slope
#> Modified Catchment Area: Modified Catchment Area
#> Topographic Wetness Index: Topographic Wetness Index
#> Suction: 10
#> Type of Area: specific catchment area
#> Type of Slope: catchment slope
#> Minimum Slope: 0
#> Offset Slope: 0.1
#> Slope Weighting: 1
#> 
#> Create index: dem
#> catchment area and slope...
#> pass 1 (981 > 0)
#> pass 2 (524 > 0)
#> pass 3 (268 > 0)
#> pass 4 (142 > 0)
#> pass 5 (71 > 0)
#> pass 6 (33 > 0)
#> pass 7 (5 > 0)
#> pass 8 (1 > 0)
#> pass 9 (0 > 0)
#> post-processing...
#> topographic wetness index...
#> 
#> total execution time: 0 milliseconds (less than 1 millisecond)
#> 
#> [SAGA Wetness Index] Execution succeeded (less than 1 millisecond)
#> Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpeGuHQQ\file31ec1c9e2784...
#> Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpeGuHQQ\file31ec63f7731a...
#> Saving grid: C:\Users\Tyler\AppData\Local\Temp\RtmpeGuHQQ\file31ec246a2fc2...
#> Saving grid: twi.tif...
#> Export GeoTIFF
#> 
#> [Export GeoTIFF] Parameters:
#> 
#> Grid System: 30.850000; 117x 117y; 794614.532615x 8931790.299603y
#> Grid(s): 1 object (twi)
#> File: twi.tif
#> Creation Options: COMPRESS=LZW
#> 
#> Band 1
#> 
#> [Export GeoTIFF] execution time: less than 1 millisecond
"twi.tif" %in% list.files()
#> [1] TRUE
OUTPUT = rast("twi.tif")
plot(OUTPUT)

Created on 2023-07-12 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_Canada.utf8
#>  ctype    English_Canada.utf8
#>  tz       America/Toronto
#>  date     2023-07-12
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package     * version date (UTC) lib source
#>    cli           3.6.1   2023-03-23 [1] CRAN (R 4.2.3)
#>    codetools     0.2-19  2023-02-01 [2] CRAN (R 4.3.1)
#>    curl          5.0.1   2023-06-07 [1] CRAN (R 4.3.1)
#>    digest        0.6.32  2023-06-26 [1] CRAN (R 4.3.1)
#>    evaluate      0.21    2023-05-05 [1] CRAN (R 4.2.3)
#>    fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.2.3)
#>    FNN           1.1.3.2 2023-03-20 [1] CRAN (R 4.2.3)
#>    foreign     * 0.8-84  2022-12-06 [1] CRAN (R 4.2.2)
#>    fs            1.6.2   2023-04-25 [1] CRAN (R 4.2.3)
#>  D glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>    gstat       * 2.1-1   2023-04-06 [1] CRAN (R 4.2.3)
#>    highr         0.10    2022-12-22 [1] CRAN (R 4.2.2)
#>    htmltools     0.5.5   2023-03-23 [1] CRAN (R 4.2.3)
#>    intervals     0.15.4  2023-06-29 [1] CRAN (R 4.3.1)
#>    knitr         1.43    2023-05-25 [1] CRAN (R 4.2.3)
#>    lattice       0.21-8  2023-04-05 [1] CRAN (R 4.2.3)
#>    lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.2)
#>  D magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>    plyr        * 1.8.8   2022-11-11 [1] CRAN (R 4.2.2)
#>    Rcpp          1.0.11  2023-07-06 [1] CRAN (R 4.3.1)
#>    reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.1)
#>    rlang         1.1.1   2023-04-28 [1] CRAN (R 4.2.3)
#>    rmarkdown     2.23    2023-07-01 [1] CRAN (R 4.3.1)
#>    RSAGA       * 1.4.0   2022-12-09 [1] CRAN (R 4.3.1)
#>    rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.1)
#>    sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.3)
#>    shapefiles  * 0.7.2   2022-08-25 [1] CRAN (R 4.2.1)
#>    sp            2.0-0   2023-06-22 [1] CRAN (R 4.3.1)
#>    spacetime     1.3-0   2023-04-05 [1] CRAN (R 4.2.3)
#>    terra       * 1.7-39  2023-06-23 [1] CRAN (R 4.3.1)
#>    withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>    xfun          0.39    2023-04-20 [1] CRAN (R 4.2.3)
#>    xml2          1.3.4   2023-04-27 [1] CRAN (R 4.2.3)
#>    xts           0.13.1  2023-04-16 [1] CRAN (R 4.2.3)
#>    yaml          2.3.7   2023-01-23 [1] CRAN (R 4.2.3)
#>    zoo           1.8-12  2023-04-13 [1] CRAN (R 4.2.3)
#> 
#>  [1] C:/Users/Tyler/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.1/library
#> 
#>  D ── DLL MD5 mismatch, broken installation.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

@jannes-m
Copy link
Collaborator

Interesting, indeed. By the way there is also a Rsagacmd package and if this package also can handle tif inputs, this would potentially mean that there is a bug with the way QGIS handles the SAGA API.

@florisvdh
Copy link
Member

Thanks a lot @tylerbhampton for these experiments both in R and QGIS! I believe the SAGA version is the cause here, I don't think QGIS itself nor the SAGA Next Gen provider is at stake:

  • your OSGeo4W installation (which has your QGIS) uses SAGA 7.8.2. At least an older version (7.3) has been observed to not support tif files, cf. #149, and that appears still the case in 7.8.2. So setting options(qgisprocess.tmp_raster_ext, ".sdat") (then you don't need TWI = "twi.sdat"), and indeed rewriting the input dem.tif as sdat (thanks!!) will make things work with these SAGA versions.
  • however your RSAGA uses SAGA 8.5.1, which clearly has been installed separately. As observed in SAGA 8.2.2 too (here and in #149) and shown by your RSAGA example, it has native support for GeoTiff (input/output) and this explains your different findings.

The QGIS solution that does not need the .sdat would be to either:

  • ask the SAGA-GIS community to update the SAGA package in OSGeo4W, wait for that to happen, and then update SAGA using your OSGeo4W installer.
  • or just provide the SAGA 8.5.1 path to the SAGA Next Gen plugin in QGIS!
    • In QGIS, go to Settings > Options; select 'Processing'.
    • Under SAGANG, you see 'SAGA folder'. Point it to your SAGA 8.5.1 installation folder.
    • This is also shown in https://www.youtube.com/watch?v=VKdaripCups (I don't need the import/export optimizations for your case to work though).

Also note that the native SAGA provider in QGIS has been dropped since QGIS 3.30.

@florisvdh
Copy link
Member

If you can let us know whether the SAGANG 'SAGA folder' setting fixes this, that would be a great confirmation @tylerbhampton ! Your SAGA 8.5.1 standalone installation is in C:\Program Files\SAGA seen from the RSAGA output; so that path should be used in the SAGANG 'SAGA folder' setting.

@tylerbhampton
Copy link
Author

Hey! So unlike the video you shared, I don't see an option to change the "SAGA Folder" in my QGIS settings
image

@florisvdh
Copy link
Member

florisvdh commented Jul 17, 2023

Thanks @tylerbhampton; are you sure that you can also not click or double click in the white area right to SAGA folder and type a path?

I could not find some logic in the plugin code that prevents editing in the case of OSGEO4W, so if that does not work it might be a bug, to be reported at https://github.com/north-road/qgis-processing-saga-nextgen

@florisvdh florisvdh changed the title Cannot create raster outputs Older SAGA versions (< 8) don't work well with GeoTIFF Aug 27, 2023
@florisvdh
Copy link
Member

#166 (comment) has the analysis of this problem – it depends on the SAGA version – and provides ways forward.

Closing this issue as this implies no work in qgisprocess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants