|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + Relief.py |
| 6 | + --------------------- |
| 7 | + Date : December 2016 |
| 8 | + Copyright : (C) 2016 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'December 2016' |
| 22 | +__copyright__ = '(C) 2016, Alexander Bruy' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +import os |
| 29 | + |
| 30 | +from qgis.PyQt.QtGui import QIcon, QColor |
| 31 | + |
| 32 | +from qgis.analysis import QgsRelief |
| 33 | + |
| 34 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 35 | +from processing.core.parameters import (Parameter, |
| 36 | + ParameterRaster, |
| 37 | + ParameterNumber, |
| 38 | + _splitParameterOptions) |
| 39 | +from processing.core.outputs import OutputRaster, OutputTable |
| 40 | +from processing.tools import raster |
| 41 | + |
| 42 | +pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] |
| 43 | + |
| 44 | + |
| 45 | +class Relief(GeoAlgorithm): |
| 46 | + |
| 47 | + INPUT_LAYER = 'INPUT_LAYER' |
| 48 | + Z_FACTOR = 'Z_FACTOR' |
| 49 | + COLORS = 'COLORS' |
| 50 | + OUTPUT_LAYER = 'OUTPUT_LAYER' |
| 51 | + FREQUENCY_DISTRIBUTION = 'FREQUENCY_DISTRIBUTION' |
| 52 | + |
| 53 | + def getIcon(self): |
| 54 | + return QIcon(os.path.join(pluginPath, 'images', 'dem.png')) |
| 55 | + |
| 56 | + def defineCharacteristics(self): |
| 57 | + self.name, self.i18n_name = self.trAlgorithm('Relief') |
| 58 | + self.group, self.i18n_group = self.trAlgorithm('Raster terrain analysis') |
| 59 | + |
| 60 | + class ParameterReliefColors(Parameter): |
| 61 | + default_metadata = { |
| 62 | + 'widget_wrapper': 'processing.algs.qgis.ui.ReliefColorsWidget.ReliefColorsWidgetWrapper' |
| 63 | + } |
| 64 | + |
| 65 | + def __init__(self, name='', description='', parent=None): |
| 66 | + Parameter.__init__(self, name, description) |
| 67 | + self.parent = parent |
| 68 | + |
| 69 | + def setValue(self, value): |
| 70 | + if value is None: |
| 71 | + return False |
| 72 | + |
| 73 | + if isinstance(value, str): |
| 74 | + self.value = value |
| 75 | + else: |
| 76 | + self.value = ParameterReliefColors.colorsToString(value) |
| 77 | + return True |
| 78 | + |
| 79 | + def getValueAsCommandLineParameter(self): |
| 80 | + return '"{}"'.format(self.value) |
| 81 | + |
| 82 | + def getAsScriptCode(self): |
| 83 | + param_type = '' |
| 84 | + param_type += 'relief colors ' |
| 85 | + return '##' + self.name + '=' + param_type |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def fromScriptCode(self, line): |
| 89 | + isOptional, name, definition = _splitParameterOptions(line) |
| 90 | + descName = _createDescriptiveName(name) |
| 91 | + parent = definition.lower().strip()[len('relief colors') + 1:] |
| 92 | + return ParameterReliefColors(name, description, parent) |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + def colorsToString(colors): |
| 96 | + s = '' |
| 97 | + for c in colors: |
| 98 | + s += '{:.2f}, {:.2f}, {:d}, {:d}, {:d};'.format(c[0], |
| 99 | + c[1], |
| 100 | + c[2], |
| 101 | + c[3], |
| 102 | + c[4]) |
| 103 | + return s[:-1] |
| 104 | + |
| 105 | + self.addParameter(ParameterRaster(self.INPUT_LAYER, |
| 106 | + self.tr('Elevation layer'))) |
| 107 | + self.addParameter(ParameterNumber(self.Z_FACTOR, |
| 108 | + self.tr('Z factor'), 1.0, 999999.99, 1.0)) |
| 109 | + self.addParameter(ParameterReliefColors(self.COLORS, |
| 110 | + self.tr('Relief colors'), self.INPUT_LAYER)) |
| 111 | + self.addOutput(OutputRaster(self.OUTPUT_LAYER, |
| 112 | + self.tr('Relief'))) |
| 113 | + self.addOutput(OutputTable(self.FREQUENCY_DISTRIBUTION, |
| 114 | + self.tr('Frequency distribution'))) |
| 115 | + |
| 116 | + def processAlgorithm(self, progress): |
| 117 | + inputFile = self.getParameterValue(self.INPUT_LAYER) |
| 118 | + zFactor = self.getParameterValue(self.Z_FACTOR) |
| 119 | + colors = self.getParameterValue(self.COLORS).split(';') |
| 120 | + outputFile = self.getOutputValue(self.OUTPUT_LAYER) |
| 121 | + frequencyDistribution = self.getOutputValue(self.FREQUENCY_DISTRIBUTION) |
| 122 | + |
| 123 | + outputFormat = raster.formatShortNameFromFileName(outputFile) |
| 124 | + |
| 125 | + reliefColors = [] |
| 126 | + for c in colors: |
| 127 | + v = c.split(',') |
| 128 | + color = QgsRelief.ReliefColor(QColor(int(v[2]), int(v[3]), int(v[4])), |
| 129 | + float(v[0]), |
| 130 | + float(v[1])) |
| 131 | + reliefColors.append(color) |
| 132 | + |
| 133 | + relief = QgsRelief(inputFile, outputFile, outputFormat) |
| 134 | + relief.setReliefColors(reliefColors) |
| 135 | + relief.setZFactor(zFactor) |
| 136 | + relief.exportFrequencyDistributionToCsv(frequencyDistribution) |
| 137 | + relief.processRaster(None) |
0 commit comments