ogrinfo.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. ***************************************************************************
  3. ogrinfo.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 (QgsProcessingException,
  23. QgsProcessingParameterVectorLayer,
  24. QgsProcessingParameterBoolean,
  25. QgsProcessingParameterFileDestination)
  26. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  27. from processing.algs.gdal.GdalUtils import GdalUtils
  28. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  29. class ogrinfo(GdalAlgorithm):
  30. INPUT = 'INPUT'
  31. SUMMARY_ONLY = 'SUMMARY_ONLY'
  32. NO_METADATA = 'NO_METADATA'
  33. OUTPUT = 'OUTPUT'
  34. def __init__(self):
  35. super().__init__()
  36. def initAlgorithm(self, config=None):
  37. self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
  38. self.tr('Input layer')))
  39. self.addParameter(QgsProcessingParameterBoolean(self.SUMMARY_ONLY,
  40. self.tr('Summary output only'),
  41. defaultValue=True))
  42. self.addParameter(QgsProcessingParameterBoolean(self.NO_METADATA,
  43. self.tr('Suppress metadata info'),
  44. defaultValue=False))
  45. self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT,
  46. self.tr('Layer information'),
  47. self.tr('HTML files (*.html)')))
  48. def icon(self):
  49. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'infomation.png'))
  50. def name(self):
  51. return 'ogrinfo'
  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 commandName(self):
  59. return 'ogrinfo'
  60. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  61. arguments = ['-al']
  62. if self.parameterAsBoolean(parameters, self.SUMMARY_ONLY, context):
  63. arguments.append('-so')
  64. if self.parameterAsBoolean(parameters, self.NO_METADATA, context):
  65. arguments.append('-nomd')
  66. inLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
  67. if inLayer is None:
  68. raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
  69. ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
  70. arguments.append(ogrLayer)
  71. arguments.append(layerName)
  72. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
  73. def processAlgorithm(self, parameters, context, feedback):
  74. console_output = GdalUtils.runGdal(self.getConsoleCommands(parameters, context, feedback), feedback)
  75. output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)
  76. with open(output, 'w') as f:
  77. f.write('<pre>')
  78. for s in console_output[1:]:
  79. f.write(str(s))
  80. f.write('</pre>')
  81. return {self.OUTPUT: output}