2013-09-14 14:35:14 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
contour.py
|
|
|
|
---------------------
|
|
|
|
Date : September 2013
|
|
|
|
Copyright : (C) 2013 by Alexander Bruy
|
|
|
|
Email : alexander 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__ = 'September 2013'
|
|
|
|
__copyright__ = '(C) 2013, Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-08-31 16:11:12 +03:00
|
|
|
from qgis.core import (QgsProcessing,
|
2018-05-06 10:29:01 +10:00
|
|
|
QgsProcessingException,
|
2017-08-31 16:11:12 +03:00
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingParameterRasterLayer,
|
|
|
|
QgsProcessingParameterBand,
|
|
|
|
QgsProcessingParameterString,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterBoolean,
|
|
|
|
QgsProcessingParameterVectorDestination)
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2017-08-31 16:11:12 +03:00
|
|
|
from processing.tools.system import isWindows
|
2014-04-17 01:41:25 +02:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
2013-09-14 14:35:14 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
class contour(GdalAlgorithm):
|
2013-09-14 14:35:14 +03:00
|
|
|
|
2017-08-31 16:11:12 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
BAND = 'BAND'
|
2013-10-01 20:52:22 +03:00
|
|
|
INTERVAL = 'INTERVAL'
|
|
|
|
FIELD_NAME = 'FIELD_NAME'
|
2017-08-31 16:11:12 +03:00
|
|
|
CREATE_3D = 'CREATE_3D'
|
|
|
|
IGNORE_NODATA = 'IGNORE_NODATA'
|
|
|
|
NODATA = 'NODATA'
|
|
|
|
OFFSET = 'OFFSET'
|
2019-06-24 13:57:22 +03:00
|
|
|
EXTRA = 'EXTRA'
|
2018-09-11 19:36:06 +01:00
|
|
|
OPTIONS = 'OPTIONS'
|
2017-08-31 16:11:12 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
2013-09-14 14:35:14 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-27 17:20:57 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-09-06 16:59:48 +03:00
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterBand(self.BAND,
|
|
|
|
self.tr('Band number'),
|
2018-12-30 09:18:23 +02:00
|
|
|
1,
|
2017-09-06 16:59:48 +03:00
|
|
|
parentLayerParameterName=self.INPUT))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.INTERVAL,
|
|
|
|
self.tr('Interval between contour lines'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
|
|
|
minValue=0.0,
|
|
|
|
defaultValue=10.0))
|
|
|
|
self.addParameter(QgsProcessingParameterString(self.FIELD_NAME,
|
|
|
|
self.tr('Attribute name (if not set, no elevation attribute is attached)'),
|
|
|
|
defaultValue='ELEV',
|
|
|
|
optional=True))
|
2017-08-31 16:11:12 +03:00
|
|
|
|
|
|
|
create_3d_param = QgsProcessingParameterBoolean(self.CREATE_3D,
|
|
|
|
self.tr('Produce 3D vector'),
|
|
|
|
defaultValue=False)
|
|
|
|
create_3d_param.setFlags(create_3d_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(create_3d_param)
|
|
|
|
|
|
|
|
ignore_nodata_param = QgsProcessingParameterBoolean(self.IGNORE_NODATA,
|
|
|
|
self.tr('Treat all raster values as valid'),
|
|
|
|
defaultValue=False)
|
|
|
|
ignore_nodata_param.setFlags(ignore_nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(ignore_nodata_param)
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
nodata_param = QgsProcessingParameterNumber(self.NODATA,
|
|
|
|
self.tr('Input pixel value to treat as "nodata"'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
2018-05-17 13:01:55 +02:00
|
|
|
defaultValue=None,
|
2017-09-06 16:59:48 +03:00
|
|
|
optional=True)
|
2017-08-31 16:11:12 +03:00
|
|
|
nodata_param.setFlags(nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(nodata_param)
|
|
|
|
|
2017-09-06 16:59:48 +03:00
|
|
|
offset_param = QgsProcessingParameterNumber(self.OFFSET,
|
|
|
|
self.tr('Offset from zero relative to which to interpret intervals'),
|
|
|
|
type=QgsProcessingParameterNumber.Double,
|
|
|
|
defaultValue=0.0,
|
|
|
|
optional=True)
|
2017-08-31 16:11:12 +03:00
|
|
|
nodata_param.setFlags(offset_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(offset_param)
|
|
|
|
|
2019-06-24 13:57:22 +03:00
|
|
|
extra_param = QgsProcessingParameterString(self.EXTRA,
|
|
|
|
self.tr('Additional command-line parameters'),
|
|
|
|
defaultValue=None,
|
|
|
|
optional=True)
|
|
|
|
extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(extra_param)
|
|
|
|
|
|
|
|
# TODO: remove in QGIS 4
|
2018-09-11 19:36:06 +01:00
|
|
|
options_param = QgsProcessingParameterString(self.OPTIONS,
|
|
|
|
self.tr('Additional creation options'),
|
|
|
|
defaultValue='',
|
|
|
|
optional=True)
|
2019-06-24 13:57:22 +03:00
|
|
|
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
|
2018-09-11 19:36:06 +01:00
|
|
|
self.addParameter(options_param)
|
|
|
|
|
2017-08-31 16:11:12 +03:00
|
|
|
self.addParameter(QgsProcessingParameterVectorDestination(
|
|
|
|
self.OUTPUT, self.tr('Contours'), QgsProcessing.TypeVectorLine))
|
2013-09-14 14:35:14 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'contour'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Contour')
|
|
|
|
|
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'contour.png'))
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster extraction')
|
|
|
|
|
2017-12-14 12:05:40 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'rasterextraction'
|
|
|
|
|
2018-05-05 19:18:41 +10:00
|
|
|
def commandName(self):
|
|
|
|
return 'gdal_contour'
|
|
|
|
|
2017-12-08 13:51:30 +10:00
|
|
|
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
2017-08-31 16:11:12 +03:00
|
|
|
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
2018-05-06 10:29:01 +10:00
|
|
|
if inLayer is None:
|
|
|
|
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
|
|
|
|
|
2017-08-31 16:11:12 +03:00
|
|
|
fieldName = self.parameterAsString(parameters, self.FIELD_NAME, context)
|
2018-05-05 20:19:00 +10:00
|
|
|
if self.NODATA in parameters and parameters[self.NODATA] is not None:
|
|
|
|
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
|
|
|
|
else:
|
|
|
|
nodata = None
|
2017-08-31 16:11:12 +03:00
|
|
|
offset = self.parameterAsDouble(parameters, self.OFFSET, context)
|
|
|
|
|
|
|
|
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
2019-02-05 12:48:43 +10:00
|
|
|
self.setOutputValue(self.OUTPUT, outFile)
|
2017-09-06 16:59:48 +03:00
|
|
|
output, outFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
|
2013-09-14 14:35:14 +03:00
|
|
|
|
|
|
|
arguments = []
|
2017-09-06 16:59:48 +03:00
|
|
|
arguments.append('-b')
|
|
|
|
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
|
|
|
|
|
2017-08-31 16:11:12 +03:00
|
|
|
if fieldName:
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-a')
|
2013-09-14 14:35:14 +03:00
|
|
|
arguments.append(fieldName)
|
2017-08-31 16:11:12 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
arguments.append('-i')
|
2017-09-06 16:59:48 +03:00
|
|
|
arguments.append(str(self.parameterAsDouble(parameters, self.INTERVAL, context)))
|
2013-09-14 14:35:14 +03:00
|
|
|
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.CREATE_3D, context):
|
2017-08-31 16:11:12 +03:00
|
|
|
arguments.append('-3d')
|
|
|
|
|
2019-04-16 08:30:00 +02:00
|
|
|
if self.parameterAsBoolean(parameters, self.IGNORE_NODATA, context):
|
2017-08-31 16:11:12 +03:00
|
|
|
arguments.append('-inodata')
|
|
|
|
|
2018-05-05 20:19:00 +10:00
|
|
|
if nodata is not None:
|
2017-08-31 16:11:12 +03:00
|
|
|
arguments.append('-snodata {}'.format(nodata))
|
|
|
|
|
|
|
|
if offset:
|
|
|
|
arguments.append('-off {}'.format(offset))
|
|
|
|
|
2019-06-24 13:57:22 +03:00
|
|
|
if outFormat:
|
|
|
|
arguments.append('-f {}'.format(outFormat))
|
|
|
|
|
|
|
|
if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
|
|
|
|
extra = self.parameterAsString(parameters, self.EXTRA, context)
|
|
|
|
arguments.append(extra)
|
|
|
|
|
|
|
|
# TODO: remove in QGIS 4
|
2018-09-11 19:36:06 +01:00
|
|
|
options = self.parameterAsString(parameters, self.OPTIONS, context)
|
|
|
|
if options:
|
|
|
|
arguments.append(options)
|
|
|
|
|
2017-08-31 16:11:12 +03:00
|
|
|
arguments.append(inLayer.source())
|
2016-06-22 11:04:10 +03:00
|
|
|
arguments.append(output)
|
2013-09-14 14:35:14 +03:00
|
|
|
|
2018-05-05 19:18:41 +10:00
|
|
|
return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
|