6

I cannot create correct grids with PyQGIS. When I create grid with PyQGIS, the grid shape is not correct, i.e. rotated grid. But it's correct when I create it with QGIS > Vector > Research Tools > Create grid option. How can I obtain correct (not rotated) grid.

Here is the PyQGIScode:

crs = QgsProject().instance().crs().toWkt() # it is EPSG:3857
params = {'TYPE':3,
          'EXTENT':'0,10,0,10',
          'HSPACING':1,
          'VSPACING':1,
          'HOVERLAY':0,
          'VOVERLAY':0,
          'CRS':crs,
          'OUTPUT':'memory'}
out1 = processing.run('native:creategrid', params)

grid = QgsVectorLayer(out1['OUTPUT'], 'grid', 'ogr')
QgsProject().instance().addMapLayer(grid)

The green grid was created with PyQGIS.

grid created with pyqgis grid created with qgis-processing(by hand) enter image description here

4
  • You are having a problem with the CRS. Pay attention to what CRS system is chosen in QGIS after your Grid is generated and displayed in ToC.It should fit what you choose in the Create Grid tool. – Nil Jan 13 at 9:58
  • Crs is the same in both Create Grid tool and the code. It is EPSG:3857 – Xi Jin Jan 13 at 10:05
  • Which QGIS version do you use? – Nil Jan 13 at 10:12
  • QGIS 3.10.9 Coruna – Xi Jin Jan 13 at 10:13
9

You use wrong TYPE code. You have to use 2 for rectangular grid. 3 means diamond.

params = {'TYPE':2, ...} # 2: rectangle

enter image description here

For more details about the algorithm try

import processing
processing.algorithmHelp("qgis:creategrid")

and get more info:

Create grid (native:creategrid)

This algorithm creates a vector layer with a grid covering a given extent. Elements in the grid can be points, lines or polygons. The size and/or placement of each element in the grid is defined using a horizontal and vertical spacing. The CRS of the output layer must be defined. The grid extent and the spacing values must be expressed in the coordinates and units of this CRS. The top-left point (minX, maxY) is used as the reference point. That means that, at that point, an element is guaranteed to be placed. Unless the width and height of the selected extent is a multiple of the selected spacing, that is not true for the other points that define that extent.


----------------
Input parameters
----------------

TYPE: Grid type

    Parameter type: QgsProcessingParameterEnum

    Available values:
        - 0: Point
        - 1: Line
        - 2: Rectangle (Polygon)
        - 3: Diamond (Polygon)
        - 4: Hexagon (Polygon)

    Accepted data types:
        - int
        - str: as string representation of int, e.g. '1'
        - QgsProperty

EXTENT: Grid extent

    Parameter type: QgsProcessingParameterExtent

    Accepted data types:
        - str: as comma delimited list of x min, x max, y min, y max. E.g. '4,10,101,105'
        - str: layer ID. Extent of layer is used.
        - str: layer name. Extent of layer is used.
        - str: layer source. Extent of layer is used.
        - QgsMapLayer: Extent of layer is used
        - QgsProcessingFeatureSourceDefinition: Extent of source is used
        - QgsProperty
        - QgsRectangle
        - QgsReferencedRectangle
        - QgsGeometry: bounding box of geometry is used

HSPACING: Horizontal spacing

    Parameter type: QgsProcessingParameterDistance

    Accepted data types:
        - int
        - float
        - QgsProperty

VSPACING: Vertical spacing

    Parameter type: QgsProcessingParameterDistance

    Accepted data types:
        - int
        - float
        - QgsProperty

HOVERLAY: Horizontal overlay

    Parameter type: QgsProcessingParameterDistance

    Accepted data types:
        - int
        - float
        - QgsProperty

VOVERLAY: Vertical overlay

    Parameter type: QgsProcessingParameterDistance

    Accepted data types:
        - int
        - float
        - QgsProperty

CRS: Grid CRS

    Parameter type: QgsProcessingParameterCrs

    Accepted data types:
        - str: 'ProjectCrs'
        - str: CRS auth ID (e.g. 'EPSG:3111')
        - str: CRS PROJ4 (e.g. 'PROJ4:…')
        - str: CRS WKT (e.g. 'WKT:…')
        - str: layer ID. CRS of layer is used.
        - str: layer name. CRS of layer is used.
        - str: layer source. CRS of layer is used.
        - QgsCoordinateReferenceSystem
        - QgsMapLayer: CRS of layer is used
        - QgsProcessingFeatureSourceDefinition: CRS of source is used
        - QgsProperty

OUTPUT: Grid

    Parameter type: QgsProcessingParameterFeatureSink

    Accepted data types:
        - str: destination vector file, e.g. 'd:/test.shp'
        - str: 'memory:' to store result in temporary memory layer
        - str: using vector provider ID prefix and destination URI, e.g. 'postgres:…' to store result in PostGIS table
        - QgsProcessingOutputLayerDefinition
        - QgsProperty

----------------
Outputs
----------------

OUTPUT:  <QgsProcessingOutputVectorLayer>
    Grid
0

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.