13

I have been using a process called "Create Polygon Graticule" to create a grid within a polygon with assigned row and column attributes. It is a SAGA tool which I have been running in QGIS. It generates a grid based on the extent of the input polygon with the cell running N to S and E to W. It would be better for my analysis if the direction of the grid was parallel to the longest edge of the polygon. For reference I have attached images of the current automated output and an output that I manually altered to align the grid with the polygon. The grids originally covered the entire extent of the polygon but have been clipped to show only the cell parts that are inside.

This is the default tool out put Above image is what the tool out puts by default.

enter image description here This is an image of a grid that was edited manually to fit the necessary parameters.

My question is, is there a way to produce grids that are aligned such as the one in the second image in an automated fashion? I will need to generate grids for a few thousand polygons.

  • 1
    Do the polygons are rectangles or general polygons with 4 edges? I ask you this because the squares don't seem to be aligned to the shorter edge. – mgri Oct 18 '17 at 16:51
  • 1
    The edges of the polygons are not necessarily right angles, which is why the edges don't align exactly. Also it was rotated with digitizing tools so there may be some human error present. The polygons are not all four sided though the shapes are generally pretty regular. This is why I was thinking the alignment would best be done using the longest edge, as the side opposite will likely have a different bearing. – Kingfisher Oct 18 '17 at 18:51
  • related: gis.stackexchange.com/q/30988/66673 – Kingfisher Oct 23 '17 at 15:31
  • Maybe related: gis.stackexchange.com/questions/210461/… – AndreJ Apr 15 '18 at 9:32
3

If you create a vector grid from Vector -> Research Tools -> Vector Grid, you can specify rotation angle.

enter image description here

  • I do not see a rotation angle input available in the Vector Grid tool. This is in version 2.18.17, 32 bit Windows. Maybe I am missing something. – jbgramm Mar 13 '18 at 5:13
  • I added the picture. i am using 2.14.18 64 bit windows – neogeomat Mar 13 '18 at 5:36
  • Can the angle value be supplied from another layer, perhaps in the graphical modeler? This looks promising, but tedious for OP, as he has many polygons. – JoshC Mar 13 '18 at 10:17
  • 1
    @JoshC It was part of the ftools plugin in 2.14, which was dismissed in favour of some processing scripts in 2.18. Look for C:\Program Files\QGIS Essen\apps\qgis-ltr\python\plugins\fTools\tools\doVectorGrid.py. Strangely this was not included in QGIS 2.8. – AndreJ Apr 15 '18 at 8:58
  • 1
    I added a regression bug report for it: issues.qgis.org/issues/18725 – AndreJ Apr 15 '18 at 9:28
1

QGIS 2.18

As @jbgramm commented to the answer provided by @neogeomat, we do not see Rotation angle option in Vector Grid tool in QGIS 2.18 (2.18.15-17 checked by @jbgramm , and the same with 2.18.18).

That leaves us with GRASS v.mkgrid function.

enter image description here

The angle is measured from north, counter-clockwise. So the -30 in the picture produces a grid oriented to N30E.

0

I also ran into this problem in QGIS 3 and found this solution in stack overflow

Basically the idea would be to apply the angle on the polygon on which the extent is defined before creating the grid. If your polygon is not a rectangle, you would need to create a layer from the extent of your polygon prior, and then rotate it. Then you can create the grid according to this new extent and then rotate your polygon and the grid back to the original Polygon extent. All this while making sure the same x, y coordinate is used as anchor point in both layers.

#Define extent of Polygon
ext = QgsVectorLayer('path_to_polygon.shp', '', 'ogr' ).extent()
xmin = ext.xMinimum()
xmax = ext.xMaximum()
ymin = ext.yMinimum()
ymax = ext.yMaximum()
coords = "%f,%f,%f,%f" %(xmin, xmax, ymin, ymax)

#Define The angle of rotation. Change value to yours
azimut = 70.043

#define anchor point for rotation
anchor = "%f, %f" % (xmin, ymax)

#define x and y spacing of grid. Update to your desired spacing.
x = 3
y = 6

#create new polygon from extent
processing.run("native:extenttolayer", {'INPUT':coords,'OUTPUT':'Path_to_Output.shp'})

#Rotate Extent
processing.run("native:rotatefeatures", {'INPUT': 'Path_to_extent_Polygon.shp','ANGLE': azimut,'ANCHOR':anchor + '[EPSG:4326]','OUTPUT': 'Path_to_rotated_extent.shp'})

#Define extent of Rotated Polygon 
ext1 = QgsVectorLayer('Path_to_Rotated_Extent.shp', '', 'ogr' ).extent()
xmin1 = ext1.xMinimum()
xmax1 = ext1.xMaximum()
ymin1 = ext1.yMinimum()
ymax1 = ext1.yMaximum()
coords1 = "%f,%f,%f,%f" %(xmin1, xmax1, ymin1, ymax1)

#Create grid 
processing.run("qgis:creategrid", {'TYPE':0,'EXTENT': coords1 +'[EPSG:4326]','HSPACING':x,'VSPACING':y,'HOVERLAY':0,'VOVERLAY':0,'CRS':'EPSG:4326','OUTPUT': 'Path_to_grid.shp'})

#Rotate Grid to original extent
processing.run("native:rotatefeatures", {'INPUT': 'path_to_grid.shp','ANGLE': - 
azimut,'ANCHOR':rotate + '[EPSG:4326]','OUTPUT': 'path_to_rotated_grid.shp'})

# Clip Grid to Original Polygon
processing.run("native:clip", {'INPUT':'path_to_rotated_grid.shp','OVERLAY': 
'path_to_original_Polygon.shp','OUTPUT':'path_to_final_grid.shp'})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.