PostGISExecuteAndLoadSQL.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. ***************************************************************************
  3. PostGISExecuteAndLoadSQL.py
  4. ---------------------
  5. Date : May 2018
  6. Copyright : (C) 2018 by Anita Graser
  7. Email : anitagraser at gmx dot at
  8. ---------------------
  9. based on PostGISExecuteSQL.py by Victor Olaya and Carterix Geomatics
  10. ***************************************************************************
  11. * *
  12. * This program is free software; you can redistribute it and/or modify *
  13. * it under the terms of the GNU General Public License as published by *
  14. * the Free Software Foundation; either version 2 of the License, or *
  15. * (at your option) any later version. *
  16. * *
  17. ***************************************************************************
  18. """
  19. __author__ = 'Anita Graser'
  20. __date__ = 'May 2018'
  21. __copyright__ = '(C) 2018, Anita Graser'
  22. from qgis.core import (QgsProcessingException,
  23. QgsProcessingParameterString,
  24. QgsVectorLayer,
  25. QgsDataSourceUri,
  26. QgsProcessing,
  27. QgsProcessingOutputVectorLayer,
  28. QgsProcessingContext,
  29. QgsProcessingParameterProviderConnection,
  30. QgsProviderRegistry,
  31. QgsProviderConnectionException,
  32. QgsProcessingAlgorithm
  33. )
  34. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  35. class PostGISExecuteAndLoadSQL(QgisAlgorithm):
  36. DATABASE = 'DATABASE'
  37. SQL = 'SQL'
  38. OUTPUT = 'OUTPUT'
  39. ID_FIELD = 'ID_FIELD'
  40. GEOMETRY_FIELD = 'GEOMETRY_FIELD'
  41. def group(self):
  42. return self.tr('Database')
  43. def groupId(self):
  44. return 'database'
  45. def __init__(self):
  46. super().__init__()
  47. def flags(self):
  48. return super().flags() | QgsProcessingAlgorithm.FlagNotAvailableInStandaloneTool | QgsProcessingAlgorithm.FlagRequiresProject
  49. def initAlgorithm(self, config=None):
  50. db_param = QgsProcessingParameterProviderConnection(
  51. self.DATABASE,
  52. self.tr('Database (connection name)'), 'postgres')
  53. self.addParameter(db_param)
  54. self.addParameter(QgsProcessingParameterString(
  55. self.SQL,
  56. self.tr('SQL query'),
  57. multiLine=True))
  58. self.addParameter(QgsProcessingParameterString(
  59. self.ID_FIELD,
  60. self.tr('Unique ID field name'),
  61. defaultValue='id'))
  62. self.addParameter(QgsProcessingParameterString(
  63. self.GEOMETRY_FIELD,
  64. self.tr('Geometry field name'),
  65. defaultValue='geom',
  66. optional=True))
  67. self.addOutput(QgsProcessingOutputVectorLayer(
  68. self.OUTPUT,
  69. self.tr("Output layer"),
  70. QgsProcessing.TypeVectorAnyGeometry))
  71. def name(self):
  72. return 'postgisexecuteandloadsql'
  73. def displayName(self):
  74. return self.tr('PostgreSQL execute and load SQL')
  75. def shortDescription(self):
  76. return self.tr('Executes a SQL command on a PostgreSQL database and loads the result as a table')
  77. def tags(self):
  78. return self.tr('postgis,table,database').split(',')
  79. def processAlgorithm(self, parameters, context, feedback):
  80. connection_name = self.parameterAsConnectionName(parameters, self.DATABASE, context)
  81. id_field = self.parameterAsString(parameters, self.ID_FIELD, context)
  82. geom_field = self.parameterAsString(
  83. parameters, self.GEOMETRY_FIELD, context)
  84. # resolve connection details to uri
  85. try:
  86. md = QgsProviderRegistry.instance().providerMetadata('postgres')
  87. conn = md.createConnection(connection_name)
  88. except QgsProviderConnectionException:
  89. raise QgsProcessingException(self.tr('Could not retrieve connection details for {}').format(connection_name))
  90. uri = QgsDataSourceUri(conn.uri())
  91. sql = self.parameterAsString(parameters, self.SQL, context)
  92. sql = sql.replace('\n', ' ')
  93. uri.setDataSource("", "(" + sql.rstrip(';') + ")", geom_field, "", id_field)
  94. vlayer = QgsVectorLayer(uri.uri(), "layername", "postgres")
  95. if not vlayer.isValid():
  96. raise QgsProcessingException(self.tr("""This layer is invalid!
  97. Please check the PostGIS log for error messages."""))
  98. context.temporaryLayerStore().addMapLayer(vlayer)
  99. context.addLayerToLoadOnCompletion(
  100. vlayer.id(),
  101. QgsProcessingContext.LayerDetails('SQL layer',
  102. context.project(),
  103. self.OUTPUT))
  104. return {self.OUTPUT: vlayer.id()}