7 votos

Creación de una cuadrícula con PyQGIS

No puedo crear cuadrículas correctas con PyQGIS. Cuando creo la rejilla con PyQGIS, la forma de la rejilla no es correcta, es decir, la rejilla rota. Pero es correcta cuando la creo con QGIS > Vector > Herramientas de investigación > Crear cuadrícula opción. ¿Cómo puedo obtener una cuadrícula correcta (no girada)?

Aquí está el código de PyQGIS:

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)

La cuadrícula verde se creó con PyQGIS.

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

10voto

nitinsavant Puntos 6

Utilizas mal TYPE código. Hay que utilizar 2 para la rejilla rectangular. 3 significa diamante.

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

enter image description here

Para más detalles sobre el algoritmo, intente

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

y obtener más información:

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

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X