Skip to content
Permalink
Browse files

Port gdal build vrt to new API

  • Loading branch information
nyalldawson committed Aug 16, 2017
1 parent f610ffa commit a5a4d3b7e1b6f2d0265c1c6dee32ca47b7fafa07
@@ -35,6 +35,7 @@

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 .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,14 +144,14 @@ def loadAlgorithms(self):
# information(),
AssignProjection(),
aspect(),
buildvrt(),
ColorRelief(),
tri(),
warp(),
# translate(),
# rgb2pct(),
# pct2rgb(),
# merge(),
# buildvrt(),
# polygonize(),
# gdaladdo(),
# ClipByExtent(),
@@ -29,14 +29,16 @@

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 @@ def __init__(self):
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 @@ def group(self):
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)]

0 comments on commit a5a4d3b

Please sign in to comment.
You can’t perform that action at this time.