grass7.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. A short guide for creating and editing GRASS GIS 7 algorithms:
  2. -----------------------------------------------------------------------
  3. Each GRASS command, to be executed from a processing framework element such as the toolbox
  4. or the model designer, needs to be described to let it know the inputs
  5. required by the commands, the output it generates and the parameters that are
  6. used to configure it. Each command is described in a separate text file, although
  7. some commands might be split in several algorithms, thus needing several files
  8. and adding more than one new entry to the algorithms list. Splitting
  9. a grass command is usually done because the processing provider does not support optional
  10. parameters, so it will call GRASS using all parameters defined in the description
  11. file.
  12. Here is an explanation of the content of these descriptions files, so you can
  13. create you own ones or edit current ones to improve them.
  14. Each file starts with three lines containing:
  15. - The name of the grass command to call to execute the algorithm (e.g. v.buffer)
  16. - The description of the algorithm to show to the user. For split commands you must
  17. include the algorithm id first, e.g.:
  18. r.sun.insoltime - Solar irradiance and irradiation model (daily sums).
  19. and
  20. r.sun.incidout - Solar irradiance and irradiation model (for the set local time).
  21. - The name of the group where you want the command to appear
  22. After these three lines, a variable number of lines appear, describing all inputs
  23. and outputs. Here is a brief explanation of the format of these lines, depending
  24. on the type of parameter or output to be described. All declarations are contained
  25. in a single line, with elements separated by the symbol "|"
  26. - A raster layer
  27. QgsProcessingParameterRasterLayer|[name of GRASS parameter]|[description of parameter to show]|[Default value, or None]|[True/False, indicating if the parameter is optional or not]
  28. Example: QgsProcessingParameterRasterLayer|base|Name of input raster map|None|False
  29. - A vector layer
  30. QgsProcessingParameterFeatureSource|[name of GRASS parameter]|[description of parameter to show]|[A number indicating the type of geometry]|[Default value, or None]|[True/False, indicating if the parameter is optional or not]
  31. Example: QgsProcessingParameterFeatureSource|input|Name of input vector map|-1|None|False
  32. To indicate the type of geometry, use the following values:
  33. -1: any geometry
  34. 0: points
  35. 1: lines
  36. 2: polygons
  37. - Multiple layers
  38. QgsProcessingParameterMultipleLayers|[name of GRASS parameter]|[description of parameter to show]|[A number indicating the type of geometry]|[Default value, or None]|[True/False, indicating if the parameter is optional or not]
  39. Example: QgsProcessingParameterMultipleLayers|input|Input rasters|3|None|False
  40. To indicate the type of geometry, use the following values:
  41. -1: any vector geometry
  42. 0: points
  43. 1: lines
  44. 2: polygons
  45. 3: raster
  46. - A file
  47. QgsProcessingParameterFile|[name of GRASS parameter]|[description of parameter to show]|QgsProcessingParameterFile.File|[file extension|[Default value, or None]|[True/False, indicating if the parameter is optional or not]
  48. - A numerical value
  49. QgsProcessingParameterNumber|[name of GRASS parameter]|[description of parameter to show]|QgsProcessingParameterNumber.Integer or QgsProcessingParameterNumber.Double|[default value]|[True/False, indicating if the parameter is optional or not]|[min value]|[max value]
  50. "None" can be used for both min and max values to indicate that there is no lower
  51. or upper limit.
  52. Example: QgsProcessingParameterNumber|levels|levels|QgsProcessingParameterNumber.Integer|32|False|1|256
  53. - A numerical range
  54. QgsProcessingParameterRange|[name of GRASS parameter]|[description of parameter to show]|QgsProcessingParameterNumber.Integer or QgsProcessingParameterNumber.Double|[default minimum and maximum values, separated by comma]|[True/False, indicating if the parameter is optional or not]
  55. - A string
  56. QgsProcessingParameterString|[name of GRASS parameter]|[description of parameter to show]|[default value]|[True/False, indicating if the parameter is optional or not]
  57. - A value to select from a list
  58. QgsProcessingParameterEnum|[name of GRASS parameter]|[description of parameter to show]|[list of possible values, separated by semicolons]|[True/False, indicating whether more than one value can be selected (allowMultiple)]|[zero-based index of default value]|[True/False, indicating if the parameter is optional or not]
  59. - A boolean value
  60. QgsProcessingParameterBoolean|[name of GRASS parameter]|[description of parameter to show]|[default value]|[True/False, indicating if the parameter is optional or not]
  61. Example: QgsProcessingParameterBoolean|-p|Output values as percentages|False|True
  62. - A pair of coordinates:
  63. QgsProcessingParameterPoint[name of GRASS parameter]|[description of parameter to show]|[default value]|[True/False, indicating if the parameter is optional or not]
  64. Example: QgsProcessingParameterPoint|coordinates|The coordinate of the center (east,north)|0,0|False
  65. - A rectangular map extent:
  66. QgsProcessingParameterExtent|[name of GRASS parameter]|[description of parameter to show]|[default value]|[True/False, indicating if the parameter is optional or not]
  67. Example: QgsProcessingParameterExtent|bbox|Bounding box for selecting features|None|True
  68. - A crs
  69. QgsProcessingParameterCrs|[name of GRASS parameter]|[description of parameter to show]|[default value]|[True/False, indicating if the parameter is optional or not]
  70. Example: QgsProcessingParameterCrs|crs|New coordinate reference system|None|False
  71. - Outputs
  72. Raster outputs are added with the following syntax:
  73. QgsProcessingParameterRasterDestination|[name of GRASS output]|[description of output to show]|[Default value, or None]|[True/False, indicating if the parameter is optional or not]
  74. Example: QgsProcessingParameterRasterDestination|length_slope|Slope length and steepness (LS) factor for USLE|None|True
  75. Vector outputs are added with the following syntax:
  76. QgsProcessingParameterVectorDestination|[name of GRASS output]|[description of output to show]|vector type|[Default value, or None]|[True/False, indicating if the parameter is optional or not]
  77. The following types are available
  78. QgsProcessing.TypeVectorPoint
  79. QgsProcessing.TypeVectorLine
  80. QgsProcessing.TypeVectorPolygon
  81. QgsProcessing.TypeVectorAnyGeometry
  82. Example: QgsProcessingParameterVectorDestination|flowline|Flow line|QgsProcessing.TypeVectorLine|None|True
  83. File outputs which are not layers or tables of a format supported by QGIS are added with the following syntax:
  84. QgsProcessingParameterFileDestination|[name of GRASS output]|[description of output to show]|[file type]|[Default value, or None]|[True/False, indicating if the parameter is optional or not]
  85. Example: QgsProcessingParameterFileDestination|reportfile|Final Report File|Txt files (*.txt)|None|True
  86. A folder:
  87. QgsProcessingParameterFolderDestination|[name of GRASS parameter]|[description of parameter to show]|[default value]|[True/False, indicating if the parameter is optional or not]
  88. Example: QgsProcessingParameterFolderDestination|output_dir|Output Directory|None|False
  89. - Advanced parameters
  90. to tag a parameter as "advanced", just add "*" before its declaration. For instance:
  91. *ParameterBoolean|-c|-c|True
  92. RELOADING ALGORITHM DESCRIPTIONS
  93. ---------------------------------------
  94. You do not need to restart QGIS after editing or creating an algorithm description - simply click the wrench icon in the processing toolbox, then click OK, and QGIS will reload the descriptions.
  95. ADVANCED PROCESSING
  96. --------------------------
  97. To save the console output from GRASS to file, simply create a QgsProcessingParameterFileDestination parameter named 'html'
  98. Example: QgsProcessingParameterFileDestination|html|List of addons|Html files (*.html)|addons_list.html|False
  99. To add additional logic to an algorithm, like a preliminary check on data, the use of more than one GRASS command,
  100. or a transformation of output data, then you need to use the ext mechanism.
  101. There are 4 different levels where you can add logic:
  102. - Checking the input parameters, e.g. if you want to verify that two mutually exclusive options have not been both enabled.
  103. - Processing inputs import: if you need to do more than importing input layers.
  104. - Processing the command itself: if you need to chain more than one GRASS command for your algorithm.
  105. - Processing the outputs: if you need to do special things before exporting layers or if you need special export methods.
  106. Whenever you want to add some logic on one (or more) level(s), you have to create a .py file named according to the algorithm name in python/plugins/grassprovider/ext, replacing '.' with '_'.
  107. Then you need to create methods using the respective names:
  108. - Input parameters: checkParameterValuesBeforeExecuting
  109. - Inputs import: processInputs
  110. - Command: processCommand
  111. - Outputs: processOutputs
  112. If there is a Python file with the algorithm name in the ext directory, methods will be imported from the file and run instead of the common methods (there are "standard" processCommand/processInputs/processOutputs/checkParameterValuesBeforeExecuting methods in the code of the GRASS provider for QGIS Processing, in python/plugins/grassprovider/Grass7Algorithm.py).
  113. If we take the example of v.what.rast, there is an ext file: ext/v_what_rast.py.
  114. In this file there is a processCommand method. It just launches the standard processCommand but with the delOutputs option set to True (we do not want to have standard outputs).
  115. Then there is also a customized processOutputs which exports the input vector as an output for QGIS. We need to do this because v.what.rast modifies values directly in the input vector layer instead of generating a new output, so we have to build this output ourself.
  116. If you want to do special things in the ext mechanism, you will need to read (and understand) the GRASS provider code standard methods in Grass7Algorithm.py.