ClipVectorByMask.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. ***************************************************************************
  3. ClipVectorByMask.py
  4. ---------------------
  5. Date : November 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__ = 'November 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. from qgis.core import (QgsProcessing,
  21. QgsProcessingAlgorithm,
  22. QgsProcessingParameterDefinition,
  23. QgsProcessingParameterString,
  24. QgsProcessingParameterFeatureSource,
  25. QgsProcessingParameterVectorDestination)
  26. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  27. from processing.algs.gdal.GdalUtils import GdalUtils
  28. class ClipVectorByMask(GdalAlgorithm):
  29. INPUT = 'INPUT'
  30. MASK = 'MASK'
  31. OPTIONS = 'OPTIONS'
  32. OUTPUT = 'OUTPUT'
  33. def __init__(self):
  34. super().__init__()
  35. def flags(self):
  36. return QgsProcessingAlgorithm.FlagSupportsBatch | QgsProcessingAlgorithm.FlagRequiresMatchingCrs # cannot cancel!
  37. def initAlgorithm(self, config=None):
  38. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  39. self.tr('Input layer')))
  40. self.addParameter(QgsProcessingParameterFeatureSource(self.MASK,
  41. self.tr('Mask layer'),
  42. [QgsProcessing.TypeVectorPolygon]))
  43. options_param = QgsProcessingParameterString(self.OPTIONS,
  44. self.tr('Additional creation options'),
  45. defaultValue='',
  46. optional=True)
  47. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  48. self.addParameter(options_param)
  49. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  50. self.tr('Clipped (mask)')))
  51. def name(self):
  52. return 'clipvectorbypolygon'
  53. def displayName(self):
  54. return self.tr('Clip vector by mask layer')
  55. def group(self):
  56. return self.tr('Vector geoprocessing')
  57. def groupId(self):
  58. return 'vectorgeoprocessing'
  59. def commandName(self):
  60. return 'ogr2ogr'
  61. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  62. inLayer, inLayerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  63. maskLayer, maskLayerName = self.getOgrCompatibleSource(self.MASK, parameters, context, feedback, executing)
  64. options = self.parameterAsString(parameters, self.OPTIONS, context)
  65. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  66. self.setOutputValue(self.OUTPUT, outFile)
  67. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  68. arguments = [
  69. '-clipsrc',
  70. maskLayer,
  71. '-clipsrclayer',
  72. maskLayerName,
  73. output,
  74. inLayer,
  75. inLayerName,
  76. ]
  77. if options:
  78. arguments.append(options)
  79. if outputFormat:
  80. arguments.append(f'-f {outputFormat}')
  81. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]