I want to create a grid with a python script in PyQgis. I have the layer from which I want to take the extent.
it seems that there is different formula : The one from QGIS doc : https://docs.qgis.org/2.6/en/docs/user_manual/processing_algs/qgis/vector_creation_tools/creategrid.html
processing.runalg('qgis:creategrid', type, width, height, hspacing, vspacing, centerx, centery, crs, output)
The one I found on different forums with a string for extent :
processing.runalg('qgis:creategrid', type, extent, hspacing, vspacing, crs, output)
I tried both and I always got :
I tried it from my script and from the console.
Here is my code if it can help :
#!/usr/bin/env Python
from glob import glob
from os import path
import numpy as np
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface
import processing
class Grid:
def __init__(self, iface,crs):
"""Initialize using the qgis.utils.iface
object passed from the console.
"""
self.iface = iface
self.crs=crs
def new_grid(self,cellsizex,cellsizey,extent,centerx,centery,grid_out):
processing.runalg('qgis:creategrid', type=1, extent=extent, hspacing=cellsizex, vspacing=cellsizey, crs=self.crs, output=grid_out)
class Extent:
def __init__(self, iface):
"""Initialize using the qgis.utils.iface
object passed from the console.
"""
self.iface = iface
def main(self):
pabs= "K:\Histo_Europe_All" #path absolu
path=pabs+"\0_Quadrillage\extent_eu.shp"
name_l="extent_eu"
layer=QgsVectorLayer(path,name_l, "ogr")
extent = QgsVectorLayer( path, '', 'ogr' ).extent()
cellsize = 1000
centerx = (extent.xMinimum() + extent.xMaximum()) / 2
centery = (extent.yMinimum() + extent.yMaximum()) / 20
#first method
width = (extent.xMaximum() - extent.xMinimum())
height = (extent.yMaximum() - extent.yMinimum())/10
#second method
extent2 = str(extent.xMinimum())+ ',' + str(extent.xMaximum())+ ',' +str(ymin)+ ',' +str(extent.yMaximum())
cellsizex = 0,002775
cellsizey = 0,002775
ymin=extent.yMinimum()+ 0,8 * (extent.yMaximum() - extent.yMinimum())
grid=pabs+"\1_Grids\Grid0.shp"
g=Grid(self.iface,layer.crs().authid())
g.new_grid(cellsizex,cellsizey,extent2,centerx,centery,grid)
Don't mind the missing spaces at the beginning of the classes, they do exist in my code.
I edit my question based on comments. I also gave up the extent as QGIS advise width and height. My code is now the following :
class Grid:
def __init__(self, iface,crs):
"""Initialize using the qgis.utils.iface
object passed from the console.
"""
self.iface = iface
self.crs=crs
def new_grid(self,cellsizex,cellsizey,width,height,centerx,centery,grid_out):
#processing.runalg("qgis:creategrid", cellsizex, cellsizey, width, height, centerx, centery, 1, layer.crs().authid(), grid_out)
#processing.runalg('qgis:creategrid', width, height, cellsizex, cellsizey, centerx, centery,"EPSG:4326", grid_out)
#processing.runalg('qgis:creategrid', type=1, extent=extent, hspacing=cellsizex, vspacing=cellsizey, crs=self.crs, output=grid_out)
processing.runalg('qgis:creategrid', 1, width,height, cellsizex, cellsizey, self.crs, output=grid_out)
class Extent:
def __init__(self, iface):
"""Initialize using the qgis.utils.iface
object passed from the console.
"""
self.iface = iface
def main(self):
pabs= "K:/Histo_Europe_All" #path absolu
path=pabs+"/0_Quadrillage/extent_eu.shp"
name_l="extent_eu"
layer=QgsVectorLayer(path,name_l, "ogr")
extent = QgsVectorLayer( path, '', 'ogr' ).extent()
cellsize = 1000
centerx = (extent.xMinimum() + extent.xMaximum()) / 2
centery = (extent.yMinimum() + extent.yMaximum()) / 20
width = (extent.xMaximum() - extent.xMinimum())
height = (extent.yMaximum() - extent.yMinimum())/10
print(width)
print(height)
cellsizex = 0.002775
cellsizey = 0.002775
ymin=extent.yMinimum()+ 0.8 * (extent.yMaximum() - extent.yMinimum())
extent2 = str(extent.xMinimum())+ ',' + str(extent.xMaximum())+ ',' +str(ymin)+ ',' +str(extent.yMaximum())
grid=pabs+"/1_Grids/Grid0.shp"
g=Grid(self.iface,layer.crs().authid())
g.new_grid(cellsizex,cellsizey,width,height,centerx,centery,grid)
Now it answers : Error: Wrong parameter value: 40.967325 (width)
self.crs=self.layer.crs.authid()
should instead beself.crs=self.layer.crs().authid()