I have the following piece of code which I execute within QGIS 2.8.6
import processing
import ogr
restrLayer = QgsVectorLayer("/home/usr/Desktop/trial/EligibleAreas_polygons_filtered.shp", "EligibleAreas_polygons_filtered", "ogr")
if not restrLayer.isValid():
print "Restrictions Layer Failed To Load"
QgsMapLayerRegistry.instance().addMapLayer(restrLayer)
cellsize = 1000
xmin = (restrLayer.extent().xMinimum())
xmax = (restrLayer.extent().xMaximum())
ymin = (restrLayer.extent().yMinimum())
ymax = (restrLayer.extent().yMaximum())
extent = str(xmin)+ ',' + str(xmax)+ ',' +str(ymin)+ ',' +str(ymax)
grid1 = "/home/usr/Desktop/trial/grid1.shp"
processing.runalg('qgis:vectorgrid', extent, cellsize, cellsize, 0, grid1)
and it works fine. When I create a standalone script and try to do the exact same thing outside of QGIS I have the following code:
import qgis
from osgeo import gdal
from osgeo import ogr
from gdalconst import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.analysis import *
from qgis.gui import *
import sys
import glob
import re
import os, fnmatch
import numpy as np
import numpy.ma as ma
import math
qgisprefix = '/usr'
# configure paths for QGIS
os.environ['PATH'] = qgisprefix+'/bin'
os.environ['LD_LIBRARY_PATH'] = qgisprefix+'/lib'
sys.path.insert(0, qgisprefix+'/share/qgis/python')
sys.path.insert(1, qgisprefix+'/share/qgis/python/plugins')
# disable QGIS debug messages
os.environ['QGIS_DEBUG'] = '-1'
# configure QGIS paths
QgsApplication.setPrefixPath(qgisprefix, True)
# initalise QGIS (load providers)
QgsApplication.initQgis()
# create a new application object
# without this importing processing causes the following error:
# QPixmap: Must construct a QApplication before a QPaintDevice
app = QgsApplication([], True)
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *
print Processing.getAlgorithm("qgis:vectorgrid")
#add the restrictions layer
restrLayer = QgsVectorLayer("/home/usr/Desktop/trial/EligibleAreas_polygons_filtered.shp", "EligibleAreas_polygons_filtered", "ogr")
if not restrLayer.isValid():
print "Restrictions Layer Failed To Load"
QgsMapLayerRegistry.instance().addMapLayer(restrLayer)
cellsize = 1000
xmin = (restrLayer.extent().xMinimum())
xmax = (restrLayer.extent().xMaximum())
ymin = (restrLayer.extent().yMinimum())
ymax = (restrLayer.extent().yMaximum())
extent = str(xmin)+ ',' + str(xmax)+ ',' +str(ymin)+ ',' +str(ymax)
grid1 = "/home/usr/Desktop/trial/grid1.shp"
processing.runalg('qgis:vectorgrid', extent, cellsize, cellsize, 0, grid1)
I get no output, so no grid1.shp. From print Processing.getAlgorithm("qgis:vectorgrid")
I get:
ALGORITHM: Vector grid
EXTENT <ParameterExtent>
STEP_X <ParameterNumber>
STEP_Y <ParameterNumber>
TYPE <ParameterSelection>
OUTPUT <OutputVector>
so the needed algorithm from processing is found but for some reason the output layer is not created. I also checked the extent I get from the standalone script and it is the same as what I have in the python console (so the layer is loaded correctly). What could be the reason why I am not getting an output?
grid1 = ""/home/usr/Desktop/trial/grid1.shp"
.processing.runalg('qgis:vectorgrid', extent, cellsize, cellsize, 0, grid1)
withgeneral.runalg('qgis:vectorgrid', extent, cellsize, cellsize, 0, grid1)
import
ordering according to this post: Why the order of imports matters in a standalone PyQGIS processing script?. In other words, putfrom PyQt4.QtGui import *
andfrom PyQt4.QtCore import *
afterfrom qgis.gui import *
. You could also try movingQgsApplication.initQgis()
afterapp = QgsApplication([], True)
.