sábado, 17 de marzo de 2018

Utilizando grass7:v.voronoi en PyQGIS 3 Processing

En un post anterior se consideró un tema similar para la versión previa de QGIS 3 y GRASS 6. La forma elegida de pasarle los parámetros es mediante un diccionario que premite, como ya se ha señalado con anterioridad, prescindir del orden y de los que no son mandatorios.

Empleando el comando processing.algorithmHelp('grass7:v.voronoi') se obtuvo la información siguiente:

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

input:  <QgsProcessingParameterVectorLayer>
 Input points layer

smoothness:  <QgsProcessingParameterNumber>
 Factor for output smoothness

thin:  <QgsProcessingParameterNumber>
 Maximum dangle length of skeletons (-1 will extract the center line)

-a:  <QgsProcessingParameterBoolean>
 Create Voronoi diagram for input areas

-s:  <QgsProcessingParameterBoolean>
 Extract skeletons for input areas

-l:  <QgsProcessingParameterBoolean>
 Output tessellation as a graph (lines), not areas

-t:  <QgsProcessingParameterBoolean>
 Do not create attribute table

output:  <QgsProcessingParameterVectorDestination>
 Voronoi

GRASS_REGION_PARAMETER:  <QgsProcessingParameterExtent>
 GRASS GIS 7 region extent

GRASS_SNAP_TOLERANCE_PARAMETER:  <QgsProcessingParameterNumber>
 v.in.ogr snap tolerance (-1 = no snap)

GRASS_MIN_AREA_PARAMETER:  <QgsProcessingParameterNumber>
 v.in.ogr min area

GRASS_OUTPUT_TYPE_PARAMETER:  <QgsProcessingParameterEnum>
 v.out.ogr output type
  0 - auto
  1 - point
  2 - line
  3 - area

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

output:  <QgsProcessingOutputVectorLayer>
 Voronoi

Tomando en consideración las capas de la imagen que se presenta a continuación y la información anterior:


se elaboró el código siguiente:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import processing

registry = QgsProject.instance()

random_points = registry.mapLayersByName('random_points')
utah_demUTM12 = registry.mapLayersByName('utah_demUTM12')

extent = utah_demUTM12[0].extent()

xmin, ymin, xmax, ymax = extent.toRectF().getCoords()

path = '/home/zeito/pyqgis_data/output_voronoi.shp'

parameters = {'input':random_points[0],
              '-l':0, #QgsProcessingParameterBoolean, Output tessellation as a graph (lines)
              '-t':1, #QgsProcessingParameterBoolean, Do not create attribute table
              '-a':0, #QgsProcessingParameterBoolean, Create Voronoi diagram for input areas
              '-s':0, #QgsProcessingParameterBoolean, Extract skeletons for input areas
              'GRASS_SNAP_TOLERANCE_PARAMETER':-1, 
              'GRASS_MIN_AREA_PARAMETER':0.000100,
              'GRASS_OUTPUT_TYPE_PARAMETER':3,
              'GRASS_REGION_PARAMETER':"%f, %f, %f, %f" % (xmin, xmax, ymin, ymax),
              'output':path}

processing.runAndLoadResults("grass7:v.voronoi", parameters)

Cuando se ejecuta en la Python Console de QGIS se obtiene:


No hay comentarios: