Skip to content
Permalink
Browse files

Merge pull request #5092 from nyalldawson/algs

Port some GDAL algs to new API
  • Loading branch information
nyalldawson committed Aug 30, 2017
2 parents 12e69d0 + d463178 commit 3b997c2ff0aaecd4e641a37d4599c150ed027e03
@@ -37,12 +37,13 @@
from .aspect import aspect
from .buildvrt import buildvrt
from .ColorRelief import ColorRelief
from .information import information
from .rgb2pct import rgb2pct
from .translate import translate
from .tri import tri
from .warp import warp
from .nearblack import nearblack
# from .information import information
# from .rgb2pct import rgb2pct
# from .translate import translate

# from .pct2rgb import pct2rgb
# from .merge import merge
# from .polygonize import polygonize
@@ -140,16 +141,16 @@ def svgIconPath(self):

def loadAlgorithms(self):
self.algs = [
nearblack(),
# information(),
AssignProjection(),
aspect(),
buildvrt(),
ColorRelief(),
information(),
nearblack(),
rgb2pct(),
translate(),
tri(),
warp(),
# translate(),
# rgb2pct(),
# pct2rgb(),
# merge(),
# polygonize(),
@@ -193,6 +193,8 @@ def getFormatShortNameFromFilename(filename):
def escapeAndJoin(strList):
joined = ''
for s in strList:
if not isinstance(s, str):
s = str(s)
if s and s[0] != '-' and ' ' in s:
escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') \
+ '"'
@@ -29,10 +29,11 @@
import os

from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterBoolean,
QgsProcessingParameterFileDestination,
QgsProcessingOutputHtml)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterBoolean
from processing.core.outputs import OutputHTML
from processing.algs.gdal.GdalUtils import GdalUtils

@@ -53,14 +54,15 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(information.INPUT,
self.tr('Input layer'), False))
self.addParameter(ParameterBoolean(information.NOGCP,
self.tr('Suppress GCP info'), False))
self.addParameter(ParameterBoolean(information.NOMETADATA,
self.tr('Suppress metadata info'), False))
self.addOutput(OutputHTML(information.OUTPUT,
self.tr('Layer information')))
self.addParameter(QgsProcessingParameterRasterLayer(information.INPUT,
self.tr('Input layer'), optional=False))
self.addParameter(QgsProcessingParameterBoolean(information.NOGCP,
self.tr('Suppress GCP info'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(information.NOMETADATA,
self.tr('Suppress metadata info'), defaultValue=False))

self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Layer information'), self.tr('HTML files (*.html)')))
self.addOutput(QgsProcessingOutputHtml(self.OUTPUT, self.tr('Layer information')))

def name(self):
return 'gdalinfo'
@@ -73,18 +75,20 @@ def group(self):

def getConsoleCommands(self, parameters, context, feedback):
arguments = []
if self.getParameterValue(information.NOGCP):
if self.parameterAsBool(parameters, information.NOGCP, context):
arguments.append('-nogcp')
if self.getParameterValue(information.NOMETADATA):
if self.parameterAsBool(parameters, information.NOMETADATA, context):
arguments.append('-nomd')
arguments.append(self.getParameterValue(information.INPUT))
arguments.append(self.parameterAsRasterLayer(parameters, information.INPUT, context).source())
return ['gdalinfo', GdalUtils.escapeAndJoin(arguments)]

def processAlgorithm(self, parameters, context, feedback):
GdalUtils.runGdal(self.getConsoleCommands(parameters), feedback)
output = self.getOutputValue(information.OUTPUT)
GdalUtils.runGdal(self.getConsoleCommands(parameters, context, feedback), feedback)
output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)
with open(output, 'w') as f:
f.write('<pre>')
for s in GdalUtils.getConsoleOutput()[1:]:
f.write(str(s))
f.write('</pre>')

return {self.OUTPUT: output}
@@ -31,10 +31,10 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputRaster
from processing.tools.system import isWindows
from processing.algs.gdal.GdalUtils import GdalUtils

@@ -57,11 +57,11 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(rgb2pct.INPUT,
self.tr('Input layer'), False))
self.addParameter(ParameterNumber(rgb2pct.NCOLORS,
self.tr('Number of colors'), 1, None, 2))
self.addOutput(OutputRaster(rgb2pct.OUTPUT, self.tr('RGB to PCT')))
self.addParameter(QgsProcessingParameterRasterLayer(rgb2pct.INPUT,
self.tr('Input layer'), optional=False))
self.addParameter(QgsProcessingParameterNumber(rgb2pct.NCOLORS,
self.tr('Number of colors'), minValue=1, defaultValue=2))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('RGB to PCT')))

