Skip to content

Commit

Permalink
Merge pull request #4948 from alexbruy/processing-raster
Browse files Browse the repository at this point in the history
[processing] restore and improve some raster algorithms
  • Loading branch information
alexbruy committed Aug 1, 2017
2 parents 0328b7a + 79adad7 commit c373c8b
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 215 deletions.
9 changes: 9 additions & 0 deletions python/core/raster/qgsrasterfilewriter.sip
Expand Up @@ -156,6 +156,15 @@ class QgsRasterFileWriter
:rtype: list of str
%End

static QString driverForExtension( const QString &extension );
%Docstring
Returns the GDAL driver name for a specified file ``extension``. E.g. the
driver name for the ".tif" extension is "GTiff".
If no suitable drivers are found then an empty string is returned.
.. versionadded:: 3.0
:rtype: str
%End

};

/************************************************************************
Expand Down
4 changes: 1 addition & 3 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -467,9 +467,7 @@ qgis:rasterlayerhistogram: >
The raster layer must have a single band.

qgis:rasterlayerstatistics: >
This algorithm computes basic statistics from the values in a raster layer.

The raster layer must have a single band.
This algorithm computes basic statistics from the values in a given band of the raster layer.

qgis:refactorfields: >
This algorithm allows editing the structure of the attributes table of a vector layer. Fields can be modified in their type and name, using a fields mapping.
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/Aspect.py
Expand Up @@ -30,11 +30,11 @@
from qgis.PyQt.QtGui import QIcon

from qgis.analysis import QgsAspectFilter
from qgis.core import (QgsProcessingParameterRasterLayer,
from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import raster
from processing.tools.dataobjects import exportRasterLayer

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
Expand Down Expand Up @@ -75,7 +75,7 @@ def processAlgorithm(self, parameters, context, feedback):

outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

outputFormat = raster.formatShortNameFromFileName(outputFile)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])

aspect = QgsAspectFilter(inputFile, outputFile, outputFormat)
aspect.setZFactor(zFactor)
Expand Down
78 changes: 50 additions & 28 deletions python/plugins/processing/algs/qgis/CreateConstantRaster.py
Expand Up @@ -25,20 +25,27 @@

__revision__ = '$Format:%H$'

from osgeo import gdal

from qgis.core import (QgsProcessingParameterRasterLayer,
import os
import math
import struct

from qgis.core import (Qgis,
QgsRasterBlock,
QgsRasterFileWriter,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
QgsProcessingParameterCrs,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools.raster import RasterWriter


class CreateConstantRaster(QgisAlgorithm):

INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
EXTENT = 'EXTENT'
TARGET_CRS = 'TARGET_CRS'
PIXEL_SIZE = 'PIXEL_SIZE'
NUMBER = 'NUMBER'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Raster tools')
Expand All @@ -47,10 +54,18 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Reference layer')))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Desired extent')))
self.addParameter(QgsProcessingParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'),
'ProjectCrs'))
self.addParameter(QgsProcessingParameterNumber(self.PIXEL_SIZE,
self.tr('Pixel size'),
QgsProcessingParameterNumber.Double,
0.1, False, 0.01, 999))
self.addParameter(QgsProcessingParameterNumber(self.NUMBER,
self.tr('Constant value'), QgsProcessingParameterNumber.Double,
self.tr('Constant value'),
QgsProcessingParameterNumber.Double,
defaultValue=1))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Constant')))

Expand All @@ -61,28 +76,35 @@ def displayName(self):
return self.tr('Create constant raster layer')

def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
extent = self.parameterAsExtent(parameters, self.EXTENT, context)
crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
value = self.parameterAsDouble(parameters, self.NUMBER, context)
pixelSize = self.parameterAsDouble(parameters, self.PIXEL_SIZE, context)

outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])

rows = max([math.ceil(extent.height() / pixelSize) + 1, 1.0])
cols = max([math.ceil(extent.width() / pixelSize) + 1, 1.0])

writer = QgsRasterFileWriter(outputFile)
writer.setOutputProviderKey('gdal')
writer.setOutputFormat(outputFormat)
provider = writer.createOneBandRaster(Qgis.Float32, cols, rows, extent, crs)
provider.setNoDataValue(1, -9999)

data = [value] * cols
block = QgsRasterBlock(Qgis.Float32, cols, 1)
block.setData(struct.pack('{}f'.format(len(data)), *data))

total = 100.0 / rows if rows else 0
for i in range(rows):
if feedback.isCanceled():
break

provider.writeBlock(block, 1, 0, i)
feedback.setProgress(int(i * rows))

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

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

w = RasterWriter(outputFile,
layer.extent().xMinimum(),
layer.extent().yMinimum(),
layer.extent().xMaximum(),
layer.extent().yMaximum(),
cellsize,
1,
layer.crs(),
geoTransform
)
w.matrix.fill(value)
w.close()
provider.setEditable(False)

return {self.OUTPUT: outputFile}
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Heatmap.py
Expand Up @@ -31,6 +31,7 @@
from qgis.PyQt.QtGui import QIcon

from qgis.core import (QgsFeatureRequest,
QgsRasterFileWriter,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
Expand All @@ -43,7 +44,6 @@
from qgis.analysis import QgsKernelDensityEstimation

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import raster

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand Down Expand Up @@ -175,7 +175,7 @@ def processAlgorithm(self, parameters, context, feedback):
decay = self.parameterAsDouble(parameters, self.DECAY, context)
output_values = self.parameterAsEnum(parameters, self.OUTPUT_VALUE, context)
outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
output_format = raster.formatShortNameFromFileName(outputFile)
output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])
weight_field = self.parameterAsString(parameters, self.WEIGHT_FIELD, context)
radius_field = self.parameterAsString(parameters, self.RADIUS_FIELD, context)

Expand Down
7 changes: 3 additions & 4 deletions python/plugins/processing/algs/qgis/Hillshade.py
Expand Up @@ -30,11 +30,11 @@
from qgis.PyQt.QtGui import QIcon

from qgis.analysis import QgsHillshadeFilter
from qgis.core import (QgsProcessingParameterRasterLayer,
from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import raster
from processing.tools.dataobjects import exportRasterLayer

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
Expand Down Expand Up @@ -84,8 +84,7 @@ def processAlgorithm(self, parameters, context, feedback):
vAngle = self.parameterAsDouble(parameters, self.V_ANGLE, context)

outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

outputFormat = raster.formatShortNameFromFileName(outputFile)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])

hillshade = QgsHillshadeFilter(inputFile, outputFile, outputFormat, azimuth, vAngle)
hillshade.setZFactor(zFactor)
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -99,6 +99,7 @@
from .RandomPointsExtent import RandomPointsExtent
from .RandomPointsLayer import RandomPointsLayer
from .RandomPointsPolygons import RandomPointsPolygons
from .RasterLayerStatistics import RasterLayerStatistics
from .RegularPoints import RegularPoints
from .ReverseLineDirection import ReverseLineDirection
from .Ruggedness import Ruggedness
Expand Down Expand Up @@ -144,7 +145,6 @@
# from .HubDistanceLines import HubDistanceLines
# from .HubLines import HubLines
# from .GeometryConvert import GeometryConvert
# from .RasterLayerStatistics import RasterLayerStatistics
# from .StatisticsByCategories import StatisticsByCategories
# from .FieldsCalculator import FieldsCalculator
# from .FieldPyculator import FieldsPyculator
Expand Down Expand Up @@ -270,6 +270,7 @@ def getAlgs(self):
RandomPointsExtent(),
RandomPointsLayer(),
RandomPointsPolygons(),
RasterLayerStatistics(),
RegularPoints(),
ReverseLineDirection(),
Ruggedness(),
Expand Down

0 comments on commit c373c8b

Please sign in to comment.