Skip to content

Instantly share code, notes, and snippets.

@ericrobskyhuntley
Last active April 10, 2019 19:40
Show Gist options
  • Save ericrobskyhuntley/78c360ab7d64d16f76b79530942a1417 to your computer and use it in GitHub Desktop.
Save ericrobskyhuntley/78c360ab7d64d16f76b79530942a1417 to your computer and use it in GitHub Desktop.
Generate Grids for Dispersed Study Regions Based on Regional Extents
from qgis import *
import processing
proj_path = QgsProject.instance().readPath("./")
layers = iface.mapCanvas().layers()
for n, i in enumerate(layers):
ext = i.extent()
size = 5280
margin = size/2
xmin = ext.xMinimum() - margin
xmax = ext.xMaximum() + margin
ymin = ext.yMinimum() - margin
ymax = ext.yMaximum() + margin
geom_name = i.name() + '_grid'
geom_loc = proj_path + '/' + geom_name + '.shp'
parameters = {'TYPE': 4,
'EXTENT': '%f, %f, %f, %f' % (xmin, xmax, ymin, ymax),
'HSPACING': size,
'VSPACING': size,
'HOVERLAY': 0.0,
'VOVERLAY': 0.0,
'CRS': QgsCoordinateReferenceSystem(i.crs().authid()),
'OUTPUT': 'memory:grid'
}
grid = processing.run('qgis:creategrid', parameters)
geom_parameters = {
'CALC_METHOD': 0,
'INPUT': grid['OUTPUT'],
'OUTPUT': QgsProcessingOutputLayerDefinition(geom_loc)
}
processing.runAndLoadResults('qgis:exportaddgeometrycolumns', geom_parameters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment