Skip to content

Commit d71643f

Browse files
authored
Create PostGISExecuteAndLoadSQL.py
1 parent 12cb6fc commit d71643f

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
PostGISExecuteAndLoadSQL.py
6+
---------------------
7+
Date : May 2018
8+
Copyright : (C) 2018 by Anita Graser
9+
Email : anitagraser at gmx dot at
10+
---------------------
11+
based on PostGISExecuteSQL.py by Victor Olaya and Carterix Geomatics
12+
***************************************************************************
13+
* *
14+
* This program is free software; you can redistribute it and/or modify *
15+
* it under the terms of the GNU General Public License as published by *
16+
* the Free Software Foundation; either version 2 of the License, or *
17+
* (at your option) any later version. *
18+
* *
19+
***************************************************************************
20+
"""
21+
22+
__author__ = 'Anita Graser'
23+
__date__ = 'May 2018'
24+
__copyright__ = '(C) 2018, Anita Graser'
25+
26+
# This will get replaced with a git SHA1 when you do a git archive
27+
28+
__revision__ = '$Format:%H$'
29+
30+
from qgis.core import (Qgis, QgsProcessingException, QgsProcessingParameterString, QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingParameterFeatureSink, QgsProcessing, QgsFeatureRequest, QgsFeature, QgsFeatureSink)
31+
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
32+
from processing.tools import postgis
33+
34+
35+
class PostGISExecuteAndLoadSQL(QgisAlgorithm):
36+
37+
DATABASE = 'DATABASE'
38+
SQL = 'SQL'
39+
OUTPUT = 'OUTPUT'
40+
41+
def group(self):
42+
return self.tr('Database')
43+
44+
def groupId(self):
45+
return 'database'
46+
47+
def __init__(self):
48+
super().__init__()
49+
50+
def initAlgorithm(self, config=None):
51+
db_param = QgsProcessingParameterString(
52+
self.DATABASE,
53+
self.tr('Database (connection name)'))
54+
db_param.setMetadata({
55+
'widget_wrapper': {
56+
'class': 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper'}})
57+
self.addParameter(db_param)
58+
self.addParameter(QgsProcessingParameterString(self.SQL, self.tr('SQL query (must return unique id and geom field)'), multiLine=True))
59+
self.addParameter(QgsProcessingParameterFeatureSink(
60+
self.OUTPUT,
61+
self.tr("Output layer"),
62+
QgsProcessing.TypeVectorAnyGeometry))
63+
64+
def name(self):
65+
return 'postgisexecuteandloadsql'
66+
67+
def displayName(self):
68+
return self.tr('PostGIS execute and load SQL')
69+
70+
def processAlgorithm(self, parameters, context, feedback):
71+
connection = self.parameterAsString(parameters, self.DATABASE, context)
72+
#db = postgis.GeoDB.from_name(connection)
73+
uri = postgis.uri_from_name(connection)
74+
sql = self.parameterAsString(parameters, self.SQL, context).replace('\n', ' ')
75+
QgsApplication.messageLog().logMessage(str(sql), level=Qgis.Info)
76+
uri.setDataSource("","("+sql+")", "geom", "","id")
77+
vlayer = QgsVectorLayer(uri.uri(), "layername", "postgres")
78+
QgsApplication.messageLog().logMessage("Valid layer: "+str(vlayer.isValid()), level=Qgis.Info)
79+
#QgsProject.instance().addMapLayer(vlayer)
80+
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
81+
vlayer.fields(), vlayer.wkbType(), vlayer.sourceCrs())
82+
83+
features = vlayer.getFeatures(QgsFeatureRequest())
84+
for feat in features:
85+
out_feat = QgsFeature()
86+
out_feat.setGeometry(feat.geometry())
87+
out_feat.setAttributes(feat.attributes())
88+
sink.addFeature(out_feat, QgsFeatureSink.FastInsert)
89+
90+
return {self.OUTPUT: dest_id}

0 commit comments

Comments
 (0)