1

I want to create a standalone python script using QGIS python API (PYQGIS) for education. First I create a model and export that model to python script.

original python script from the export :

##=name
##raster=raster
##stat=output html
outputs_QGISRASTERLAYERSTATISTICS_1=processing.runalg('qgis:rasterlayerstatistics', raster,stat)

Next I read this Using PyQGIS in standalone scripts and I tried to follow this tutorial to create my script.

Final script :

import sys
from qgis.core import QgsApplication
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework 
sys.path.append('/home/username/.qgis2/python')
from processing.core.Processing import Processing
dem='aster.tif'
slope='slope.tif'
aspect='aspect.tif'
outputs_SAGASLOPEASPECTCURVATURE_1=processing.runalg('saga:slopeaspectcurvature', dem,6,1,1,slope,aspect,None,None,None,None,None,None,None,None,None,None)
Processing.initialize()

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()

If I run this script it works fine without error but I don't take some export like aspect and slope (these can't be found?). Any idea why I can't take exports ?

I use Ubuntu and python 2.7 and QGIS 2.18.1.


i try to this script :

import sys
from qgis.core import QgsApplication
sys.path.append('/usr/share/qgis/python/plugins')
import processing
a=processing.alglist()
print a
processing.alghelp("saga:slopeaspectcurvature")

and i take that print any idea why ?

Algorithm not found

but if i use this module in QGIS work fine.

  • Did you define the full path for all your export files? i.e. aspect="path/to/aspect.tif"? – Joseph Dec 12 '16 at 10:20
  • yes i have define paths and no change,no exports – Mar Dec 12 '16 at 15:37
  • Perhaps your Processing is installed in /usr/share/qgis/python/plugins, so you might need to use sys.path.append('/usr/share/qgis/python/plugins') instead. – Germán Carrillo Dec 15 '16 at 11:53
  • not work again,work without error but i dont take exports,dont create the exports – Mar Dec 18 '16 at 22:28
  • maybe i need more commands to import raster layer in pyqgis ? – Mar Dec 18 '16 at 22:42

Your Answer

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

Browse other questions tagged or ask your own question.