mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-06 00:05:02 -05:00
91 lines
3.7 KiB
Python
91 lines
3.7 KiB
Python
# -*- 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'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
from qgis.core import (Qgis, QgsProcessingException, QgsProcessingParameterString, QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingParameterFeatureSink, QgsProcessing, QgsFeatureRequest, QgsFeature, QgsFeatureSink)
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
|
from processing.tools import postgis
|
|
|
|
|
|
class PostGISExecuteAndLoadSQL(QgisAlgorithm):
|
|
|
|
DATABASE = 'DATABASE'
|
|
SQL = 'SQL'
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
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)
|
|
self.addParameter(QgsProcessingParameterString(self.SQL, self.tr('SQL query (must return unique id and geom field)'), multiLine=True))
|
|
self.addParameter(QgsProcessingParameterFeatureSink(
|
|
self.OUTPUT,
|
|
self.tr("Output layer"),
|
|
QgsProcessing.TypeVectorAnyGeometry))
|
|
|
|
def name(self):
|
|
return 'postgisexecuteandloadsql'
|
|
|
|
def displayName(self):
|
|
return self.tr('PostGIS execute and load SQL')
|
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
|
connection = self.parameterAsString(parameters, self.DATABASE, context)
|
|
#db = postgis.GeoDB.from_name(connection)
|
|
uri = postgis.uri_from_name(connection)
|
|
sql = self.parameterAsString(parameters, self.SQL, context).replace('\n', ' ')
|
|
QgsApplication.messageLog().logMessage(str(sql), level=Qgis.Info)
|
|
uri.setDataSource("","("+sql+")", "geom", "","id")
|
|
vlayer = QgsVectorLayer(uri.uri(), "layername", "postgres")
|
|
QgsApplication.messageLog().logMessage("Valid layer: "+str(vlayer.isValid()), level=Qgis.Info)
|
|
#QgsProject.instance().addMapLayer(vlayer)
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
vlayer.fields(), vlayer.wkbType(), vlayer.sourceCrs())
|
|
|
|
features = vlayer.getFeatures(QgsFeatureRequest())
|
|
for feat in features:
|
|
out_feat = QgsFeature()
|
|
out_feat.setGeometry(feat.geometry())
|
|
out_feat.setAttributes(feat.attributes())
|
|
sink.addFeature(out_feat, QgsFeatureSink.FastInsert)
|
|
|
|
return {self.OUTPUT: dest_id}
|