3

According to Using QGIS3 Processing algorithms from standalone PyQGIS scripts (outside of GUI) I try to implement the existing "Split Lines with Lines" algorithm into a QGIS Plugin.

To do this I need the path, where the algorithm file is stored, but I do not manage to find it. Can you tell me where it is stored and if there is an easy way to find out, where algorithms are stored?

3
  • C:\OSGeo4W64\apps\qgis\python\plugins\processing\algs\saga\description\SplitLineswithLines.txt
    – neogeomat
    Oct 24 '18 at 19:34
  • Ok I found that one too. But is it the actual algorithm? Since it contains no code, I thought it's just metadata or something.
    – gHupf
    Oct 24 '18 at 20:52
  • I think the codes are inside the files out of that folders, but I couldn't find them. Maybe you can open the folders in a text editor and perform a folder wide search.
    – neogeomat
    Oct 25 '18 at 16:58
4

A solution is to call SAGA through the processing algorithm available in the Processing Toolbox. It's what you see below. I also answer in a second part why you do not find directly the script.

split_layer = QgsProject.instance().mapLayersByName("layername name lines input for spliting")[0]
lines_layer = QgsProject.instance().mapLayersByName("layername name for spliting")[0]

result = processing.run("saga:splitlineswithlines", {
    'LINES': lines_layer,
    'SPLIT': split_layer,
    'INTERSECT': 'TEMPORARY_OUTPUT',
    'OUTPUT': 1
})

QgsProject.instance().addMapLayer(QgsVectorLayer(result['INTERSECT'], 'output intersect', 'ogr'))

For SAGA, algorithms are created from configuration files e.g https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py#L114

You see in above link that SagaAlgorithmProvider use it method loadAlgorithms to list description files, create each time an instance SagaAlgorithm and then add it to the list of SAGA available algorithms.

Grass does the same as SAGA: it uses metatada files in a description directory to create it algorithms list.

GDAL and QGIS algorithms do not take the same approach. One file is more or less one algorithm (maybe some exceptions, not sure here)

See below a shortened output of algs directory

/usr/share/qgis/python/plugins/processing/algs/
├── gdal
│   ├── aspect.py
│   ├── AssignProjection.py
│   ├── Buffer.py
│   ├── __init__.py
│   ├── ui
│   │   ├── __init__.py
│   │   └── RasterOptionsWidget.py
│   ├── viewshed.py
│   └── warp.py
├── grass7
│   ├── description
│   │   ├── i.albedo.txt
│   │   ├── i.aster.toar.txt
│   │   ├── i.atcorr.txt
│   │   ├── i.biomass.txt
│   │   └── v.what.vect.txt
│   ├── ext
│   │   ├── i_albedo.py
│   │   └── v_what_vect.py
│   ├── Grass7AlgorithmProvider.py
│   ├── Grass7Algorithm.py
│   ├── grass7.txt
│   ├── Grass7Utils.py
│   └── __init__.py
├── help
│   ├── __init__.py
│   ├── qgis.yaml
│   └── saga.yaml
├── __init__.py
├── otb
│   ├── __init__.py
│   ├── OtbAlgorithmProvider.py
│   ├── OtbAlgorithm.py
│   ├── OtbChoiceWidget.py
│   └── OtbUtils.py
├── qgis
│   ├── BarPlot.py
│   ├── BasicStatistics.py
│   ├── BoxPlot.py
│   ├── Buffer.py
│   ├── ui
│   │   ├── AddNewExpressionDialog.ui
│   │   ├── ExecuteSQLWidgetBase.ui
│   │   ├── __init__.py
│   │   └── ReliefColorsWidget.py
│   ├── UniqueValues.py
│   └── voronoi.py
└── saga
    ├── description
    │   ├── AccumulatedCost(Anisotropic).txt
    │   ├── AccumulatedCost(Isotropic).txt
    │   ├── AccumulatedCost.txt
    │   ├── ZonalGridStatistics.txt
    │   └── ZonalMultipleRegressionAnalysis(PointsandPredictorGrids).txt
    ├── ext
    │   ├── __init__.py
    │   └── supervisedclassification.py
    ├── __init__.py
    ├── SagaAlgorithmBase.py
    ├── SagaAlgorithmProvider.py
    ├── SagaAlgorithm.py
    ├── SagaDescriptionCreator.py
    ├── SagaNameDecorator.py
    ├── SagaParameters.py
    ├── SagaUtils.py
    ├── SplitRGBBands.py
    └── versioncheck.py
1

To use the processing module in a stand-alone Python script, I execute this code:

import sys

# qgis
import qgis.core
import PyQt5.QtCore
import gdal
import qgis.PyQt.QtCore

# for function gdal_merge
sys.path.append(r'C:\OSGeo4W64\bin')
import gdal_merge

# initializing processing module
qgis.core.QgsApplication.setPrefixPath(r'C:\OSGeo4W64\apps\qgis', True)
qgs = qgis.core.QgsApplication([], False)
qgs.initQgis()
sys.path.append(r'C:\OSGeo4W64\apps\qgis\python\plugins')
import processing
processing.core.Processing.Processing.initialize()
qgis.core.QgsApplication.processingRegistry().addProvider(qgis.analysis.QgsNativeAlgorithms())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.