ClipVectorByExtent.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. ***************************************************************************
  3. ClipVectorByExtent.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 (QgsVectorLayer,
  21. QgsProcessing,
  22. QgsProcessingException,
  23. QgsProcessingParameterDefinition,
  24. QgsProcessingParameterFeatureSource,
  25. QgsProcessingParameterExtent,
  26. QgsProcessingParameterString,
  27. QgsProcessingParameterVectorDestination)
  28. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  29. from processing.algs.gdal.GdalUtils import GdalUtils
  30. class ClipVectorByExtent(GdalAlgorithm):
  31. INPUT = 'INPUT'
  32. EXTENT = 'EXTENT'
  33. OPTIONS = 'OPTIONS'
  34. OUTPUT = 'OUTPUT'
  35. def __init__(self):
  36. super().__init__()
  37. def initAlgorithm(self, config=None):
  38. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  39. self.tr('Input layer')))
  40. self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
  41. self.tr('Clipping extent')))
  42. options_param = QgsProcessingParameterString(self.OPTIONS,
  43. self.tr('Additional creation options'),
  44. defaultValue='',
  45. optional=True)
  46. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  47. self.addParameter(options_param)
  48. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  49. self.tr('Clipped (extent)')))
  50. def name(self):
  51. return 'clipvectorbyextent'
  52. def displayName(self):
  53. return self.tr('Clip vector by extent')
  54. def group(self):
  55. return self.tr('Vector geoprocessing')
  56. def groupId(self):
  57. return 'vectorgeoprocessing'
  58. def commandName(self):
  59. return 'ogr2ogr'
  60. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  61. ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  62. source = self.parameterAsSource(parameters, self.INPUT, context)
  63. if source is None:
  64. raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
  65. extent = self.parameterAsExtent(parameters, self.EXTENT, context, source.sourceCrs())
  66. options = self.parameterAsString(parameters, self.OPTIONS, context)
  67. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  68. self.setOutputValue(self.OUTPUT, outFile)
  69. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  70. arguments = [
  71. '-spat',
  72. str(extent.xMinimum()),
  73. str(extent.yMaximum()),
  74. str(extent.xMaximum()),
  75. str(extent.yMinimum()),
  76. '-clipsrc spat_extent',
  77. output,
  78. ogrLayer,
  79. layerName
  80. ]
  81. if options:
  82. arguments.append(options)
  83. if outputFormat:
  84. arguments.append(f'-f {outputFormat}')
  85. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]