The algorithm you are running requires some references to common QGIS components, which are available to plugins via an iface (QgisInterface) object. In this case you need a custom iface object because i) there is no QGIS instance running, ii) you are running a plugin, and iii) the algorithm you are running requires access to QGIS components (e.g. map canvas).
You can emulate the iface object by yourself, but thankfully my custom iface is included in Processing, so we can use it.
However, because of some changes to QgisInterface in the official QGIS project, you would need to either i) edit the file .../plugins/processing/tests/qgis_interface.py
to make it equivalent to this one, or ii) download the file and replace the existing one. I've just changed 5 lines.
This is the working script:
import os, sys, glob
from qgis.core import *
from qgis.gui import QgsMapCanvas
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
from os.path import expanduser
home = expanduser("~")
# Folder path of the Results for shapefiles
path_dir = home + "/Test/"
path_res = path_dir + "/Results"
# Prepare processing framework
sys.path.append( home + '/.qgis2/python/plugins' )
# Get an iface object
canvas = QgsMapCanvas()
from processing.tests.qgis_interface import QgisInterface
iface = QgisInterface( canvas )
# Initialize the Processing plugin passing an iface object
from processing.ProcessingPlugin import ProcessingPlugin
plugin = ProcessingPlugin(iface)
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)
outputs_2=general.runalg("qgis:fieldcalculator", outputs_1['SAVENAME'], 'ID', 1, 10, 0, True, '$rownum', path_res + "/" + "grid.shp")
outputs_3=general.runalg("qgis:deletecolumn", outputs_2['OUTPUT_LAYER'], 'longitude', None)
outputs_4=general.runalg("qgis:deletecolumn", outputs_3['SAVENAME'], 'latitude', path_res + "/" + "grid.shp")
# Paths of the shapefiles in the Result folder with list comprehension
output = [shp for shp in glob.glob(path_res + "*.shp")]
run()
QgsApplication.exitQgis()
app.exit()
I tested it on GNU/Linux succesfully.
This should be enough to get the extent of your Shapefile:
layerPath = path_dir + "Input district shapefile\\" + "District.shp"
extent = QgsVectorLayer( layerPath, '', 'ogr' ).extent()
The result is a QgsRectangle object, which has xMinimum()
, xMaximum()
, yMinimum()
, and yMaximum()
methods.
Best Answer
You are getting the error in line 3
from qgis.utils import QGis
. In QGIS 3, geometry types is specified byQgsWkbTypes
enum incore
library. Thus, removefrom qgis.utils import QGis
Use script in the following way. Notice highlighted lines with
####
.