Skip to content

Commit

Permalink
Port Create Constant Raster to new API and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 27, 2017
1 parent e23617a commit 8218204
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
32 changes: 15 additions & 17 deletions python/plugins/processing/algs/qgis/CreateConstantRaster.py
Expand Up @@ -27,12 +27,10 @@

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


Expand All @@ -49,14 +47,12 @@ def __init__(self):
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'
Expand All @@ -65,18 +61,18 @@ def displayName(self):
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()

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(),
Expand All @@ -88,3 +84,5 @@ def processAlgorithm(self, parameters, context, feedback):
)
w.matrix.fill(value)
w.close()

return {self.OUTPUT: outputFile}
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -49,6 +49,7 @@
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
Expand Down Expand Up @@ -146,7 +147,6 @@
# 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
Expand Down Expand Up @@ -205,7 +205,7 @@ def getAlgs(self):
# PointsFromLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(),
# HypsometricCurves(),
# CreateConstantRaster(),
#
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(),
# DefineProjection(),
Expand All @@ -228,6 +228,7 @@ def getAlgs(self):
CheckValidity(),
ConcaveHull(),
CreateAttributeIndex(),
CreateConstantRaster(),
Delaunay(),
DeleteColumn(),
DeleteHoles(),
Expand Down
Binary file not shown.
@@ -0,0 +1,10 @@
<PAMDataset>
<PAMRasterBand band="1">
<Metadata>
<MDI key="STATISTICS_MAXIMUM">3</MDI>
<MDI key="STATISTICS_MEAN">3</MDI>
<MDI key="STATISTICS_MINIMUM">3</MDI>
<MDI key="STATISTICS_STDDEV">0</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>
13 changes: 13 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -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
Expand Down

0 comments on commit 8218204

Please sign in to comment.