def name(self):
return 'rgbtopct'
@@ -72,11 +72,11 @@ def displayName(self):
def getConsoleCommands(self, parameters, context, feedback):
arguments = []
arguments.append('-n')
arguments.append(str(self.getParameterValue(rgb2pct.NCOLORS)))
arguments.append(str(self.parameterAsInt(parameters, rgb2pct.NCOLORS, context)))
arguments.append('-of')
out = self.getOutputValue(rgb2pct.OUTPUT)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.append(self.getParameterValue(rgb2pct.INPUT))
arguments.append(self.parameterAsRasterLayer(parameters, self.INPUT, context).source())
arguments.append(out)

if isWindows():
@@ -30,15 +30,16 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterString,
QgsProcessingParameterEnum,
QgsProcessingParameterCrs,
QgsProcessingParameterExtent,
QgsProcessingParameterRasterDestination,
QgsProcessingUtils)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import (ParameterRaster,
ParameterString,
ParameterNumber,
ParameterBoolean,
ParameterSelection,
ParameterExtent,
ParameterCrs)
from processing.core.outputs import OutputRaster

from processing.algs.gdal.GdalUtils import GdalUtils

@@ -67,34 +68,35 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer')))
self.addParameter(ParameterNumber(self.OUTSIZE,
self.tr('Set the size of the output file (In pixels or %)'),
1, None, 100))
self.addParameter(ParameterBoolean(self.OUTSIZE_PERC,
self.tr('Output size is a percentage of input size'), True))
self.addParameter(ParameterString(self.NO_DATA,
self.tr("Nodata value, leave blank to take the nodata value from input"),
'', optional=True))
self.addParameter(ParameterSelection(self.EXPAND,
self.tr('Expand'), ['none', 'gray', 'rgb', 'rgba'], default=0))
self.addParameter(ParameterCrs(self.SRS,
self.tr('Output projection for output file [leave blank to use input projection]'), None, optional=True))
self.addParameter(ParameterExtent(self.PROJWIN,
self.tr('Subset based on georeferenced coordinates'), optional=True))
self.addParameter(ParameterBoolean(self.SDS,
self.tr('Copy all subdatasets of this file to individual output files'),
False))

self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'),
optional=True,
metadata={'widget_wrapper': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}))
self.addParameter(ParameterSelection(self.RTYPE,
self.tr('Output raster type'),
self.TYPE, 5))

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Converted')))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterNumber(self.OUTSIZE,
self.tr('Set the size of the output file (In pixels or %)'),
minValue=1, defaultValue=100))
self.addParameter(QgsProcessingParameterBoolean(self.OUTSIZE_PERC,
self.tr('Output size is a percentage of input size'), defaultValue=True))
self.addParameter(QgsProcessingParameterString(self.NO_DATA,
self.tr("Nodata value, leave blank to take the nodata value from input"),
defaultValue='', optional=True))
self.addParameter(QgsProcessingParameterEnum(self.EXPAND,
self.tr('Expand'), options=['none', 'gray', 'rgb', 'rgba'], defaultValue=0))
self.addParameter(QgsProcessingParameterCrs(self.SRS,
self.tr('Output projection for output file [leave blank to use input projection]'), defaultValue=None, optional=True))
self.addParameter(QgsProcessingParameterExtent(self.PROJWIN,
self.tr('Subset based on georeferenced coordinates'), optional=True))
self.addParameter(QgsProcessingParameterBoolean(self.SDS,
self.tr('Copy all subdatasets of this file to individual output files'),
defaultValue=False))

