QGIS/python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py

120 lines
4.5 KiB
Python
Raw Normal View History

2018-05-24 21:59:56 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
PostGISExecuteAndLoadSQL.py
---------------------
Date : May 2018
Copyright : (C) 2018 by Anita Graser
Email : anitagraser at gmx dot at
---------------------
based on PostGISExecuteSQL.py by Victor Olaya and Carterix Geomatics
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Anita Graser'
__date__ = 'May 2018'
__copyright__ = '(C) 2018, Anita Graser'
2018-05-28 19:52:00 +02:00
from qgis.core import (Qgis,
QgsProcessingException,
QgsProcessingParameterString,
QgsApplication,
QgsVectorLayer,
QgsProject,
QgsProcessing,
QgsProcessingException,
QgsProcessingOutputVectorLayer,
QgsProcessingContext,
QgsProcessingFeedback)
2018-05-24 21:59:56 +02:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import postgis
class PostGISExecuteAndLoadSQL(QgisAlgorithm):
DATABASE = 'DATABASE'
SQL = 'SQL'
OUTPUT = 'OUTPUT'
2018-05-29 20:57:12 +02:00
ID_FIELD = 'ID_FIELD'
GEOMETRY_FIELD = 'GEOMETRY_FIELD'
2018-05-24 21:59:56 +02:00
def group(self):
return self.tr('Database')
def groupId(self):
return 'database'
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
db_param = QgsProcessingParameterString(
self.DATABASE,
self.tr('Database (connection name)'))
db_param.setMetadata({
'widget_wrapper': {
'class': 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper'}})
self.addParameter(db_param)
2018-05-28 19:52:00 +02:00
self.addParameter(QgsProcessingParameterString(
self.SQL,
2018-06-22 18:40:17 +02:00
self.tr('SQL query'),
2018-05-28 19:52:00 +02:00
multiLine=True))
2018-05-29 20:57:12 +02:00
self.addParameter(QgsProcessingParameterString(
self.ID_FIELD,
2018-06-22 18:40:17 +02:00
self.tr('Unique ID field name'),
2018-05-29 20:57:12 +02:00
defaultValue='id'))
self.addParameter(QgsProcessingParameterString(
self.GEOMETRY_FIELD,
self.tr('Geometry field name'),
defaultValue='geom',
optional=True))
2018-05-28 19:52:00 +02:00
self.addOutput(QgsProcessingOutputVectorLayer(
self.OUTPUT,
self.tr("Output layer"),
QgsProcessing.TypeVectorAnyGeometry))
2018-05-24 21:59:56 +02:00
def name(self):
return 'postgisexecuteandloadsql'
def displayName(self):
return self.tr('PostgreSQL execute and load SQL')
def shortDescription(self):
return self.tr('Executes a SQL command on a PostgreSQL database and loads the result as a table')
def tags(self):
return self.tr('postgis,table,database').split(',')
2018-05-24 21:59:56 +02:00
def processAlgorithm(self, parameters, context, feedback):
connection = self.parameterAsString(parameters, self.DATABASE, context)
2018-05-29 20:57:12 +02:00
id_field = self.parameterAsString(parameters, self.ID_FIELD, context)
2018-05-31 13:43:00 +02:00
geom_field = self.parameterAsString(
parameters, self.GEOMETRY_FIELD, context)
2018-05-24 21:59:56 +02:00
uri = postgis.uri_from_name(connection)
2018-05-28 19:52:00 +02:00
sql = self.parameterAsString(parameters, self.SQL, context)
sql = sql.replace('\n', ' ')
uri.setDataSource("", "(" + sql.rstrip(';') + ")", geom_field, "", id_field)
2018-05-28 19:52:00 +02:00
2018-05-24 21:59:56 +02:00
vlayer = QgsVectorLayer(uri.uri(), "layername", "postgres")
2018-05-28 19:52:00 +02:00
if not vlayer.isValid():
raise QgsProcessingException(self.tr("""This layer is invalid!
Please check the PostGIS log for error messages."""))
context.temporaryLayerStore().addMapLayer(vlayer)
context.addLayerToLoadOnCompletion(
vlayer.id(),
QgsProcessingContext.LayerDetails('SQL layer',
context.project(),
self.OUTPUT))
return {self.OUTPUT: vlayer.id()}