Skip to content

Commit

Permalink
Merge pull request #47205 from agiudiceandrea/fix-47200-gdal-rasteriz…
Browse files Browse the repository at this point in the history
…e-extent-optional

[processing] GDAL "Rasterize (vector to raster)" alg: make the 'EXTENT' parameter optional and transform it to the source layer CRS
  • Loading branch information
alexbruy committed Feb 8, 2022
2 parents c93849f + 5f26250 commit e21ece0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
11 changes: 8 additions & 3 deletions python/plugins/processing/algs/gdal/rasterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsRasterFileWriter,
QgsProcessingException,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
Expand Down Expand Up @@ -101,7 +102,8 @@ def initAlgorithm(self, config=None):
minValue=0.0,
defaultValue=0.0))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Output extent')))
self.tr('Output extent'),
optional=True))
nodataParam = QgsProcessingParameterNumber(self.NODATA,
self.tr('Assign a specified nodata value to output bands'),
type=QgsProcessingParameterNumber.Double,
Expand Down Expand Up @@ -169,8 +171,11 @@ def commandName(self):
return 'gdal_rasterize'

def getConsoleCommands(self, parameters, context, feedback, executing=True):
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
source = self.parameterAsSource(parameters, self.INPUT, context)
if source is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
arguments = [
'-l',
layerName
Expand Down Expand Up @@ -210,7 +215,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append('-a_nodata')
arguments.append(nodata)

extent = self.parameterAsExtent(parameters, self.EXTENT, context)
extent = self.parameterAsExtent(parameters, self.EXTENT, context, source.sourceCrs())
if not extent.isNull():
arguments.append('-te')
arguments.append(extent.xMinimum())
Expand Down
23 changes: 23 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsRasterTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,8 @@ def testRasterize(self):
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'polys.gml')
sourceZ = os.path.join(testDataPath, 'pointsz.gml')
extent4326 = QgsReferencedRectangle(QgsRectangle(-1, -3, 10, 6), QgsCoordinateReferenceSystem('EPSG:4326'))
extent3857 = QgsReferencedRectangle(QgsRectangle(-111319.491, -334111.171, 1113194.908, 669141.057), QgsCoordinateReferenceSystem('EPSG:3857'))
alg = rasterize()
alg.initAlgorithm()

Expand Down Expand Up @@ -1650,6 +1652,27 @@ def testRasterize(self):
sourceZ + ' ' +
outdir + '/check.jpg'])

# with EXTENT in the same CRS as the input layer source
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'FIELD': 'id',
'EXTENT': extent4326,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdal_rasterize',
'-l polys2 -a id -ts 0.0 0.0 -te -1.0 -3.0 10.0 6.0 -ot Float32 -of JPEG ' +
source + ' ' +
outdir + '/check.jpg'])
# with EXTENT in a different CRS than that of the input layer source
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'FIELD': 'id',
'EXTENT': extent3857,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdal_rasterize',
'-l polys2 -a id -ts 0.0 0.0 -te -1.000000001857055 -2.9999999963940835 10.000000000604244 5.99999999960471 -ot Float32 -of JPEG ' +
source + ' ' +
outdir + '/check.jpg'])

def testRasterizeOver(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,28 @@ tests:
hash: 30409eb496900df4ceeab37200a91552c350dbc7761eb089dd75a329
type: rasterhash

- algorithm: gdal:rasterize
name: Test extent crs (gdal:rasterize)
params:
BURN: 0.0
DATA_TYPE: 5
EXTENT: -111319.491,1113194.908,-334111.171,669141.057 [EPSG:3857]
FIELD: intval
HEIGHT: 10.0
INIT: 0.0
INPUT:
name: polys.gml
type: vector
INVERT: false
NODATA: 0.0
OPTIONS: ''
UNITS: 0
WIDTH: 10.0
results:
OUTPUT:
hash: 30409eb496900df4ceeab37200a91552c350dbc7761eb089dd75a329
type: rasterhash

- algorithm: gdal:roughness
name: Roughness
params:
Expand Down

0 comments on commit e21ece0

Please sign in to comment.