1

This question already has an answer here:

NOTE: THIS IS FOR QGIS 2.14.1 and uses VectorGrid instead of CreateGrid also though the question How to generate a grid programmatically in Python without GUI? is answered fantastically the PyQGIS language is outdated

We have a .txt file "Extent.txt", in this example this file contains the coordinates of 4 points formatted like so;

Point,Lat,Lon
TL,-29.788254, 30.820720
TR,-29.788254, 30.825372
BL,-29.790710, 30.820720
BR,-29.790710, 30.825372

We want to add this shapefile in our QGIS Map Document, and then create a vector grid that has the same extents of this .txt file, all using PyQGIS. We add the .txt file with a PyQGIS script

import csv, ctypes, os, sys, datetime, string
import processing
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface

#Load CSV
gridSettings = QSettings()
gridoldValidation = gridSettings.value( "/Projections/defaultBehaviour")
gridSettings.setValue( "/Projections/defaultBehaviour", "useGlobal" )
gridInFlnm='Extent.txt'
gridInDrPth='PATH_TO_CSV_FOLDER'
gridInFlPth="file:///"+gridInDrPth+gridInFlnm
griduri = gridInFlPth+"?delimiter=%s&xField=%s&yField=%s" % (",","lon","lat")
gridbh = QgsVectorLayer(griduri, gridInFlnm, "delimitedtext")
gridbh.isValid()
gridbh.setCrs(QgsCoordinateReferenceSystem(32365,  QgsCoordinateReferenceSystem.EpsgCrsId))
QgsMapLayerRegistry.instance().addMapLayer(gridbh)
gridSettings.setValue( "/Projections/defaultBehaviour", gridoldValidation )

This produces our point vector layer from our .txt file! enter image description here

So...How do we get our Vector Grid?

NOTE: THIS IS FOR QGIS 2.14.1 and uses VectorGrid instead of CreateGrid also though the question How to generate a grid programmatically in Python without GUI? is answered fantastically the PyQGIS language is outdated

marked as duplicate by Midavalo, Mapperz Jun 14 '16 at 18:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • NOTE: THIS IS FOR QGIS 2.14.1 and uses VectorGrid instead of CreateGrid also though the question How to generate a grid programmatically in Python without GUI? is answered fantastically the PyQGIS language in that is outdated – user32496 Jun 14 '16 at 15:03
2

To Create the Vector Grid using the extents of the Vector Layer, we use:

processing.runalg('qgis:vectorgrid', extent, step_x, step_y, type, output)

from https://docs.qgis.org/2.8/en/docs/user_manual/processing_algs/qgis/vector_creation_tools.html#create-grid

Here is the code on how to achieve the desired result, please note this will work on all vector layers, not just point vector Layers

cellsize = 0.0001 #Cell Size in WGS 84 will be 10 x 10 meters 
crs = "EPSG:4326" #WGS 84 System 
input = processing.getObject(gridbh.name()) #Use the processing.getObject to get information from our vector layer
xmin = (input.extent().xMinimum()) #extract the minimum x coord from our layer
xmax =  (input.extent().xMaximum()) #extract our maximum x coord from our layer
ymin = (input.extent().yMinimum()) #extract our minimum y coord from our layer
ymax = (input.extent().yMaximum()) #extract our maximum y coord from our layer
#prepare the extent in a format the VectorGrid tool can interpret (xmin,xmax,ymin,ymax)
extent = str(xmin)+ ',' + str(xmax)+ ',' +str(ymin)+ ',' +str(ymax)  
grid="PATH_FOR_VECTORGRID_CREATION"
processing.runalg('qgis:vectorgrid',  extent, cellsize, cellsize,  0, grid)

enter image description here