From 82182040ab7a6756ab666cb4176b02fa35582ede Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 27 Jul 2017 16:02:52 +1000 Subject: [PATCH] Port Create Constant Raster to new API and add test --- .../algs/qgis/CreateConstantRaster.py | 32 ++++++++---------- .../algs/qgis/QGISAlgorithmProvider.py | 5 +-- .../testdata/expected/constant_raster.tif | Bin 0 -> 1232 bytes .../expected/constant_raster.tif.aux.xml | 10 ++++++ .../tests/testdata/qgis_algorithm_tests.yaml | 13 +++++++ 5 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 python/plugins/processing/tests/testdata/expected/constant_raster.tif create mode 100644 python/plugins/processing/tests/testdata/expected/constant_raster.tif.aux.xml diff --git a/python/plugins/processing/algs/qgis/CreateConstantRaster.py b/python/plugins/processing/algs/qgis/CreateConstantRaster.py index 72363b18f0f..e8a6dc9f9a8 100644 --- a/python/plugins/processing/algs/qgis/CreateConstantRaster.py +++ b/python/plugins/processing/algs/qgis/CreateConstantRaster.py @@ -27,12 +27,10 @@ __revision__ = '$Format:%H$' from osgeo import gdal -from qgis.core import (QgsApplication, - QgsProcessingUtils) +from qgis.core import (QgsProcessingParameterRasterLayer, + QgsProcessingParameterNumber, + QgsProcessingParameterRasterDestination) from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm -from processing.core.parameters import ParameterRaster -from processing.core.parameters import ParameterNumber -from processing.core.outputs import OutputRaster from processing.tools.raster import RasterWriter @@ -49,14 +47,12 @@ class CreateConstantRaster(QgisAlgorithm): super().__init__() def initAlgorithm(self, config=None): - self.addParameter(ParameterRaster(self.INPUT, - self.tr('Reference layer'))) - self.addParameter(ParameterNumber(self.NUMBER, - self.tr('Constant value'), - default=1.0)) - - self.addOutput(OutputRaster(self.OUTPUT, - self.tr('Constant'))) + self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, + self.tr('Reference layer'))) + self.addParameter(QgsProcessingParameterNumber(self.NUMBER, + self.tr('Constant value'), QgsProcessingParameterNumber.Double, + defaultValue=1)) + self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Constant'))) def name(self): return 'createconstantrasterlayer' @@ -65,10 +61,10 @@ class CreateConstantRaster(QgisAlgorithm): return self.tr('Create constant raster layer') def processAlgorithm(self, parameters, context, feedback): - layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context) - value = self.getParameterValue(self.NUMBER) + layer = self.parameterAsRasterLayer(parameters, self.INPUT, context) + value = self.parameterAsDouble(parameters, self.NUMBER, context) - output = self.getOutputFromName(self.OUTPUT) + outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) raster = gdal.Open(layer.source(), gdal.GA_ReadOnly) geoTransform = raster.GetGeoTransform() @@ -76,7 +72,7 @@ class CreateConstantRaster(QgisAlgorithm): cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum()) \ / layer.width() - w = RasterWriter(output.getCompatibleFileName(self), + w = RasterWriter(outputFile, layer.extent().xMinimum(), layer.extent().yMinimum(), layer.extent().xMaximum(), @@ -88,3 +84,5 @@ class CreateConstantRaster(QgisAlgorithm): ) w.matrix.fill(value) w.close() + + return {self.OUTPUT: outputFile} diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index c72b4e57e14..a15b174383f 100644 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -49,6 +49,7 @@ from .BoundingBox import BoundingBox from .CheckValidity import CheckValidity from .ConcaveHull import ConcaveHull from .CreateAttributeIndex import CreateAttributeIndex +from .CreateConstantRaster import CreateConstantRaster from .Delaunay import Delaunay from .DeleteColumn import DeleteColumn from .DeleteHoles import DeleteHoles @@ -146,7 +147,6 @@ from .ZonalStatistics import ZonalStatistics # from .FieldsCalculator import FieldsCalculator # from .FieldPyculator import FieldsPyculator # from .JoinAttributes import JoinAttributes -# from .CreateConstantRaster import CreateConstantRaster # from .PointsDisplacement import PointsDisplacement # from .PointsFromPolygons import PointsFromPolygons # from .PointsFromLines import PointsFromLines @@ -205,7 +205,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider): # PointsFromLines(), PointsToPaths(), # SetVectorStyle(), SetRasterStyle(), # HypsometricCurves(), - # CreateConstantRaster(), + # # FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(), # OrientedMinimumBoundingBox(), # DefineProjection(), @@ -228,6 +228,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider): CheckValidity(), ConcaveHull(), CreateAttributeIndex(), + CreateConstantRaster(), Delaunay(), DeleteColumn(), DeleteHoles(), diff --git a/python/plugins/processing/tests/testdata/expected/constant_raster.tif b/python/plugins/processing/tests/testdata/expected/constant_raster.tif new file mode 100644 index 0000000000000000000000000000000000000000..cc388cedad49daf116ec735ff804fa0bc31a4e9c GIT binary patch literal 1232 zcmebD)MDUZU|Xqtfk6gIO)!)V6lUOS=3xNQ=YY7Wg@=I+NIwJO_3g|IDnMEcX#R$Fh?+Pc zdt*BjgA`D_4CEjXC`ffRs8n%K^>pHyEvDiC7Q;i_xbwlqp-sT?-nSzutxSQAFr|!O z-vGmf1sE(K2&I`=HnxiZ6)|#bY-eQa2Um-VKABo$V&qN(6Eu3 literal 0 HcmV?d00001 diff --git a/python/plugins/processing/tests/testdata/expected/constant_raster.tif.aux.xml b/python/plugins/processing/tests/testdata/expected/constant_raster.tif.aux.xml new file mode 100644 index 00000000000..5dea3552bd8 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/constant_raster.tif.aux.xml @@ -0,0 +1,10 @@ + + + + 3 + 3 + 3 + 0 + + + diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index a17ff63fad1..b5ca4f7e688 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -1215,6 +1215,19 @@ tests: # hash: 7fe0e0174185fd743e23760f33615adf10f771b4275f320db6f7f4f8 # type: rasterhash + - algorithm: qgis:createconstantrasterlayer + name: Create constant raster + params: + INPUT: + name: raster.tif + type: raster + NUMBER: 3.0 + results: + OUTPUT: + hash: 4cb3e82e8512cdbb75d9c39a10adc818dd6842c5dc6361fbc43dd9aa + type: rasterhash + + # Case 1: Keep all fields - algorithm: qgis:lineintersections name: Line Intersection Keep All Fields from Both