Port gdal build vrt to new API

This commit is contained in:
Nyall Dawson 2017-08-15 21:29:47 +10:00
parent f610ffaf52
commit a5a4d3b7e1
2 changed files with 35 additions and 24 deletions

View File

@ -35,6 +35,7 @@ from .GdalUtils import GdalUtils
from .AssignProjection import AssignProjection
from .aspect import aspect
from .buildvrt import buildvrt
from .ColorRelief import ColorRelief
from .tri import tri
from .warp import warp
@ -44,7 +45,6 @@ from .nearblack import nearblack
# from .translate import translate
# from .pct2rgb import pct2rgb
# from .merge import merge
# from .buildvrt import buildvrt
# from .polygonize import polygonize
# from .gdaladdo import gdaladdo
# from .ClipByExtent import ClipByExtent
@ -144,6 +144,7 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
# information(),
AssignProjection(),
aspect(),
buildvrt(),
ColorRelief(),
tri(),
warp(),
@ -151,7 +152,6 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
# rgb2pct(),
# pct2rgb(),
# merge(),
# buildvrt(),
# polygonize(),
# gdaladdo(),
# ClipByExtent(),

View File

@ -29,14 +29,16 @@ import os
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsProcessingUtils
from qgis.core import (QgsProcessing,
QgsProperty,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterEnum,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination,
QgsProcessingOutputLayerDefinition,
QgsProcessingUtils)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.outputs import OutputRaster
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterSelection
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.tools import dataobjects
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
@ -55,15 +57,15 @@ class buildvrt(GdalAlgorithm):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(ParameterMultipleInput(self.INPUT,
self.tr('Input layers'), dataobjects.TYPE_RASTER))
self.addParameter(ParameterSelection(self.RESOLUTION,
self.tr('Resolution'), self.RESOLUTION_OPTIONS, 0))
self.addParameter(ParameterBoolean(self.SEPARATE,
self.tr('Layer stack'), True))
self.addParameter(ParameterBoolean(self.PROJ_DIFFERENCE,
self.tr('Allow projection difference'), False))
self.addOutput(OutputRaster(buildvrt.OUTPUT, self.tr('Virtual')))
self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUT,
self.tr('Input layers'), QgsProcessing.TypeRaster))
self.addParameter(QgsProcessingParameterEnum(self.RESOLUTION,
self.tr('Resolution'), options=self.RESOLUTION_OPTIONS, defaultValue=0))
self.addParameter(QgsProcessingParameterBoolean(self.SEPARATE,
self.tr('Layer stack'), defaultValue=True))
self.addParameter(QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE,
self.tr('Allow projection difference'), defaultValue=False))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Virtual')))
def name(self):
return 'buildvirtualraster'
@ -80,25 +82,34 @@ class buildvrt(GdalAlgorithm):
def getConsoleCommands(self, parameters, context, feedback):
arguments = []
arguments.append('-resolution')
arguments.append(self.RESOLUTION_OPTIONS[self.getParameterValue(self.RESOLUTION)])
if self.getParameterValue(buildvrt.SEPARATE):
arguments.append(self.RESOLUTION_OPTIONS[self.parameterAsEnum(parameters, self.RESOLUTION, context)])
if self.parameterAsBool(parameters, buildvrt.SEPARATE, context):
arguments.append('-separate')
if self.getParameterValue(buildvrt.PROJ_DIFFERENCE):
if self.parameterAsBool(parameters, buildvrt.PROJ_DIFFERENCE, context):
arguments.append('-allow_projection_difference')
# Always write input files to a text file in case there are many of them and the
# length of the command will be longer then allowed in command prompt
listFile = os.path.join(QgsProcessingUtils.tempFolder(), 'buildvrtInputFiles.txt')
with open(listFile, 'w') as f:
f.write(self.getParameterValue(buildvrt.INPUT).replace(';', '\n'))
layers = []
for l in self.parameterAsLayerList(parameters, self.INPUT, context):
layers.append(l.source())
f.write('\n'.join(layers))
arguments.append('-input_file_list')
arguments.append(listFile)
out = self.getOutputValue(buildvrt.OUTPUT)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
# Ideally the file extensions should be limited to just .vrt but I'm not sure how
# to do it simply so instead a check is performed.
_, ext = os.path.splitext(out)
if not ext.lower() == '.vrt':
out = out.replace(ext, '.vrt')
self.setOutputValue(self.OUTPUT, out)
out = out[:-len(ext)] + '.vrt'
if isinstance(parameters[self.OUTPUT], QgsProcessingOutputLayerDefinition):
output_def = QgsProcessingOutputLayerDefinition(parameters[self.OUTPUT])
output_def.sink = QgsProperty.fromValue(out)
self.setOutputValue(self.OUTPUT, output_def)
else:
self.setOutputValue(self.OUTPUT, out)
arguments.append(out)
return ['gdalbuildvrt', GdalUtils.escapeAndJoin(arguments)]