r-spatial / qgisprocess

R package to use QGIS processing algorithms
https://r-spatial.github.io/qgisprocess/
GNU General Public License v3.0
198 stars 20 forks source link

How to access algorithms of QGIS plugin Visibility Analysis? #73

Closed cesarkero closed 2 years ago

cesarkero commented 2 years ago

How can be added the plugins to the algorithm list ? Exactly this: visibility:Viewshed...

unbrother commented 2 years ago

I have this question too. I intend to use QNEAT3

florisvdh commented 2 years ago

It seems that the qgis_process command (backend of R package qgisprocess) currently doesn't yet support or recognize every plugin. I wonder how this is regarded by QGIS - seems to have relations with https://github.com/qgis/QGIS/issues/37989 and https://github.com/qgis/QGIS/issues/41218.

Having installed 'Visibility Analysis', just a limited subset of installed or active QGIS plugins are enlisted:

> cat(qgis_run("--help")$stdout)
QGIS Processing Executor - 3.22.3-Białowieża 'Białowieża' (3.22.3-Białowieża)
Usage: /usr/bin/qgis_process.bin [--help] [--version] [--json] [--verbose] [command] [algorithm id or path to model file] [parameters]

Options:
    --help or -h        Output the help
    --version or -v     Output all versions related to QGIS Process
    --json      Output results as JSON objects
    --verbose   Output verbose logs

Available commands:
    plugins     list available and active plugins
    plugins enable  enables an installed plugin. The plugin name must be specified, e.g. "plugins enable cartography_tools"
    plugins disable disables an installed plugin. The plugin name must be specified, e.g. "plugins disable cartography_tools"
    list        list all available processing algorithms
    help        show help for an algorithm. The algorithm id or a path to a model file must be specified.
    run     runs an algorithm. The algorithm id or a path to a model file and parameter values must be specified. Parameter values are specified after -- with PARAMETER=VALUE syntax. Ordered list values for a parameter can be created by specifying the parameter multiple times, e.g. --LAYERS=layer1.shp --LAYERS=layer2.shp
            If required, the ellipsoid to use for distance and area calculations can be specified via the "--ELLIPSOID=name" argument.
            If required, an existing QGIS project to use during the algorithm execution can be specified via the "--PROJECT_PATH=path" argument.
> cat(qgis_run("plugins")$stdout)
Available plugins
(* indicates loaded plugins which implement Processing providers)

* grassprovider
* processing
* sagaprovider
  otbprovider
> qgis_run("plugins enable 'Visibility Analysis'")
Error in processx::run(path, args, ...) : 
  System command 'qgis_process' failed, exit status: 1, stderr:
E> qt5ct: using qt5ct plugin
E> Command plugins enable 'Visibility Analysis' not known!
Type .Last.error.trace to see where the error occurred
> qgis_run("plugins enable visibility_analysis")
Error in processx::run(path, args, ...) : 
  System command 'qgis_process' failed, exit status: 1, stderr:
E> qt5ct: using qt5ct plugin
E> Command plugins enable visibility_analysis not known!
Type .Last.error.trace to see where the error occurred
JanCaha commented 2 years ago

It seems that qgis_process is only listing core plugins included in default QGIS install. Looks like it does not look in user profiles where installed plugins are located.

florisvdh commented 2 years ago

I'm unsure what's the specific condition for plugin uptake. In the qgis_process help, cartography_tools is cited as an example but on my PC it wasn't installed yet. In the GUI, the plugin is called 'Cartography Tools'. It turns out, after installing the plugin in QGIS GUI, this plugin is indeed seen by qgis_process:

$ qgis_process plugins
qt5ct: using qt5ct plugin
Available plugins
(* indicates loaded plugins which implement Processing providers)

* cartography_tools
* grassprovider
* processing
* sagaprovider
  otbprovider
JanCaha commented 2 years ago

The difference between cartography_tools and Cartography Tools is pretty much plugin id, as used by QGIS internally, and plugin name, which is seen by users. References to plugin is usually done by plugin id.

I will try to dig in to see what is the issue here.

JanCaha commented 2 years ago

