我也在QGIS 3中遇到了这个问题,并在堆栈溢出中找到了此解决方案
基本上,该想法是在创建网格之前将角度应用于在其上定义了范围的多边形上。如果多边形不是矩形,则需要先根据多边形的范围创建一个图层,然后旋转它。然后,您可以根据此新范围创建网格,然后将多边形和网格旋转回原始的Polygon范围。所有这些都确保在两个图层中将相同的x,y坐标用作锚点。
#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'})