OffsetCurve.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. ***************************************************************************
  3. offsetcurve.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. QgsProcessingParameterDefinition,
  22. QgsProcessingParameterDistance,
  23. QgsProcessingParameterFeatureSource,
  24. QgsProcessingParameterString,
  25. QgsProcessingException,
  26. QgsProcessingParameterNumber,
  27. QgsProcessingParameterVectorDestination)
  28. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  29. from processing.algs.gdal.GdalUtils import GdalUtils
  30. class OffsetCurve(GdalAlgorithm):
  31. INPUT = 'INPUT'
  32. GEOMETRY = 'GEOMETRY'
  33. DISTANCE = 'DISTANCE'
  34. OPTIONS = 'OPTIONS'
  35. OUTPUT = 'OUTPUT'
  36. def __init__(self):
  37. super().__init__()
  38. def initAlgorithm(self, config=None):
  39. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  40. self.tr('Input layer'),
  41. [QgsProcessing.TypeVectorLine]))
  42. self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
  43. self.tr('Geometry column name'),
  44. defaultValue='geometry'))
  45. self.addParameter(QgsProcessingParameterDistance(self.DISTANCE,
  46. self.tr('Offset distance (left-sided: positive, right-sided: negative)'),
  47. parentParameterName=self.INPUT,
  48. defaultValue=10))
  49. options_param = QgsProcessingParameterString(self.OPTIONS,
  50. self.tr('Additional creation options'),
  51. defaultValue='',
  52. optional=True)
  53. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  54. self.addParameter(options_param)
  55. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  56. self.tr('Offset curve'),
  57. QgsProcessing.TypeVectorLine))
  58. def name(self):
  59. return 'offsetcurve'
  60. def displayName(self):
  61. return self.tr('Offset curve')
  62. def group(self):
  63. return self.tr('Vector geoprocessing')
  64. def groupId(self):
  65. return 'vectorgeoprocessing'
  66. def commandName(self):
  67. return 'ogr2ogr'
  68. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  69. source = self.parameterAsSource(parameters, self.INPUT, context)
  70. if source is None:
  71. raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
  72. fields = source.fields()
  73. ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  74. geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
  75. distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
  76. options = self.parameterAsString(parameters, self.OPTIONS, context)
  77. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  78. self.setOutputValue(self.OUTPUT, outFile)
  79. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  80. other_fields_exist = any(
  81. True for f in fields
  82. if f.name() != geometry
  83. )
  84. other_fields = ',*' if other_fields_exist else ''
  85. arguments = [
  86. output,
  87. ogrLayer,
  88. '-dialect',
  89. 'sqlite',
  90. '-sql'
  91. ]
  92. sql = f'SELECT ST_OffsetCurve({geometry}, {distance}) AS {geometry}{other_fields} FROM "{layerName}"'
  93. arguments.append(sql)
  94. if options:
  95. arguments.append(options)
  96. if outputFormat:
  97. arguments.append(f'-f {outputFormat}')
  98. return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]