OneSideBuffer.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """
  2. ***************************************************************************
  3. OneSideBuffer.py
  4. ---------------------
  5. Date : Janaury 2015
  6. Copyright : (C) 2015 by Giovanni Manghi
  7. Email : giovanni dot manghi at naturalgis dot pt
  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__ = 'Giovanni Manghi'
  18. __date__ = 'January 2015'
  19. __copyright__ = '(C) 2015, Giovanni Manghi'
  20. from qgis.core import (QgsProcessing,
  21. QgsProcessingException,
  22. QgsProcessingParameterDefinition,
  23. QgsProcessingParameterDistance,
  24. QgsProcessingParameterFeatureSource,
  25. QgsProcessingParameterEnum,
  26. QgsProcessingParameterField,
  27. QgsProcessingParameterString,
  28. QgsProcessingParameterNumber,
  29. QgsProcessingParameterBoolean,
  30. QgsProcessingParameterVectorDestination)
  31. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  32. from processing.algs.gdal.GdalUtils import GdalUtils
  33. class OneSideBuffer(GdalAlgorithm):
  34. INPUT = 'INPUT'
  35. FIELD = 'FIELD'
  36. BUFFER_SIDE = 'BUFFER_SIDE'
  37. GEOMETRY = 'GEOMETRY'
  38. DISTANCE = 'DISTANCE'
  39. DISSOLVE = 'DISSOLVE'
  40. EXPLODE_COLLECTIONS = 'EXPLODE_COLLECTIONS'
  41. OPTIONS = 'OPTIONS'
  42. OUTPUT = 'OUTPUT'
  43. def __init__(self):
  44. super().__init__()
  45. def initAlgorithm(self, config=None):
  46. self.bufferSides = [self.tr('Right'), self.tr('Left')]
  47. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  48. self.tr('Input layer'),
  49. [QgsProcessing.TypeVectorLine]))
  50. self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
  51. self.tr('Geometry column name'),
  52. defaultValue='geometry'))
  53. self.addParameter(QgsProcessingParameterDistance(self.DISTANCE,
  54. self.tr('Buffer distance'),
  55. defaultValue=10,
  56. parentParameterName=self.INPUT))
  57. self.addParameter(QgsProcessingParameterEnum(self.BUFFER_SIDE,
  58. self.tr('Buffer side'),
  59. options=self.bufferSides,
  60. allowMultiple=False,
  61. defaultValue=0))
  62. self.addParameter(QgsProcessingParameterField(self.FIELD,
  63. self.tr('Dissolve by attribute'),
  64. None,
  65. self.INPUT,
  66. QgsProcessingParameterField.Any,
  67. optional=True))
  68. self.addParameter(QgsProcessingParameterBoolean(self.DISSOLVE,
  69. self.tr('Dissolve all results'),
  70. defaultValue=False))
  71. self.addParameter(QgsProcessingParameterBoolean(self.EXPLODE_COLLECTIONS,
  72. self.tr('Produce one feature for each geometry in any kind of geometry collection in the source file'),
  73. defaultValue=False))
  74. options_param = QgsProcessingParameterString(self.OPTIONS,
  75. self.tr('Additional creation options'),
  76. defaultValue='',
  77. optional=True)
  78. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  79. self.addParameter(options_param)
  80. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  81. self.tr('One-sided buffer'),
  82. QgsProcessing.TypeVectorPolygon))
  83. def name(self):
  84. return 'onesidebuffer'
  85. def displayName(self):
  86. return self.tr('One side buffer')
  87. def group(self):
  88. return self.tr('Vector geoprocessing')
  89. def groupId(self):
  90. return 'vectorgeoprocessing'
  91. def commandName(self):
  92. return 'ogr2ogr'
  93. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  94. source = self.parameterAsSource(parameters, self.INPUT, context)
  95. if source is None:
  96. raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
  97. fields = source.fields()
  98. ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  99. geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
  100. distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
  101. side = self.parameterAsEnum(parameters, self.BUFFER_SIDE, context)
  102. fieldName = self.parameterAsString(parameters, self.FIELD, context)
  103. dissolve = self.parameterAsBoolean(parameters, self.DISSOLVE, context)
  104. options = self.parameterAsString(parameters, self.OPTIONS, context)
  105. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  106. self.setOutputValue(self.OUTPUT, outFile)
  107. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  108. other_fields_exist = any(
  109. True for f in fields
  110. if f.name() != geometry
  111. )
  112. other_fields = ',*' if other_fields_exist else ''
  113. arguments = [
  114. output,
  115. ogrLayer,
  116. '-dialect',
  117. 'sqlite',
  118. '-sql'
  119. ]
  120. if dissolve or fieldName:
  121. sql = f'SELECT ST_Union(ST_SingleSidedBuffer({geometry}, {distance}, {side})) AS {geometry}{other_fields} FROM "{layerName}"'
  122. else:
  123. sql = f'SELECT ST_SingleSidedBuffer({geometry}, {distance}, {side}) AS {geometry}{other_fields} FROM "{layerName}"'
  124. if fieldName:
  125. sql = f'{sql} GROUP BY "{fieldName}"'
  126. arguments.append(sql)
  127. if self.parameterAsBoolean(parameters, self.EXPLODE_COLLECTIONS, context):
  128. arguments.append('-explodecollections')
  129. if options:
  130. arguments.append(options)
  131. if outputFormat:
  132. arguments.append(f'-f {outputFormat}')
  133. return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]