Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] implement missed functionality from Raster terrain analysis
plugin
  • Loading branch information
alexbruy committed Dec 19, 2016
1 parent f9db068 commit 0d1c9a3
Show file tree
Hide file tree
Showing 5 changed files with 556 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -167,6 +167,7 @@
from .Ruggedness import Ruggedness
from .Hillshade import Hillshade
from .ReliefAuto import ReliefAuto
from .Relief import Relief
from .IdwInterpolationZValue import IdwInterpolationZValue
from .IdwInterpolationAttribute import IdwInterpolationAttribute
from .TinInterpolationZValue import TinInterpolationZValue
Expand Down Expand Up @@ -247,7 +248,7 @@ def __init__(self):
OffsetLine(), PolygonCentroids(), Translate(),
SingleSidedBuffer(), PointsAlongGeometry(),
Aspect(), Slope(), Ruggedness(), Hillshade(),
ReliefAuto(), ZonalStatisticsQgis(),
ReliefAuto(), Relief(), ZonalStatisticsQgis(),
IdwInterpolationZValue(), IdwInterpolationAttribute(),
TinInterpolationZValue(), TinInterpolationAttribute(),
RemoveNullGeometry(), ExtractByExpression(),
Expand Down
137 changes: 137 additions & 0 deletions python/plugins/processing/algs/qgis/Relief.py
@@ -0,0 +1,137 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
Relief.py
---------------------
Date : December 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy 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__ = 'Alexander Bruy'
__date__ = 'December 2016'
__copyright__ = '(C) 2016, Alexander Bruy'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import os

from qgis.PyQt.QtGui import QIcon, QColor

from qgis.analysis import QgsRelief

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import (Parameter,
ParameterRaster,
ParameterNumber,
_splitParameterOptions)
from processing.core.outputs import OutputRaster, OutputTable
from processing.tools import raster

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


class Relief(GeoAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
Z_FACTOR = 'Z_FACTOR'
COLORS = 'COLORS'
OUTPUT_LAYER = 'OUTPUT_LAYER'
FREQUENCY_DISTRIBUTION = 'FREQUENCY_DISTRIBUTION'

def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Relief')
self.group, self.i18n_group = self.trAlgorithm('Raster terrain analysis')

class ParameterReliefColors(Parameter):
default_metadata = {
'widget_wrapper': 'processing.algs.qgis.ui.ReliefColorsWidget.ReliefColorsWidgetWrapper'
}

def __init__(self, name='', description='', parent=None):
Parameter.__init__(self, name, description)
self.parent = parent

def setValue(self, value):
if value is None:
return False

if isinstance(value, str):
self.value = value
else:
self.value = ParameterReliefColors.colorsToString(value)
return True

def getValueAsCommandLineParameter(self):
return '"{}"'.format(self.value)

def getAsScriptCode(self):
param_type = ''
param_type += 'relief colors '
return '##' + self.name + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
parent = definition.lower().strip()[len('relief colors') + 1:]
return ParameterReliefColors(name, description, parent)

@staticmethod
def colorsToString(colors):
s = ''
for c in colors:
s += '{:.2f}, {:.2f}, {:d}, {:d}, {:d};'.format(c[0],
c[1],
c[2],
c[3],
c[4])
return s[:-1]

self.addParameter(ParameterRaster(self.INPUT_LAYER,
self.tr('Elevation layer')))
self.addParameter(ParameterNumber(self.Z_FACTOR,
self.tr('Z factor'), 1.0, 999999.99, 1.0))
self.addParameter(ParameterReliefColors(self.COLORS,
self.tr('Relief colors'), self.INPUT_LAYER))
self.addOutput(OutputRaster(self.OUTPUT_LAYER,
self.tr('Relief')))
self.addOutput(OutputTable(self.FREQUENCY_DISTRIBUTION,
self.tr('Frequency distribution')))

def processAlgorithm(self, progress):
inputFile = self.getParameterValue(self.INPUT_LAYER)
zFactor = self.getParameterValue(self.Z_FACTOR)
colors = self.getParameterValue(self.COLORS).split(';')
outputFile = self.getOutputValue(self.OUTPUT_LAYER)
frequencyDistribution = self.getOutputValue(self.FREQUENCY_DISTRIBUTION)

outputFormat = raster.formatShortNameFromFileName(outputFile)

reliefColors = []
for c in colors:
v = c.split(',')
color = QgsRelief.ReliefColor(QColor(int(v[2]), int(v[3]), int(v[4])),
float(v[0]),
float(v[1]))
reliefColors.append(color)

relief = QgsRelief(inputFile, outputFile, outputFormat)
relief.setReliefColors(reliefColors)
relief.setZFactor(zFactor)
relief.exportFrequencyDistributionToCsv(frequencyDistribution)
relief.processRaster(None)

0 comments on commit 0d1c9a3

Please sign in to comment.