The problem here is actually in the plugin itself. The way the author loads the algorithms in probably not compatible with qgis_process. I have raised an issue zoran-cuckovic/QGIS-visibility-analysis#59

florisvdh commented 2 years ago

References to plugin is usually done by plugin id.

Thanks!

In the QGIS Python console, it appears the ids can be retrieved as follows (from https://gis.stackexchange.com/a/126531):

>>> qgis.utils.available_plugins
['geopunt4Qgis', 'ViewshedAnalysis', 'cartography_tools', 'quick_map_services', 'grassprovider', 'processing', 'sagaprovider', 'db_manager', 'MetaSearch', 'otbprovider']
JanCaha commented 2 years ago

There is actually more then one issue with the plugin, fortunately all are fairly simple to solve. But all together cause that the the plugin can be loaded from QGIS GUI but not from qgis_process.

florisvdh commented 2 years ago

Thanks to the effort by @JanCaha (https://github.com/zoran-cuckovic/QGIS-visibility-analysis/issues/59) this has been solved quite some time ago in the plugin.

For now (but see #93), and at its current version (ViewshedAnalysis 1.8), the plugin can be loaded (if not yet loaded) and its algorithms accessed as follows:

library(qgisprocess)
#> Using 'qgis_process' in the system PATH.
#> QGIS version: 3.26.3-Buenos Aires
#> Configuration loaded from '~/.cache/R-qgisprocess/cache-0.0.0.9000.rds'
#> Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.
#> >>> If you need another installed QGIS version, run `qgis_configure()`;
#>     see its documentation if you need to preset the path of qgis_process.
#> - Using JSON for input serialization.
#> - Using JSON for output serialization.

qgis_providers() # plugin is missing
#>             provider    provider_title
#> 1                 3d         QGIS (3D)
#> 2   cartographytools Cartography tools
#> 7               gdal              GDAL
#> 63            grass7             GRASS
#> 367           native QGIS (native c++)
#> 609              otb               OTB
#> 718             qgis              QGIS
#> 768             saga              SAGA

# is plugin available?

cat(qgis_run("plugins")$stdout)
#> Available plugins
#> (* indicates loaded plugins which implement Processing providers)
#> 
#>   ViewshedAnalysis
#> * cartography_tools
#> * grassprovider
#> * processing
#> * sagaprovider
#> * otbprovider

# enable plugin:
cat(qgis_run(c("plugins", "enable", "ViewshedAnalysis"))$stdout)
#> Enabling plugin: "ViewshedAnalysis"
#> Enabled ViewshedAnalysis (Visibility Analysis)
#> 
#> Available plugins
#> (* indicates enabled plugins which implement Processing providers)
#> 
#> * ViewshedAnalysis
#> * cartography_tools
#> * grassprovider
#> * processing
#> * sagaprovider
#> * otbprovider

qgis_providers() # this just list plugins known in qgisprocess's cache
#>             provider    provider_title
#> 1                 3d         QGIS (3D)
#> 2   cartographytools Cartography tools
#> 7               gdal              GDAL
#> 63            grass7             GRASS
#> 367           native QGIS (native c++)
#> 609              otb               OTB
#> 718             qgis              QGIS
#> 768             saga              SAGA

qgis_configure() # update the cache
#> getOption('qgisprocess.path') was not found.
#> Sys.getenv('R_QGISPROCESS_PATH') was not found.
#> Trying 'qgis_process' on PATH...
#> Success!
#> QGIS version: 3.26.3-Buenos Aires
#> Saving configuration to '~/.cache/R-qgisprocess/cache-0.0.0.9000.rds'
#> Metadata of 1137 algorithms queried and stored in cache.
#> Run `qgis_algorithms()` to see them.
#> - Using JSON for input serialization.
#> - Using JSON for output serialization.

qgis_providers()
#> # A tibble: 9 × 2
#>   provider         provider_title     
#>   <chr>            <chr>              
#> 1 3d               QGIS (3D)          
#> 2 cartographytools Cartography tools  
#> 3 gdal             GDAL               
#> 4 grass7           GRASS              
#> 5 native           QGIS (native c++)  
#> 6 otb              OTB                
#> 7 qgis             QGIS               
#> 8 saga             SAGA               
#> 9 visibility       Visibility analysis

# available algorithms per provider:
table(qgis_algorithms()$provider_id) |> as.matrix()
#>                  [,1]
#> 3d                  1
#> cartographytools    5
#> gdal               56
#> grass7            304
#> native            242
#> otb               109
#> qgis               50
#> saga              366
#> visibility          4

Created on 2022-10-20 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23) #> os Linux Mint 20 #> 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 2022-10-20 #> pandoc 2.18 @ /usr/lib/rstudio/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0) #> evaluate 0.16 2022-08-09 [1] CRAN (R 4.2.1) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0) #> htmltools 0.5.3 2022-07-18 [1] RSPM (R 4.2.1) #> jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.2.0) #> knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> pillar 1.8.0 2022-07-18 [1] RSPM (R 4.2.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> processx 3.7.0 2022-07-07 [1] RSPM (R 4.2.1) #> ps 1.7.1 2022-06-18 [1] RSPM (R 4.2.1) #> qgisprocess * 0.0.0.9000 2022-09-01 [1] local #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> rappdirs 0.3.3 2021-01-31 [1] CRAN (R 4.2.0) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.2.0) #> rlang 1.0.4 2022-07-12 [1] RSPM (R 4.2.1) #> rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> stringi 1.7.8 2022-07-11 [1] RSPM (R 4.2.1) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0) #> tibble 3.1.8 2022-07-22 [1] RSPM (R 4.2.1) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> vctrs 0.4.1 2022-04-13 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.32 2022-08-10 [1] CRAN (R 4.2.1) #> yaml 2.3.5 2022-02-21 [1] CRAN (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 #> #> ────────────────────────────────────────────────────────────────────────────── ```
florisvdh commented 2 years ago

With relation to QNEAT3 (https://github.com/paleolimbot/qgisprocess/issues/73#issuecomment-1019114800), this plugin seems currently not supported by qgis_process. For that to happen, it should be specified as a QGIS Processing provider IIUC.

After installing the plugin, the qgis_process command cannot see it indeed:

> cat(qgis_run("plugins")$stdout)
Available plugins
(* indicates loaded plugins which implement Processing providers)

* ViewshedAnalysis
* cartography_tools
* grassprovider
* processing
* sagaprovider
* otbprovider
JanCaha commented 2 years ago

The plugin is not correctly initialized. The processing is setted up in initGui() of the plugin but it should be done in initProcessing() instead. That means that processing tools will only be available if Gui of QGIS exists ;)

florisvdh commented 1 year ago

In https://github.com/root676/QNEAT3/issues/57 this has been solved for QNEAT3 :rocket: ! After installing current master of QNEAT3 in QGIS and reconfiguring qgisprocess (qgis_configure()), its algorithms are available to qgisprocess:

> qgis_configure()
getOption('qgisprocess.path') was not found.
Sys.getenv('R_QGISPROCESS_PATH') was not found.
Trying 'qgis_process' on PATH...
Success!
QGIS version: 3.26.3-Buenos Aires
Saving configuration to '~/.cache/R-qgisprocess/cache-0.0.0.9000.rds'
Metadata of 1151 algorithms queried and stored in cache.
Run `qgis_algorithms()` to see them.
- Using JSON for input serialization.
- Using JSON for output serialization.
>
> subset(qgis_algorithms(), provider == "qneat3")[,1:3]
# A tibble: 14 × 3
   provider provider_title                         algorithm                             
   <chr>    <chr>                                  <chr>                                 
 1 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:OdMatrixFromLayersAsLines      
 2 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:OdMatrixFromLayersAsTable      
 3 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:OdMatrixFromPointsAsCsv        
 4 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:OdMatrixFromPointsAsLines      
 5 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:OdMatrixFromPointsAsTable      
 6 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaascontoursfromlayer     
 7 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaascontoursfrompoint     
 8 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaasinterpolationfromlayer
 9 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaasinterpolationfrompoint
10 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaaspointcloudfromlayer   
11 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaaspointcloudfrompoint   
12 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaaspolygonsfromlayer     
13 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:isoareaaspolygonsfrompoint     
14 qneat3   QNEAT3 - Qgis Network Analysis Toolbox qneat3:shortestpathpointtopoint