VectorProject.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. ***************************************************************************
  3. VectorProject.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 QgsCoordinateReferenceSystem, 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 VectorProject(GdalAlgorithm):
  33. INPUT = 'INPUT'
  34. T_SRS = 'T_SRS'
  35. OUTPUT = 'OUTPUT'
  36. def __init__(self):
  37. super().__init__()
  38. def flags(self):
  39. return QgsProcessingAlgorithm.FlagSupportsBatch | QgsProcessingAlgorithm.FlagRequiresMatchingCrs # cannot cancel!
  40. def initAlgorithm(self, config=None):
  41. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  42. self.tr('数据源')))
  43. crs = QgsCoordinateReferenceSystem("EPSG:4525")
  44. crs_parameter = QgsProcessingParameterCrs(self.T_SRS,
  45. self.tr('输出坐标系'),
  46. defaultValue=crs)
  47. self.addParameter(crs_parameter)
  48. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  49. self.tr('导出位置'), QgsProcessing.TypeVectorPolygon))
  50. def name(self):
  51. return 'vectorproject'
  52. def displayName(self):
  53. return self.tr('矢量数据重投影')
  54. def group(self):
  55. return self.tr('数据处理')
  56. def groupId(self):
  57. return 'datahandle'
  58. def icon(self):
  59. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'vectorproject.png'))
  60. def commandName(self):
  61. return 'ogr2ogr'
  62. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  63. inLayer, inLayerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  64. print(inLayer)
  65. print(inLayerName)
  66. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  67. self.setOutputValue(self.OUTPUT, outFile)
  68. tsrs = GdalUtils.gdal_crs_string(self.parameterAsCrs(parameters, self.T_SRS, context))
  69. print(tsrs)
  70. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  71. print(output)
  72. arguments = [
  73. '--config',
  74. 'SHAPE_ENCODING',
  75. '"GBK2312"',
  76. '-t_srs',
  77. tsrs,
  78. output,
  79. inLayer
  80. ]
  81. arguments = [
  82. '-lco',
  83. 'ENCODING=UTF-8',
  84. '-t_srs',
  85. tsrs,
  86. output,
  87. inLayer
  88. ]
  89. if inLayer.__contains__("PG:"):
  90. arguments.append(inLayerName)
  91. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]