ExportVectorByMask.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """
  2. ***************************************************************************
  3. ExportVectorByMask.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. import os
  21. from PyQt5.QtGui import QIcon
  22. from qgis._core import QgsProcessingParameterCrs
  23. from qgis.core import (QgsProcessing,
  24. QgsProcessingAlgorithm,
  25. QgsProcessingParameterDefinition,
  26. QgsProcessingParameterString,
  27. QgsProcessingParameterFeatureSource,
  28. QgsProcessingParameterVectorDestination)
  29. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  30. from processing.algs.gdal.GdalUtils import GdalUtils
  31. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  32. class ExportVectorByMask(GdalAlgorithm):
  33. INPUT = 'INPUT'
  34. MASK = 'MASK'
  35. OPTIONS = 'OPTIONS'
  36. OUTPUT = 'OUTPUT'
  37. S_SRS = "S_SRS"
  38. T_SRS = "T_SRS"
  39. def __init__(self):
  40. super().__init__()
  41. def flags(self):
  42. return QgsProcessingAlgorithm.FlagSupportsBatch | QgsProcessingAlgorithm.FlagRequiresMatchingCrs # cannot cancel!
  43. def initAlgorithm(self, config=None):
  44. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  45. self.tr('数据源')))
  46. self.addParameter(QgsProcessingParameterCrs(self.S_SRS, self.tr('数据源坐标系'), optional=True))
  47. self.addParameter(QgsProcessingParameterFeatureSource(self.MASK,
  48. self.tr('导出的数据范围'),
  49. [QgsProcessing.TypeVectorPolygon]))
  50. self.addParameter(QgsProcessingParameterCrs(self.T_SRS, self.tr('数据范围坐标系'), optional=True))
  51. options_param = QgsProcessingParameterString(self.OPTIONS,
  52. self.tr('Additional creation options'),
  53. defaultValue='',
  54. optional=True)
  55. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  56. # self.addParameter(options_param)
  57. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  58. self.tr('导出位置')))
  59. def name(self):
  60. return 'vectorexport'
  61. def displayName(self):
  62. return self.tr('矢量数据分发')
  63. def group(self):
  64. return self.tr('数据分发')
  65. def groupId(self):
  66. return 'dataexport'
  67. def icon(self):
  68. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'export-vector.png'))
  69. def commandName(self):
  70. return 'ogr2ogr'
  71. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  72. inLayer, inLayerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  73. maskLayer, maskLayerName = self.getOgrCompatibleSource(self.MASK, parameters, context, feedback, executing)
  74. options = self.parameterAsString(parameters, self.OPTIONS, context)
  75. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  76. self.setOutputValue(self.OUTPUT, outFile)
  77. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  78. tsrs = self.parameterAsCrs(parameters, self.T_SRS, context)
  79. ssrs = self.parameterAsCrs(parameters, self.S_SRS, context)
  80. arguments = [
  81. # '--config',
  82. # 'SHAPE_ENCODING',
  83. # 'ISO-8859-1',
  84. '-lco',
  85. 'ENCODING=UTF-8',
  86. # 'LCID=CHINESE_SIMPLIFIED',
  87. # 'CP=936',
  88. '-clipsrc',
  89. maskLayer,
  90. '-clipsrclayer',
  91. maskLayerName,
  92. output,
  93. inLayer,
  94. inLayerName,
  95. ]
  96. if tsrs.isValid():
  97. arguments.append('-t_srs')
  98. arguments.append(GdalUtils.gdal_crs_string(tsrs))
  99. if ssrs.isValid():
  100. arguments.append('-s_srs')
  101. arguments.append(GdalUtils.gdal_crs_string(ssrs))
  102. if options:
  103. arguments.append(options)
  104. if outputFormat:
  105. arguments.append(f'-f {outputFormat}')
  106. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]