ogr2ogr.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. ***************************************************************************
  3. ogr2ogr.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 (QgsProcessing,
  23. QgsProcessingException,
  24. QgsProcessingParameterBoolean,
  25. QgsProcessingParameterDefinition,
  26. QgsProcessingParameterFeatureSource,
  27. QgsProcessingParameterString,
  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 ogr2ogr(GdalAlgorithm):
  33. INPUT = 'INPUT'
  34. CONVERT_ALL_LAYERS = 'CONVERT_ALL_LAYERS'
  35. OPTIONS = 'OPTIONS'
  36. OUTPUT = 'OUTPUT'
  37. def __init__(self):
  38. super().__init__()
  39. def initAlgorithm(self, config=None):
  40. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  41. self.tr('Input layer'),
  42. types=[QgsProcessing.TypeVector]))
  43. convert_all_layers_param = QgsProcessingParameterBoolean(self.CONVERT_ALL_LAYERS,
  44. self.tr('Convert all layers from dataset'), defaultValue=False)
  45. convert_all_layers_param.setHelp(self.tr("Use convert all layers to convert a whole dataset. "
  46. "Supported output formats for this option are GPKG and GML."))
  47. self.addParameter(convert_all_layers_param)
  48. options_param = QgsProcessingParameterString(self.OPTIONS,
  49. self.tr('Additional creation options'),
  50. defaultValue='',
  51. optional=True)
  52. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  53. self.addParameter(options_param)
  54. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
  55. self.tr('Converted')))
  56. def name(self):
  57. return 'convertformat'
  58. def displayName(self):
  59. return self.tr('矢量数据格式转换')
  60. def group(self):
  61. return self.tr('数据处理')
  62. def groupId(self):
  63. return 'datahandle'
  64. def icon(self):
  65. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'translate.png'))
  66. def commandName(self):
  67. return 'ogr2ogr'
  68. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  69. ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  70. convertAllLayers = self.parameterAsBoolean(parameters, self.CONVERT_ALL_LAYERS, context)
  71. options = self.parameterAsString(parameters, self.OPTIONS, context)
  72. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  73. self.setOutputValue(self.OUTPUT, outFile)
  74. output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
  75. if outputFormat in ('SQLite', 'GPKG') and os.path.isfile(output):
  76. raise QgsProcessingException(self.tr('Output file "{}" already exists.').format(output))
  77. arguments = []
  78. if outputFormat:
  79. arguments.append(f'-f {outputFormat}')
  80. if options:
  81. arguments.append(options)
  82. arguments.append(output)
  83. arguments.append(ogrLayer)
  84. if not convertAllLayers:
  85. arguments.append(layerName)
  86. return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
  87. def shortHelpString(self):
  88. return self.tr("The algorithm converts simple features data between file formats.\n\n"
  89. "Use convert all layers to convert a whole dataset.\n"
  90. "Supported output formats for this option are:\n"
  91. "- GPKG\n"
  92. "- GML"
  93. )