contour.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. """
  2. ***************************************************************************
  3. contour.py
  4. ---------------------
  5. Date : September 2013
  6. Copyright : (C) 2013 by Alexander Bruy
  7. Email : alexander bruy at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Alexander Bruy'
  18. __date__ = 'September 2013'
  19. __copyright__ = '(C) 2013, Alexander Bruy'
  20. import os
  21. from qgis.PyQt.QtGui import QIcon
  22. from qgis.core import (QgsProcessing,
  23. QgsProcessingException,
  24. QgsProcessingParameterDefinition,
  25. QgsProcessingParameterRasterLayer,
  26. QgsProcessingParameterBand,
  27. QgsProcessingParameterString,
  28. QgsProcessingParameterNumber,
  29. QgsProcessingParameterBoolean,
  30. QgsProcessingParameterVectorDestination)
  31. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  32. from processing.tools.system import isWindows
  33. from processing.algs.gdal.GdalUtils import GdalUtils
  34. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  35. class contour(GdalAlgorithm):
  36. INPUT = 'INPUT'
  37. BAND = 'BAND'
  38. INTERVAL = 'INTERVAL'
  39. FIELD_NAME = 'FIELD_NAME'
  40. CREATE_3D = 'CREATE_3D'
  41. IGNORE_NODATA = 'IGNORE_NODATA'
  42. NODATA = 'NODATA'
  43. OFFSET = 'OFFSET'
  44. EXTRA = 'EXTRA'
  45. OPTIONS = 'OPTIONS'
  46. OUTPUT = 'OUTPUT'
  47. def __init__(self):
  48. super().__init__()
  49. def initAlgorithm(self, config=None):
  50. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
  51. self.tr('Input layer')))
  52. self.addParameter(QgsProcessingParameterBand(self.BAND,
  53. self.tr('Band number'),
  54. 1,
  55. parentLayerParameterName=self.INPUT))
  56. self.addParameter(QgsProcessingParameterNumber(self.INTERVAL,
  57. self.tr('Interval between contour lines'),
  58. type=QgsProcessingParameterNumber.Double,
  59. minValue=0.0,
  60. defaultValue=10.0))
  61. self.addParameter(QgsProcessingParameterString(self.FIELD_NAME,
  62. self.tr('Attribute name (if not set, no elevation attribute is attached)'),
  63. defaultValue='ELEV',
  64. optional=True))
  65. create_3d_param = QgsProcessingParameterBoolean(self.CREATE_3D,
  66. self.tr('Produce 3D vector'),
  67. defaultValue=False)
  68. create_3d_param.setFlags(create_3d_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  69. self.addParameter(create_3d_param)
  70. ignore_nodata_param = QgsProcessingParameterBoolean(self.IGNORE_NODATA,
  71. self.tr('Treat all raster values as valid'),
  72. defaultValue=False)
  73. ignore_nodata_param.setFlags(ignore_nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  74. self.addParameter(ignore_nodata_param)
  75. nodata_param = QgsProcessingParameterNumber(self.NODATA,
  76. self.tr('Input pixel value to treat as "nodata"'),
  77. type=QgsProcessingParameterNumber.Double,
  78. defaultValue=None,
  79. optional=True)
  80. nodata_param.setFlags(nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  81. self.addParameter(nodata_param)
  82. offset_param = QgsProcessingParameterNumber(self.OFFSET,
  83. self.tr('Offset from zero relative to which to interpret intervals'),
  84. type=QgsProcessingParameterNumber.Double,
  85. defaultValue=0.0,
  86. optional=True)
  87. nodata_param.setFlags(offset_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  88. self.addParameter(offset_param)
  89. extra_param = QgsProcessingParameterString(self.EXTRA,
  90. self.tr('Additional command-line parameters'),
  91. defaultValue=None,
  92. optional=True)
  93. extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  94. self.addParameter(extra_param)
  95. # TODO: remove in QGIS 4
  96. options_param = QgsProcessingParameterString(self.OPTIONS,
  97. self.tr('Additional creation options'),
  98. defaultValue='',
  99. optional=True)
  100. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagHidden)
  101. self.addParameter(options_param)
  102. self.addParameter(QgsProcessingParameterVectorDestination(
  103. self.OUTPUT, self.tr('Contours'), QgsProcessing.TypeVectorLine))
  104. def name(self):
  105. return 'contour'
  106. def displayName(self):
  107. return self.tr('Contour')
  108. def icon(self):
  109. return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'contour.png'))
  110. def group(self):
  111. return self.tr('Raster extraction')
  112. def groupId(self):
  113. return 'rasterextraction'
  114. def commandName(self):
  115. return 'gdal_contour'
  116. def _buildArgsList(self, parameters, context, feedback, executing):
  117. inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  118. if inLayer is None:
  119. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  120. fieldName = self.parameterAsString(parameters, self.FIELD_NAME, context)
  121. if self.NODATA in parameters and parameters[self.NODATA] is not None:
  122. nodata = self.parameterAsDouble(parameters, self.NODATA, context)
  123. else:
  124. nodata = None
  125. offset = self.parameterAsDouble(parameters, self.OFFSET, context)
  126. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  127. self.setOutputValue(self.OUTPUT, outFile)
  128. output, outFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  129. arguments = [
  130. '-b',
  131. str(self.parameterAsInt(parameters, self.BAND, context))
  132. ]
  133. if fieldName:
  134. arguments.append('-a')
  135. arguments.append(fieldName)
  136. arguments.append('-i')
  137. arguments.append(str(self.parameterAsDouble(parameters, self.INTERVAL, context)))
  138. if self.parameterAsBoolean(parameters, self.CREATE_3D, context):
  139. arguments.append('-3d')
  140. if self.parameterAsBoolean(parameters, self.IGNORE_NODATA, context):
  141. arguments.append('-inodata')
  142. if nodata is not None:
  143. arguments.append(f'-snodata {nodata}')
  144. if offset:
  145. arguments.append(f'-off {offset}')
  146. if outFormat:
  147. arguments.append(f'-f {outFormat}')
  148. if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
  149. extra = self.parameterAsString(parameters, self.EXTRA, context)
  150. arguments.append(extra)
  151. # TODO: remove in QGIS 4
  152. options = self.parameterAsString(parameters, self.OPTIONS, context)
  153. if options:
  154. arguments.append(options)
  155. arguments.append(inLayer.source())
  156. arguments.append(output)
  157. return arguments
  158. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  159. arguments = self._buildArgsList(parameters, context, feedback, executing)
  160. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
  161. class contour_polygon(contour):
  162. FIELD_NAME_MIN = 'FIELD_NAME_MIN'
  163. FIELD_NAME_MAX = 'FIELD_NAME_MAX'
  164. def __init__(self):
  165. super().__init__()
  166. def initAlgorithm(self, config=None):
  167. super().initAlgorithm(config)
  168. # FIELD_NAME isn't used in polygon mode
  169. self.removeParameter(contour.FIELD_NAME)
  170. self.addParameter(QgsProcessingParameterString(self.FIELD_NAME_MIN,
  171. self.tr('Attribute name for minimum elevation of contour polygon'),
  172. defaultValue='ELEV_MIN',
  173. optional=True))
  174. self.addParameter(QgsProcessingParameterString(self.FIELD_NAME_MAX,
  175. self.tr('Attribute name for maximum elevation of contour polygon'),
  176. defaultValue='ELEV_MAX',
  177. optional=True))
  178. # Need to replace the output parameter, as we are producing a different type of output
  179. self.removeParameter(contour.OUTPUT)
  180. self.addParameter(QgsProcessingParameterVectorDestination(
  181. contour.OUTPUT, self.tr('Contours'), QgsProcessing.TypeVectorPolygon))
  182. def name(self):
  183. return 'contour_polygon'
  184. def displayName(self):
  185. return self.tr('Contour Polygons')
  186. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  187. arguments = self._buildArgsList(parameters, context, feedback, executing)
  188. fieldNameMin = self.parameterAsString(parameters, self.FIELD_NAME_MIN, context)
  189. fieldNameMax = self.parameterAsString(parameters, self.FIELD_NAME_MAX, context)
  190. if fieldNameMin:
  191. arguments.insert(0, fieldNameMin)
  192. arguments.insert(0, '-amin')
  193. if fieldNameMax:
  194. arguments.insert(0, fieldNameMax)
  195. arguments.insert(0, '-amax')
  196. arguments.insert(0, "-p")
  197. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]