3

I am trying to run the script generated by my graphical modeller through QGIS script. But I get the following error.

# Prepare the environment
import sys
from qgis.core import QgsApplication
#from PyQt4.QtGui import QApplication
qgs = QgsApplication([], False)
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
# Prepare processing framework 
sys.path.append('/usr/share/qgis/python/plugins/')
#sys.path.append('usr/lib/qgis/plugins')
from processing.core.Processing import processing
#processing.initialize()
processing.run('script:AppPointsToGrid', 
{'inputdatapoints':'file:///home/vinura/Documents/L900_10000.csv?type=csv&detectTypes=yes&xField=Longitude&yField=Latitude&crs=EPSG:4244&spatialIndex=no&subsetIndex=no&watchFile=no','Output':'/home/vinura/Documents/4.geojson'})
# Exit applications
QgsApplication.exitQgis()
QApplication.exit()

Error

python3 Script2.py
Traceback (most recent call last):
 File "Script2.py", line 15, in <module>
 processing.run("script:PointsToGrid", {'inputdatapoints':'file:///home/vinura/Documents/L900_10000.csv?type=csv&detectTypes=yes&xField=Longitude&yField=Latitude&crs=EPSG:4244&spatialIndex=no&subsetIndex=no&watchFile=no','Output':'/home/vinura/Documents/4.geojson'})
 File "/usr/share/qgis/python/plugins/processing/tools/general.py", line 106, in run
 return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
 File "/usr/share/qgis/python/plugins/processing/core/Processing.py", line 125, in runAlgorithm
 raise QgsProcessingException(msg)
_core.QgsProcessingException: Error: Algorithm script:PointsToGrid not found

Simply, the script doesn't get the script:PointsToGrid I made using graphical modeller.

Germán Carrillo
37.3k5 gold badges127 silver badges182 bronze badges
asked Sep 23, 2019 at 7:57
1
  • 1
    Your script seems to be correct. can you try running from qgis.core import QgsApplication for alg in QgsApplication.processingRegistry().algorithms(): print("{}:{} --> {}".format(alg.provider().name(), alg.name(), alg.displayName())) as suggested here. It should give you a list of available processing algorithms, check that your script is there. Commented Sep 23, 2019 at 15:01

1 Answer 1

4

Good question. It is not obvious, but you need to copy your script algorithms to a specific folder to make your standalone script able to run them.

You can easily see where your script algorithms should be located by adding a couple of lines to your standalone script:

from processing.script import ScriptUtils
print("Folder for script algorithms:", ScriptUtils.scriptsFolders())

Once you copy your script algorithms there, run again your standalone script and it should be able to run them.


NOTE: If your script algorithm makes use of other algorithms not installed by default by Processing, you need to add them. See Using QGIS3 Processing algorithms from standalone PyQGIS scripts (outside of GUI) for an example to make Native C++ algorithms available for your standalone script.

Example

Here is a sample script that you can take as a reference (adjust paths to your own QGIS location):

import sys
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms
# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix
QgsApplication.setPrefixPath('/docs/dev/qgis/QGIS/build_20190918/output', True)
qgs = QgsApplication([], False)
qgs.initQgis()
# Append the path where processing plugin can be found
sys.path.append('/docs/dev/qgis/QGIS/build_20190918/output/python/plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
from processing.script import ScriptUtils
print("[INFO] Folder for script algorithms:", ScriptUtils.scriptsFolders())
print("[INFO] Script algorithms available:", 
 [s.displayName() for s in QgsApplication.processingRegistry().providerById("script").algorithms()])
params = {'inputlayer':'/geodata/points.shp',
 'OUTPUT':'TEMPORARY_OUTPUT'}
res = processing.run("script:my_model", params)
output_layer = res['OUTPUT'] # Access your output layer
print(output_layer.featureCount())
answered Sep 24, 2019 at 3:53
1
  • This worked, but I had to import my CSV file first and convert it to a vector then feed it to the script. Anyway THANK YOU !!!!! Commented Sep 24, 2019 at 6:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.