Skip to content

Commit e21ece0

Browse files
authoredFeb 8, 2022
Merge pull request #47205 from agiudiceandrea/fix-47200-gdal-rasterize-extent-optional
[processing] GDAL "Rasterize (vector to raster)" alg: make the 'EXTENT' parameter optional and transform it to the source layer CRS
2 parents c93849f + 5f26250 commit e21ece0

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed
 

‎python/plugins/processing/algs/gdal/rasterize.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from qgis.PyQt.QtGui import QIcon
2828

2929
from qgis.core import (QgsRasterFileWriter,
30+
QgsProcessingException,
3031
QgsProcessingParameterDefinition,
3132
QgsProcessingParameterFeatureSource,
3233
QgsProcessingParameterField,
@@ -101,7 +102,8 @@ def initAlgorithm(self, config=None):
101102
minValue=0.0,
102103
defaultValue=0.0))
103104
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
104-
self.tr('Output extent')))
105+
self.tr('Output extent'),
106+
optional=True))
105107
nodataParam = QgsProcessingParameterNumber(self.NODATA,
106108
self.tr('Assign a specified nodata value to output bands'),
107109
type=QgsProcessingParameterNumber.Double,
@@ -169,8 +171,11 @@ def commandName(self):
169171
return 'gdal_rasterize'
170172

171173
def getConsoleCommands(self, parameters, context, feedback, executing=True):
172-
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
174+
source = self.parameterAsSource(parameters, self.INPUT, context)
175+
if source is None:
176+
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
173177

178+
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
174179
arguments = [
175180
'-l',
176181
layerName
@@ -210,7 +215,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
210215
arguments.append('-a_nodata')
211216
arguments.append(nodata)
212217

213-
extent = self.parameterAsExtent(parameters, self.EXTENT, context)
218+
extent = self.parameterAsExtent(parameters, self.EXTENT, context, source.sourceCrs())
214219
if not extent.isNull():
215220
arguments.append('-te')
216221
arguments.append(extent.xMinimum())

‎python/plugins/processing/tests/GdalAlgorithmsRasterTest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,8 @@ def testRasterize(self):
15751575
feedback = QgsProcessingFeedback()
15761576
source = os.path.join(testDataPath, 'polys.gml')
15771577
sourceZ = os.path.join(testDataPath, 'pointsz.gml')
1578+
extent4326 = QgsReferencedRectangle(QgsRectangle(-1, -3, 10, 6), QgsCoordinateReferenceSystem('EPSG:4326'))
1579+
extent3857 = QgsReferencedRectangle(QgsRectangle(-111319.491, -334111.171, 1113194.908, 669141.057), QgsCoordinateReferenceSystem('EPSG:3857'))
15781580
alg = rasterize()
15791581
alg.initAlgorithm()
15801582

@@ -1650,6 +1652,27 @@ def testRasterize(self):
16501652
sourceZ + ' ' +
16511653
outdir + '/check.jpg'])
16521654

1655+
# with EXTENT in the same CRS as the input layer source
1656+
self.assertEqual(
1657+
alg.getConsoleCommands({'INPUT': source,
1658+
'FIELD': 'id',
1659+
'EXTENT': extent4326,
1660+
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
1661+
['gdal_rasterize',
1662+
'-l polys2 -a id -ts 0.0 0.0 -te -1.0 -3.0 10.0 6.0 -ot Float32 -of JPEG ' +
1663+
source + ' ' +
1664+
outdir + '/check.jpg'])
1665+
# with EXTENT in a different CRS than that of the input layer source
1666+
self.assertEqual(
1667+
alg.getConsoleCommands({'INPUT': source,
1668+
'FIELD': 'id',
1669+
'EXTENT': extent3857,
1670+
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
1671+
['gdal_rasterize',
1672+
'-l polys2 -a id -ts 0.0 0.0 -te -1.000000001857055 -2.9999999963940835 10.000000000604244 5.99999999960471 -ot Float32 -of JPEG ' +
1673+
source + ' ' +
1674+
outdir + '/check.jpg'])
1675+
16531676
def testRasterizeOver(self):
16541677
context = QgsProcessingContext()
16551678
feedback = QgsProcessingFeedback()

‎python/plugins/processing/tests/testdata/gdal_algorithm_raster_tests.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,28 @@ tests:
363363
hash: 30409eb496900df4ceeab37200a91552c350dbc7761eb089dd75a329
364364
type: rasterhash
365365

366+
- algorithm: gdal:rasterize
367+
name: Test extent crs (gdal:rasterize)
368+
params:
369+
BURN: 0.0
370+
DATA_TYPE: 5
371+
EXTENT: -111319.491,1113194.908,-334111.171,669141.057 [EPSG:3857]
372+
FIELD: intval
373+
HEIGHT: 10.0
374+
INIT: 0.0
375+
INPUT:
376+
name: polys.gml
377+
type: vector
378+
INVERT: false
379+
NODATA: 0.0
380+
OPTIONS: ''
381+
UNITS: 0
382+
WIDTH: 10.0
383+
results:
384+
OUTPUT:
385+
hash: 30409eb496900df4ceeab37200a91552c350dbc7761eb089dd75a329
386+
type: rasterhash
387+
366388
- algorithm: gdal:roughness
367389
name: Roughness
368390
params:

0 commit comments

Comments
 (0)
Please sign in to comment.