Skip to main content
added 21 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

Why it fails

qgis:vectorgrid algorithm is tied to the QGIS application because it uses the iface object (see line 76 of the script). That means itIt depends on the Coordinate Reference System (CRS) from an existing map canvas. In the error you were getting ('NoneType' object has no attribute 'mapCanvas' See log for more details.) NoneType object is the iface object, which does not exist because QGIS is not running.

In a standalone scenario we don't usually have a map canvas nor an iface object available (although it is certainly possible, see for example this post).

Solution

Fortunately, QGIS offers an analog algorithm to create vector grids, it is called qgis:creategrid (also available in QGIS 2.8). The Create Grid algorithm is much better than qgis:vectorgrid because it doesn't depend on an existing map canvas. Instead, it receives the CRS of the output grid as an additional parameter. You could get such CRS from your restrLayer layer and call the algorithm in this way:

crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

NOTE: The parameter 1 corresponds to a rectangular (polygon) grid. An example of the crs parameter may be 'EPSG:3115'.

I have tested the following standalone script using QGIS 2.14.8, I hope it also works for you (QGIS v2.8.6):

# Prepare the environment
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import general

restrLayer = QgsVectorLayer("/my/own/data/file.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 = "/tmp/grid1.shp"
crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

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

qgis:vectorgrid algorithm is tied to the QGIS application because it uses the iface object (see line 76 of the script). That means it depends on the Coordinate Reference System (CRS) from an existing map canvas. In the error you were getting ('NoneType' object has no attribute 'mapCanvas' See log for more details.) NoneType object is the iface object, which does not exist because QGIS is not running.

In a standalone scenario we don't usually have a map canvas nor an iface object available (although it is certainly possible, see for example this post).

Fortunately, QGIS offers an analog algorithm to create vector grids, it is called qgis:creategrid (also available in QGIS 2.8). The Create Grid algorithm is much better than qgis:vectorgrid because it doesn't depend on an existing map canvas. Instead, it receives the CRS of the output grid as an additional parameter. You could get such CRS from your restrLayer layer and call the algorithm in this way:

crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

NOTE: The parameter 1 corresponds to a rectangular (polygon) grid. An example of the crs parameter may be 'EPSG:3115'.

I have tested the following standalone script using QGIS 2.14.8, I hope it also works for you (QGIS v2.8.6):

# Prepare the environment
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import general

restrLayer = QgsVectorLayer("/my/own/data/file.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 = "/tmp/grid1.shp"
crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

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

Why it fails

qgis:vectorgrid algorithm is tied to QGIS application because it uses the iface object (see line 76 of the script). It depends on the Coordinate Reference System (CRS) from an existing map canvas. In the error you were getting ('NoneType' object has no attribute 'mapCanvas' See log for more details.) NoneType object is the iface object, which does not exist because QGIS is not running.

In a standalone scenario we don't usually have a map canvas nor an iface object available (although it is certainly possible, see for example this post).

Solution

Fortunately, QGIS offers an analog algorithm to create vector grids, it is called qgis:creategrid (also available in QGIS 2.8). The Create Grid algorithm is much better than qgis:vectorgrid because it doesn't depend on an existing map canvas. Instead, it receives the CRS of the output grid as an additional parameter. You could get such CRS from your restrLayer layer and call the algorithm in this way:

crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

NOTE: The parameter 1 corresponds to a rectangular (polygon) grid. An example of the crs parameter may be 'EPSG:3115'.

I have tested the following standalone script using QGIS 2.14.8, I hope it also works for you (QGIS v2.8.6):

# Prepare the environment
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import general

restrLayer = QgsVectorLayer("/my/own/data/file.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 = "/tmp/grid1.shp"
crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()
added 52 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

qgis:vectorgrid algorithm is tied to the QGIS application because it uses the iface object (see line 76 of the script). That means it depends on the Coordinate Reference System (CRS) from an existing map canvas. In the error you were getting ('NoneType' object has no attribute 'mapCanvas' See log for more details.) NoneType object is the iface object, which does not exist because QGIS is not running.

In a standalone scenario we don't usually have a map canvas nor an iface object available (although it is certainly possible, see for example this post).

Fortunately, QGIS offers an analog algorithm to create vector grids, it is called qgis:creategrid (also available in QGIS 2.8). The Create Grid algorithm is much better than qgis:vectorgrid because it doesn't depend on an existing map canvas. Instead, it receives the CRS of the output grid as an additional parameter. You could get such CRS from your restrLayer layer and call the algorithm in this way:

crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

NOTE: The parameter 1 corresponds to a rectangular (polygon) grid. An example of the crs parameter may be 'EPSG:3115'.

I have tested the following standalone script using QGIS 2.14.8, I hope it also works for you (QGIS v2.8.6):

# Prepare the environment
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import general

restrLayer = QgsVectorLayer("/my/own/data/file.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 = "/tmp/grid1.shp"
crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

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

qgis:vectorgrid algorithm is tied to the QGIS application because it uses the iface object (see line 76 of the script). That means it depends on the Coordinate Reference System (CRS) from an existing map canvas. In the error you were getting ('NoneType' object has no attribute 'mapCanvas' See log for more details.) NoneType object is the iface object, which does not exist because QGIS is not running.

In a standalone scenario we don't usually have a map canvas nor an iface object available (although it is certainly possible, see for example this post).

Fortunately, QGIS offers an analog algorithm to create vector grids, it is called qgis:creategrid (also available in QGIS 2.8). The Create Grid algorithm is much better than qgis:vectorgrid because it doesn't depend on an existing map canvas. Instead, it receives the CRS of the output grid as an additional parameter. You could get such CRS from your restrLayer layer and call the algorithm in this way:

crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

NOTE: The parameter 1 corresponds to a rectangular (polygon) grid.

I have tested the following standalone script using QGIS 2.14.8, I hope it also works for you (QGIS v2.8.6):

# Prepare the environment
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import general

restrLayer = QgsVectorLayer("/my/own/data/file.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 = "/tmp/grid1.shp"
crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

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

qgis:vectorgrid algorithm is tied to the QGIS application because it uses the iface object (see line 76 of the script). That means it depends on the Coordinate Reference System (CRS) from an existing map canvas. In the error you were getting ('NoneType' object has no attribute 'mapCanvas' See log for more details.) NoneType object is the iface object, which does not exist because QGIS is not running.

In a standalone scenario we don't usually have a map canvas nor an iface object available (although it is certainly possible, see for example this post).

Fortunately, QGIS offers an analog algorithm to create vector grids, it is called qgis:creategrid (also available in QGIS 2.8). The Create Grid algorithm is much better than qgis:vectorgrid because it doesn't depend on an existing map canvas. Instead, it receives the CRS of the output grid as an additional parameter. You could get such CRS from your restrLayer layer and call the algorithm in this way:

crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

NOTE: The parameter 1 corresponds to a rectangular (polygon) grid. An example of the crs parameter may be 'EPSG:3115'.

I have tested the following standalone script using QGIS 2.14.8, I hope it also works for you (QGIS v2.8.6):

# Prepare the environment
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import general

restrLayer = QgsVectorLayer("/my/own/data/file.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 = "/tmp/grid1.shp"
crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

qgis:vectorgrid algorithm is tied to the QGIS application because it uses the iface object (see line 76 of the script). That means it depends on the Coordinate Reference System (CRS) from an existing map canvas. In the error you were getting ('NoneType' object has no attribute 'mapCanvas' See log for more details.) NoneType object is the iface object, which does not exist because QGIS is not running.

In a standalone scenario we don't usually have a map canvas nor an iface object available (although it is certainly possible, see for example this post).

Fortunately, QGIS offers an analog algorithm to create vector grids, it is called qgis:creategrid (also available in QGIS 2.8). The Create Grid algorithm is much better than qgis:vectorgrid because it doesn't depend on an existing map canvas. Instead, it receives the CRS of the output grid as an additional parameter. You could get such CRS from your restrLayer layer and call the algorithm in this way:

crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

NOTE: The parameter 1 corresponds to a rectangular (polygon) grid.

I have tested the following standalone script using QGIS 2.14.8, I hope it also works for you (QGIS v2.8.6):

# Prepare the environment
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsMapLayerRegistry
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import general

restrLayer = QgsVectorLayer("/my/own/data/file.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 = "/tmp/grid1.shp"
crs = restrLayer.crs().authid()
general.runalg('qgis:creategrid', 1, extent, cellsize, cellsize, crs, grid1 )

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