Skip to content

Commit

Permalink
[gdal] rasterize - Include the possibility to use Z of feature to ext…
Browse files Browse the repository at this point in the history
…ract burn values
  • Loading branch information
talledodiego committed Mar 3, 2021
1 parent 2f42895 commit 0e6ffc7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion python/plugins/processing/algs/gdal/rasterize.py
Expand Up @@ -47,6 +47,7 @@ class rasterize(GdalAlgorithm):
INPUT = 'INPUT'
FIELD = 'FIELD'
BURN = 'BURN'
USE_Z = 'USE_Z'
WIDTH = 'WIDTH'
HEIGHT = 'HEIGHT'
UNITS = 'UNITS'
Expand Down Expand Up @@ -82,6 +83,10 @@ def initAlgorithm(self, config=None):
type=QgsProcessingParameterNumber.Double,
defaultValue=0.0,
optional=True))
self.addParameter(QgsProcessingParameterBoolean(self.USE_Z,
self.tr('Burn value extracted from the "Z" values of the feature'),
defaultValue=False,
optional=True))
self.addParameter(QgsProcessingParameterEnum(self.UNITS,
self.tr('Output raster size units'),
self.units))
Expand Down Expand Up @@ -171,7 +176,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
layerName
]
fieldName = self.parameterAsString(parameters, self.FIELD, context)
if fieldName:
use_z = self.parameterAsBoolean(parameters, self.USE_Z, context)
if use_z:
arguments.append('-3d')
elif fieldName:
arguments.append('-a')
arguments.append(fieldName)
else:
Expand Down
22 changes: 22 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsRasterTest.py
Expand Up @@ -1538,6 +1538,7 @@ def testRasterize(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'polys.gml')
sourceZ = os.path.join(testDataPath, 'pointsz.gml')
alg = rasterize()
alg.initAlgorithm()

Expand Down Expand Up @@ -1592,6 +1593,27 @@ def testRasterize(self):
source + ' ' +
outdir + '/check.jpg'])

# use_Z selected with no field
self.assertEqual(
alg.getConsoleCommands({'INPUT': sourceZ,
'USE_Z': True,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdal_rasterize',
'-l pointsz -3d -ts 0.0 0.0 -ot Float32 -of JPEG ' +
sourceZ + ' ' +
outdir + '/check.jpg'])

# use_Z selected with field indicated (should prefer use_Z)
self.assertEqual(
alg.getConsoleCommands({'INPUT': sourceZ,
'FIELD': 'elev',
'USE_Z': True,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdal_rasterize',
'-l pointsz -3d -ts 0.0 0.0 -ot Float32 -of JPEG ' +
sourceZ + ' ' +
outdir + '/check.jpg'])

def testRasterizeOver(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
Expand Down

0 comments on commit 0e6ffc7

Please sign in to comment.