lunes, 19 de marzo de 2018

Utilizando gdal:translate (PyQGIS Processing) para convertir values a nodata en QGIS 3

En un post anterior se usó el comando de PyQGIS Processing gdalogr:translate para convertir values a nodata en la versión previa de QGIS. No obstante, el nombre del comando cambió en QGIS 3 (ahora se denomina gdal:translate) y el número de parámetros también se redujo considerablemente. Estos pueden averiguarse con processing.algorithmHelp('gdal:translate') y la salida del comando se presenta a continuación:

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

INPUT:  <QgsProcessingParameterRasterLayer>
 Input layer

TARGET_CRS:  <QgsProcessingParameterCrs>
 Override the projection for the output file

NODATA:  <QgsProcessingParameterNumber>
 Assign a specified nodata value to output bands

COPY_SUBDATASETS:  <QgsProcessingParameterBoolean>
 Copy all subdatasets of this file to individual output files

OPTIONS:  <QgsProcessingParameterString>
 Additional creation parameters

DATA_TYPE:  <QgsProcessingParameterEnum>
 Output data type
  0 - Byte
  1 - Int16
  2 - UInt16
  3 - UInt32
  4 - Int32
  5 - Float32
  6 - Float64
  7 - CInt16
  8 - CInt32
  9 - CFloat32
  10 - CFloat64

OUTPUT:  <QgsProcessingParameterRasterDestination>
 Converted

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

OUTPUT:  <QgsProcessingOutputRasterLayer>
 Converted

De los parámetros anteriores hay que señalar que los data tipos de GDAL/OGR no coinciden con los de PyQGIS y, por tanto, hay que implementar en el código la equivalencia mediante dos diccionarios: QGIS_TYPE y RTYPE. Por otra parte, el código siguiente incluye parámetros mandatorios y opcionales para determinar la forma exacta de cómo se pasan tales parámetros.

 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
26
27
28
import processing

layer = iface.activeLayer()

path = '/home/zeito/pyqgis_data/new_raster.tif'

type = layer.type()
provider = layer.dataProvider()

dataType = provider.dataType(type)

QGIS_TYPE = {Qgis.Byte:'Byte', Qgis.Int16:'Int16', Qgis.UInt16:'UInt16', Qgis.UInt32:'UInt32', Qgis.Int32:'Int32', Qgis.Float32:'Float32',
             Qgis.Float64:'Float64', Qgis.CInt16:'CInt16', Qgis.CInt32:'CInt32', Qgis.CFloat32:'CFloat32', Qgis.CFloat64:'CFloat64'}

new_type = QGIS_TYPE[dataType]

RTYPE = { 'Byte': 0, 'Int16': 1, 'UInt16': 2, 'UInt32':3, 'Int32':4, 'Float32':5, 
          'Float64':6, 'CInt16':7, 'CInt32':8, 'CFloat32':9, 'CFloat64':10}

processing.runAndLoadResults('gdal:translate', 
                             {'INPUT':layer,
                              'TARGET_CRS':layer.crs().authid(),
                              'NODATA':5,
                              'COPY_SUBDATASETS':True,
                              'DATA_TYPE':RTYPE[new_type],
                              'JPEGCOMPRESSION':75.0,
                              'TILED':False,
                              'OUTPUT':path})

La ejecución del código anterior en la Python Console de QGIS permite obtener el resultado siguiente. Como todavía no ha sido desarrollado para QGIS 3 el Value Tool plugin entonces coloreamos el ráster resultante y los nodate (valores igual a 5 en el original) corresponden a los píxeles grises (dos de ellos encerrados en la elipse verde).


No hay comentarios: