Skip to main content
replaced http://gis.stackexchange.com/ with https://gis.stackexchange.com/
Source Link

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts.

This answer is based on answers to Importing qgis.core when writing standalone PyQGIS scriptImporting qgis.core when writing standalone PyQGIS script and to Error: Algorithm not foundError: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

I suggest you to follow the work flow given in Importing qgis.core when writing standalone PyQGIS scriptImporting qgis.core when writing standalone PyQGIS script to enable your QGIS libraries in your OSGeo4W Shell. Once you have your QGIS libraries working properly, we can proceed to the 2nd part of your question: running processing algorithms in a standalone PyQGIS script.

I've modified your original script a bit and tested it on Windows 7 and GNU/Linux. I use processing version 2.2.0-2 and suggest you to use this version, which is the current one at the moment of writing the answer.

import os, sys, glob

# Prepare the environment
from qgis.core import * # qgis.core must be imported before PyQt4.QtGui!!!
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Brighton\\apps\\qgis", True) # The True value is important
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare processing framework 
sys.path.append( home + '\.qgis2\python\plugins' )
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

def run():
    outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
    #   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):        
        outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)

run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Note that I have taken the grid creation out of the for loop, as you don't really need a new grid to perform each clip.

This should do the trick!

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts.

This answer is based on answers to Importing qgis.core when writing standalone PyQGIS script and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

I suggest you to follow the work flow given in Importing qgis.core when writing standalone PyQGIS script to enable your QGIS libraries in your OSGeo4W Shell. Once you have your QGIS libraries working properly, we can proceed to the 2nd part of your question: running processing algorithms in a standalone PyQGIS script.

I've modified your original script a bit and tested it on Windows 7 and GNU/Linux. I use processing version 2.2.0-2 and suggest you to use this version, which is the current one at the moment of writing the answer.

import os, sys, glob

# Prepare the environment
from qgis.core import * # qgis.core must be imported before PyQt4.QtGui!!!
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Brighton\\apps\\qgis", True) # The True value is important
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare processing framework 
sys.path.append( home + '\.qgis2\python\plugins' )
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

def run():
    outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
    #   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):        
        outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)

run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Note that I have taken the grid creation out of the for loop, as you don't really need a new grid to perform each clip.

This should do the trick!

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts.

This answer is based on answers to Importing qgis.core when writing standalone PyQGIS script and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

I suggest you to follow the work flow given in Importing qgis.core when writing standalone PyQGIS script to enable your QGIS libraries in your OSGeo4W Shell. Once you have your QGIS libraries working properly, we can proceed to the 2nd part of your question: running processing algorithms in a standalone PyQGIS script.

I've modified your original script a bit and tested it on Windows 7 and GNU/Linux. I use processing version 2.2.0-2 and suggest you to use this version, which is the current one at the moment of writing the answer.

import os, sys, glob

# Prepare the environment
from qgis.core import * # qgis.core must be imported before PyQt4.QtGui!!!
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Brighton\\apps\\qgis", True) # The True value is important
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare processing framework 
sys.path.append( home + '\.qgis2\python\plugins' )
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

def run():
    outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
    #   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):        
        outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)

run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Note that I have taken the grid creation out of the for loop, as you don't really need a new grid to perform each clip.

This should do the trick!

Bounty Ended with 100 reputation awarded by Joseph
added 4 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts.

This answer is based on answers to Importing qgis.core when writing standalone PyQGIS script and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

I suggest you to follow the work flow given in Importing qgis.core when writing standalone PyQGIS script to enable your QGIS libraries in your OSGeo4W Shell. Once you have your QGIS libraries working properly, we can proceed to the 2nd part of your question: running processing algorithms in a standalone PyQGIS script.

I've modified your original script a bit and tested it on Windows 7 and GNU/Linux. I use processing version 2.2.0-2 and suggest you to use this version, which is the current one at the moment of writing the answer.

import os, sys, glob

