Skip to content
Permalink
Browse files

[processing] restore CreateConstantRaster algorithm (fix #14860)

  • Loading branch information
alexbruy committed May 23, 2016
1 parent 3809da6 commit 6207412bf731d718a16246b37de7dfcc6eec9d6f
@@ -25,6 +25,8 @@

__revision__ = '$Format:%H$'

from osgeo import gdal

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
@@ -46,18 +48,22 @@ def defineCharacteristics(self):
self.addParameter(ParameterRaster(self.INPUT,
self.tr('Reference layer')))
self.addParameter(ParameterNumber(self.NUMBER,
self.tr('Constant value'), default=1.0))
self.tr('Constant value'),
default=1.0))

self.addOutput(OutputRaster(self.OUTPUT,
self.tr('Constant')))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
value = self.getOutputValue(self.NUMBER)
value = self.getParameterValue(self.NUMBER)

output = self.getOutputFromName(self.OUTPUT)

raster = gdal.Open(layer.source(), gdal.GA_ReadOnly)
geoTransform = raster.GetGeoTransform()

cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum()) \
/ layer.width()

@@ -68,7 +74,8 @@ def processAlgorithm(self, progress):
layer.extent().yMaximum(),
cellsize,
1,
self.crs,
layer.crs(),
geoTransform
)
w.matrix[:] = value
w.matrix.fill(value)
w.close()
@@ -85,17 +85,18 @@ class RasterWriter:
NODATA = -99999.0

def __init__(self, fileName, minx, miny, maxx, maxy, cellsize,
nbands, crs):
nbands, crs, geotransform=None):
self.fileName = fileName
self.nx = int((maxx - minx) / float(cellsize))
self.ny = int((maxy - miny) / float(cellsize))
self.nbands = nbands
self.matrix = numpy.ones(shape=(self.ny, self.nx), dtype=numpy.float32)
self.matrix[:] = self.NODATA
self.matrix = numpy.empty(shape=(self.ny, self.nx), dtype=numpy.float32)
self.matrix.fill(self.NODATA)
self.cellsize = cellsize
self.crs = crs
self.minx = minx
self.maxy = maxy
self.geotransform = geotransform

def setValue(self, value, x, y, band=0):
try:
@@ -115,7 +116,10 @@ def close(self):
dst_ds = driver.Create(self.fileName, self.nx, self.ny, 1,
gdal.GDT_Float32)
dst_ds.SetProjection(str(self.crs.toWkt()))
dst_ds.SetGeoTransform([self.minx, self.cellsize, 0,
self.maxy, self.cellsize, 0])
if self.geotransform is None:
dst_ds.SetGeoTransform([self.minx, self.cellsize, 0,
self.maxy, self.cellsize, 0])
else:
dst_ds.SetGeoTransform(self.geotransform)
dst_ds.GetRasterBand(1).WriteArray(self.matrix)
dst_ds = None

0 comments on commit 6207412

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