Skip to content
Permalink
Browse files

Port Create Constant Raster to new API and add test

  • Loading branch information
nyalldawson committed Jul 27, 2017
1 parent e23617a commit 82182040ab7a6756ab666cb4176b02fa35582ede
@@ -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


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

return {self.OUTPUT: outputFile}
@@ -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
@@ -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
@@ -205,7 +205,7 @@ def getAlgs(self):
# PointsFromLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(),
# HypsometricCurves(),
# CreateConstantRaster(),
#
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(),
# DefineProjection(),
@@ -228,6 +228,7 @@ def getAlgs(self):
CheckValidity(),
ConcaveHull(),
CreateAttributeIndex(),
CreateConstantRaster(),
Delaunay(),
DeleteColumn(),
DeleteHoles(),
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>
@@ -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

0 comments on commit 8218204

Please sign in to comment.
You can’t perform that action at this time.