-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] implement missed functionality from Raster terrain analysis
plugin
- Loading branch information
Showing
5 changed files
with
556 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Oops, something went wrong.