# Prepare the environment
from qgis.core import * # qgis.core must be imported before PyQt4.QtGui!!!
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Brighton\\apps\\qgis", True) # The True value is important
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare processing framework 
sys.path.append( home + '\.qgis2\python\plugins' )
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

def run():
    outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
    #   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):        
        outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)

run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Note that I have taken the grid creation out of the for loop, as you don't really need a new grid to perform clipseach clip.

This should do the trick!

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts.

This answer is based on answers to Importing qgis.core when writing standalone PyQGIS script and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

I suggest you to follow the work flow given in Importing qgis.core when writing standalone PyQGIS script to enable your QGIS libraries in your OSGeo4W Shell. Once you have your QGIS libraries working properly, we can proceed to the 2nd part of your question: running processing algorithms in a standalone PyQGIS script.

I've modified your original script a bit and tested it on Windows 7 and GNU/Linux. I use processing version 2.2.0-2 and suggest you to use this version, which is the current one at the moment of writing the answer.

import os, sys, glob

# Prepare the environment
from qgis.core import * # qgis.core must be imported before PyQt4.QtGui!!!
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Brighton\\apps\\qgis", True) # The True value is important
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare processing framework 
sys.path.append( home + '\.qgis2\python\plugins' )
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

def run():
    outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
    #   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):        
        outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)

run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Note that I have taken the grid creation out of the for loop, as you don't really need a new grid to perform clips.

This should do the trick!

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts.

This answer is based on answers to Importing qgis.core when writing standalone PyQGIS script and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

I suggest you to follow the work flow given in Importing qgis.core when writing standalone PyQGIS script to enable your QGIS libraries in your OSGeo4W Shell. Once you have your QGIS libraries working properly, we can proceed to the 2nd part of your question: running processing algorithms in a standalone PyQGIS script.

I've modified your original script a bit and tested it on Windows 7 and GNU/Linux. I use processing version 2.2.0-2 and suggest you to use this version, which is the current one at the moment of writing the answer.

import os, sys, glob

# Prepare the environment
from qgis.core import * # qgis.core must be imported before PyQt4.QtGui!!!
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Brighton\\apps\\qgis", True) # The True value is important
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare processing framework 
sys.path.append( home + '\.qgis2\python\plugins' )
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

def run():
    outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
    #   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):        
        outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)

run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Note that I have taken the grid creation out of the for loop, as you don't really need a new grid to perform each clip.

This should do the trick!

Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts.

This answer is based on answers to Importing qgis.core when writing standalone PyQGIS script and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion.

I suggest you to follow the work flow given in Importing qgis.core when writing standalone PyQGIS script to enable your QGIS libraries in your OSGeo4W Shell. Once you have your QGIS libraries working properly, we can proceed to the 2nd part of your question: running processing algorithms in a standalone PyQGIS script.

I've modified your original script a bit and tested it on Windows 7 and GNU/Linux. I use processing version 2.2.0-2 and suggest you to use this version, which is the current one at the moment of writing the answer.

import os, sys, glob

# Prepare the environment
from qgis.core import * # qgis.core must be imported before PyQt4.QtGui!!!
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Brighton\\apps\\qgis", True) # The True value is important
QgsApplication.initQgis()

from os.path import expanduser
home = expanduser("~")

#   Folder path of the Results for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

# Prepare processing framework 
sys.path.append( home + '\.qgis2\python\plugins' )
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

def run():
    outputs_1=general.runalg("qgis:creategrid", 1000, 1000, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405',  None)
    #   Set directory, search for all polygon .shp files and run the Create Grid and Clip algorithms then output results into Results folder
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"):        
        outputs_2=general.runalg("qgis:clip", outputs_1['SAVENAME'], fname, path_res  + "/"+ fname)

run()

QgsApplication.exitQgis()
#   Remove the above line when running in QGIS

Note that I have taken the grid creation out of the for loop, as you don't really need a new grid to perform clips.

This should do the trick!