2013-02-03 10:27:42 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2013-10-01 20:52:22 +03:00
|
|
|
CreateConstantRaster.py
|
2013-02-03 10:27:42 +01:00
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-03 10:27:42 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-03 10:27:42 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-05-23 12:38:05 +03:00
|
|
|
from osgeo import gdal
|
|
|
|
|
2017-05-02 14:36:23 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterRaster
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.outputs import OutputRaster
|
2015-01-17 14:57:21 +02:00
|
|
|
from processing.tools.raster import RasterWriter
|
2013-02-03 10:27:42 +01:00
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class CreateConstantRaster(QgisAlgorithm):
|
2013-02-03 10:27:42 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
NUMBER = 'NUMBER'
|
2013-02-03 10:27:42 +01:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-17 14:57:21 +02:00
|
|
|
self.addParameter(ParameterRaster(self.INPUT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Reference layer')))
|
2015-01-17 14:57:21 +02:00
|
|
|
self.addParameter(ParameterNumber(self.NUMBER,
|
2016-05-23 12:38:05 +03:00
|
|
|
self.tr('Constant value'),
|
|
|
|
default=1.0))
|
2015-01-17 14:57:21 +02:00
|
|
|
|
|
|
|
self.addOutput(OutputRaster(self.OUTPUT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Constant')))
|
2015-01-17 14:57:21 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'createconstantrasterlayer'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Create constant raster layer')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2016-05-23 12:38:05 +03:00
|
|
|
value = self.getParameterValue(self.NUMBER)
|
2015-01-17 14:57:21 +02:00
|
|
|
|
|
|
|
output = self.getOutputFromName(self.OUTPUT)
|
|
|
|
|
2016-05-23 12:38:05 +03:00
|
|
|
raster = gdal.Open(layer.source(), gdal.GA_ReadOnly)
|
|
|
|
geoTransform = raster.GetGeoTransform()
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum()) \
|
|
|
|
/ layer.width()
|
2015-01-17 14:57:21 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
w = RasterWriter(output.getCompatibleFileName(self),
|
|
|
|
layer.extent().xMinimum(),
|
|
|
|
layer.extent().yMinimum(),
|
|
|
|
layer.extent().xMaximum(),
|
|
|
|
layer.extent().yMaximum(),
|
|
|
|
cellsize,
|
|
|
|
1,
|
2016-05-23 12:38:05 +03:00
|
|
|
layer.crs(),
|
|
|
|
geoTransform
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
)
|
2016-05-23 12:38:05 +03:00
|
|
|
w.matrix.fill(value)
|
2013-02-03 10:27:42 +01:00
|
|
|
w.close()
|