create_options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options'),
optional=True)
create_options_param.setMetadata({'widget_wrapper': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'})
self.addParameter(create_options_param)
self.addParameter(QgsProcessingParameterEnum(self.RTYPE,
self.tr('Output raster type'),
options=self.TYPE, defaultValue=5))

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Converted')))

def name(self):
return 'translate'
@@ -106,18 +108,19 @@ def group(self):
return self.tr('Raster conversion')

def getConsoleCommands(self, parameters, context, feedback):
inLayer = self.getParameterValue(self.INPUT)
out = self.getOutputValue(translate.OUTPUT)
outsize = str(self.getParameterValue(self.OUTSIZE))
outsizePerc = str(self.getParameterValue(self.OUTSIZE_PERC))
noData = self.getParameterValue(self.NO_DATA)
expand = parameters[self.EXPAND].options[self.getParameterValue(self.EXPAND)][1]
projwin = str(self.getParameterValue(self.PROJWIN))
if not projwin:
projwin = QgsProcessingUtils.combineLayerExtents([inLayer])
crsId = self.getParameterValue(self.SRS)
sds = self.getParameterValue(self.SDS)
opts = self.getParameterValue(self.OPTIONS)
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
outsize = str(self.parameterAsInt(parameters, self.OUTSIZE, context))
outsizePerc = self.parameterAsBool(parameters, self.OUTSIZE_PERC, context)
noData = self.parameterAsString(parameters, self.NO_DATA, context)
expand = self.parameterDefinition(self.EXPAND).options()[self.parameterAsEnum(parameters, self.EXPAND, context)]

proj_extent = self.parameterAsExtent(parameters, self.PROJWIN, context)
if proj_extent.isNull():
proj_extent = QgsProcessingUtils.combineLayerExtents([inLayer])
crsId = self.parameterAsCrs(parameters, self.SRS, context).authid()
sds = self.parameterAsBool(parameters, self.SDS, context)
opts = self.parameterAsString(parameters, self.OPTIONS, context)

if noData is not None:
noData = str(noData)
@@ -126,8 +129,8 @@ def getConsoleCommands(self, parameters, context, feedback):
arguments.append('-of')
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.append('-ot')
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
if outsizePerc == 'True':
arguments.append(self.TYPE[self.parameterAsEnum(parameters, self.RTYPE, context)])
if outsizePerc:
arguments.append('-outsize')
arguments.append(outsize + '%')
arguments.append(outsize + '%')
@@ -141,14 +144,13 @@ def getConsoleCommands(self, parameters, context, feedback):
if expand != 'none':
arguments.append('-expand')
arguments.append(expand)
regionCoords = projwin.split(',')
try:
projwin = []
projwin.append('-projwin')
projwin.append(regionCoords[0])
projwin.append(regionCoords[3])
projwin.append(regionCoords[1])
projwin.append(regionCoords[2])
projwin.append(proj_extent.xMinimum())
projwin.append(proj_extent.yMaximum())
projwin.append(proj_extent.xMaximum())
projwin.append(proj_extent.yMinimum())
except IndexError:
projwin = []
if projwin:
@@ -163,7 +165,7 @@ def getConsoleCommands(self, parameters, context, feedback):
arguments.append('-co')
arguments.append(opts)

arguments.append(self.getParameterValue(self.INPUT))
arguments.append(inLayer.source())
arguments.append(out)

return ['gdal_translate', GdalUtils.escapeAndJoin(arguments)]
@@ -1,23 +1,23 @@
# See ../README.md for a description of the file format

tests:
# - algorithm: gdal:gdalinfo
# name: gdalinfo
# params:
# INPUT:
# name: raster.tif
# type: raster
# NOGCP: false
# NOMETADATA: false
# results:
# OUTPUT:
# name: expected/gdal/raster_info.html
# type: regex
# rules:
# - 'Origin = \(270736.067325068172067,4459029.574521748349071\)'
# - 'Band 1 Block=16x14 Type=Float32, ColorInterp=Gray'
# - ' NoData Value=-32768'
#
- algorithm: gdal:gdalinfo
name: gdalinfo
params:
INPUT:
name: raster.tif
type: raster
NOGCP: false
NOMETADATA: false
results:
OUTPUT:
name: expected/gdal/raster_info.html
type: regex
rules:
- 'Origin = \(270736.067325068172067,4459029.574521748349071\)'
- 'Band 1 Block=16x14 Type=Float32, ColorInterp=Gray'
- ' NoData Value=-32768'

# - algorithm: gdal:ogrinfo
# name: ogrinfo
# params:

0 comments on commit 3b997c2

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