polygonize.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """
  2. ***************************************************************************
  3. polygonize.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf 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__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  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. QgsProcessingParameterBoolean,
  29. QgsProcessingParameterVectorDestination)
  30. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  31. from processing.tools.system import isWindows
  32. from processing.algs.gdal.GdalUtils import GdalUtils
  33. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  34. class polygonize(GdalAlgorithm):
  35. INPUT = 'INPUT'
  36. BAND = 'BAND'
  37. FIELD = 'FIELD'
  38. EIGHT_CONNECTEDNESS = 'EIGHT_CONNECTEDNESS'
  39. EXTRA = 'EXTRA'
  40. OUTPUT = 'OUTPUT'
  41. def __init__(self):
  42. super().__init__()
  43. def initAlgorithm(self, config=None):
  44. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
  45. self.addParameter(QgsProcessingParameterBand(self.BAND,
  46. self.tr('Band number'),
  47. 1,
  48. parentLayerParameterName=self.INPUT))
  49. self.addParameter(QgsProcessingParameterString(self.FIELD,
  50. self.tr('Name of the field to create'),
  51. defaultValue='DN'))
  52. self.addParameter(QgsProcessingParameterBoolean(self.EIGHT_CONNECTEDNESS,
  53. self.tr('Use 8-connectedness'),
  54. defaultValue=False))
  55. extra_param = QgsProcessingParameterString(self.EXTRA,
  56. self.tr('Additional command-line parameters'),
  57. defaultValue=None,
  58. optional=True)
  59. extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  60. self.addParameter(extra_param)
  61. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  62. self.tr('Vectorized'),
  63. QgsProcessing.TypeVectorPolygon))
  64. def name(self):
  65. return 'polygonize'
  66. def displayName(self):
  67. return self.tr('Polygonize (raster to vector)')
  68. def group(self):
  69. return self.tr('Raster conversion')
  70. def groupId(self):
  71. return 'rasterconversion'
  72. def icon(self):
  73. return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'polygonize.png'))
  74. def commandName(self):
  75. return 'gdal_polygonize'
  76. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  77. arguments = []
  78. if self.parameterAsBoolean(parameters, self.EIGHT_CONNECTEDNESS, context):
  79. arguments.append('-8')
  80. if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
  81. extra = self.parameterAsString(parameters, self.EXTRA, context)
  82. arguments.append(extra)
  83. inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  84. if inLayer is None:
  85. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  86. arguments.append(inLayer.source())
  87. arguments.append('-b')
  88. arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
  89. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  90. self.setOutputValue(self.OUTPUT, outFile)
  91. output, outFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  92. if outFormat:
  93. arguments.append(f'-f {outFormat}')
  94. arguments.append(output)
  95. layerName = GdalUtils.ogrOutputLayerName(output)
  96. if layerName:
  97. arguments.append(layerName)
  98. arguments.append(self.parameterAsString(parameters, self.FIELD, context))
  99. return [self.commandName() + ('.bat' if isWindows() else '.py'), GdalUtils.escapeAndJoin(arguments)]